Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from Workiva/preserve_includes
Browse files Browse the repository at this point in the history
Preserve pedantic include in analysis_options.yaml
  • Loading branch information
rmconsole4-wk authored Feb 27, 2019
2 parents 1496730 + b3dc490 commit af10c68
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 25 deletions.
62 changes: 42 additions & 20 deletions lib/src/update.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,57 @@ import 'package:yaml/yaml.dart';
import 'package:abide/src/constants.dart';
import 'package:abide/src/util.dart';

Future<Null> updateAnalysisOption(YamlMap abideYaml,
{bool uncommentClean}) async {
final YamlMap currentAnalysisOptions =
loadAnalysisOptions(renameDeprecatedFilename: true);
final String currentAnalysisOptionsString = loadAnalysisOptionsAsString();
Future<String> updateAnalysisOption(YamlMap abideYaml,
{String pathToAnalysisOptionsFile,
bool uncommentClean,
bool writeToFile = true}) async {
YamlMap currentAnalysisOptions = loadAnalysisOptions(
pathToAnalysisOptionsFile: pathToAnalysisOptionsFile,
renameDeprecatedFilename: true);

final String currentAnalysisOptionsString = loadAnalysisOptionsAsString(
pathToAnalysisOptionsFile: pathToAnalysisOptionsFile);

Map<String, Map<String, int>> lintErrorCounts = <String, Map<String, int>>{};
// Write a version with ALL lint rules enabled
// and then run dart analyzer to get counts of lint per rule
// those counts are passed into the 2nd write run to decide
// whether or not to comment out a lint rule with a lot of
// new lint warnings
writeAnalyisOptionsFile(
all: true,
abideYaml: abideYaml,
currentAnalysisOptions: currentAnalysisOptions,
currentAnalysisOptionsString: currentAnalysisOptionsString);
lintErrorCounts = await getLintErrorCounts(abideYaml: abideYaml);
writeAnalyisOptionsFile(
// new lint warnings. If we're not writing to a file, then lint
// counts won't work, so skip that step.
if (writeToFile) {
String allRules = generateAnalyisOptionsContent(
all: true,
abideYaml: abideYaml,
currentAnalysisOptions: currentAnalysisOptions,
currentAnalysisOptionsString: currentAnalysisOptionsString);
writeAnalysisOptionsFile(allRules);
lintErrorCounts = await getLintErrorCounts(abideYaml: abideYaml);
}
String newContent = generateAnalyisOptionsContent(
abideYaml: abideYaml,
currentAnalysisOptions: currentAnalysisOptions,
currentAnalysisOptionsString: currentAnalysisOptionsString,
lintErrorCounts: lintErrorCounts,
uncommentClean: uncommentClean);
if (writeToFile) {
writeAnalysisOptionsFile(newContent);
}
return newContent;
}

void writeAnalyisOptionsFile(
String generateAnalyisOptionsContent(
{YamlMap abideYaml,
YamlMap currentAnalysisOptions,
String currentAnalysisOptionsString,
bool all = false,
bool uncommentClean = false,
Map<String, Map<String, int>> lintErrorCounts:
Map<String, Map<String, int>> lintErrorCounts =
const <String, Map<String, int>>{}}) {
currentAnalysisOptions ??= new YamlMap();

String linterVersion = abideYaml['__linter_version'];
final String currentInclude = getYamlValue(currentAnalysisOptions, 'include');
final bool currentImplicitCasts = getYamlValue(
currentAnalysisOptions, 'analyzer:strong-mode:implicit-casts', true);
final bool currentImplicitDynamic = getYamlValue(
Expand All @@ -65,6 +81,11 @@ void writeAnalyisOptionsFile(
# To find the latest version of the linter package visit https://pub.dartlang.org/packages/linter
#
# analysis_options.yaml docs: https://www.dartlang.org/guides/language/analysis-options
''');
if (currentInclude != null && currentInclude.isNotEmpty) {
sb.writeln('include: $currentInclude');
}
sb.write('''
analyzer:
# Strong mode is required. Applies to the current project.
strong-mode:
Expand Down Expand Up @@ -122,7 +143,7 @@ analyzer:
final bool wasPresentCommented =
commentedOutLintRule.hasMatch(currentAnalysisOptionsString);
final bool wasPresent =
getYamlValue(currentAnalysisOptions, 'linter:rules:$lint');
getYamlValue(currentAnalysisOptions, 'linter:rules:$lint') ?? false;

String issues = _lintResultFor(lint, lintErrorCounts);
final bool hasIssues = issues.isNotEmpty;
Expand Down Expand Up @@ -202,14 +223,15 @@ $output
''');
}

new File(analysisOptionsFilename).writeAsStringSync(sb.toString());
if (!all) {
print('Wrote $analysisOptionsFilename');
}
if (nMissingRecommendations > 0) {
print(
'There were missing recommendations. Please inform the maintainers of the abide tool to perform an abide upgrade.');
}
return sb.toString();
}

void writeAnalysisOptionsFile(String content) {
new File(analysisOptionsFilename).writeAsStringSync(content);
}

String _lintResultFor(String lint, Map<String, Map<String, int>> lintErrors) {
Expand Down
15 changes: 11 additions & 4 deletions lib/src/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,19 @@ YamlMap loadPubspec() => loadYamlFile(pubspecFilename);
YamlMap loadSmithy() =>
loadYamlFile(smithyFilename) ?? loadYamlFile(smithyFilename2);

YamlMap loadAnalysisOptions({bool renameDeprecatedFilename: false}) =>
loadYamlFile(findAnalysisOptionsFile(
YamlMap loadAnalysisOptions(
{String pathToAnalysisOptionsFile, bool renameDeprecatedFilename: false}) {
if (pathToAnalysisOptionsFile != null) {
return loadYamlFile(pathToAnalysisOptionsFile);
} else {
return loadYamlFile(findAnalysisOptionsFile(
renameDeprecatedFilename: renameDeprecatedFilename));
}
}

String loadAnalysisOptionsAsString() {
final String filename = findAnalysisOptionsFile();
String loadAnalysisOptionsAsString({String pathToAnalysisOptionsFile}) {
final String filename =
pathToAnalysisOptionsFile ?? findAnalysisOptionsFile();
if (filename == null) {
return '';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:pedantic/analysis_options.yaml
2 changes: 1 addition & 1 deletion test/vm/check_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Future<Null> main() async {
}
});

group('Verifiy pubspec check', () {
group('Verify pubspec check', () {
final YamlMap pubspec = loadPubspec();
final YamlMap pubspecWithMissingDeps =
loadYamlFile('test/fixtures/pubspec_missing_deps.yaml');
Expand Down
38 changes: 38 additions & 0 deletions test/vm/update_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2017-2019 Workiva Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

@TestOn('vm')
import 'dart:async';
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';

import 'package:abide/src/util.dart';
import 'package:abide/src/update.dart';

Future<Null> main() async {
YamlMap abideYaml = await loadAbideYaml();

group('Update', () {
test('preserves an existing include key', () async {
String result = await updateAnalysisOption(abideYaml,
pathToAnalysisOptionsFile:
'test/fixtures/analysis/includes_pedantic/analysis_options.yaml',
uncommentClean: false,
writeToFile: false);
YamlMap yamlOutput = loadYaml(result);
expect(yamlOutput['include'] == 'package:pedantic/analysis_options.yaml',
isTrue);
});
});
}

0 comments on commit af10c68

Please sign in to comment.