11// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22// SPDX-License-Identifier: Apache-2.0
33
4- import 'package:analyzer/error/listener.dart' ;
5- import 'package:custom_lint_builder/custom_lint_builder.dart' ;
4+ import 'package:analyzer/analysis_rule/analysis_rule.dart' ;
5+ import 'package:analyzer/analysis_rule/rule_context.dart' ;
6+ import 'package:analyzer/analysis_rule/rule_visitor_registry.dart' ;
7+ import 'package:analyzer/dart/ast/ast.dart' ;
8+ import 'package:analyzer/dart/ast/visitor.dart' ;
9+ import 'package:analyzer/error/error.dart' ;
610
711/// The expected license header lines.
812const licenseHeader = [
@@ -25,32 +29,54 @@ const licenseHeader = [
2529/// ```dart
2630/// library my_library;
2731/// ```
28- class MissingLicenseHeader extends DartLintRule {
32+ class MissingLicenseHeader extends AnalysisRule {
2933 /// Creates a new [MissingLicenseHeader] lint rule.
30- MissingLicenseHeader () : super (code: _code);
34+ MissingLicenseHeader ()
35+ : super (
36+ name: 'missing_license_header' ,
37+ description:
38+ 'Dart files must start with the Amazon copyright and '
39+ 'Apache-2.0 license header.' ,
40+ );
3141
32- static const _code = LintCode (
33- name : 'missing_license_header' ,
34- problemMessage :
35- 'Dart files must start with the Amazon copyright and '
36- 'Apache-2.0 license header.' ,
42+ /// The lint code reported by this rule.
43+ static const LintCode code = LintCode (
44+ 'missing_license_header' ,
45+ 'Dart files must start with the Amazon copyright and Apache-2.0 '
46+ 'license header.' ,
3747 correctionMessage: 'Add the license header to the top of this file.' ,
3848 );
3949
4050 @override
41- void run (
42- CustomLintResolver resolver,
43- ErrorReporter reporter,
44- CustomLintContext context,
51+ LintCode get diagnosticCode => code;
52+
53+ @override
54+ void registerNodeProcessors (
55+ RuleVisitorRegistry registry,
56+ RuleContext context,
4557 ) {
46- context.registry.addCompilationUnit ((node) {
47- final content = resolver.source.contents.data;
58+ final visitor = _Visitor (this , context);
59+ registry.addCompilationUnit (this , visitor);
60+ }
61+ }
62+
63+ class _Visitor extends SimpleAstVisitor <void > {
64+ _Visitor (this .rule, this .context);
65+
66+ final AnalysisRule rule;
67+ final RuleContext context;
68+
69+ @override
70+ void visitCompilationUnit (CompilationUnit node) {
71+ final unit = context.currentUnit;
72+ if (unit == null ) return ;
4873
49- if (_hasLicenseHeader (content)) return ;
74+ // Use the content provided by the analysis context (no need to read
75+ // the file from disk).
76+ if (_hasLicenseHeader (unit.content)) return ;
5077
51- // Report the lint on the first token in the file.
52- reporter.atToken (node.beginToken, _code);
53- });
78+ // Report the lint on the first token in the file.
79+ rule.reportAtToken (node.beginToken);
5480 }
5581}
5682
0 commit comments