| // Copyright (c) 2023, 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 'context_collection_resolution.dart'; | |
| import 'node_text_expectations.dart'; | |
| main() { | |
| defineReflectiveSuite(() { | |
| defineReflectiveTests(FunctionDeclarationStatementResolutionTest); | |
| defineReflectiveTests(UpdateNodeTextExpectations); | |
| }); | |
| } | |
| @reflectiveTest | |
| class FunctionDeclarationStatementResolutionTest | |
| extends PubPackageResolutionTest { | |
| test_generic() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| T g<T, U>(T a, U b) => a; | |
| // ^ | |
| // [diag.unusedElement] The declaration 'g' isn't referenced. | |
| } | |
| '''); | |
| var node = result.findNode.singleFunctionDeclarationStatement; | |
| assertResolvedNodeText(node, r''' | |
| FunctionDeclarationStatement | |
| functionDeclaration: FunctionDeclaration | |
| returnType: NamedType | |
| name: T | |
| element: #E0 T | |
| type: T | |
| name: g | |
| functionExpression: FunctionExpression | |
| typeParameters: TypeParameterList | |
| leftBracket: < | |
| typeParameters | |
| TypeParameter | |
| name: T | |
| declaredFragment: <testLibraryFragment> T@17 | |
| defaultType: dynamic | |
| TypeParameter | |
| name: U | |
| declaredFragment: <testLibraryFragment> U@20 | |
| defaultType: dynamic | |
| rightBracket: > | |
| parameters: FormalParameterList | |
| leftParenthesis: ( | |
| requiredPositionalFormalParameters | |
| RegularFormalParameter | |
| type: NamedType | |
| name: T | |
| element: #E0 T | |
| type: T | |
| name: a | |
| declaredFragment: <testLibraryFragment> a@25 | |
| element: isPublic | |
| type: T | |
| RegularFormalParameter | |
| type: NamedType | |
| name: U | |
| element: #E1 U | |
| type: U | |
| name: b | |
| declaredFragment: <testLibraryFragment> b@30 | |
| element: isPublic | |
| type: U | |
| rightParenthesis: ) | |
| parameters(v1): FormalParameterList | |
| leftParenthesis: ( | |
| parameter: RegularFormalParameter | |
| type: NamedType | |
| name: T | |
| element: #E0 T | |
| type: T | |
| name: a | |
| declaredFragment: <testLibraryFragment> a@25 | |
| element: isPublic | |
| type: T | |
| parameter: RegularFormalParameter | |
| type: NamedType | |
| name: U | |
| element: #E1 U | |
| type: U | |
| name: b | |
| declaredFragment: <testLibraryFragment> b@30 | |
| element: isPublic | |
| type: U | |
| rightParenthesis: ) | |
| body: ExpressionFunctionBody | |
| functionDefinition: => | |
| expression2: UnqualifiedNameExpression | |
| name: a | |
| resolution: VariableReadResolution | |
| element: a@25 | |
| type: T | |
| staticType: T | |
| expression(v1): SimpleIdentifier | |
| token: a | |
| element: a@25 | |
| staticType: T | |
| semicolon: ; | |
| declaredFragment: <testLibraryFragment> g@15 | |
| element: g@15 | |
| type: T Function<T, U>(T, U) | |
| staticType: T Function<T, U>(T, U) | |
| declaredFragment: <testLibraryFragment> g@15 | |
| element: g@15 | |
| type: T Function<T, U>(T, U) | |
| '''); | |
| } | |
| test_generic_fBounded() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| void g<T extends U, U, V extends U>(T x, U y, V z) {} | |
| // ^ | |
| // [diag.unusedElement] The declaration 'g' isn't referenced. | |
| } | |
| '''); | |
| var node = result.findNode.singleFunctionDeclarationStatement; | |
| assertResolvedNodeText(node, r''' | |
| FunctionDeclarationStatement | |
| functionDeclaration: FunctionDeclaration | |
| returnType: NamedType | |
| name: void | |
| element: <null> | |
| type: void | |
| name: g | |
| functionExpression: FunctionExpression | |
| typeParameters: TypeParameterList | |
| leftBracket: < | |
| typeParameters | |
| TypeParameter | |
| name: T | |
| extendsKeyword: extends | |
| bound: NamedType | |
| name: U | |
| element: #E0 U | |
| type: U | |
| declaredFragment: <testLibraryFragment> T@20 | |
| defaultType: dynamic | |
| TypeParameter | |
| name: U | |
| declaredFragment: <testLibraryFragment> U@33 | |
| defaultType: dynamic | |
| TypeParameter | |
| name: V | |
| extendsKeyword: extends | |
| bound: NamedType | |
| name: U | |
| element: #E0 U | |
| type: U | |
| declaredFragment: <testLibraryFragment> V@36 | |
| defaultType: dynamic | |
| rightBracket: > | |
| parameters: FormalParameterList | |
| leftParenthesis: ( | |
| requiredPositionalFormalParameters | |
| RegularFormalParameter | |
| type: NamedType | |
| name: T | |
| element: #E1 T | |
| type: T | |
| name: x | |
| declaredFragment: <testLibraryFragment> x@51 | |
| element: isPublic | |
| type: T | |
| RegularFormalParameter | |
| type: NamedType | |
| name: U | |
| element: #E0 U | |
| type: U | |
| name: y | |
| declaredFragment: <testLibraryFragment> y@56 | |
| element: isPublic | |
| type: U | |
| RegularFormalParameter | |
| type: NamedType | |
| name: V | |
| element: #E2 V | |
| type: V | |
| name: z | |
| declaredFragment: <testLibraryFragment> z@61 | |
| element: isPublic | |
| type: V | |
| rightParenthesis: ) | |
| parameters(v1): FormalParameterList | |
| leftParenthesis: ( | |
| parameter: RegularFormalParameter | |
| type: NamedType | |
| name: T | |
| element: #E1 T | |
| type: T | |
| name: x | |
| declaredFragment: <testLibraryFragment> x@51 | |
| element: isPublic | |
| type: T | |
| parameter: RegularFormalParameter | |
| type: NamedType | |
| name: U | |
| element: #E0 U | |
| type: U | |
| name: y | |
| declaredFragment: <testLibraryFragment> y@56 | |
| element: isPublic | |
| type: U | |
| parameter: RegularFormalParameter | |
| type: NamedType | |
| name: V | |
| element: #E2 V | |
| type: V | |
| name: z | |
| declaredFragment: <testLibraryFragment> z@61 | |
| element: isPublic | |
| type: V | |
| rightParenthesis: ) | |
| body: BlockFunctionBody | |
| block: Block | |
| leftBracket: { | |
| rightBracket: } | |
| declaredFragment: <testLibraryFragment> g@18 | |
| element: g@18 | |
| type: void Function<T extends U, U, V extends U>(T, U, V) | |
| staticType: void Function<T extends U, U, V extends U>(T, U, V) | |
| declaredFragment: <testLibraryFragment> g@18 | |
| element: g@18 | |
| type: void Function<T extends U, U, V extends U>(T, U, V) | |
| '''); | |
| } | |
| test_generic_formalParameters_optionalNamed() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| void g<T>({T? a}) {} | |
| // ^ | |
| // [diag.unusedElement] The declaration 'g' isn't referenced. | |
| } | |
| '''); | |
| var node = result.findNode.singleFunctionDeclarationStatement; | |
| assertResolvedNodeText(node, r''' | |
| FunctionDeclarationStatement | |
| functionDeclaration: FunctionDeclaration | |
| returnType: NamedType | |
| name: void | |
| element: <null> | |
| type: void | |
| name: g | |
| functionExpression: FunctionExpression | |
| typeParameters: TypeParameterList | |
| leftBracket: < | |
| typeParameters | |
| TypeParameter | |
| name: T | |
| declaredFragment: <testLibraryFragment> T@20 | |
| defaultType: dynamic | |
| rightBracket: > | |
| parameters: FormalParameterList | |
| leftParenthesis: ( | |
| delimitedFormalParameters: DelimitedFormalParameters | |
| leftDelimiter: { | |
| formalParameters | |
| RegularFormalParameter | |
| type: NamedType | |
| name: T | |
| question: ? | |
| element: #E0 T | |
| type: T? | |
| name: a | |
| declaredFragment: <testLibraryFragment> a@27 | |
| element: isPublic | |
| type: T? | |
| rightDelimiter: } | |
| rightParenthesis: ) | |
| parameters(v1): FormalParameterList | |
| leftParenthesis: ( | |
| leftDelimiter: { | |
| parameter: RegularFormalParameter | |
| type: NamedType | |
| name: T | |
| question: ? | |
| element: #E0 T | |
| type: T? | |
| name: a | |
| declaredFragment: <testLibraryFragment> a@27 | |
| element: isPublic | |
| type: T? | |
| rightDelimiter: } | |
| rightParenthesis: ) | |
| body: BlockFunctionBody | |
| block: Block | |
| leftBracket: { | |
| rightBracket: } | |
| declaredFragment: <testLibraryFragment> g@18 | |
| element: g@18 | |
| type: void Function<T>({T? a}) | |
| staticType: void Function<T>({T? a}) | |
| declaredFragment: <testLibraryFragment> g@18 | |
| element: g@18 | |
| type: void Function<T>({T? a}) | |
| '''); | |
| } | |
| test_generic_formalParameters_optionalPositional() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| void g<T>([T? a]) {} | |
| // ^ | |
| // [diag.unusedElement] The declaration 'g' isn't referenced. | |
| } | |
| '''); | |
| var node = result.findNode.singleFunctionDeclarationStatement; | |
| assertResolvedNodeText(node, r''' | |
| FunctionDeclarationStatement | |
| functionDeclaration: FunctionDeclaration | |
| returnType: NamedType | |
| name: void | |
| element: <null> | |
| type: void | |
| name: g | |
| functionExpression: FunctionExpression | |
| typeParameters: TypeParameterList | |
| leftBracket: < | |
| typeParameters | |
| TypeParameter | |
| name: T | |
| declaredFragment: <testLibraryFragment> T@20 | |
| defaultType: dynamic | |
| rightBracket: > | |
| parameters: FormalParameterList | |
| leftParenthesis: ( | |
| delimitedFormalParameters: DelimitedFormalParameters | |
| leftDelimiter: [ | |
| formalParameters | |
| RegularFormalParameter | |
| type: NamedType | |
| name: T | |
| question: ? | |
| element: #E0 T | |
| type: T? | |
| name: a | |
| declaredFragment: <testLibraryFragment> a@27 | |
| element: isPublic | |
| type: T? | |
| rightDelimiter: ] | |
| rightParenthesis: ) | |
| parameters(v1): FormalParameterList | |
| leftParenthesis: ( | |
| leftDelimiter: [ | |
| parameter: RegularFormalParameter | |
| type: NamedType | |
| name: T | |
| question: ? | |
| element: #E0 T | |
| type: T? | |
| name: a | |
| declaredFragment: <testLibraryFragment> a@27 | |
| element: isPublic | |
| type: T? | |
| rightDelimiter: ] | |
| rightParenthesis: ) | |
| body: BlockFunctionBody | |
| block: Block | |
| leftBracket: { | |
| rightBracket: } | |
| declaredFragment: <testLibraryFragment> g@18 | |
| element: g@18 | |
| type: void Function<T>([T?]) | |
| staticType: void Function<T>([T?]) | |
| declaredFragment: <testLibraryFragment> g@18 | |
| element: g@18 | |
| type: void Function<T>([T?]) | |
| '''); | |
| } | |
| test_generic_formalParameters_requiredNamed() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| void g<T>({required T? a}) {} | |
| // ^ | |
| // [diag.unusedElement] The declaration 'g' isn't referenced. | |
| } | |
| '''); | |
| var node = result.findNode.singleFunctionDeclarationStatement; | |
| assertResolvedNodeText(node, r''' | |
| FunctionDeclarationStatement | |
| functionDeclaration: FunctionDeclaration | |
| returnType: NamedType | |
| name: void | |
| element: <null> | |
| type: void | |
| name: g | |
| functionExpression: FunctionExpression | |
| typeParameters: TypeParameterList | |
| leftBracket: < | |
| typeParameters | |
| TypeParameter | |
| name: T | |
| declaredFragment: <testLibraryFragment> T@20 | |
| defaultType: dynamic | |
| rightBracket: > | |
| parameters: FormalParameterList | |
| leftParenthesis: ( | |
| delimitedFormalParameters: DelimitedFormalParameters | |
| leftDelimiter: { | |
| formalParameters | |
| RegularFormalParameter | |
| requiredKeyword: required | |
| type: NamedType | |
| name: T | |
| question: ? | |
| element: #E0 T | |
| type: T? | |
| name: a | |
| declaredFragment: <testLibraryFragment> a@36 | |
| element: isPublic | |
| type: T? | |
| rightDelimiter: } | |
| rightParenthesis: ) | |
| parameters(v1): FormalParameterList | |
| leftParenthesis: ( | |
| leftDelimiter: { | |
| parameter: RegularFormalParameter | |
| requiredKeyword: required | |
| type: NamedType | |
| name: T | |
| question: ? | |
| element: #E0 T | |
| type: T? | |
| name: a | |
| declaredFragment: <testLibraryFragment> a@36 | |
| element: isPublic | |
| type: T? | |
| rightDelimiter: } | |
| rightParenthesis: ) | |
| body: BlockFunctionBody | |
| block: Block | |
| leftBracket: { | |
| rightBracket: } | |
| declaredFragment: <testLibraryFragment> g@18 | |
| element: g@18 | |
| type: void Function<T>({required T? a}) | |
| staticType: void Function<T>({required T? a}) | |
| declaredFragment: <testLibraryFragment> g@18 | |
| element: g@18 | |
| type: void Function<T>({required T? a}) | |
| '''); | |
| } | |
| test_generic_formalParameters_requiredPositional() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| void g<T>(T a) {} | |
| // ^ | |
| // [diag.unusedElement] The declaration 'g' isn't referenced. | |
| } | |
| '''); | |
| var node = result.findNode.singleFunctionDeclarationStatement; | |
| assertResolvedNodeText(node, r''' | |
| FunctionDeclarationStatement | |
| functionDeclaration: FunctionDeclaration | |
| returnType: NamedType | |
| name: void | |
| element: <null> | |
| type: void | |
| name: g | |
| functionExpression: FunctionExpression | |
| typeParameters: TypeParameterList | |
| leftBracket: < | |
| typeParameters | |
| TypeParameter | |
| name: T | |
| declaredFragment: <testLibraryFragment> T@20 | |
| defaultType: dynamic | |
| rightBracket: > | |
| parameters: FormalParameterList | |
| leftParenthesis: ( | |
| requiredPositionalFormalParameters | |
| RegularFormalParameter | |
| type: NamedType | |
| name: T | |
| element: #E0 T | |
| type: T | |
| name: a | |
| declaredFragment: <testLibraryFragment> a@25 | |
| element: isPublic | |
| type: T | |
| rightParenthesis: ) | |
| parameters(v1): FormalParameterList | |
| leftParenthesis: ( | |
| parameter: RegularFormalParameter | |
| type: NamedType | |
| name: T | |
| element: #E0 T | |
| type: T | |
| name: a | |
| declaredFragment: <testLibraryFragment> a@25 | |
| element: isPublic | |
| type: T | |
| rightParenthesis: ) | |
| body: BlockFunctionBody | |
| block: Block | |
| leftBracket: { | |
| rightBracket: } | |
| declaredFragment: <testLibraryFragment> g@18 | |
| element: g@18 | |
| type: void Function<T>(T) | |
| staticType: void Function<T>(T) | |
| declaredFragment: <testLibraryFragment> g@18 | |
| element: g@18 | |
| type: void Function<T>(T) | |
| '''); | |
| } | |
| test_returnType_implicit_blockBody() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| g() {} | |
| //^ | |
| // [diag.unusedElement] The declaration 'g' isn't referenced. | |
| } | |
| '''); | |
| var node = result.findNode.singleFunctionDeclarationStatement; | |
| assertResolvedNodeText(node, r''' | |
| FunctionDeclarationStatement | |
| functionDeclaration: FunctionDeclaration | |
| name: g | |
| functionExpression: FunctionExpression | |
| parameters: FormalParameterList | |
| leftParenthesis: ( | |
| rightParenthesis: ) | |
| body: BlockFunctionBody | |
| block: Block | |
| leftBracket: { | |
| rightBracket: } | |
| declaredFragment: <testLibraryFragment> g@13 | |
| element: g@13 | |
| type: Null Function() | |
| staticType: Null Function() | |
| declaredFragment: <testLibraryFragment> g@13 | |
| element: g@13 | |
| type: Null Function() | |
| '''); | |
| } | |
| test_returnType_implicit_expressionBody() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| g() => 0; | |
| //^ | |
| // [diag.unusedElement] The declaration 'g' isn't referenced. | |
| } | |
| '''); | |
| var node = result.findNode.singleFunctionDeclarationStatement; | |
| assertResolvedNodeText(node, r''' | |
| FunctionDeclarationStatement | |
| functionDeclaration: FunctionDeclaration | |
| name: g | |
| functionExpression: FunctionExpression | |
| parameters: FormalParameterList | |
| leftParenthesis: ( | |
| rightParenthesis: ) | |
| body: ExpressionFunctionBody | |
| functionDefinition: => | |
| expression2: IntegerLiteral | |
| literal: 0 | |
| staticType: int | |
| semicolon: ; | |
| declaredFragment: <testLibraryFragment> g@13 | |
| element: g@13 | |
| type: int Function() | |
| staticType: int Function() | |
| declaredFragment: <testLibraryFragment> g@13 | |
| element: g@13 | |
| type: int Function() | |
| '''); | |
| } | |
| } |