diff --git a/crates/oxc_linter/src/generated/rule_runner_impls.rs b/crates/oxc_linter/src/generated/rule_runner_impls.rs index 1f89dbe365..9e743e2fd9 100644 --- a/crates/oxc_linter/src/generated/rule_runner_impls.rs +++ b/crates/oxc_linter/src/generated/rule_runner_impls.rs @@ -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; } diff --git a/crates/oxc_linter/src/rules/typescript/prefer_as_const.rs b/crates/oxc_linter/src/rules/typescript/prefer_as_const.rs index 5fcf367fd7..4abebb8744 100644 --- a/crates/oxc_linter/src/rules/typescript/prefer_as_const.rs +++ b/crates/oxc_linter/src/rules/typescript/prefer_as_const.rs @@ -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;"), diff --git a/tasks/track_linter_timings/linter_timings.snap b/tasks/track_linter_timings/linter_timings.snap index 1b78a60ed1..7709e00b0a 100644 --- a/tasks/track_linter_timings/linter_timings.snap +++ b/tasks/track_linter_timings/linter_timings.snap @@ -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