Skip to content

Commit 526b857

Browse files
committed
Fix linter
1 parent b7947ed commit 526b857

File tree

5 files changed

+41
-61
lines changed

5 files changed

+41
-61
lines changed

packages/amplify_lints/lib/amplify_lints.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33

44
/// Custom lint rules for Amplify Flutter packages.
55
///
6-
/// This package provides an analyzer plugin that is automatically discovered
7-
/// by the Dart Analysis Server when enabled in `analysis_options.yaml`.
6+
/// This package provides custom lint rules using `custom_lint` that are
7+
/// automatically discovered by the Dart Analysis Server when `custom_lint`
8+
/// is enabled in `analysis_options.yaml` and this package is a dependency.
89
library amplify_lints;
910

10-
export 'src/fixes/add_license_header_fix.dart' show AddLicenseHeaderFix;
11-
export 'src/lints/missing_license_header.dart' show MissingLicenseHeader;
12-
export 'src/plugin.dart' show AmplifyLintsPlugin;
11+
import 'package:custom_lint_builder/custom_lint_builder.dart';
12+
13+
import 'src/lints/missing_license_header.dart';
14+
15+
/// Creates the plugin instance for custom_lint to discover.
16+
PluginBase createPlugin() => _AmplifyLintsPlugin();
17+
18+
class _AmplifyLintsPlugin extends PluginBase {
19+
@override
20+
List<LintRule> getLintRules(CustomLintConfigs configs) => [
21+
MissingLicenseHeader(),
22+
];
23+
}

packages/amplify_lints/lib/app.yaml

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

3-
plugins:
4-
amplify_lints:
5-
path: ../
6-
73
analyzer:
4+
plugins:
5+
- custom_lint
86
language:
97
strict-casts: true
108
strict-inference: true

packages/amplify_lints/lib/library.yaml

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

3-
plugins:
4-
amplify_lints:
5-
path: ../
6-
73
analyzer:
4+
plugins:
5+
- custom_lint
86
language:
97
strict-casts: true
108
strict-inference: true

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

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

packages/amplify_lints/pubspec.yaml

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

1111
dependencies:
12-
analysis_server_plugin: any
1312
analyzer: any
1413
analyzer_plugin: any
14+
custom_lint: any
15+
custom_lint_builder: any
1516
flutter_lints: ^6.0.0
1617
lints: ^6.0.0
1718

1819
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)