-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2022, 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 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:collection/collection.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:yaml/yaml.dart'; | ||
import 'package:yaml_edit/yaml_edit.dart'; | ||
|
||
import '../repo_tweak.dart'; | ||
|
||
final _instance = DropLintTweak._(); | ||
|
||
class DropLintTweak extends RepoTweak { | ||
factory DropLintTweak() => _instance; | ||
|
||
DropLintTweak._() | ||
: super( | ||
id: 'drop-lint', | ||
description: 'Drop deprecated lints from analysis_options.yaml', | ||
); | ||
|
||
@override | ||
bool shouldRunByDefault(Directory checkout, String repoSlug) => true; | ||
|
||
@override | ||
FutureOr<FixResult> fix(Directory checkout, String repoSlug) async { | ||
final analysisOptionsFile = | ||
File(p.join(checkout.path, 'analysis_options.yaml')); | ||
|
||
if (!analysisOptionsFile.existsSync()) { | ||
return FixResult.noFixesMade; | ||
} | ||
|
||
final yamlSource = loadYaml( | ||
analysisOptionsFile.readAsStringSync(), | ||
sourceUrl: analysisOptionsFile.uri, | ||
); | ||
|
||
if (yamlSource is YamlMap) { | ||
final linterNode = yamlSource['linter']; | ||
if (linterNode is YamlMap) { | ||
final rules = linterNode['rules']; | ||
|
||
if (rules is YamlList) { | ||
final fixes = <String>{}; | ||
final badIndexes = rules | ||
.mapIndexed((index, value) { | ||
if (_deprecatedLints.contains(value)) { | ||
fixes.add('Removed "$value".'); | ||
return index; | ||
} | ||
return -1; | ||
}) | ||
.where((e) => e >= 0) | ||
.toList(); | ||
|
||
final editor = YamlEditor(analysisOptionsFile.readAsStringSync()); | ||
for (var index in badIndexes.reversed) { | ||
editor.remove(['linter', 'rules', index]); | ||
} | ||
|
||
analysisOptionsFile.writeAsStringSync(editor.toString(), | ||
mode: FileMode.writeOnly); | ||
|
||
return FixResult(fixes: fixes.toList()..sort()); | ||
} | ||
|
||
if (rules == null) { | ||
return FixResult(fixes: []); | ||
} | ||
|
||
throw UnimplementedError('not sure what to do with ${rules}'); | ||
} | ||
} | ||
|
||
return FixResult(fixes: []); | ||
} | ||
} | ||
|
||
final _deprecatedLints = { | ||
'avoid_null_checks_in_equality_operators', | ||
'package_api_docs', | ||
'unsafe_html', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// 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:blast_repo/src/tweaks/drop_lint_tweak.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:test_descriptor/test_descriptor.dart' as d; | ||
|
||
void main() { | ||
late DropLintTweak tweak; | ||
|
||
setUp(() async { | ||
tweak = DropLintTweak(); | ||
}); | ||
|
||
test('removes the dead items', () async { | ||
await d.dir('foo', [ | ||
d.file('analysis_options.yaml', r''' | ||
analyzer: | ||
language: | ||
strict-inference: true | ||
strict-raw-types: true | ||
linter: | ||
rules: | ||
- avoid_classes_with_only_static_members | ||
- avoid_null_checks_in_equality_operators | ||
- no_runtimeType_toString | ||
- package_api_docs | ||
- prefer_const_declarations | ||
- unsafe_html | ||
- use_if_null_to_convert_nulls_to_bools | ||
''') | ||
]).create(); | ||
final dir = d.dir('foo').io; | ||
|
||
var results = await tweak.fix(dir, 'my_org/my_repo'); | ||
expect(results.fixes, hasLength(3)); | ||
|
||
await d.dir('foo', [ | ||
d.file('analysis_options.yaml', r''' | ||
analyzer: | ||
language: | ||
strict-inference: true | ||
strict-raw-types: true | ||
linter: | ||
rules: | ||
- avoid_classes_with_only_static_members | ||
- no_runtimeType_toString | ||
- prefer_const_declarations | ||
- use_if_null_to_convert_nulls_to_bools | ||
''') | ||
]).validate(); | ||
}); | ||
|
||
test('handles no linter section', () async { | ||
await d.dir('foo', [ | ||
d.file('analysis_options.yaml', r''' | ||
analyzer: | ||
language: | ||
strict-inference: true | ||
strict-raw-types: true | ||
''') | ||
]).create(); | ||
final dir = d.dir('foo').io; | ||
|
||
var results = await tweak.fix(dir, 'my_org/my_repo'); | ||
expect(results.fixes, isEmpty); | ||
|
||
await d.dir('foo', [ | ||
d.file('analysis_options.yaml', r''' | ||
analyzer: | ||
language: | ||
strict-inference: true | ||
strict-raw-types: true | ||
''') | ||
]).validate(); | ||
}); | ||
|
||
test('handles no analysis options file', () async { | ||
await d.dir('foo', []).create(); | ||
final dir = d.dir('foo').io; | ||
|
||
var results = await tweak.fix(dir, 'my_org/my_repo'); | ||
expect(results.fixes, isEmpty); | ||
}); | ||
|
||
test('handles no bad lints', () async { | ||
await d.dir('foo', [ | ||
d.file('analysis_options.yaml', r''' | ||
analyzer: | ||
language: | ||
strict-inference: true | ||
strict-raw-types: true | ||
linter: | ||
rules: | ||
- avoid_classes_with_only_static_members | ||
- no_runtimeType_toString | ||
- prefer_const_declarations | ||
- use_if_null_to_convert_nulls_to_bools | ||
''') | ||
]).create(); | ||
final dir = d.dir('foo').io; | ||
|
||
var results = await tweak.fix(dir, 'my_org/my_repo'); | ||
expect(results.fixes, isEmpty); | ||
|
||
await d.dir('foo', [ | ||
d.file('analysis_options.yaml', r''' | ||
analyzer: | ||
language: | ||
strict-inference: true | ||
strict-raw-types: true | ||
linter: | ||
rules: | ||
- avoid_classes_with_only_static_members | ||
- no_runtimeType_toString | ||
- prefer_const_declarations | ||
- use_if_null_to_convert_nulls_to_bools | ||
''') | ||
]).validate(); | ||
}); | ||
|
||
test('handles rules as map', skip: 'not implemented yet!', () async { | ||
print('TODO!'); | ||
}); | ||
} |