Skip to content

Commit 1fad5e4

Browse files
pqCommit Queue
authored and
Commit Queue
committed
skip computing lints for macro generated augmentations
Fixes: #54875 Change-Id: Ia1e1f54268ffce82b6348b6edf3d1f0c976298fa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352461 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 885126e commit 1fad5e4

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ class LibraryAnalyzer {
338338
List<LinterContextUnit> allUnits, {
339339
required AnalysisOptionsImpl analysisOptions,
340340
}) {
341+
// Skip computing lints on macro generated augmentations.
342+
// See: https://github.com/dart-lang/sdk/issues/54875
343+
if (unitAnalysis.file.isMacroAugmentation) return;
344+
341345
var unit = currentUnit.unit;
342346
var errorReporter = unitAnalysis.errorReporter;
343347

pkg/analyzer/test/src/dart/resolution/macro_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,4 +1064,39 @@ prefix0.int get x => 0;
10641064
---
10651065
''');
10661066
}
1067+
1068+
test_withLints() async {
1069+
writeTestPackageAnalysisOptionsFile(AnalysisOptionsFileConfig(
1070+
lints: ['unnecessary_this'],
1071+
experiments: ['macros'],
1072+
));
1073+
1074+
/// A macro that will produce an augmented class with `unnecessary_this`
1075+
/// violations.
1076+
var macroFile = newFile(
1077+
'$testPackageLibPath/auto_to_string.dart',
1078+
getMacroCode('example/auto_to_string.dart'),
1079+
);
1080+
await assertErrorsInFile2(macroFile, []);
1081+
1082+
var testFile = newFile('$testPackageLibPath/test.dart', r'''
1083+
import 'auto_to_string.dart';
1084+
1085+
class User {
1086+
final String name;
1087+
final int age;
1088+
User(this.name, this.age);
1089+
1090+
@override
1091+
@AutoToString()
1092+
String toString();
1093+
}
1094+
''');
1095+
await assertErrorsInFile2(testFile, []);
1096+
1097+
var macroGeneratedFile = getFile('$testPackageLibPath/test.macro.dart');
1098+
await assertErrorsInFile2(macroGeneratedFile, [
1099+
// No lints.
1100+
]);
1101+
}
10671102
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:_fe_analyzer_shared/src/macros/api.dart';
6+
7+
/*macro*/ class AutoToString implements MethodDefinitionMacro {
8+
const AutoToString();
9+
10+
@override
11+
Future<void> buildDefinitionForMethod(
12+
MethodDeclaration method, FunctionDefinitionBuilder builder) async {
13+
var clazz = await builder.typeDeclarationOf(method.definingType);
14+
var fields = await builder.fieldsOf(clazz);
15+
builder.augment(FunctionBodyCode.fromParts([
16+
'=> """\n${clazz.identifier.name} {\n',
17+
for (var field in fields) ...[
18+
' ${field.identifier.name}: \${', // e.g., `age: `
19+
field.identifier, // e.g., `${this.age}`
20+
'}\n',
21+
],
22+
'}""";',
23+
]));
24+
}
25+
}

0 commit comments

Comments
 (0)