同步
This commit is contained in:
46
tools/xianyan_lint/lib/src/rules/double_angle_brackets.dart
Normal file
46
tools/xianyan_lint/lib/src/rules/double_angle_brackets.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
/// ============================================================
|
||||
/// 闲言APP — 双书名号检测规则
|
||||
/// 创建时间: 2026-06-02
|
||||
/// 更新时间: 2026-06-02
|
||||
/// 作用: 检测字符串中的双书名号《《,预防多人协作风格不一致
|
||||
/// 上次更新: 初始创建
|
||||
/// ============================================================
|
||||
|
||||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/error/listener.dart';
|
||||
import 'package:custom_lint_builder/custom_lint_builder.dart';
|
||||
|
||||
class DoubleAngleBracketsRule extends DartLintRule {
|
||||
DoubleAngleBracketsRule() : super(code: _code);
|
||||
|
||||
static const _code = LintCode(
|
||||
name: 'double_angle_brackets',
|
||||
problemMessage: '检测到双书名号《《,应为单书名号《》',
|
||||
correctionMessage: '将《《替换为《,确保书名号正确配对',
|
||||
);
|
||||
|
||||
@override
|
||||
void run(
|
||||
CustomLintResolver resolver,
|
||||
DiagnosticReporter reporter,
|
||||
CustomLintContext context,
|
||||
) {
|
||||
context.registry.addSimpleStringLiteral((node) {
|
||||
_check(node.value, node, reporter);
|
||||
});
|
||||
|
||||
context.registry.addStringInterpolation((node) {
|
||||
for (final element in node.elements) {
|
||||
if (element is InterpolationString) {
|
||||
_check(element.value, element, reporter);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _check(String value, AstNode node, DiagnosticReporter reporter) {
|
||||
if (value.contains('《《')) {
|
||||
reporter.atNode(node, _code);
|
||||
}
|
||||
}
|
||||
}
|
||||
53
tools/xianyan_lint/lib/src/rules/hardcoded_color.dart
Normal file
53
tools/xianyan_lint/lib/src/rules/hardcoded_color.dart
Normal file
@@ -0,0 +1,53 @@
|
||||
/// ============================================================
|
||||
/// 闲言APP — 硬编码颜色检测规则
|
||||
/// 创建时间: 2026-06-02
|
||||
/// 更新时间: 2026-06-02
|
||||
/// 作用: 检测非主题系统的硬编码颜色值,确保使用统一设计令牌
|
||||
/// 上次更新: 初始创建
|
||||
/// ============================================================
|
||||
|
||||
import 'package:analyzer/error/listener.dart';
|
||||
import 'package:custom_lint_builder/custom_lint_builder.dart';
|
||||
|
||||
class HardcodedColorRule extends DartLintRule {
|
||||
HardcodedColorRule() : super(code: _code);
|
||||
|
||||
static const _code = LintCode(
|
||||
name: 'hardcoded_color',
|
||||
problemMessage: '检测到硬编码颜色值,应使用主题系统变量',
|
||||
correctionMessage: '使用 AppTheme.ext(context) 获取颜色,或添加到 app_colors.dart',
|
||||
);
|
||||
|
||||
static final _hexColorPattern = RegExp(r'0x[0-9A-Fa-f]{8}');
|
||||
|
||||
static const _excludedFiles = <String>[
|
||||
'app_colors.dart',
|
||||
'app_theme.dart',
|
||||
'color_weak_filter.dart',
|
||||
'glass_tokens.dart',
|
||||
'app_radius.dart',
|
||||
'app_shadow.dart',
|
||||
];
|
||||
|
||||
@override
|
||||
void run(
|
||||
CustomLintResolver resolver,
|
||||
DiagnosticReporter reporter,
|
||||
CustomLintContext context,
|
||||
) {
|
||||
final filePath = resolver.path;
|
||||
if (_excludedFiles.any((e) => filePath.contains(e))) return;
|
||||
|
||||
context.registry.addInstanceCreationExpression((node) {
|
||||
final typeName = node.constructorName.type.name.lexeme;
|
||||
if (typeName != 'Color') return;
|
||||
|
||||
for (final arg in node.argumentList.arguments) {
|
||||
final argStr = arg.toSource();
|
||||
if (_hexColorPattern.hasMatch(argStr)) {
|
||||
reporter.atNode(arg, _code);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
22
tools/xianyan_lint/lib/xianyan_lint.dart
Normal file
22
tools/xianyan_lint/lib/xianyan_lint.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
/// ============================================================
|
||||
/// 闲言APP — 自定义Lint规则入口
|
||||
/// 创建时间: 2026-06-02
|
||||
/// 更新时间: 2026-06-02
|
||||
/// 作用: 注册所有自定义lint规则插件
|
||||
/// 上次更新: 移除硬编码中文检测规则
|
||||
/// ============================================================
|
||||
|
||||
import 'package:custom_lint_builder/custom_lint_builder.dart';
|
||||
|
||||
import 'src/rules/double_angle_brackets.dart';
|
||||
import 'src/rules/hardcoded_color.dart';
|
||||
|
||||
PluginBase createPlugin() => _XianyanLintPlugin();
|
||||
|
||||
class _XianyanLintPlugin extends PluginBase {
|
||||
@override
|
||||
List<LintRule> getLintRules(CustomLintConfigs configs) => [
|
||||
DoubleAngleBracketsRule(),
|
||||
HardcodedColorRule(),
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user