Skip to content

Commit fd2fc3a

Browse files
authored
avoid computeNode calls (#91)
* avoid computeNode calls in annotation.dart Going through computeNode() and AST types is not guaranteed to work for all Element sources. Instead go through only statically available Element types. - Avoid computeNode() on the ConstructorElement by reading parameters and names directly - Avoid computedNode() on the PropertyAccessorElement.variable by checking if it is a ConstVariableElement with an available InstanceCreationExpression - Update imports for Element classes to a non-deprecated library * Bump version and update changelog * Fix unnecessarily typed variable
1 parent 01e4a83 commit fd2fc3a

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.1+2
2+
3+
* Avoid calling `computeNode()` while instantiating annotation values
4+
15
## 0.5.1+1
26

37
* Support the latest version of `build` package.

lib/src/annotation.dart

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import 'dart:mirrors';
1010
import 'package:analyzer/dart/ast/ast.dart';
1111
import 'package:analyzer/dart/element/element.dart';
1212
import 'package:analyzer/dart/element/type.dart';
13+
import 'package:analyzer/src/dart/element/element.dart';
1314
import 'package:analyzer/src/generated/constant.dart';
14-
import 'package:analyzer/src/generated/element.dart'
15-
show ConstructorElementImpl;
1615
import 'package:analyzer/src/generated/resolver.dart';
1716
import 'package:analyzer/src/generated/utilities_dart.dart';
1817
import 'package:path/path.dart' as p;
@@ -32,11 +31,13 @@ dynamic instantiateAnnotation(ElementAnnotation annotation) {
3231
var element = annotation.element;
3332

3433
if (element is PropertyAccessorElement) {
35-
var initializer = ((element as PropertyAccessorElement)
36-
.variable
37-
.computeNode() as VariableDeclaration)
38-
.initializer as InstanceCreationExpression;
39-
element = initializer.staticElement;
34+
var variable = (element as PropertyAccessorElement).variable;
35+
if (variable is ConstVariableElement) {
36+
var expression = (variable as ConstVariableElement).constantInitializer;
37+
if (expression is InstanceCreationExpression) {
38+
element = expression.staticElement;
39+
}
40+
}
4041
}
4142

4243
if (element is ConstructorElement) {
@@ -136,11 +137,9 @@ final _cannotCreate = new Object();
136137

137138
dynamic _createFromConstructor(
138139
ConstructorElementImpl ctor, DartObjectImpl obj) {
139-
var ctorDeclaration = ctor.computeNode();
140-
141140
var positionalArgs = [];
142141
var namedArgs = <Symbol, dynamic>{};
143-
for (var p in ctorDeclaration.parameters.parameterElements) {
142+
for (var p in ctor.parameters) {
144143
var paramName = p.name;
145144
String fieldName;
146145
if (p is FieldFormalParameterElement) {
@@ -178,12 +177,7 @@ dynamic _createFromConstructor(
178177
}
179178
}
180179

181-
Symbol ctorName;
182-
if (ctorDeclaration.name == null) {
183-
ctorName = const Symbol('');
184-
} else {
185-
ctorName = new Symbol(ctorDeclaration.name.name);
186-
}
180+
var ctorName = new Symbol(ctor.name ?? '');
187181

188182
var declarationMirror =
189183
_getDeclarationMirrorFromType(ctor.enclosingElement.type) as ClassMirror;

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 0.5.1+1
2+
version: 0.5.1+2
33
author: Dart Team <[email protected]>
44
description: Automatic sourcecode generation for Dart
55
homepage: https://github.com/dart-lang/source_gen

0 commit comments

Comments
 (0)