diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 61a46acad07d9..d52ab6b55cba4 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -116,10 +116,10 @@ impl Linter { let ctx = self.create_ctx(path, semantic); let semantic = Rc::clone(ctx.semantic()); - let ctx = ctx.with_fix(self.options.fix).with_eslint_config(&self.eslint_config); let rules = self .rules .iter() + .filter(|rule| rule.should_run(&ctx)) .map(|rule| { let rule_name = rule.name(); let plugin_name = self.map_jest(rule.plugin_name(), rule_name); diff --git a/crates/oxc_linter/src/rule.rs b/crates/oxc_linter/src/rule.rs index b17537938bf36..cf7ff27f8ab2e 100644 --- a/crates/oxc_linter/src/rule.rs +++ b/crates/oxc_linter/src/rule.rs @@ -22,6 +22,17 @@ pub trait Rule: Sized + Default + fmt::Debug { /// Run only once. Useful for inspecting scopes and trivias etc. fn run_once(&self, _ctx: &LintContext) {} + + /// Check if a rule should be run at all. + /// + /// You usually do not need to implement this function. If you do, use it to + /// enable rules on a file-by-file basis. Do not check if plugins are + /// enabled/disabled; this is handled by the [`linter`]. + /// + /// [`linter`]: crate::Linter + fn should_run(&self, _ctx: &LintContext) -> bool { + true + } } pub trait RuleMeta { diff --git a/crates/oxc_linter/src/rules/react/button_has_type.rs b/crates/oxc_linter/src/rules/react/button_has_type.rs index 460a7f7c03f99..6d35d1eac0d79 100644 --- a/crates/oxc_linter/src/rules/react/button_has_type.rs +++ b/crates/oxc_linter/src/rules/react/button_has_type.rs @@ -146,6 +146,10 @@ impl Rule for ButtonHasType { .unwrap_or(true), } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } impl ButtonHasType { diff --git a/crates/oxc_linter/src/rules/react/jsx_key.rs b/crates/oxc_linter/src/rules/react/jsx_key.rs index bfc4c277ea77c..9fca6582c17ca 100644 --- a/crates/oxc_linter/src/rules/react/jsx_key.rs +++ b/crates/oxc_linter/src/rules/react/jsx_key.rs @@ -63,6 +63,10 @@ impl Rule for JsxKey { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } enum InsideArrayOrIterator { diff --git a/crates/oxc_linter/src/rules/react/jsx_no_comment_textnodes.rs b/crates/oxc_linter/src/rules/react/jsx_no_comment_textnodes.rs index 43e8fd31b2670..d598df66927c0 100644 --- a/crates/oxc_linter/src/rules/react/jsx_no_comment_textnodes.rs +++ b/crates/oxc_linter/src/rules/react/jsx_no_comment_textnodes.rs @@ -60,6 +60,10 @@ impl Rule for JsxNoCommentTextnodes { ctx.diagnostic(jsx_no_comment_textnodes_diagnostic(jsx_text.span)); } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } fn control_patterns(pattern: &str) -> bool { diff --git a/crates/oxc_linter/src/rules/react/jsx_no_duplicate_props.rs b/crates/oxc_linter/src/rules/react/jsx_no_duplicate_props.rs index 1b5f91437dc0e..5e18f76396341 100644 --- a/crates/oxc_linter/src/rules/react/jsx_no_duplicate_props.rs +++ b/crates/oxc_linter/src/rules/react/jsx_no_duplicate_props.rs @@ -69,6 +69,10 @@ impl Rule for JsxNoDuplicateProps { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/jsx_no_target_blank.rs b/crates/oxc_linter/src/rules/react/jsx_no_target_blank.rs index cb680e9666bdb..7c1a50aedbfe6 100644 --- a/crates/oxc_linter/src/rules/react/jsx_no_target_blank.rs +++ b/crates/oxc_linter/src/rules/react/jsx_no_target_blank.rs @@ -243,6 +243,10 @@ impl Rule for JsxNoTargetBlank { .unwrap_or(false), } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } fn check_is_external_link(link: &str) -> bool { diff --git a/crates/oxc_linter/src/rules/react/jsx_no_undef.rs b/crates/oxc_linter/src/rules/react/jsx_no_undef.rs index 85cf52b30e786..75061dcbd84db 100644 --- a/crates/oxc_linter/src/rules/react/jsx_no_undef.rs +++ b/crates/oxc_linter/src/rules/react/jsx_no_undef.rs @@ -72,6 +72,10 @@ impl Rule for JsxNoUndef { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/jsx_no_useless_fragment.rs b/crates/oxc_linter/src/rules/react/jsx_no_useless_fragment.rs index a0130d239fffc..e9f171742de6c 100644 --- a/crates/oxc_linter/src/rules/react/jsx_no_useless_fragment.rs +++ b/crates/oxc_linter/src/rules/react/jsx_no_useless_fragment.rs @@ -74,6 +74,10 @@ impl Rule for JsxNoUselessFragment { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } impl JsxNoUselessFragment { diff --git a/crates/oxc_linter/src/rules/react/no_children_prop.rs b/crates/oxc_linter/src/rules/react/no_children_prop.rs index 530c2844249ba..d2b89b472b63d 100644 --- a/crates/oxc_linter/src/rules/react/no_children_prop.rs +++ b/crates/oxc_linter/src/rules/react/no_children_prop.rs @@ -85,6 +85,10 @@ impl Rule for NoChildrenProp { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/no_danger.rs b/crates/oxc_linter/src/rules/react/no_danger.rs index 9b3f2927ddd86..2727739128176 100644 --- a/crates/oxc_linter/src/rules/react/no_danger.rs +++ b/crates/oxc_linter/src/rules/react/no_danger.rs @@ -74,6 +74,10 @@ impl Rule for NoDanger { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/no_direct_mutation_state.rs b/crates/oxc_linter/src/rules/react/no_direct_mutation_state.rs index 582c5e36eab07..360995e09525e 100644 --- a/crates/oxc_linter/src/rules/react/no_direct_mutation_state.rs +++ b/crates/oxc_linter/src/rules/react/no_direct_mutation_state.rs @@ -117,6 +117,10 @@ impl Rule for NoDirectMutationState { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } // check current node is this.state.xx diff --git a/crates/oxc_linter/src/rules/react/no_find_dom_node.rs b/crates/oxc_linter/src/rules/react/no_find_dom_node.rs index a940360713c97..c81c173abcbf5 100644 --- a/crates/oxc_linter/src/rules/react/no_find_dom_node.rs +++ b/crates/oxc_linter/src/rules/react/no_find_dom_node.rs @@ -66,6 +66,10 @@ impl Rule for NoFindDomNode { }; ctx.diagnostic(no_find_dom_node_diagnostic(span)); } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/no_is_mounted.rs b/crates/oxc_linter/src/rules/react/no_is_mounted.rs index dc0287fd4fb9a..f69eeea35544c 100644 --- a/crates/oxc_linter/src/rules/react/no_is_mounted.rs +++ b/crates/oxc_linter/src/rules/react/no_is_mounted.rs @@ -66,6 +66,10 @@ impl Rule for NoIsMounted { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/no_render_return_value.rs b/crates/oxc_linter/src/rules/react/no_render_return_value.rs index db27908281879..c27f860c4245a 100644 --- a/crates/oxc_linter/src/rules/react/no_render_return_value.rs +++ b/crates/oxc_linter/src/rules/react/no_render_return_value.rs @@ -78,6 +78,10 @@ impl Rule for NoRenderReturnValue { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/no_set_state.rs b/crates/oxc_linter/src/rules/react/no_set_state.rs index d04ac1fa2df0b..19bee4cd24388 100644 --- a/crates/oxc_linter/src/rules/react/no_set_state.rs +++ b/crates/oxc_linter/src/rules/react/no_set_state.rs @@ -71,6 +71,10 @@ impl Rule for NoSetState { ctx.diagnostic(no_set_state_diagnostic(call_expr.callee.span())); } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/no_string_refs.rs b/crates/oxc_linter/src/rules/react/no_string_refs.rs index 11706254f820e..a6c171b6cdebb 100644 --- a/crates/oxc_linter/src/rules/react/no_string_refs.rs +++ b/crates/oxc_linter/src/rules/react/no_string_refs.rs @@ -127,6 +127,10 @@ impl Rule for NoStringRefs { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/no_unescaped_entities.rs b/crates/oxc_linter/src/rules/react/no_unescaped_entities.rs index fc246cfdbd893..3917cdb8e8cec 100644 --- a/crates/oxc_linter/src/rules/react/no_unescaped_entities.rs +++ b/crates/oxc_linter/src/rules/react/no_unescaped_entities.rs @@ -61,6 +61,10 @@ impl Rule for NoUnescapedEntities { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } pub const DEFAULTS: Map = phf_map! { diff --git a/crates/oxc_linter/src/rules/react/no_unknown_property.rs b/crates/oxc_linter/src/rules/react/no_unknown_property.rs index 422700b2fad72..bd2c55cebd913 100644 --- a/crates/oxc_linter/src/rules/react/no_unknown_property.rs +++ b/crates/oxc_linter/src/rules/react/no_unknown_property.rs @@ -529,6 +529,10 @@ impl Rule for NoUnknownProperty { ); }); } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/prefer_es6_class.rs b/crates/oxc_linter/src/rules/react/prefer_es6_class.rs index 09670c2066608..f05c5697142f2 100644 --- a/crates/oxc_linter/src/rules/react/prefer_es6_class.rs +++ b/crates/oxc_linter/src/rules/react/prefer_es6_class.rs @@ -74,6 +74,10 @@ impl Rule for PreferEs6Class { )); } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[derive(Debug, Default, Clone)] diff --git a/crates/oxc_linter/src/rules/react/react_in_jsx_scope.rs b/crates/oxc_linter/src/rules/react/react_in_jsx_scope.rs index 29d08783ba81e..1ea93d1362d8e 100644 --- a/crates/oxc_linter/src/rules/react/react_in_jsx_scope.rs +++ b/crates/oxc_linter/src/rules/react/react_in_jsx_scope.rs @@ -54,6 +54,10 @@ impl Rule for ReactInJsxScope { ctx.diagnostic(react_in_jsx_scope_diagnostic(node_span)); } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react/require_render_return.rs b/crates/oxc_linter/src/rules/react/require_render_return.rs index fbdb82f414d58..32ba615b19307 100644 --- a/crates/oxc_linter/src/rules/react/require_render_return.rs +++ b/crates/oxc_linter/src/rules/react/require_render_return.rs @@ -78,6 +78,10 @@ impl Rule for RequireRenderReturn { }; } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[derive(Clone, Copy, Debug, Default, PartialEq)] diff --git a/crates/oxc_linter/src/rules/react/void_dom_elements_no_children.rs b/crates/oxc_linter/src/rules/react/void_dom_elements_no_children.rs index e241bf84ca6cd..6860b588850ae 100644 --- a/crates/oxc_linter/src/rules/react/void_dom_elements_no_children.rs +++ b/crates/oxc_linter/src/rules/react/void_dom_elements_no_children.rs @@ -137,6 +137,10 @@ impl Rule for VoidDomElementsNoChildren { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } #[test] diff --git a/crates/oxc_linter/src/rules/react_perf/jsx_no_jsx_as_prop.rs b/crates/oxc_linter/src/rules/react_perf/jsx_no_jsx_as_prop.rs index 2fabfd8a4b66c..23210ceee3569 100644 --- a/crates/oxc_linter/src/rules/react_perf/jsx_no_jsx_as_prop.rs +++ b/crates/oxc_linter/src/rules/react_perf/jsx_no_jsx_as_prop.rs @@ -42,6 +42,10 @@ impl Rule for JsxNoJsxAsProp { check_jsx_element(jsx_elem, ctx); } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } fn check_jsx_element<'a>(jsx_elem: &JSXElement<'a>, ctx: &LintContext<'a>) { diff --git a/crates/oxc_linter/src/rules/react_perf/jsx_no_new_array_as_prop.rs b/crates/oxc_linter/src/rules/react_perf/jsx_no_new_array_as_prop.rs index 00f08cb8da8ba..8aeaa914df844 100644 --- a/crates/oxc_linter/src/rules/react_perf/jsx_no_new_array_as_prop.rs +++ b/crates/oxc_linter/src/rules/react_perf/jsx_no_new_array_as_prop.rs @@ -50,6 +50,10 @@ impl Rule for JsxNoNewArrayAsProp { check_jsx_element(jsx_elem, ctx); } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } fn check_jsx_element<'a>(jsx_elem: &JSXElement<'a>, ctx: &LintContext<'a>) { diff --git a/crates/oxc_linter/src/rules/react_perf/jsx_no_new_function_as_prop.rs b/crates/oxc_linter/src/rules/react_perf/jsx_no_new_function_as_prop.rs index d4f861d539cfe..4540409e7ba9d 100644 --- a/crates/oxc_linter/src/rules/react_perf/jsx_no_new_function_as_prop.rs +++ b/crates/oxc_linter/src/rules/react_perf/jsx_no_new_function_as_prop.rs @@ -45,6 +45,10 @@ impl Rule for JsxNoNewFunctionAsProp { check_jsx_element(jsx_elem, ctx); } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } fn check_jsx_element<'a>(jsx_elem: &JSXElement<'a>, ctx: &LintContext<'a>) { diff --git a/crates/oxc_linter/src/rules/react_perf/jsx_no_new_object_as_prop.rs b/crates/oxc_linter/src/rules/react_perf/jsx_no_new_object_as_prop.rs index e12240cf40483..e679923f6c08b 100644 --- a/crates/oxc_linter/src/rules/react_perf/jsx_no_new_object_as_prop.rs +++ b/crates/oxc_linter/src/rules/react_perf/jsx_no_new_object_as_prop.rs @@ -49,6 +49,10 @@ impl Rule for JsxNoNewObjectAsProp { check_jsx_element(jsx_elem, ctx); } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_jsx() + } } fn check_jsx_element<'a>(jsx_elem: &JSXElement<'a>, ctx: &LintContext<'a>) { diff --git a/crates/oxc_linter/src/rules/typescript/adjacent_overload_signatures.rs b/crates/oxc_linter/src/rules/typescript/adjacent_overload_signatures.rs index c7da96fe5eaf5..1cd96663e63a7 100644 --- a/crates/oxc_linter/src/rules/typescript/adjacent_overload_signatures.rs +++ b/crates/oxc_linter/src/rules/typescript/adjacent_overload_signatures.rs @@ -319,6 +319,10 @@ impl Rule for AdjacentOverloadSignatures { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/array_type.rs b/crates/oxc_linter/src/rules/typescript/array_type.rs index 6c37162b4654a..b175de4d23b32 100644 --- a/crates/oxc_linter/src/rules/typescript/array_type.rs +++ b/crates/oxc_linter/src/rules/typescript/array_type.rs @@ -126,6 +126,10 @@ impl Rule for ArrayType { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } fn check( diff --git a/crates/oxc_linter/src/rules/typescript/ban_ts_comment.rs b/crates/oxc_linter/src/rules/typescript/ban_ts_comment.rs index d906d8794837d..b3a2c50452cbc 100644 --- a/crates/oxc_linter/src/rules/typescript/ban_ts_comment.rs +++ b/crates/oxc_linter/src/rules/typescript/ban_ts_comment.rs @@ -204,6 +204,10 @@ impl Rule for BanTsComment { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } impl BanTsComment { diff --git a/crates/oxc_linter/src/rules/typescript/ban_types.rs b/crates/oxc_linter/src/rules/typescript/ban_types.rs index a39e918626cb7..fc388c95b6bb5 100644 --- a/crates/oxc_linter/src/rules/typescript/ban_types.rs +++ b/crates/oxc_linter/src/rules/typescript/ban_types.rs @@ -83,6 +83,10 @@ impl Rule for BanTypes { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/consistent_indexed_object_style.rs b/crates/oxc_linter/src/rules/typescript/consistent_indexed_object_style.rs index 1b3df6e874060..3457ec9239c52 100644 --- a/crates/oxc_linter/src/rules/typescript/consistent_indexed_object_style.rs +++ b/crates/oxc_linter/src/rules/typescript/consistent_indexed_object_style.rs @@ -244,6 +244,10 @@ impl Rule for ConsistentIndexedObjectStyle { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/consistent_type_definitions.rs b/crates/oxc_linter/src/rules/typescript/consistent_type_definitions.rs index cefe8dbf76178..ddad23a22432c 100644 --- a/crates/oxc_linter/src/rules/typescript/consistent_type_definitions.rs +++ b/crates/oxc_linter/src/rules/typescript/consistent_type_definitions.rs @@ -214,6 +214,10 @@ impl Rule for ConsistentTypeDefinitions { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/consistent_type_imports.rs b/crates/oxc_linter/src/rules/typescript/consistent_type_imports.rs index f42aad8ac96ee..097c4be7da957 100644 --- a/crates/oxc_linter/src/rules/typescript/consistent_type_imports.rs +++ b/crates/oxc_linter/src/rules/typescript/consistent_type_imports.rs @@ -266,6 +266,10 @@ impl Rule for ConsistentTypeImports { ); } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } // Given an array of words, returns an English-friendly concatenation, separated with commas, with diff --git a/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs b/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs index 599672ee105e2..71b2c57242d5b 100644 --- a/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs +++ b/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs @@ -249,6 +249,10 @@ impl Rule for ExplicitFunctionReturnType { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } impl ExplicitFunctionReturnType { diff --git a/crates/oxc_linter/src/rules/typescript/no_confusing_non_null_assertion.rs b/crates/oxc_linter/src/rules/typescript/no_confusing_non_null_assertion.rs index becf0ffac5662..e8f1afabcf4a1 100644 --- a/crates/oxc_linter/src/rules/typescript/no_confusing_non_null_assertion.rs +++ b/crates/oxc_linter/src/rules/typescript/no_confusing_non_null_assertion.rs @@ -92,6 +92,10 @@ impl Rule for NoConfusingNonNullAssertion { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs b/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs index 4a27a99361a2c..827d7f6f1a60a 100644 --- a/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs +++ b/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs @@ -64,6 +64,10 @@ impl Rule for NoDuplicateEnumValues { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/no_empty_interface.rs b/crates/oxc_linter/src/rules/typescript/no_empty_interface.rs index 077fe109f1cd5..53d60dc855692 100644 --- a/crates/oxc_linter/src/rules/typescript/no_empty_interface.rs +++ b/crates/oxc_linter/src/rules/typescript/no_empty_interface.rs @@ -68,6 +68,10 @@ impl Rule for NoEmptyInterface { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/no_explicit_any.rs b/crates/oxc_linter/src/rules/typescript/no_explicit_any.rs index e1dc97dcb0c8c..c9c5141b87d2c 100644 --- a/crates/oxc_linter/src/rules/typescript/no_explicit_any.rs +++ b/crates/oxc_linter/src/rules/typescript/no_explicit_any.rs @@ -112,6 +112,10 @@ impl Rule for NoExplicitAny { Self { fix_to_unknown, ignore_rest_args } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } impl NoExplicitAny { diff --git a/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs b/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs index 0e5171abad162..bf2a73dc11fdf 100644 --- a/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs +++ b/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs @@ -64,6 +64,10 @@ impl Rule for NoExtraNonNullAssertion { ctx.diagnostic(no_extra_non_null_assertion_diagnostic(Span::new(end, end))); } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/no_import_type_side_effects.rs b/crates/oxc_linter/src/rules/typescript/no_import_type_side_effects.rs index de611d29a1042..047ba524a01f7 100644 --- a/crates/oxc_linter/src/rules/typescript/no_import_type_side_effects.rs +++ b/crates/oxc_linter/src/rules/typescript/no_import_type_side_effects.rs @@ -109,6 +109,10 @@ impl Rule for NoImportTypeSideEffects { }, ); } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/no_namespace.rs b/crates/oxc_linter/src/rules/typescript/no_namespace.rs index 671b7e97424a9..2a33a5bbc0e19 100644 --- a/crates/oxc_linter/src/rules/typescript/no_namespace.rs +++ b/crates/oxc_linter/src/rules/typescript/no_namespace.rs @@ -98,6 +98,10 @@ impl Rule for NoNamespace { ctx.diagnostic(no_namespace_diagnostic(span)); } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } fn is_declaration(node: &AstNode, ctx: &LintContext) -> bool { diff --git a/crates/oxc_linter/src/rules/typescript/no_non_null_asserted_nullish_coalescing.rs b/crates/oxc_linter/src/rules/typescript/no_non_null_asserted_nullish_coalescing.rs index 6813edda41989..73357c4bda49a 100644 --- a/crates/oxc_linter/src/rules/typescript/no_non_null_asserted_nullish_coalescing.rs +++ b/crates/oxc_linter/src/rules/typescript/no_non_null_asserted_nullish_coalescing.rs @@ -47,6 +47,10 @@ impl Rule for NoNonNullAssertedNullishCoalescing { ctx.diagnostic(no_non_null_asserted_nullish_coalescing_diagnostic(ts_non_null_expr.span)); } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } fn has_assignment_before_node( symbol_id: SymbolId, diff --git a/crates/oxc_linter/src/rules/typescript/no_non_null_asserted_optional_chain.rs b/crates/oxc_linter/src/rules/typescript/no_non_null_asserted_optional_chain.rs index 2e53e404e359c..1d19e999cbfc1 100644 --- a/crates/oxc_linter/src/rules/typescript/no_non_null_asserted_optional_chain.rs +++ b/crates/oxc_linter/src/rules/typescript/no_non_null_asserted_optional_chain.rs @@ -88,6 +88,10 @@ impl Rule for NoNonNullAssertedOptionalChain { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } fn is_parent_member_or_call(node: &AstNode<'_>, ctx: &LintContext<'_>) -> bool { diff --git a/crates/oxc_linter/src/rules/typescript/no_non_null_assertion.rs b/crates/oxc_linter/src/rules/typescript/no_non_null_assertion.rs index 9a72311c220fd..bf36fbf01f5e2 100644 --- a/crates/oxc_linter/src/rules/typescript/no_non_null_assertion.rs +++ b/crates/oxc_linter/src/rules/typescript/no_non_null_assertion.rs @@ -36,6 +36,10 @@ impl Rule for NoNonNullAssertion { let AstKind::TSNonNullExpression(expr) = node.kind() else { return }; ctx.diagnostic(no_non_null_assertion_diagnostic(expr.span)); } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/no_this_alias.rs b/crates/oxc_linter/src/rules/typescript/no_this_alias.rs index cdaa7cb7c5f22..b8cd28825f9a2 100644 --- a/crates/oxc_linter/src/rules/typescript/no_this_alias.rs +++ b/crates/oxc_linter/src/rules/typescript/no_this_alias.rs @@ -87,9 +87,6 @@ impl Rule for NoThisAlias { } fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { - if !ctx.source_type().is_typescript() { - return; - } match node.kind() { AstKind::VariableDeclarator(decl) => { let Some(init) = &decl.init else { return }; @@ -146,6 +143,9 @@ impl Rule for NoThisAlias { _ => {} } } + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } fn rhs_is_this_reference(rhs_expression: &Expression) -> bool { diff --git a/crates/oxc_linter/src/rules/typescript/no_unnecessary_type_constraint.rs b/crates/oxc_linter/src/rules/typescript/no_unnecessary_type_constraint.rs index b754b3bec02b9..be98cad392d1a 100644 --- a/crates/oxc_linter/src/rules/typescript/no_unnecessary_type_constraint.rs +++ b/crates/oxc_linter/src/rules/typescript/no_unnecessary_type_constraint.rs @@ -67,6 +67,10 @@ impl Rule for NoUnnecessaryTypeConstraint { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/no_unsafe_declaration_merging.rs b/crates/oxc_linter/src/rules/typescript/no_unsafe_declaration_merging.rs index d090f57fe006e..7770b48a2f799 100644 --- a/crates/oxc_linter/src/rules/typescript/no_unsafe_declaration_merging.rs +++ b/crates/oxc_linter/src/rules/typescript/no_unsafe_declaration_merging.rs @@ -36,10 +36,6 @@ declare_oxc_lint!( impl Rule for NoUnsafeDeclarationMerging { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { - if !ctx.source_type().is_typescript() { - return; - } - match node.kind() { AstKind::Class(decl) => { if let Some(ident) = decl.id.as_ref() { @@ -64,6 +60,10 @@ impl Rule for NoUnsafeDeclarationMerging { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } fn check_and_diagnostic( diff --git a/crates/oxc_linter/src/rules/typescript/no_var_requires.rs b/crates/oxc_linter/src/rules/typescript/no_var_requires.rs index 7cf2706f3f1e5..352e7c04e1fe7 100644 --- a/crates/oxc_linter/src/rules/typescript/no_var_requires.rs +++ b/crates/oxc_linter/src/rules/typescript/no_var_requires.rs @@ -34,9 +34,6 @@ declare_oxc_lint!( impl Rule for NoVarRequires { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { - if !ctx.source_type().is_typescript() { - return; - } let AstKind::CallExpression(expr) = node.kind() else { return; }; @@ -68,6 +65,10 @@ impl Rule for NoVarRequires { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] 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 817fa215d73b2..1b7ada4315900 100644 --- a/crates/oxc_linter/src/rules/typescript/prefer_as_const.rs +++ b/crates/oxc_linter/src/rules/typescript/prefer_as_const.rs @@ -80,6 +80,10 @@ impl Rule for PreferAsConst { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } fn check_and_report( diff --git a/crates/oxc_linter/src/rules/typescript/prefer_enum_initializers.rs b/crates/oxc_linter/src/rules/typescript/prefer_enum_initializers.rs index 97b2c0cfbf74d..0a3ca2403d703 100644 --- a/crates/oxc_linter/src/rules/typescript/prefer_enum_initializers.rs +++ b/crates/oxc_linter/src/rules/typescript/prefer_enum_initializers.rs @@ -51,6 +51,10 @@ impl Rule for PreferEnumInitializers { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/prefer_function_type.rs b/crates/oxc_linter/src/rules/typescript/prefer_function_type.rs index e42812b85c340..a689644b3dd06 100644 --- a/crates/oxc_linter/src/rules/typescript/prefer_function_type.rs +++ b/crates/oxc_linter/src/rules/typescript/prefer_function_type.rs @@ -392,6 +392,10 @@ impl Rule for PreferFunctionType { _ => {} } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/prefer_literal_enum_member.rs b/crates/oxc_linter/src/rules/typescript/prefer_literal_enum_member.rs index df14d8ae14698..3b33bc90b057a 100644 --- a/crates/oxc_linter/src/rules/typescript/prefer_literal_enum_member.rs +++ b/crates/oxc_linter/src/rules/typescript/prefer_literal_enum_member.rs @@ -108,6 +108,10 @@ impl Rule for PreferLiteralEnumMember { ctx.diagnostic(prefer_literal_enum_member_diagnostic(decl.span)); } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } #[test] diff --git a/crates/oxc_linter/src/rules/typescript/prefer_ts_expect_error.rs b/crates/oxc_linter/src/rules/typescript/prefer_ts_expect_error.rs index 1e9c83407eb42..737e3b7c6429a 100644 --- a/crates/oxc_linter/src/rules/typescript/prefer_ts_expect_error.rs +++ b/crates/oxc_linter/src/rules/typescript/prefer_ts_expect_error.rs @@ -71,6 +71,10 @@ impl Rule for PreferTsExpectError { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } fn get_last_comment_line(comment: CommentKind, raw: &str) -> String { diff --git a/crates/oxc_linter/src/rules/typescript/triple_slash_reference.rs b/crates/oxc_linter/src/rules/typescript/triple_slash_reference.rs index 5e277c7927ca0..cde5c3bc6e02d 100644 --- a/crates/oxc_linter/src/rules/typescript/triple_slash_reference.rs +++ b/crates/oxc_linter/src/rules/typescript/triple_slash_reference.rs @@ -162,6 +162,10 @@ impl Rule for TripleSlashReference { } } } + + fn should_run(&self, ctx: &LintContext) -> bool { + ctx.source_type().is_typescript() + } } fn get_attr_key_and_value(raw: &str) -> Option<(String, String)> { diff --git a/crates/oxc_macros/src/declare_all_lint_rules.rs b/crates/oxc_macros/src/declare_all_lint_rules.rs index e72329852db9a..399155f105c31 100644 --- a/crates/oxc_macros/src/declare_all_lint_rules.rs +++ b/crates/oxc_macros/src/declare_all_lint_rules.rs @@ -118,6 +118,12 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream { #(Self::#struct_names(rule) => rule.run_once(ctx)),* } } + + pub(super) fn should_run(&self, ctx: &LintContext) -> bool { + match self { + #(Self::#struct_names(rule) => rule.should_run(ctx)),* + } + } } impl std::hash::Hash for RuleEnum {