11// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22// SPDX-License-Identifier: Apache-2.0
33
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' ;
4+ import 'package:analyzer/error/listener.dart' ;
5+ import 'package:custom_lint_builder/custom_lint_builder.dart' ;
106
117/// The expected license header lines.
128const licenseHeader = [
@@ -29,54 +25,32 @@ const licenseHeader = [
2925/// ```dart
3026/// library my_library;
3127/// ```
32- class MissingLicenseHeader extends AnalysisRule {
28+ class MissingLicenseHeader extends DartLintRule {
3329 /// Creates a new [MissingLicenseHeader] lint rule.
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- );
30+ MissingLicenseHeader () : super (code: _code);
4131
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.' ,
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.' ,
4737 correctionMessage: 'Add the license header to the top of this file.' ,
4838 );
4939
5040 @override
51- LintCode get diagnosticCode => code;
52-
53- @override
54- void registerNodeProcessors (
55- RuleVisitorRegistry registry,
56- RuleContext context,
41+ void run (
42+ CustomLintResolver resolver,
43+ ErrorReporter reporter,
44+ CustomLintContext context,
5745 ) {
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 ;
46+ context.registry.addCompilationUnit ((node) {
47+ final content = resolver.source.contents.data;
7348
74- // Use the content provided by the analysis context (no need to read
75- // the file from disk).
76- if (_hasLicenseHeader (unit.content)) return ;
49+ if (_hasLicenseHeader (content)) return ;
7750
78- // Report the lint on the first token in the file.
79- rule.reportAtToken (node.beginToken);
51+ // Report the lint on the first token in the file.
52+ reporter.atToken (node.beginToken, _code);
53+ });
8054 }
8155}
8256
0 commit comments