Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 1730993003: Validation of @protected property accesses. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/resolver_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analyzer.src.generated.resolver; 5 library analyzer.src.generated.resolver;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/token.dart'; 10 import 'package:analyzer/dart/ast/token.dart';
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 @override 231 @override
232 Object visitRedirectingConstructorInvocation( 232 Object visitRedirectingConstructorInvocation(
233 RedirectingConstructorInvocation node) { 233 RedirectingConstructorInvocation node) {
234 _checkForDeprecatedMemberUse(node.staticElement, node); 234 _checkForDeprecatedMemberUse(node.staticElement, node);
235 return super.visitRedirectingConstructorInvocation(node); 235 return super.visitRedirectingConstructorInvocation(node);
236 } 236 }
237 237
238 @override 238 @override
239 Object visitSimpleIdentifier(SimpleIdentifier node) { 239 Object visitSimpleIdentifier(SimpleIdentifier node) {
240 _checkForDeprecatedMemberUseAtIdentifier(node); 240 _checkForDeprecatedMemberUseAtIdentifier(node);
241 _checkForInvalidProtectedPropertyAccess(node);
241 return super.visitSimpleIdentifier(node); 242 return super.visitSimpleIdentifier(node);
242 } 243 }
243 244
244 @override 245 @override
245 Object visitSuperConstructorInvocation(SuperConstructorInvocation node) { 246 Object visitSuperConstructorInvocation(SuperConstructorInvocation node) {
246 _checkForDeprecatedMemberUse(node.staticElement, node); 247 _checkForDeprecatedMemberUse(node.staticElement, node);
247 return super.visitSuperConstructorInvocation(node); 248 return super.visitSuperConstructorInvocation(node);
248 } 249 }
249 250
250 @override 251 @override
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 if (!_typeSystem.isAssignableTo(bestRightType, leftType)) { 603 if (!_typeSystem.isAssignableTo(bestRightType, leftType)) {
603 _errorReporter.reportTypeErrorForNode( 604 _errorReporter.reportTypeErrorForNode(
604 HintCode.INVALID_ASSIGNMENT, rhs, [bestRightType, leftType]); 605 HintCode.INVALID_ASSIGNMENT, rhs, [bestRightType, leftType]);
605 return true; 606 return true;
606 } 607 }
607 } 608 }
608 return false; 609 return false;
609 } 610 }
610 611
611 /** 612 /**
613 * Produces a hint if the given identifier is a protected field or getter
614 * accessed outside a subclass.
615 */
616 void _checkForInvalidProtectedPropertyAccess(SimpleIdentifier identifier) {
617 if (identifier.inDeclarationContext()) {
618 return;
619 }
620 Element element = identifier.bestElement;
621 if (element != null) {
Brian Wilkerson 2016/02/24 19:54:25 The 'is' test below should negate the need to the
pquitslund 2016/02/24 20:34:43 Nice. I missed that when I refactored. Thanks!
622 if (element is PropertyAccessorElement &&
623 (element.isProtected || element.variable.isProtected)) {
624 ClassElement definingClass = element.enclosingElement;
625 if (definingClass == null) {
626 return;
627 }
628 ClassDeclaration accessingClass =
629 identifier.getAncestor((AstNode node) => node is ClassDeclaration);
630
631 if (accessingClass == null) {
632 _errorReporter.reportErrorForNode(
633 HintCode.INVALID_USE_OF_PROTECTED_MEMBER,
634 identifier,
635 [identifier.name.toString(), definingClass.name]);
636 } else if (!_hasSuperClassOrMixin(
637 accessingClass.element, definingClass.type)) {
638 _errorReporter.reportErrorForNode(
639 HintCode.INVALID_USE_OF_PROTECTED_MEMBER,
640 identifier,
641 [identifier.name.toString(), definingClass.name]);
642 }
643 }
644 }
645 }
646
647 /**
612 * Produces a hint if the given invocation is of a protected method outside 648 * Produces a hint if the given invocation is of a protected method outside
613 * a subclass instance method. 649 * a subclass instance method.
614 */ 650 */
615 void _checkForInvalidProtectedMethodCalls(MethodInvocation node) { 651 void _checkForInvalidProtectedMethodCalls(MethodInvocation node) {
616 Element element = node.methodName.bestElement; 652 Element element = node.methodName.bestElement;
617 if (element == null || !element.isProtected) { 653 if (element == null || !element.isProtected) {
618 return; 654 return;
619 } 655 }
620 656
621 ClassElement definingClass = element.enclosingElement; 657 ClassElement definingClass = element.enclosingElement;
(...skipping 12324 matching lines...) Expand 10 before | Expand all | Expand 10 after
12946 nonFields.add(node); 12982 nonFields.add(node);
12947 return null; 12983 return null;
12948 } 12984 }
12949 12985
12950 @override 12986 @override
12951 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); 12987 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this);
12952 12988
12953 @override 12989 @override
12954 Object visitWithClause(WithClause node) => null; 12990 Object visitWithClause(WithClause node) => null;
12955 } 12991 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698