This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: avoid-non-ascii-symbols rule (#825)
* feat: avoid-non-ascii-symbols rule * test: update test case * fix: add break in order not to have multiple highlights Co-authored-by: Dmitry Krutskikh <dmitry.krutskikh@gmail.com>
- Loading branch information
1 parent
374f392
commit ded9d0e
Showing
8 changed files
with
168 additions
and
1 deletion.
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
45 changes: 45 additions & 0 deletions
45
.../lint_analyzer/rules/rules_list/avoid_non_ascii_symbols/avoid_non_ascii_symbols_rule.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,45 @@ | ||
// 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/severity.dart'; | ||
import '../../models/common_rule.dart'; | ||
import '../../rule_utils.dart'; | ||
|
||
part 'visitor.dart'; | ||
|
||
class AvoidNonAsciiSymbolsRule extends CommonRule { | ||
static const String ruleId = 'avoid-non-ascii-symbols'; | ||
|
||
static const _warning = 'Avoid using non ascii symbols in string literals.'; | ||
|
||
AvoidNonAsciiSymbolsRule([Map<String, Object> config = const {}]) | ||
: super( | ||
id: ruleId, | ||
severity: readSeverity(config, Severity.warning), | ||
excludes: readExcludes(config), | ||
); | ||
|
||
@override | ||
Iterable<Issue> check(InternalResolvedUnitResult source) { | ||
final visitor = _Visitor(); | ||
|
||
source.unit.visitChildren(visitor); | ||
|
||
return visitor.literals | ||
.map((literal) => createIssue( | ||
rule: this, | ||
location: nodeLocation( | ||
node: literal, | ||
source: source, | ||
), | ||
message: _warning, | ||
)) | ||
.toList(growable: false); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_non_ascii_symbols/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,31 @@ | ||
part of 'avoid_non_ascii_symbols_rule.dart'; | ||
|
||
final _onlyAsciiSymbolsRegExp = RegExp(r'^[\u0000-\u007f]*$'); | ||
|
||
class _Visitor extends RecursiveAstVisitor<void> { | ||
final _literals = <SingleStringLiteral>[]; | ||
|
||
Iterable<SingleStringLiteral> get literals => _literals; | ||
|
||
@override | ||
void visitSimpleStringLiteral(SimpleStringLiteral node) { | ||
super.visitSimpleStringLiteral(node); | ||
|
||
if (!_onlyAsciiSymbolsRegExp.hasMatch(node.value)) { | ||
_literals.add(node); | ||
} | ||
} | ||
|
||
@override | ||
void visitStringInterpolation(StringInterpolation node) { | ||
super.visitStringInterpolation(node); | ||
|
||
for (final element in node.elements) { | ||
if (element is InterpolationString && | ||
!_onlyAsciiSymbolsRegExp.hasMatch(element.value)) { | ||
_literals.add(node); | ||
break; | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
..._analyzer/rules/rules_list/avoid_non_ascii_symbols/avoid_non_ascii_symbols_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,45 @@ | ||
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/models/severity.dart'; | ||
import 'package:dart_code_metrics/src/analyzers/lint_analyzer/rules/rules_list/avoid_non_ascii_symbols/avoid_non_ascii_symbols_rule.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../../../../helpers/rule_test_helper.dart'; | ||
|
||
const _examplePath = 'avoid_non_ascii_symbols/examples/example.dart'; | ||
|
||
void main() { | ||
group('AvoidNonAsciiSymbolsRule', () { | ||
test('initialization', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = AvoidNonAsciiSymbolsRule().check(unit); | ||
|
||
RuleTestHelper.verifyInitialization( | ||
issues: issues, | ||
ruleId: 'avoid-non-ascii-symbols', | ||
severity: Severity.warning, | ||
); | ||
}); | ||
|
||
test('reports about found issues', () async { | ||
final unit = await RuleTestHelper.resolveFromFile(_examplePath); | ||
final issues = AvoidNonAsciiSymbolsRule().check(unit); | ||
|
||
RuleTestHelper.verifyIssues( | ||
issues: issues, | ||
startLines: [3, 4, 6, 7], | ||
startColumns: [19, 19, 27, 23], | ||
locationTexts: [ | ||
"'hello 汉字'", | ||
"'hello привет'", | ||
r"'#!$_&- éè ;∞¥₤€'", | ||
"'inform@tiv€'", | ||
], | ||
messages: [ | ||
'Avoid using non ascii symbols in string literals.', | ||
'Avoid using non ascii symbols in string literals.', | ||
'Avoid using non ascii symbols in string literals.', | ||
'Avoid using non ascii symbols in string literals.', | ||
], | ||
); | ||
}); | ||
}); | ||
} |
8 changes: 8 additions & 0 deletions
8
...rc/analyzers/lint_analyzer/rules/rules_list/avoid_non_ascii_symbols/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,8 @@ | ||
void main() { | ||
final english = 'hello'; | ||
final chinese = 'hello 汉字'; // LINT | ||
final russian = 'hello привет'; // LINT | ||
final someGenericSymbols = '!@#${english}%^'; | ||
final nonAsciiSymbols = '#!$_&- éè ;∞¥₤€'; // LINT | ||
final misspelling = 'inform@tiv€'; // LINT | ||
} |
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,31 @@ | ||
# Avoid non ascii symbols | ||
|
||
## Rule id {#rule-id} | ||
|
||
avoid-non-ascii-symbols | ||
|
||
## Severity {#severity} | ||
|
||
Warning | ||
|
||
## Description {#description} | ||
|
||
Warns when a string literal contains non ascii characters. This might indicate that the string was not localized. | ||
|
||
### Example {#example} | ||
|
||
Bad: | ||
|
||
```dart | ||
final chinese = 'hello 汉字'; // LINT | ||
final russian = 'hello привет'; // LINT | ||
final withSomeNonAsciiSymbols = '#!$_&- éè ;∞¥₤€'; // LINT | ||
final misspelling = 'inform@tiv€'; // LINT | ||
``` | ||
|
||
Good: | ||
|
||
```dart | ||
final english = 'hello'; | ||
final someGenericSymbols ='!@#$%^'; | ||
``` |
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