mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
perf(linter/typescript/prefer-as-const): narrow AST dispatch (#28357)
Visit literal types and recover their supported annotation or assertion owner. Preserve existing fixes and reduce fixture callbacks from 12,866 to 1,054. --- <sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
1 parent
d65531f41c
commit
1108352314
3 files changed
+15
-7
No files matched your search
+2
-5
@@ -2058,11 +2058,8 @@ impl RuleRunner for crate::rules::typescript::parameter_properties::ParameterPro
|
||||
}
|
||||
|
||||
impl RuleRunner for crate::rules::typescript::prefer_as_const::PreferAsConst {
|
||||
const NODE_TYPES: Option<&AstTypesBitset> = Some(&AstTypesBitset::from_types(&[
|
||||
AstType::PropertyDefinition,
|
||||
AstType::TSAsExpression,
|
||||
AstType::VariableDeclarator,
|
||||
]));
|
||||
const NODE_TYPES: Option<&AstTypesBitset> =
|
||||
Some(&AstTypesBitset::from_types(&[AstType::TSLiteralType]));
|
||||
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,13 @@ declare_oxc_lint!(
|
||||
|
||||
impl Rule for PreferAsConst {
|
||||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
|
||||
match node.kind() {
|
||||
let AstKind::TSLiteralType(_) = node.kind() else { return };
|
||||
let parent = ctx.nodes().parent_node(node.id());
|
||||
let owner = match parent.kind() {
|
||||
AstKind::TSTypeAnnotation(_) => ctx.nodes().parent_kind(parent.id()),
|
||||
kind => kind,
|
||||
};
|
||||
match owner {
|
||||
AstKind::VariableDeclarator(variable_declarator) => {
|
||||
let Some(type_annotation) = &variable_declarator.type_annotation else {
|
||||
return;
|
||||
@@ -189,6 +195,10 @@ fn test() {
|
||||
"let foo: 2 = 3;",
|
||||
"let foo = 'bar';",
|
||||
"let foo: 'bar';",
|
||||
"let foo: 'bar' | 'baz' = 'bar';",
|
||||
"let foo = 'bar' as ('bar' | 'baz');",
|
||||
"function foo(value: 'bar' = 'bar') {}",
|
||||
"class Foo { accessor value: 'bar' = 'bar'; }",
|
||||
"let foo = { bar };",
|
||||
"let foo: 'baz' = 'baz' as const;",
|
||||
"
|
||||
@@ -307,6 +317,7 @@ fn test() {
|
||||
];
|
||||
|
||||
let fix = vec![
|
||||
("let a: 'a' = 'a', b: 'b' = 'b';", "let a = 'a' as const, b = 'b' as const;"),
|
||||
("let foo = { bar: 'baz' as 'baz' };", "let foo = { bar: 'baz' as const };"),
|
||||
("let foo = { bar: 1 as 1 };", "let foo = { bar: 1 as const };"),
|
||||
("let foo: 'bar' = 'bar' as 'bar';", "let foo: 'bar' = 'bar' as const;"),
|
||||
|
||||
@@ -502,7 +502,7 @@ typescript/no-useless-empty-export | 5
|
||||
typescript/no-var-requires | 62606
|
||||
typescript/no-wrapper-object-types | 13199
|
||||
typescript/parameter-properties | 1070
|
||||
typescript/prefer-as-const | 12866
|
||||
typescript/prefer-as-const | 1054
|
||||
typescript/prefer-enum-initializers | 23
|
||||
typescript/prefer-for-of | 557
|
||||
typescript/prefer-function-type | 14798
|
||||
|
||||
Reference in new issue
Block a user