File tree 3 files changed +64
-0
lines changed 3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -338,6 +338,10 @@ class LibraryAnalyzer {
338
338
List <LinterContextUnit > allUnits, {
339
339
required AnalysisOptionsImpl analysisOptions,
340
340
}) {
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
+
341
345
var unit = currentUnit.unit;
342
346
var errorReporter = unitAnalysis.errorReporter;
343
347
Original file line number Diff line number Diff line change @@ -1064,4 +1064,39 @@ prefix0.int get x => 0;
1064
1064
---
1065
1065
''' );
1066
1066
}
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
+ }
1067
1102
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments