skip computing lints for macro generated augmentations

Fixes: https://github.com/dart-lang/sdk/issues/54875

Change-Id: Ia1e1f54268ffce82b6348b6edf3d1f0c976298fa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352461
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
pq 2024-02-14 19:34:37 +00:00 committed by Commit Queue
parent 885126e51b
commit 1fad5e41f6
3 changed files with 64 additions and 0 deletions

View file

@ -338,6 +338,10 @@ class LibraryAnalyzer {
List<LinterContextUnit> allUnits, {
required AnalysisOptionsImpl analysisOptions,
}) {
// Skip computing lints on macro generated augmentations.
// See: https://github.com/dart-lang/sdk/issues/54875
if (unitAnalysis.file.isMacroAugmentation) return;
var unit = currentUnit.unit;
var errorReporter = unitAnalysis.errorReporter;

View file

@ -1064,4 +1064,39 @@ prefix0.int get x => 0;
---
''');
}
test_withLints() async {
writeTestPackageAnalysisOptionsFile(AnalysisOptionsFileConfig(
lints: ['unnecessary_this'],
experiments: ['macros'],
));
/// A macro that will produce an augmented class with `unnecessary_this`
/// violations.
var macroFile = newFile(
'$testPackageLibPath/auto_to_string.dart',
getMacroCode('example/auto_to_string.dart'),
);
await assertErrorsInFile2(macroFile, []);
var testFile = newFile('$testPackageLibPath/test.dart', r'''
import 'auto_to_string.dart';
class User {
final String name;
final int age;
User(this.name, this.age);
@override
@AutoToString()
String toString();
}
''');
await assertErrorsInFile2(testFile, []);
var macroGeneratedFile = getFile('$testPackageLibPath/test.macro.dart');
await assertErrorsInFile2(macroGeneratedFile, [
// No lints.
]);
}
}

View file

@ -0,0 +1,25 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:_fe_analyzer_shared/src/macros/api.dart';
/*macro*/ class AutoToString implements MethodDefinitionMacro {
const AutoToString();
@override
Future<void> buildDefinitionForMethod(
MethodDeclaration method, FunctionDefinitionBuilder builder) async {
var clazz = await builder.typeDeclarationOf(method.definingType);
var fields = await builder.fieldsOf(clazz);
builder.augment(FunctionBodyCode.fromParts([
'=> """\n${clazz.identifier.name} {\n',
for (var field in fields) ...[
' ${field.identifier.name}: \${', // e.g., `age: `
field.identifier, // e.g., `${this.age}`
'}\n',
],
'}""";',
]));
}
}