Skip to content
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: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ analyzer:
strict-casts: true
strict-inference: true
strict-raw-types: true
errors:
deprecated_member_use_from_same_package: ignore

linter:
rules:
Expand Down
6 changes: 3 additions & 3 deletions source_gen/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ class CombiningBuilder implements Builder {
.join('\n\n');
if (assets.isEmpty) return;

final inputLibrary = await buildStep.inputLibrary;
final inputLibrary = await buildStep.inputLibrary2;
final outputId = buildStep.allowedOutputs.single;
final partOfUri = uriOfPartial(inputLibrary, buildStep.inputId, outputId);
final partOfUri = uriOfPartial2(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
${languageOverrideForLibrary(inputLibrary)}$ignoreForFile$preamble
${languageOverrideForLibrary2(inputLibrary)}$ignoreForFile$preamble
part of '$partOfUri';

$assets
Expand Down
19 changes: 12 additions & 7 deletions source_gen/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,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';
// ignore: implementation_imports
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:build/build.dart';
import 'package:dart_style/dart_style.dart';
import 'package:pub_semver/pub_semver.dart';
Expand Down Expand Up @@ -114,7 +112,6 @@ class _Builder extends Builder {
LibraryElement2 library2,
BuildStep buildStep,
) async {
final library = library2.asElement;
final generatedOutputs =
await _generate(library2, _generators, buildStep).toList();

Expand All @@ -133,12 +130,12 @@ class _Builder extends Builder {

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

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

Expand Down Expand Up @@ -181,12 +178,12 @@ class _Builder extends Builder {

try {
genPartContent =
formatOutput(genPartContent, library.languageVersion.effective);
formatOutput(genPartContent, library2.languageVersion.effective);
} catch (e, stack) {
log.severe(
'''
An error `${e.runtimeType}` occurred while formatting the generated source for
`${library.identifier}`
`${library2.identifier}`
which was output to
`${outputId.path}`.
This may indicate an issue in the generator, the input source code, or in the
Expand Down Expand Up @@ -428,13 +425,21 @@ const partIdRegExpLiteral = r'[A-Za-z_\d-]+';

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

@Deprecated('Use languageOverrideForLibrary2 instead')
String languageOverrideForLibrary(LibraryElement 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';
}

/// A comment configuring `dart_style` to use the default code width so no
/// configuration discovery is required.
const dartFormatWidth = '// dart format width=80';
1 change: 1 addition & 0 deletions source_gen/lib/src/constants/revive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import '../utils.dart';
/// **NOTE**: Some returned [Revivable] instances are not representable as valid
/// Dart source code (such as referencing private constructors). It is up to the
/// build tool(s) using this library to surface error messages to the user.
@Deprecated('use reviveInstance2 instead')
Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
final objectType = object.type;
Element? element = objectType!.alias?.element;
Expand Down
2 changes: 1 addition & 1 deletion source_gen/lib/src/constants/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void assertHasField(InterfaceElement root, String name) {

/// Throws a [FormatException] if [root] does not have a given field [name].
///
/// Super types [InterfaceElement.supertype] are also checked before throwing.
/// Super types [InterfaceElement2.supertype] are also checked before throwing.
void assertHasField2(InterfaceElement2 root, String name) {
InterfaceElement2? element = root;
while (element != null) {
Expand Down
8 changes: 5 additions & 3 deletions source_gen/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class InvalidGenerationSource implements Exception {
/// code, or if the location was passed with [element].
final AstNode? node;

@Deprecated('use v2 instead')
InvalidGenerationSource(
this.message, {
this.todo = '',
Expand All @@ -74,15 +75,16 @@ class InvalidGenerationSource implements Exception {
this.node,
}) : element2 = element;

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

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

if (element case final element?) {
if (element2 case final element2?) {
try {
final span = spanForElement(element);
final span = spanForElement2(element2);
buffer
..writeln()
..writeln(span.start.toolString)
Expand All @@ -92,7 +94,7 @@ class InvalidGenerationSource implements Exception {
// associated source. We can still give the name.
buffer
..writeln()
..writeln('Cause: $element');
..writeln('Cause: $element2');
}
}

Expand Down
13 changes: 8 additions & 5 deletions source_gen/lib/src/generator_for_annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import 'type_checker.dart';
/// Elements which are not at the top level, such as the members of a class or
/// extension, are not searched for annotations. To operate on, for instance,
/// annotated fields of a class ensure that the class itself is annotated with
/// [T] and use the [Element] to iterate over fields. The [TypeChecker] utility
/// [T] and use the [Element2] to iterate over fields. The [TypeChecker] utility
/// may be helpful to check which elements have a given annotation.
abstract class GeneratorForAnnotation<T> extends Generator {
final bool throwOnUnresolved;
Expand Down Expand Up @@ -74,13 +74,15 @@ abstract class GeneratorForAnnotation<T> extends Generator {
typeChecker,
throwOnUnresolved: throwOnUnresolved,
)) {
var generatedValue = generateForAnnotatedElement(
annotatedElement.element,
var generatedValue = generateForAnnotatedElement2(
annotatedElement.element2,
annotatedElement.annotation,
buildStep,
);
generatedValue ??= generateForAnnotatedElement2(
annotatedElement.element2,
generatedValue ??= generateForAnnotatedElement(
// Support "generateForAnnotatedElements" until it's removed.
// ignore: analyzer_use_new_elements
annotatedElement.element,
annotatedElement.annotation,
buildStep,
);
Expand Down Expand Up @@ -110,6 +112,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 generateForAnnotatedElement2 instead')
dynamic generateForAnnotatedElement(
Element element,
ConstantReader annotation,
Expand Down
34 changes: 18 additions & 16 deletions source_gen/lib/src/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class AnnotatedElement {

const AnnotatedElement(this.annotation, this.element2);

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

Metadata? get metadata2 {
Expand All @@ -40,17 +41,20 @@ class AnnotatedElement {
}
}

/// A high-level wrapper API with common functionality for [LibraryElement].
/// A high-level wrapper API with common functionality for [LibraryElement2].
class LibraryReader {
final LibraryElement2 element2;

@Deprecated('use v2 instead')
LibraryReader(LibraryElement element) : this.v2(element.asElement2);

LibraryReader.v2(this.element2);

@Deprecated('use element2 instead')
LibraryElement get element => element2.asElement;

/// All of the declarations in this library.
@Deprecated('use allElements2 instead')
Iterable<Element> get allElements => [
element,
...element.topLevelElements,
Expand All @@ -63,13 +67,15 @@ class LibraryReader {
Iterable<Element2> get allElements2 => [element2, ...element2.children2];

/// All of the elements representing classes in this library.
@Deprecated('use classes2 instead')
Iterable<ClassElement> get classes =>
element.units.expand((cu) => cu.classes);

/// All of the elements representing classes in this library.
Iterable<ClassElement2> get classes2 => element2.classes;

/// All of the elements representing enums in this library.
@Deprecated('use enums2 instead')
Iterable<EnumElement> get enums => element.units.expand((cu) => cu.enums);

/// All of the elements representing enums in this library.
Expand All @@ -80,19 +86,14 @@ class LibraryReader {
TypeChecker checker, {
bool throwOnUnresolved = true,
}) sync* {
for (final element in allElements) {
final annotation = checker.firstAnnotationOf(
for (final element in allElements2) {
final annotation = checker.firstAnnotationOf2(
element,
throwOnUnresolved: throwOnUnresolved,
);

final element2 = element.asElement2;
if (element2 == null) {
return;
}

if (annotation != null) {
yield AnnotatedElement(ConstantReader(annotation), element2);
yield AnnotatedElement(ConstantReader(annotation), element);
}
}
}
Expand Down Expand Up @@ -126,13 +127,13 @@ class LibraryReader {
TypeChecker checker, {
bool throwOnUnresolved = true,
}) sync* {
for (final element in allElements) {
final annotation = checker.firstAnnotationOfExact(
for (final element in allElements2) {
final annotation = checker.firstAnnotationOfExact2(
element,
throwOnUnresolved: throwOnUnresolved,
);
if (annotation != null) {
yield AnnotatedElement(ConstantReader(annotation), element.asElement2!);
yield AnnotatedElement(ConstantReader(annotation), element);
}
}
}
Expand All @@ -147,12 +148,12 @@ class LibraryReader {
return type is ClassElement ? type : null;
}

/// Returns a top-level [ClassElement] publicly visible in by [name].
/// Returns a top-level [ClassElement2] publicly visible in by [name].
///
/// Unlike [LibraryElement.getClass], this also correctly traverses
/// Unlike `LibraryElement2.getClass`, this also correctly traverses
/// identifiers that are accessible via one or more `export` directives.
ClassElement2? findType2(String name) {
final type = element.exportNamespace.get2(name);
final type = element2.exportNamespace.get2(name);
return type is ClassElement2 ? type : null;
}

Expand All @@ -166,6 +167,7 @@ class LibraryReader {
///
/// This is a typed convenience function for using [pathToUrl], and the same
/// API restrictions hold around supported schemes and relative paths.
@Deprecated('use pathToElement2 instead')
Uri pathToElement(Element element) => pathToUrl(element.source!.uri);

/// Returns a [Uri] from the current library to the target [element].
Expand Down Expand Up @@ -210,7 +212,7 @@ class LibraryReader {
if (to.pathSegments.length > 1 && to.pathSegments[1] == 'lib') {
return assetToPackageUrl(to);
}
var from = element.source.uri;
var from = element2.uri;
// Normalize (convert to an asset: URL).
from = normalizeUrl(from);
if (_isRelative(from, to)) {
Expand Down
1 change: 1 addition & 0 deletions source_gen/lib/src/span_for_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'utils.dart';
///
/// Not all results from the analyzer API may return source information as part
/// of the element, so [file] may need to be manually provided in those cases.
@Deprecated('use spanForElement2 instead')
SourceSpan spanForElement(Element element, [SourceFile? file]) {
final url = assetToPackageUrl(element.source!.uri);
if (file == null) {
Expand Down
Loading