Skip to content

[breaking] Use element2 implementations everywhere, deprecate API with "2". #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/lib/src/member_count_library_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MemberCountLibraryGenerator extends Generator {
final topLevelVarCount = topLevelNumVariables(library).length;

return '''
// Source library: ${library.element2.uri}
// Source library: ${library.element.uri}
const topLevelNumVarCount = $topLevelVarCount;
''';
}
Expand Down
2 changes: 1 addition & 1 deletion example/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:source_gen/source_gen.dart';
/// Returns all [TopLevelVariableElement2] members in [reader]'s library that
/// have a type of [num].
Iterable<TopLevelVariableElement2> topLevelNumVariables(LibraryReader reader) =>
reader.allElements2.whereType<TopLevelVariableElement2>().where(
reader.allElements.whereType<TopLevelVariableElement2>().where(
(element) =>
element.type.isDartCoreNum ||
element.type.isDartCoreInt ||
Expand Down
4 changes: 2 additions & 2 deletions source_gen/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class CombiningBuilder implements Builder {

final inputLibrary = await buildStep.inputLibrary2;
final outputId = buildStep.allowedOutputs.single;
final partOfUri = uriOfPartial2(inputLibrary, buildStep.inputId, outputId);
final partOfUri = uriOfPartial(inputLibrary, buildStep.inputId, outputId);

// Ensure that the input has a correct `part` statement.
final libraryUnit =
Expand All @@ -143,7 +143,7 @@ class CombiningBuilder implements Builder {

final output = '''
$defaultFileHeader
${languageOverrideForLibrary2(inputLibrary)}$ignoreForFile$preamble
${languageOverrideForLibrary(inputLibrary)}$ignoreForFile$preamble
part of '$partOfUri';

$assets
Expand Down
19 changes: 7 additions & 12 deletions source_gen/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import 'dart:convert';

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:build/build.dart';
import 'package:dart_style/dart_style.dart';
Expand Down Expand Up @@ -130,12 +129,12 @@ class _Builder extends Builder {

if (!_isLibraryBuilder) {
final asset = buildStep.inputId;
final partOfUri = uriOfPartial2(library2, asset, outputId);
final partOfUri = uriOfPartial(library2, asset, outputId);
contentBuffer.writeln();

if (this is PartBuilder) {
contentBuffer
..write(languageOverrideForLibrary2(library2))
..write(languageOverrideForLibrary(library2))
..writeln('part of \'$partOfUri\';');
final part = computePartUrl(buildStep.inputId, outputId);

Expand Down Expand Up @@ -358,7 +357,7 @@ Stream<GeneratedOutput> _generate(
List<Generator> generators,
BuildStep buildStep,
) async* {
final libraryReader = LibraryReader.v2(library2);
final libraryReader = LibraryReader(library2);
for (var i = 0; i < generators.length; i++) {
final gen = generators[i];
var msg = 'Running $gen';
Expand Down Expand Up @@ -425,20 +424,16 @@ const partIdRegExpLiteral = r'[A-Za-z_\d-]+';

final _partIdRegExp = RegExp('^$partIdRegExpLiteral\$');

@Deprecated('Use languageOverrideForLibrary2 instead')
String languageOverrideForLibrary(LibraryElement library) {
String languageOverrideForLibrary(LibraryElement2 library) {
final override = library.languageVersion.override;
return override == null
? ''
: '// @dart=${override.major}.${override.minor}\n';
}

String languageOverrideForLibrary2(LibraryElement2 library) {
final override = library.languageVersion.override;
return override == null
? ''
: '// @dart=${override.major}.${override.minor}\n';
}
@Deprecated('Use languageOverrideForLibrary instead')
String languageOverrideForLibrary2(LibraryElement2 library) =>
languageOverrideForLibrary(library);

/// A comment configuring `dart_style` to use the default code width so no
/// configuration discovery is required.
Expand Down
2 changes: 1 addition & 1 deletion source_gen/lib/src/constants/reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class _DartObjectConstant extends ConstantReader {
ConstantReader read(String field) {
final reader = peek(field);
if (reader == null) {
assertHasField2(objectValue.type!.element3 as InterfaceElement2, field);
assertHasField(objectValue.type!.element3 as InterfaceElement2, field);
return const _NullConstant();
}
return reader;
Expand Down
11 changes: 6 additions & 5 deletions source_gen/lib/src/constants/revive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Revivable reviveInstance(DartObject object, [LibraryElement2? origin]) {
}
}
origin ??= element!.library2;
var url = Uri.parse(urlOfElement2(element!));
var url = Uri.parse(urlOfElement(element!));
if (element is TopLevelFunctionElement || element is LocalFunctionElement) {
return Revivable._(source: url.removeFragment(), accessor: element.name3!);
}
Expand Down Expand Up @@ -67,8 +67,9 @@ Revivable reviveInstance(DartObject object, [LibraryElement2? origin]) {
}

for (final type in origin!.classes) {
for (final e in type.fields2
.where((f) => f.isConst && f.computeConstantValue() == object)) {
for (final e in type.fields2.where(
(f) => f.isConst && f.computeConstantValue() == object,
)) {
final result = Revivable._(
source: url.removeFragment(),
accessor: '${type.name3}.${e.name3}',
Expand All @@ -80,7 +81,7 @@ Revivable reviveInstance(DartObject object, [LibraryElement2? origin]) {
}
final i = (object as DartObjectImpl).getInvocation();
if (i != null) {
url = Uri.parse(urlOfElement2(i.constructor2.enclosingElement2));
url = Uri.parse(urlOfElement(i.constructor2.enclosingElement2));
final result = Revivable._(
source: url,
accessor: i.constructor.name,
Expand All @@ -95,7 +96,7 @@ Revivable reviveInstance(DartObject object, [LibraryElement2? origin]) {
(f) => f.isConst && f.computeConstantValue() == object,
)) {
final result = Revivable._(
source: Uri.parse(urlOfElement2(origin)).replace(fragment: ''),
source: Uri.parse(urlOfElement(origin)).replace(fragment: ''),
accessor: e.name3!,
);
if (tryResult(result)) {
Expand Down
34 changes: 8 additions & 26 deletions source_gen/lib/src/constants/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,12 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';

/// Throws a [FormatException] if [root] does not have a given field [name].
///
/// Super types [InterfaceElement.supertype] are also checked before throwing.
@Deprecated('Use assertHasField2() instead')
void assertHasField(InterfaceElement root, String name) {
InterfaceElement? element = root;
while (element != null) {
final field = element.getField(name);
if (field != null) {
return;
}
element = element.supertype?.element;
}
final allFields = {
...root.fields,
for (var t in root.allSupertypes) ...t.element.fields,
};

throw FormatException(
'Class ${root.name} does not have field "$name".',
'Fields: \n - ${allFields.map((e) => e.name).join('\n - ')}',
);
}

/// Throws a [FormatException] if [root] does not have a given field [name].
///
/// Super types [InterfaceElement2.supertype] are also checked before throwing.
void assertHasField2(InterfaceElement2 root, String name) {
void assertHasField(InterfaceElement2 root, String name) {
InterfaceElement2? element = root;
while (element != null) {
final field = element.getField2(name);
Expand All @@ -53,6 +28,13 @@ void assertHasField2(InterfaceElement2 root, String name) {
);
}

/// Throws a [FormatException] if [root] does not have a given field [name].
///
/// Super types [InterfaceElement2.supertype] are also checked before throwing.
@Deprecated('Use assertHasField() instead')
void assertHasField2(InterfaceElement2 root, String name) =>
assertHasField(root, name);

/// Returns whether or not [object] is or represents a `null` value.
bool isNullLike(DartObject? object) => object?.isNull != false;

Expand Down
23 changes: 10 additions & 13 deletions source_gen/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import 'dart:async';

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
// ignore: implementation_imports
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:build/build.dart';

import 'library.dart';
Expand Down Expand Up @@ -52,39 +49,39 @@ class InvalidGenerationSource implements Exception {
///
/// May be `null` if the error had no associated element, or if the location
/// was passed with [node].
final Element2? element2;
final Element2? element;

/// The AST Node associated with this error.
///
/// May be `null` if the error has no associated node in the input source
/// code, or if the location was passed with [element].
final AstNode? node;

@Deprecated('use v2 instead')
InvalidGenerationSource(
this.message, {
this.todo = '',
Element? element,
this.element,
this.node,
}) : element2 = element?.asElement2;
});

@Deprecated('use the unnamed constructor instead')
InvalidGenerationSource.v2(
this.message, {
this.todo = '',
Element2? element,
this.element,
this.node,
}) : element2 = element;
});

@Deprecated('use element2 instead')
Element? get element => element2?.asElement;
@Deprecated('use element instead')
Element2? get element2 => element;

@override
String toString() {
final buffer = StringBuffer(message);

if (element2 case final element2?) {
if (element case final element2?) {
try {
final span = spanForElement2(element2);
final span = spanForElement(element2);
buffer
..writeln()
..writeln(span.start.toolString)
Expand Down
13 changes: 5 additions & 8 deletions source_gen/lib/src/generator_for_annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'dart:async';

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:build/build.dart';

Expand Down Expand Up @@ -74,14 +73,12 @@ abstract class GeneratorForAnnotation<T> extends Generator {
typeChecker,
throwOnUnresolved: throwOnUnresolved,
)) {
var generatedValue = generateForAnnotatedElement2(
annotatedElement.element2,
var generatedValue = generateForAnnotatedElement(
annotatedElement.element,
annotatedElement.annotation,
buildStep,
);
generatedValue ??= generateForAnnotatedElement(
// Support "generateForAnnotatedElements" until it's removed.
// ignore: analyzer_use_new_elements
generatedValue ??= generateForAnnotatedElement2(
annotatedElement.element,
annotatedElement.annotation,
buildStep,
Expand Down Expand Up @@ -112,9 +109,8 @@ abstract class GeneratorForAnnotation<T> extends Generator {
///
/// Implementations should return `null` when no content is generated. Empty
/// or whitespace-only [String] instances are also ignored.
@Deprecated('use generateForAnnotatedElement2 instead')
dynamic generateForAnnotatedElement(
Element element,
Element2 element,
ConstantReader annotation,
BuildStep buildStep,
) {}
Expand All @@ -136,6 +132,7 @@ abstract class GeneratorForAnnotation<T> extends Generator {
///
/// Implementations should return `null` when no content is generated. Empty
/// or whitespace-only [String] instances are also ignored.
@Deprecated('use generateForAnnotatedElement instead')
dynamic generateForAnnotatedElement2(
Element2 element,
ConstantReader annotation,
Expand Down
Loading