-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from byshy/prefer_single_quotes
Add prefer single quotes rule
- Loading branch information
Showing
6 changed files
with
167 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
61 changes: 61 additions & 0 deletions
61
...c/analyzers/lint_analyzer/rules/rules_list/prefer_single_quotes/prefer_single_qoutes.dart
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,61 @@ | ||
// ignore_for_file: public_member_api_docs | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/dart/ast/visitor.dart'; | ||
|
||
import '../../../../../utils/node_utils.dart'; | ||
import '../../../lint_utils.dart'; | ||
import '../../../models/internal_resolved_unit_result.dart'; | ||
import '../../../models/issue.dart'; | ||
import '../../../models/replacement.dart'; | ||
import '../../../models/severity.dart'; | ||
import '../../models/dart_rule.dart'; | ||
import '../../rule_utils.dart'; | ||
|
||
part 'visitor.dart'; | ||
|
||
class PreferSingleQuotesRule extends DartRule { | ||
static const ruleId = 'prefer-single-quotes'; | ||
static const _warningMessage = 'Use single quotation marks instead of double quotations.'; | ||
static const _replaceComment = "Replace with ''."; | ||
|
||
PreferSingleQuotesRule([Map<String, Object> config = const {}]) | ||
: super( | ||
id: ruleId, | ||
severity: readSeverity(config, Severity.style), | ||
excludes: readExcludes(config), | ||
includes: readIncludes(config), | ||
); | ||
|
||
@override | ||
Iterable<Issue> check(InternalResolvedUnitResult source) { | ||
final visitor = _Visitor(); | ||
source.unit.visitChildren(visitor); | ||
|
||
return visitor.expressions | ||
.map((expression) => createIssue( | ||
rule: this, | ||
location: nodeLocation(node: expression, source: source), | ||
message: _warningMessage, | ||
replacement: _createReplacement(expression), | ||
)) | ||
.toList(growable: false); | ||
} | ||
|
||
Replacement _createReplacement(Expression expression) { | ||
if (expression is StringLiteral) { | ||
final originalString = expression.stringValue ?? ''; | ||
final singleQuotedString = "'$originalString'"; | ||
|
||
return Replacement( | ||
comment: _replaceComment, | ||
replacement: singleQuotedString, | ||
); | ||
} | ||
|
||
return Replacement( | ||
comment: _replaceComment, | ||
replacement: expression.toSource(), | ||
); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_single_quotes/visitor.dart
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,18 @@ | ||
part of 'prefer_single_qoutes.dart'; | ||
|
||
class _Visitor extends RecursiveAstVisitor<void> { | ||
final _expressions = <Expression>[]; | ||
|
||
Iterable<Expression> get expressions => _expressions; | ||
|
||
@override | ||
void visitSimpleStringLiteral(SimpleStringLiteral node) { | ||
super.visitSimpleStringLiteral(node); | ||
|
||
if (node.isSingleQuoted) { | ||
return; // Already using single quotes | ||
} | ||
|
||
_expressions.add(node); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
test/src/analyzers/lint_analyzer/rules/rules_list/prefer_single_quotes/examples/example.dart
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,18 @@ | ||
// ignore_for_file: unnecessary_cast, unused_local_variable | ||
|
||
import 'dart:collection'; | ||
|
||
void func() { | ||
final String value1 = "some value"; // Lint | ||
final String value2 = 'some value'; | ||
|
||
final String value3 = "some value \"another text\""; // Lint | ||
final String value4 = 'some value \"another text\"'; | ||
|
||
final String value5 = """ | ||
multi line text | ||
"""; | ||
final String value6 = ''' | ||
multi line text | ||
'''; | ||
} |
67 changes: 67 additions & 0 deletions
67
...s/lint_analyzer/rules/rules_list/prefer_single_quotes/prefer_single_quotes_rule_test.dart
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,67 @@ | ||
import 'package:dart_code_linter/src/analyzers/lint_analyzer/models/severity.dart'; | ||
import 'package:dart_code_linter/src/analyzers/lint_analyzer/rules/rules_list/prefer_single_quotes/prefer_single_qoutes.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../../../../helpers/rule_test_helper.dart'; | ||
|
||
const _examplePath = 'prefer_single_quotes/examples/example.dart'; | ||
|
||
void main() { | ||
group( | ||
'PreferSingleQuotesRule', | ||
() { | ||
test('initialization', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = PreferSingleQuotesRule().check(unit); | ||
|
||
RuleTestHelper.verifyInitialization( | ||
issues: issues, | ||
ruleId: PreferSingleQuotesRule.ruleId, | ||
severity: Severity.style, | ||
); | ||
}); | ||
|
||
test('reports about found issues', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = PreferSingleQuotesRule().check(unit); | ||
|
||
RuleTestHelper.verifyIssues( | ||
issues: issues, | ||
startLines: [ | ||
6, | ||
9, | ||
12, | ||
], | ||
startColumns: [ | ||
25, | ||
25, | ||
25, | ||
], | ||
messages: [ | ||
'Use single quotation marks instead of double quotations.', | ||
'Use single quotation marks instead of double quotations.', | ||
'Use single quotation marks instead of double quotations.', | ||
], | ||
replacementComments: [ | ||
"Replace with ''.", | ||
"Replace with ''.", | ||
"Replace with ''.", | ||
], | ||
replacements: [ | ||
"'some value'", | ||
'\'some value "another text"\'', | ||
"' multi line text\n" | ||
" '", | ||
], | ||
locationTexts: [ | ||
'"some value"', | ||
r'"some value \"another text\""', | ||
'"""\n' | ||
' multi line text\n' | ||
' """', | ||
], | ||
); | ||
}); | ||
}, | ||
); | ||
} |