Skip to content

Commit 6442b8a

Browse files
author
Jonas Greifenhain
committed
Fix linter
1 parent 526b857 commit 6442b8a

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
lines changed

packages/amplify_lints/lib/app.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
include: package:flutter_lints/flutter.yaml
22

3+
plugins:
4+
amplify_lints: any
5+
36
analyzer:
4-
plugins:
5-
- custom_lint
67
language:
78
strict-casts: true
89
strict-inference: true
@@ -16,7 +17,7 @@ linter:
1617
- avoid_field_initializers_in_const_classes # To prefer using getters over fields.
1718
- cancel_subscriptions # To avoid memory leaks and to prevent code from firing after a subscription is no longer being used.
1819
- close_sinks # To avoid memory leaks.
19-
- directives_ordering # To maintain visual separation of a files imports.
20+
- directives_ordering # To maintain visual separation of a file's imports.
2021
- eol_at_end_of_file # To provide consistency across our repos/languages.
2122
- flutter_style_todos # To ensure traceability of TODOs.
2223
- invalid_case_patterns # To prevent invalid case statements.

packages/amplify_lints/lib/library.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
include: package:lints/recommended.yaml
22

3+
plugins:
4+
amplify_lints: any
5+
36
analyzer:
4-
plugins:
5-
- custom_lint
67
language:
78
strict-casts: true
89
strict-inference: true
@@ -33,7 +34,7 @@ linter:
3334
- conditional_uri_does_not_exist # To prevent accidentally referencing a nonexistent file.
3435
- depend_on_referenced_packages # To prevent issues publishing.
3536
- deprecated_consistency # To encourage correct usage of deprecation and provide a better DX.
36-
- directives_ordering # To maintain visual separation of a files imports.
37+
- directives_ordering # To maintain visual separation of a file's imports.
3738
- eol_at_end_of_file # To provide consistency across our repos/languages.
3839
- flutter_style_todos # To ensure traceability of TODOs.
3940
- invalid_case_patterns # To prevent invalid case statements.

packages/amplify_lints/lib/src/lints/missing_license_header.dart

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
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.
812
const 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

packages/amplify_lints/pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ environment:
99
sdk: ^3.9.0
1010

1111
dependencies:
12+
analysis_server_plugin: any
1213
analyzer: any
1314
analyzer_plugin: any
14-
custom_lint: any
15-
custom_lint_builder: any
1615
flutter_lints: ^6.0.0
1716
lints: ^6.0.0
1817

1918
dev_dependencies:
19+
analyzer_testing: ^0.1.0
2020
test: ^1.24.0
21+
test_reflective_loader: ^0.2.0

0 commit comments

Comments
 (0)