This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Preserve pedantic include in analysis_options.yaml #11
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0316204
Preserve pedantic include in analysis_options.yaml
robrbecker 16b5066
fix main return type for dart 1
robrbecker f92da58
more fixes
robrbecker 6195ffc
use newer Dart in CI, only write file if asked
robrbecker 82861d7
Merge remote-tracking branch 'origin/master' into preserve_includes
robrbecker 8f563b2
fix bug that showed up in tests : null bool
robrbecker b3dc490
refactor based on CR
robrbecker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
|
@@ -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(''' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's the meat of this PR. Store the existing include if it is there, and emit it back to the file if present. |
||
analyzer: | ||
# Strong mode is required. Applies to the current project. | ||
strong-mode: | ||
|
@@ -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; | ||
|
@@ -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) { | ||
|
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 @@ | ||
include: package:pedantic/analysis_options.yaml |
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,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); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the return type to return the analysis options file contents as a String. This aids in testing since you don't actually have to do filesystem operations. Before it would assume it would read and write the file to the current directory. While that was convenient, it made running tests difficult. Changing the working directory changes it for the whole process. That meant tests couldn't be run concurrently. Instead, I changed the functions to return the content and have a flag on whether or not to actually write to the filesystem and the path to the file to read from.