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

Side by Side Diff: pkg/analyzer/lib/src/dart/element/builder.dart

Issue 1798223002: Fix visible range of local variables in 'for' loops. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 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
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.dart.element.builder; 5 library analyzer.src.dart.element.builder;
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 525 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 } 536 }
537 537
538 @override 538 @override
539 Object visitDeclaredIdentifier(DeclaredIdentifier node) { 539 Object visitDeclaredIdentifier(DeclaredIdentifier node) {
540 SimpleIdentifier variableName = node.identifier; 540 SimpleIdentifier variableName = node.identifier;
541 LocalVariableElementImpl element = 541 LocalVariableElementImpl element =
542 new LocalVariableElementImpl.forNode(variableName); 542 new LocalVariableElementImpl.forNode(variableName);
543 _setCodeRange(element, node); 543 _setCodeRange(element, node);
544 element.metadata = _createElementAnnotations(node.metadata); 544 element.metadata = _createElementAnnotations(node.metadata);
545 ForEachStatement statement = node.parent as ForEachStatement; 545 ForEachStatement statement = node.parent as ForEachStatement;
546 int declarationEnd = node.offset + node.length; 546 element.setVisibleRange(statement.offset, statement.length);
547 int statementEnd = statement.offset + statement.length;
548 element.setVisibleRange(declarationEnd, statementEnd - declarationEnd - 1);
549 element.const3 = node.isConst; 547 element.const3 = node.isConst;
550 element.final2 = node.isFinal; 548 element.final2 = node.isFinal;
551 if (node.type == null) { 549 if (node.type == null) {
552 element.hasImplicitType = true; 550 element.hasImplicitType = true;
553 } 551 }
554 _currentHolder.addLocalVariable(element); 552 _currentHolder.addLocalVariable(element);
555 variableName.staticElement = element; 553 variableName.staticElement = element;
556 return super.visitDeclaredIdentifier(node); 554 return super.visitDeclaredIdentifier(node);
557 } 555 }
558 556
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after
1194 } else if (_inFunction) { 1192 } else if (_inFunction) {
1195 SimpleIdentifier variableName = node.name; 1193 SimpleIdentifier variableName = node.name;
1196 LocalVariableElementImpl variable; 1194 LocalVariableElementImpl variable;
1197 if (isConst && hasInitializer) { 1195 if (isConst && hasInitializer) {
1198 variable = new ConstLocalVariableElementImpl.forNode(variableName); 1196 variable = new ConstLocalVariableElementImpl.forNode(variableName);
1199 } else { 1197 } else {
1200 variable = new LocalVariableElementImpl.forNode(variableName); 1198 variable = new LocalVariableElementImpl.forNode(variableName);
1201 } 1199 }
1202 element = variable; 1200 element = variable;
1203 _setCodeRange(element, node); 1201 _setCodeRange(element, node);
1204 Block enclosingBlock = node.getAncestor((node) => node is Block); 1202 _setVariableVisibleRange(variable, node);
1205 // TODO(brianwilkerson) This isn't right for variables declared in a for
1206 // loop.
1207 variable.setVisibleRange(enclosingBlock.offset, enclosingBlock.length);
1208 variable.hasImplicitType = varList.type == null; 1203 variable.hasImplicitType = varList.type == null;
1209 _currentHolder.addLocalVariable(variable); 1204 _currentHolder.addLocalVariable(variable);
1210 variableName.staticElement = element; 1205 variableName.staticElement = element;
1211 } else { 1206 } else {
1212 SimpleIdentifier variableName = node.name; 1207 SimpleIdentifier variableName = node.name;
1213 TopLevelVariableElementImpl variable; 1208 TopLevelVariableElementImpl variable;
1214 if (isConst && hasInitializer) { 1209 if (isConst && hasInitializer) {
1215 variable = new ConstTopLevelVariableElementImpl.forNode(variableName); 1210 variable = new ConstTopLevelVariableElementImpl.forNode(variableName);
1216 } else { 1211 } else {
1217 variable = new TopLevelVariableElementImpl.forNode(variableName); 1212 variable = new TopLevelVariableElementImpl.forNode(variableName);
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
1394 * Sets the visible source range for formal parameter. 1389 * Sets the visible source range for formal parameter.
1395 */ 1390 */
1396 void _setParameterVisibleRange( 1391 void _setParameterVisibleRange(
1397 FormalParameter node, ParameterElementImpl element) { 1392 FormalParameter node, ParameterElementImpl element) {
1398 FunctionBody body = _getFunctionBody(node); 1393 FunctionBody body = _getFunctionBody(node);
1399 if (body is BlockFunctionBody || body is ExpressionFunctionBody) { 1394 if (body is BlockFunctionBody || body is ExpressionFunctionBody) {
1400 element.setVisibleRange(body.offset, body.length); 1395 element.setVisibleRange(body.offset, body.length);
1401 } 1396 }
1402 } 1397 }
1403 1398
1399 void _setVariableVisibleRange(
1400 LocalVariableElementImpl element, VariableDeclaration node) {
1401 AstNode scopeNode;
1402 AstNode parent2 = node.parent.parent;
1403 if (parent2 is ForStatement) {
1404 scopeNode = parent2;
1405 } else {
1406 scopeNode = node.getAncestor((node) => node is Block);
1407 }
1408 element.setVisibleRange(scopeNode.offset, scopeNode.length);
1409 }
1410
1404 /** 1411 /**
1405 * Make the given holder be the current holder while visiting the given node. 1412 * Make the given holder be the current holder while visiting the given node.
1406 * 1413 *
1407 * @param holder the holder that will gather elements that are built while vis iting the children 1414 * @param holder the holder that will gather elements that are built while vis iting the children
1408 * @param node the node to be visited 1415 * @param node the node to be visited
1409 */ 1416 */
1410 void _visit(ElementHolder holder, AstNode node) { 1417 void _visit(ElementHolder holder, AstNode node) {
1411 if (node != null) { 1418 if (node != null) {
1412 ElementHolder previousHolder = _currentHolder; 1419 ElementHolder previousHolder = _currentHolder;
1413 _currentHolder = holder; 1420 _currentHolder = holder;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1489 return null; 1496 return null;
1490 } 1497 }
1491 1498
1492 /** 1499 /**
1493 * Return the lexical identifiers associated with the given [identifiers]. 1500 * Return the lexical identifiers associated with the given [identifiers].
1494 */ 1501 */
1495 static List<String> _getIdentifiers(NodeList<SimpleIdentifier> identifiers) { 1502 static List<String> _getIdentifiers(NodeList<SimpleIdentifier> identifiers) {
1496 return identifiers.map((identifier) => identifier.name).toList(); 1503 return identifiers.map((identifier) => identifier.name).toList();
1497 } 1504 }
1498 } 1505 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698