Skip to content

skip files with only known annotation names #765

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 3 commits into from
Jul 15, 2025
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
5 changes: 5 additions & 0 deletions source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.0.0-wip

- Update the GeneratorForAnnotation optimization to skip files with well known
annotation names such as `override`, `Deprecated`, and `pragma`.

## 3.0.0-dev

- **Breaking Change**: use the new `element2` APIs in `analyzer`. Builders that
Expand Down
22 changes: 18 additions & 4 deletions source_gen/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class _Builder extends Builder {
if (!await resolver.isLibrary(buildStep.inputId)) return;

if (_generators.every((g) => g is GeneratorForAnnotation) &&
!(await _hasAnyTopLevelAnnotations(
!(await _hasInterestingTopLevelAnnotations(
buildStep.inputId,
resolver,
buildStep,
Expand Down Expand Up @@ -382,7 +382,7 @@ Stream<GeneratedOutput> _generate(
}
}

Future<bool> _hasAnyTopLevelAnnotations(
Future<bool> _hasInterestingTopLevelAnnotations(
AssetId input,
Resolver resolver,
BuildStep buildStep,
Expand All @@ -399,16 +399,30 @@ Future<bool> _hasAnyTopLevelAnnotations(
}
}
for (var declaration in parsed.declarations) {
if (declaration.metadata.isNotEmpty) return true;
if (declaration.metadata.any(_isUnknownAnnotation)) {
return true;
}
}
for (var partId in partIds) {
if (await _hasAnyTopLevelAnnotations(partId, resolver, buildStep)) {
if (await _hasInterestingTopLevelAnnotations(partId, resolver, buildStep)) {
return true;
}
}
return false;
}

bool _isUnknownAnnotation(Annotation annotation) {
const knownAnnotationNames = {
// Annotations from dart:core, there is an assumption that people are not
// overriding these.
'deprecated',
'Deprecated',
'override',
'pragma',
};
return !knownAnnotationNames.contains(annotation.name.name);
}

const defaultFileHeader = '// GENERATED CODE - DO NOT MODIFY BY HAND';

String _defaultFormatOutput(String code, Version version) =>
Expand Down
2 changes: 1 addition & 1 deletion source_gen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: source_gen
version: 3.0.0-dev
version: 3.0.0-wip
description: >-
Source code generation builders and utilities for the Dart build system
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen
Expand Down
6 changes: 5 additions & 1 deletion source_gen/test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,11 @@ foo generated content
''',
testLibPartContent: '''
part of 'test_lib.dart';
@deprecated
// Use this to avoid the short circuit.
const deprecated2 = deprecated;
@deprecated2
int x;
''',
);
Expand Down
64 changes: 36 additions & 28 deletions source_gen/test/generator_for_annotation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,37 @@ $dartFormatWidth
}
});

test(
'Does not resolve the library if there are no top level annotations',
() async {
final builder = LibraryBuilder(
_StubGenerator<Deprecated>('Deprecated', elementBehavior: (_) => null),
);
final input = AssetId('a', 'lib/a.dart');
final assets = {input: 'main() {}'};

final readerWriter =
TestReaderWriter()..testing.writeString(input, assets[input]!);

final resolver = _TestingResolver(assets);

await runBuilder(
builder,
[input],
readerWriter,
readerWriter,
_FixedResolvers(resolver),
);
test('Does not resolve the library if there are no interesting top level '
'annotations', () async {
final builder = LibraryBuilder(
_StubGenerator<Deprecated>('Deprecated', elementBehavior: (_) => null),
);
final input = AssetId('a', 'lib/a.dart');
final assets = {
input: '''
@Deprecated()
@deprecated
@override
@pragma('')
main() {}''',
};

final readerWriter =
TestReaderWriter()..testing.writeString(input, assets[input]!);

final resolver = _TestingResolver(assets);

await runBuilder(
builder,
[input],
readerWriter,
readerWriter,
_FixedResolvers(resolver),
);

expect(resolver.parsedUnits, {input});
expect(resolver.resolvedLibs, isEmpty);
},
);
expect(resolver.parsedUnits, {input});
expect(resolver.resolvedLibs, isEmpty);
});

test('applies to annotated libraries', () async {
final builder = LibraryBuilder(
Expand Down Expand Up @@ -289,13 +294,16 @@ class _StubGenerator<T> extends GeneratorForAnnotation<T> {

const _inputMap = {
'a|lib/file.dart': '''
@deprecated
// Use this to avoid the short circuit.
const deprecated2 = deprecated;

@deprecated2
final foo = 'foo';

@deprecated
@deprecated2
final bar = 'bar';

@deprecated
@deprecated2
final baz = 'baz';
''',
};
Expand Down