| // Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
| import '../dart/resolution/context_collection_resolution.dart'; | |
| import '../dart/resolution/node_text_expectations.dart'; | |
| main() { | |
| defineReflectiveSuite(() { | |
| defineReflectiveTests(ReferencedBeforeDeclarationTest); | |
| defineReflectiveTests(UpdateNodeTextExpectations); | |
| }); | |
| } | |
| @reflectiveTest | |
| class ReferencedBeforeDeclarationTest extends PubPackageResolutionTest { | |
| test_block_patternVariable_after() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| var v = 0; | |
| void f() { | |
| v; | |
| //^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| var [v] = [0]; | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: VariableReadResolution | |
| element: v@34 | |
| type: InvalidType | |
| staticType: InvalidType | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@34 | |
| staticType: InvalidType | |
| '''); | |
| } | |
| test_block_patternVariable_before() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| var v = 0; | |
| void f() { | |
| var [v] = [0]; | |
| v; | |
| } | |
| '''); | |
| } | |
| test_cascade_after_declaration() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| testRequestHandler() {} | |
| main() { | |
| var s1 = null; | |
| testRequestHandler() | |
| ..stream(s1); | |
| var stream = 123; | |
| print(stream); | |
| } | |
| '''); | |
| } | |
| test_forElement_forPartsWithDeclarations_initializer() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| [for (var x = x;;) x]; | |
| // ^ | |
| // [context 1] The declaration of 'x' is here. | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'x' can't be referenced before it is declared. | |
| } | |
| '''); | |
| } | |
| test_forStatement_forPartsWithDeclarations_initializer() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| for (var x = x;;) { | |
| // ^ | |
| // [context 1] The declaration of 'x' is here. | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'x' can't be referenced before it is declared. | |
| x; | |
| } | |
| } | |
| '''); | |
| } | |
| test_hideInBlock_comment() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| main() { | |
| /// [v] is a variable. | |
| var v = 2; | |
| // ^ | |
| // [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. | |
| } | |
| print(x) {} | |
| '''); | |
| } | |
| test_hideInBlock_function() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| var v = 1; | |
| main() { | |
| print(v); | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| v() {} | |
| //^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| print(x) {} | |
| '''); | |
| } | |
| test_hideInBlock_local() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| var v = 1; | |
| main() { | |
| print(v); | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| var v = 2; | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| print(x) {} | |
| '''); | |
| } | |
| test_hideInBlock_local_subBlock() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| var v = 1; | |
| main() { | |
| { | |
| print(v); | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| } | |
| var v = 2; | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| print(x) {} | |
| '''); | |
| } | |
| test_hideInSwitchCase_function() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| var v = 0; | |
| void f(int a) { | |
| switch (a) { | |
| case 0: | |
| v; | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| void v() {} | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: ExecutableTearOffResolution | |
| element: v@75 | |
| type: void Function() | |
| staticType: void Function() | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@75 | |
| staticType: void Function() | |
| '''); | |
| } | |
| test_hideInSwitchCase_function_beforePatterns() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| // %before-language-feature: patterns | |
| var v = 0; | |
| void f(int a) { | |
| switch (a) { | |
| case 0: | |
| v; | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| void v() {} | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: ExecutableTearOffResolution | |
| element: v@91 | |
| type: void Function() | |
| staticType: void Function() | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@91 | |
| staticType: void Function() | |
| '''); | |
| } | |
| test_hideInSwitchCase_local() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| var v = 0; | |
| void f(int a) { | |
| switch (a) { | |
| case 0: | |
| v; | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| var v = 1; | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: VariableReadResolution | |
| element: v@74 | |
| type: dynamic | |
| staticType: dynamic | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@74 | |
| staticType: dynamic | |
| '''); | |
| } | |
| test_hideInSwitchCase_local_beforePatterns() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| // %before-language-feature: patterns | |
| var v = 0; | |
| void f(int a) { | |
| switch (a) { | |
| case 0: | |
| v; | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| var v = 1; | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: VariableReadResolution | |
| element: v@90 | |
| type: dynamic | |
| staticType: dynamic | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@90 | |
| staticType: dynamic | |
| '''); | |
| } | |
| test_hideInSwitchDefault_function() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| var v = 0; | |
| void f(int a) { | |
| switch (a) { | |
| default: | |
| v; | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| void v() {} | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: ExecutableTearOffResolution | |
| element: v@76 | |
| type: void Function() | |
| staticType: void Function() | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@76 | |
| staticType: void Function() | |
| '''); | |
| } | |
| test_hideInSwitchDefault_function_beforePatterns() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| // %before-language-feature: patterns | |
| var v = 0; | |
| void f(int a) { | |
| switch (a) { | |
| default: | |
| v; | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| void v() {} | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: ExecutableTearOffResolution | |
| element: v@92 | |
| type: void Function() | |
| staticType: void Function() | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@92 | |
| staticType: void Function() | |
| '''); | |
| } | |
| test_hideInSwitchDefault_local() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| var v = 0; | |
| void f(int a) { | |
| switch (a) { | |
| default: | |
| v; | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| var v = 1; | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: VariableReadResolution | |
| element: v@75 | |
| type: dynamic | |
| staticType: dynamic | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@75 | |
| staticType: dynamic | |
| '''); | |
| } | |
| test_hideInSwitchDefault_local_beforePatterns() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| // %before-language-feature: patterns | |
| var v = 0; | |
| void f(int a) { | |
| switch (a) { | |
| default: | |
| v; | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| var v = 1; | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| } | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: VariableReadResolution | |
| element: v@91 | |
| type: dynamic | |
| staticType: dynamic | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@91 | |
| staticType: dynamic | |
| '''); | |
| } | |
| test_inInitializer_closure() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| main() { | |
| var v = () => v; | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| } | |
| '''); | |
| } | |
| test_inInitializer_directly() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| main() { | |
| var v = v; | |
| // ^ | |
| // [context 1] The declaration of 'v' is here. | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'v' can't be referenced before it is declared. | |
| } | |
| '''); | |
| } | |
| test_inInitializer_incrementOrDecrement() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| var a = ++a; | |
| // ^ | |
| // [context 1] The declaration of 'a' is here. | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'a' can't be referenced before it is declared. | |
| var b = --b; | |
| // ^ | |
| // [context 2] The declaration of 'b' is here. | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 2] Local variable 'b' can't be referenced before it is declared. | |
| var c = c++; | |
| // ^ | |
| // [context 3] The declaration of 'c' is here. | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 3] Local variable 'c' can't be referenced before it is declared. | |
| var d = d--; | |
| // ^ | |
| // [context 4] The declaration of 'd' is here. | |
| // ^ | |
| // [diag.referencedBeforeDeclaration][context 4] Local variable 'd' can't be referenced before it is declared. | |
| } | |
| '''); | |
| } | |
| test_labeledStatement_function() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| // ignore:unused_label | |
| label: void v() {} | |
| v; | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: ExecutableTearOffResolution | |
| element: v@50 | |
| type: void Function() | |
| staticType: void Function() | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@50 | |
| staticType: void Function() | |
| '''); | |
| } | |
| test_labeledStatement_local() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| // ignore:unused_label | |
| label: var v = 0; | |
| v; | |
| } | |
| '''); | |
| var node = result.findNode.unqualifiedNameExpression('v;'); | |
| assertResolvedNodeText(node, r''' | |
| UnqualifiedNameExpression | |
| name: v | |
| resolution: VariableReadResolution | |
| element: v@49 | |
| type: int | |
| staticType: int | |
| V1: SimpleIdentifier | |
| token: v | |
| element: v@49 | |
| staticType: int | |
| '''); | |
| } | |
| test_type_localFunction() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| void testTypeRef() { | |
| String s = ''; | |
| //^^^^^^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'String' can't be referenced before it is declared. | |
| int String(int x) => x + 1; | |
| // ^^^^^^ | |
| // [context 1] The declaration of 'String' is here. | |
| print(s + String); | |
| } | |
| '''); | |
| } | |
| test_type_localVariable() async { | |
| await resolveTestCodeWithDiagnostics(r''' | |
| void testTypeRef() { | |
| String s = ''; | |
| //^^^^^^ | |
| // [diag.referencedBeforeDeclaration][context 1] Local variable 'String' can't be referenced before it is declared. | |
| var String = ''; | |
| // ^^^^^^ | |
| // [context 1] The declaration of 'String' is here. | |
| print(s + String); | |
| } | |
| '''); | |
| } | |
| } |