From 1fad5e41f631c4fda7bd8beca970ea83f909983a Mon Sep 17 00:00:00 2001 From: pq Date: Wed, 14 Feb 2024 19:34:37 +0000 Subject: [PATCH] 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 Reviewed-by: Konstantin Shcheglov Commit-Queue: Phil Quitslund --- .../src/dart/analysis/library_analyzer.dart | 4 +++ .../test/src/dart/resolution/macro_test.dart | 35 +++++++++++++++++++ .../summary/macro/example/auto_to_string.dart | 25 +++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 pkg/analyzer/test/src/summary/macro/example/auto_to_string.dart diff --git a/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart b/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart index cab160f6cc5..ecb2e4db55b 100644 --- a/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart +++ b/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart @@ -338,6 +338,10 @@ class LibraryAnalyzer { List 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; diff --git a/pkg/analyzer/test/src/dart/resolution/macro_test.dart b/pkg/analyzer/test/src/dart/resolution/macro_test.dart index 119e52836a3..35a0591fd21 100644 --- a/pkg/analyzer/test/src/dart/resolution/macro_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/macro_test.dart @@ -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. + ]); + } } diff --git a/pkg/analyzer/test/src/summary/macro/example/auto_to_string.dart b/pkg/analyzer/test/src/summary/macro/example/auto_to_string.dart new file mode 100644 index 00000000000..1cd0aba6f85 --- /dev/null +++ b/pkg/analyzer/test/src/summary/macro/example/auto_to_string.dart @@ -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 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', + ], + '}""";', + ])); + } +}