Skip to content

Commit

Permalink
Merge pull request #530 from davidmorgan/analyzer-patch
Browse files Browse the repository at this point in the history
Update for analyzer 0.33.3.
  • Loading branch information
davidmorgan authored Nov 8, 2018
2 parents 05651f0 + 39d4b4b commit 9161418
Show file tree
Hide file tree
Showing 19 changed files with 221 additions and 57 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# 6.1.6

- Switch to new analyzer API in version `0.33.3`.

# 6.1.5

- Bump versions of `analyzer`, `analyzer_plugin`, `build`, `build_runner`.
Expand Down
1 change: 1 addition & 0 deletions built_value_generator/lib/built_value_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BuiltValueGenerator extends Generator {
@override
Future<String> generate(LibraryReader library, BuildStep buildStep) async {
final result = new StringBuffer();

try {
final enumCode = new EnumSourceLibrary(library.element).generateCode();
if (enumCode != null) result.writeln(enumCode);
Expand Down
38 changes: 27 additions & 11 deletions built_value_generator/lib/src/enum_source_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

library built_value_generator.enum_source_class;

import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/analysis/results.dart'; // ignore: implementation_imports
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value_generator/src/enum_source_field.dart';
Expand All @@ -16,10 +18,16 @@ abstract class EnumSourceClass
implements Built<EnumSourceClass, EnumSourceClassBuilder> {
ClassElement get element;

factory EnumSourceClass(ClassElement element) =>
factory EnumSourceClass(
ParsedLibraryResult parsedLibrary, ClassElement element) =>
new _$EnumSourceClass._(element: element);
EnumSourceClass._();

@memoized
ParsedLibraryResult get parsedLibrary =>
// ignore: deprecated_member_use
ParsedLibraryResultImpl.tmp(element.library);

@memoized
String get name => element.name;

Expand All @@ -42,17 +50,20 @@ abstract class EnumSourceClass

@memoized
BuiltList<EnumSourceField> get fields =>
EnumSourceField.fromClassElement(element);
EnumSourceField.fromClassElement(parsedLibrary, element);

@memoized
BuiltList<String> get constructors => new BuiltList<String>(
element.constructors.map((element) => element.computeNode().toString()));
BuiltList<String> get constructors =>
new BuiltList<String>(element.constructors.map((element) {
final declaration = parsedLibrary.getElementDeclaration(element);
return declaration?.node?.toSource() ?? '';
}));

@memoized
String get valuesIdentifier {
final getter = element.getGetter('values');
if (getter == null) return null;
final source = getter.computeNode().toSource();
final source = parsedLibrary.getElementDeclaration(getter).node.toSource();
final matches = new RegExp(r'static BuiltSet<' +
element.displayName +
r'> get values => (_\$\w+)\;')
Expand All @@ -64,7 +75,7 @@ abstract class EnumSourceClass
String get valueOfIdentifier {
final getter = element.getMethod('valueOf');
if (getter == null) return null;
final source = getter.computeNode().toSource();
final source = parsedLibrary.getElementDeclaration(getter).node.toSource();
final matches = new RegExp(r'static ' +
element.displayName +
r' valueOf\(String name\) \=\> (\_\$\w+)\(name\)\;')
Expand All @@ -76,8 +87,11 @@ abstract class EnumSourceClass
bool get usesMixin => element.library.getType(name + 'Mixin') != null;

@memoized
String get mixinDeclaration =>
element.library.getType(name + 'Mixin')?.computeNode()?.toString();
String get mixinDeclaration {
final mixinElement = element.library.getType(name + 'Mixin');
if (mixinElement == null) return null;
return parsedLibrary.getElementDeclaration(mixinElement).node.toSource();
}

@memoized
Iterable<String> get identifiers {
Expand All @@ -87,10 +101,12 @@ abstract class EnumSourceClass
]);
}

static bool isMissingImportFor(ClassElement classElement) {
static bool isMissingImportFor(
ParsedLibraryResult parsedLibrary, ClassElement classElement) {
return classElement.supertype.displayName != 'EnumClass' &&
classElement
.computeNode()
parsedLibrary
.getElementDeclaration(classElement)
.node
.toSource()
.contains('class ${classElement.displayName} extends EnumClass');
}
Expand Down
5 changes: 5 additions & 0 deletions built_value_generator/lib/src/enum_source_class.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions built_value_generator/lib/src/enum_source_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

library built_value_generator.enum_source_field;

import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
Expand All @@ -12,10 +13,12 @@ part 'enum_source_field.g.dart';

abstract class EnumSourceField
implements Built<EnumSourceField, EnumSourceFieldBuilder> {
ParsedLibraryResult get parsedLibrary;
FieldElement get element;

factory EnumSourceField(FieldElement element) =>
new _$EnumSourceField._(element: element);
factory EnumSourceField(
ParsedLibraryResult parsedLibrary, FieldElement element) =>
new _$EnumSourceField._(parsedLibrary: parsedLibrary, element: element);
EnumSourceField._();

@memoized
Expand All @@ -38,7 +41,11 @@ abstract class EnumSourceField
@memoized
String get generatedIdentifier {
final fieldName = element.displayName;
return element.computeNode().toString().substring('$fieldName = '.length);
return parsedLibrary
.getElementDeclaration(element)
.node
.toSource()
.substring('$fieldName = '.length);
}

@memoized
Expand All @@ -48,15 +55,15 @@ abstract class EnumSourceField
bool get isStatic => element.isStatic;

static BuiltList<EnumSourceField> fromClassElement(
ClassElement classElement) {
ParsedLibraryResult parsedLibrary, ClassElement classElement) {
final result = new ListBuilder<EnumSourceField>();

final enumName = classElement.displayName;
for (final fieldElement in classElement.fields) {
final type = fieldElement.getter.returnType.displayName;
if (!fieldElement.isSynthetic &&
(type == enumName || type == 'dynamic')) {
result.add(new EnumSourceField(fieldElement));
result.add(new EnumSourceField(parsedLibrary, fieldElement));
}
}

Expand Down
23 changes: 19 additions & 4 deletions built_value_generator/lib/src/enum_source_field.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions built_value_generator/lib/src/enum_source_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

library built_value_generator.enum_source_library;

import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/analysis/results.dart'; // ignore: implementation_imports
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value_generator/src/enum_source_class.dart';
Expand All @@ -22,6 +24,11 @@ abstract class EnumSourceLibrary
new _$EnumSourceLibrary._(element: element);
EnumSourceLibrary._();

@memoized
ParsedLibraryResult get parsedLibrary =>
// ignore: deprecated_member_use
ParsedLibraryResultImpl.tmp(element.library);

@memoized
String get name => element.name;

Expand All @@ -36,12 +43,12 @@ abstract class EnumSourceLibrary
final result = new ListBuilder<EnumSourceClass>();

for (final classElement in LibraryElements.getClassElements(element)) {
if (EnumSourceClass.isMissingImportFor(classElement)) {
if (EnumSourceClass.isMissingImportFor(parsedLibrary, classElement)) {
throw _makeError([
"Import EnumClass: import 'package:built_value/built_value.dart';"
]);
} else if (EnumSourceClass.needsEnumClass(classElement)) {
result.add(new EnumSourceClass(classElement));
result.add(new EnumSourceClass(parsedLibrary, classElement));
}
}
return result.build();
Expand Down
5 changes: 5 additions & 0 deletions built_value_generator/lib/src/enum_source_library.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions built_value_generator/lib/src/serializer_source_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

library built_value_generator.source_class;

import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/analysis/results.dart'; // ignore: implementation_imports
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value_generator/src/enum_source_class.dart';
Expand All @@ -29,13 +31,19 @@ abstract class SerializerSourceClass
element.library.getType(element.displayName + 'Builder'));
SerializerSourceClass._();

@memoized
ParsedLibraryResult get parsedLibrary =>
// ignore: deprecated_member_use
ParsedLibraryResultImpl.tmp(element.library);

// TODO(davidmorgan): share common code in a nicer way.
@memoized
BuiltValue get builtValueSettings => new ValueSourceClass(element).settings;

// TODO(davidmorgan): share common code in a nicer way.
@memoized
BuiltValueEnum get enumClassSettings => new EnumSourceClass(element).settings;
BuiltValueEnum get enumClassSettings =>
new EnumSourceClass(parsedLibrary, element).settings;

@memoized
String get name => element.name;
Expand All @@ -57,12 +65,16 @@ abstract class SerializerSourceClass
element.fields.where((field) => field.name == 'serializer').toList();
if (serializerFields.isEmpty) return '';
final serializerField = serializerFields.single;
return serializerField.getter?.computeNode()?.toString() ?? '';
return parsedLibrary
.getElementDeclaration(serializerField.getter)
?.node
?.toSource() ??
'';
}

@memoized
BuiltList<String> get genericParameters => new BuiltList<String>(
element.typeParameters.map((e) => e.computeNode().toString()));
BuiltList<String> get genericParameters =>
new BuiltList<String>(element.typeParameters.map((e) => e.name));

@memoized
BuiltList<String> get genericBounds =>
Expand Down Expand Up @@ -94,7 +106,7 @@ abstract class SerializerSourceClass
final builderFieldElement =
builderElement?.getField(fieldElement.displayName);
final sourceField = new SerializerSourceField(
builtValueSettings, fieldElement, builderFieldElement);
builtValueSettings, parsedLibrary, fieldElement, builderFieldElement);
if (sourceField.isSerializable) {
result.add(sourceField);
}
Expand Down Expand Up @@ -258,7 +270,7 @@ class _\$${name}Serializer implements StructuredSerializer<$name> {
.fields
.where((field) => field.isConst && field.isStatic)
.forEach((field) {
final enumSourceField = new EnumSourceField(field);
final enumSourceField = new EnumSourceField(parsedLibrary, field);
if (enumSourceField.settings.wireName != null) {
b[field.name] = enumSourceField.settings.wireName;
}
Expand Down
Loading

0 comments on commit 9161418

Please sign in to comment.