@@ -11,7 +11,9 @@ part of code_builder;
1111/// the top level and within other methods) via [toFunctionAst] .
1212///
1313/// To return nothing (`void` ), use [MethodBuilder.returnVoid] .
14- class MethodBuilder implements CodeBuilder <Declaration > {
14+ class MethodBuilder implements
15+ CodeBuilder <Declaration >,
16+ ScopeAware <Declaration > {
1517 static Token _abstract = new KeywordToken (Keyword .ABSTRACT , 0 );
1618 static Token _semicolon = new Token (TokenType .SEMICOLON , 0 );
1719 static Token _static = new KeywordToken (Keyword .STATIC , 0 );
@@ -41,6 +43,9 @@ class MethodBuilder implements CodeBuilder<Declaration> {
4143
4244 MethodBuilder ._(this ._name, this ._returnType);
4345
46+ @override
47+ List <String > get requiredImports => null ;
48+
4449 /// Lazily adds [annotation] .
4550 ///
4651 /// When the method is emitted as an AST, [AnnotationBuilder.toAst] is used.
@@ -76,14 +81,14 @@ class MethodBuilder implements CodeBuilder<Declaration> {
7681 /// [toMethodAst] .
7782 @override
7883 @visibleForTesting
79- Declaration toAst () => toFunctionAst ( );
84+ Declaration toAst () => toScopedAst ( const Scope . identity () );
8085
8186 /// Returns a copy-safe [FunctionDeclaration] AST representing current state.
82- FunctionDeclaration toFunctionAst () {
87+ FunctionDeclaration toFunctionAst ({ Scope scope : const Scope . identity ()} ) {
8388 var functionAst = _emptyFunction ()
8489 ..metadata.addAll (_annotations.map/*<Annotation>*/ ((a) => a.toAst ()))
8590 ..name = _stringId (_name)
86- ..returnType = _returnType? .toAst ( );
91+ ..returnType = _returnType? .toScopedAst (scope );
8792 if (_returnExpression != null ) {
8893 functionAst.functionExpression = _returnExpression.toFunctionExpression ();
8994 } else {
@@ -95,7 +100,7 @@ class MethodBuilder implements CodeBuilder<Declaration> {
95100 }
96101 if (_parameters.isNotEmpty) {
97102 functionAst.functionExpression.parameters.parameters
98- .addAll (_parameters.map/*<FormalParameter>*/ ((p) => p.toAst ( )));
103+ .addAll (_parameters.map/*<FormalParameter>*/ ((p) => p.toScopedAst (scope )));
99104 }
100105 return functionAst;
101106 }
@@ -104,11 +109,12 @@ class MethodBuilder implements CodeBuilder<Declaration> {
104109 MethodDeclaration toMethodAst ({
105110 bool static : false ,
106111 bool canBeAbstract: false ,
112+ Scope scope: const Scope .identity (),
107113 }) {
108114 var methodAst = _emptyMethod ()
109115 ..metadata.addAll (_annotations.map/*<Annotation>*/ ((a) => a.toAst ()))
110116 ..name = _stringId (_name)
111- ..returnType = _returnType? .toAst ( );
117+ ..returnType = _returnType? .toScopedAst (scope );
112118 FunctionBody methodBody = _returnExpression? .toFunctionBody ();
113119 if (static ) {
114120 methodAst.modifierKeyword = _static;
@@ -123,12 +129,15 @@ class MethodBuilder implements CodeBuilder<Declaration> {
123129 }
124130 if (_parameters.isNotEmpty) {
125131 methodAst.parameters.parameters
126- .addAll (_parameters.map/*<FormalParameter>*/ ((p) => p.toAst ( )));
132+ .addAll (_parameters.map/*<FormalParameter>*/ ((p) => p.toScopedAst (scope )));
127133 }
128134 methodAst.body = methodBody;
129135 return methodAst;
130136 }
131137
138+ @override
139+ Declaration toScopedAst (Scope scope) => toFunctionAst (scope: scope);
140+
132141 @override
133142 String toString () => 'MethodBuilder ${toAst ().toSource ()}' ;
134143
@@ -156,7 +165,7 @@ class MethodBuilder implements CodeBuilder<Declaration> {
156165 _blockBody (),
157166 ),
158167 );
159-
168+ // TODO: implement requiredImports
160169 static MethodDeclaration _emptyMethod () => new MethodDeclaration (
161170 null ,
162171 null ,
0 commit comments