-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Set up custom lint #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: code check | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
analyze: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 15 | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- uses: subosito/flutter-action@v2 | ||
with: | ||
channel: 'stable' | ||
cache: true | ||
|
||
- name: Install Melos | ||
uses: bluefireteam/melos-action@v3 | ||
|
||
- name: Analyze packages | ||
run: melos analyze | ||
|
||
- name: Custom lint | ||
run: melos custom_lint | ||
|
||
- name: Check for the existence of unformatted files | ||
# Cannot use `melos format` as it requires excluding files generated from the target file | ||
run: melos run format:ci --no-select |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
# https://pub.dev/packages/altive_lints | ||
include: package:altive_lints/altive_lints.yaml | ||
analyzer: | ||
plugins: | ||
- custom_lint | ||
|
||
custom_lint: | ||
rules: | ||
# Unnecessary if not localizing or not using Japanese. | ||
# - avoid_hardcoded_japanese: false | ||
Comment on lines
+9
to
+10
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. I have provided an example of rule exclusion. |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,6 @@ dependencies: | |
sdk: flutter | ||
|
||
dev_dependencies: | ||
altive_lints: ^1.10.0 | ||
altive_lints: | ||
path: ../ | ||
custom_lint: ^0.6.4 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
import 'src/lints/avoid_hardcoded_japanese.dart'; | ||
|
||
PluginBase createPlugin() => _AltivePlugin(); | ||
|
||
class _AltivePlugin extends PluginBase { | ||
@override | ||
List<LintRule> getLintRules(CustomLintConfigs configs) => [ | ||
const AvoidHardcodedJapanese(), | ||
Comment on lines
+9
to
+10
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. As an example, I have defined custom lint rules that have already been implemented in another repository. |
||
]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
/// An `avoid_hardcoded_japanese` rule which detects | ||
/// and reports hardcoded Japanese text strings within the code. | ||
/// | ||
/// This rule ensures that all user-facing text is | ||
/// properly internationalized to support Japanese localization efforts. | ||
/// | ||
/// ### Example | ||
/// | ||
/// #### BAD: | ||
/// | ||
/// ```dart | ||
/// final message = 'ใใใซใกใฏ'; // LINT | ||
/// print('ใจใฉใผใ็บ็ใใพใใ'); // LINT | ||
/// ``` | ||
/// | ||
/// #### GOOD: | ||
/// | ||
/// ```dart | ||
/// final message = AppLocalizations.of(context).hello; | ||
/// print(AppLocalizations.of(context).errorOccurred); | ||
/// ``` | ||
/// | ||
class AvoidHardcodedJapanese extends DartLintRule { | ||
const AvoidHardcodedJapanese() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'avoid_hardcoded_japanese', | ||
problemMessage: 'This string appears to be untranslated to Japanese.\n' | ||
'Ensure all user-facing text is properly internationalized for ' | ||
'Japanese localization.', | ||
); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ErrorReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
context.registry.addSimpleStringLiteral((node) { | ||
final stringValue = node.stringValue; | ||
if (stringValue == null) { | ||
return; | ||
} | ||
if (isJapanese(stringValue)) { | ||
reporter.reportErrorForNode(_code, node); | ||
} | ||
}); | ||
|
||
context.registry.addStringInterpolation((node) { | ||
final stringValue = node.toSource(); | ||
if (isJapanese(stringValue)) { | ||
reporter.reportErrorForNode(_code, node); | ||
} | ||
}); | ||
} | ||
|
||
/// Checks if the string contains Japanese characters | ||
/// (Hiragana, Katakana, Kanji). | ||
bool isJapanese(String value) => | ||
RegExp(r'[\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FD0]').hasMatch(value); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# https://pub.dev/packages/altive_lints | ||
include: package:altive_lints/altive_lints.yaml | ||
analyzer: | ||
plugins: | ||
- custom_lint |
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. Since the test does not start from the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Check the `avoid_hardcoded_japanese` rule. | ||
|
||
// expect_lint: avoid_hardcoded_japanese | ||
const hiragana = 'ใใใใใ'; | ||
|
||
// expect_lint: avoid_hardcoded_japanese | ||
const katakana = 'ใขใคใฆใจใช'; | ||
|
||
// expect_lint: avoid_hardcoded_japanese | ||
const kanji = 'ๆผขๅญ'; | ||
|
||
const notJapanese = 'abc'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: altive_lints_test | ||
description: A starting point for Dart libraries or applications. | ||
publish_to: none | ||
|
||
environment: | ||
sdk: ^3.0.0 | ||
|
||
dev_dependencies: | ||
altive_lints: | ||
path: ../ | ||
custom_lint: ^0.6.4 |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,7 @@ topics: | |
|
||
environment: | ||
sdk: ^3.0.0 | ||
|
||
dependencies: | ||
analyzer: ^6.4.1 | ||
custom_lint_builder: ^0.6.4 |
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.
I have added a workflow to run tests for analyze and custom_lint using GitHub Actions.