diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 61a46acad07d9c..d52ab6b55cba4d 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 b17537938bf36f..cf7ff27f8ab2e2 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 460a7f7c03f990..6d35d1eac0d799 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 bfc4c277ea77c6..9fca6582c17ca7 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 43e8fd31b2670c..d598df66927c07 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 1b5f91437dc0ef..5e18f76396341b 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 cb680e9666bdbc..7c1a50aedbfe64 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 85cf52b30e7867..75061dcbd84db8 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 a0130d239fffcf..e9f171742de6c5 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 530c2844249ba7..d2b89b472b63db 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 9b3f2927ddd86b..27277391281767 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 582c5e36eab074..360995e09525e9 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 a940360713c975..c81c173abcbf50 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 dc0287fd4fb9ae..f69eeea35544cb 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 db279082818794..c27f860c4245a5 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 d04ac1fa2df0bc..19bee4cd243887 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 11706254f820ed..a6c171b6cdebba 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 fc246cfdbd893f..3917cdb8e8cece 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 422700b2fad727..bd2c55cebd913e 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 09670c20666086..f05c5697142f26 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 29d08783ba81ee..1ea93d1362d8ef 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 fbdb82f414d583..32ba615b193074 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 e241bf84ca6cda..6860b588850ae6 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 2fabfd8a4b66c8..23210ceee35691 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 00f08cb8da8ba3..8aeaa914df8447 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 d4f861d539cfef..4540409e7ba9d9 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 e12240cf404833..e679923f6c08b6 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 c7da96fe5eaf58..1cd96663e63a7a 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 6c37162b4654a4..b175de4d23b327 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 d906d8794837d9..b3a2c50452cbc8 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 a39e918626cb73..fc388c95b6bb52 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 1b3df6e8740608..3457ec9239c527 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 cefe8dbf76178b..ddad23a22432c0 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 f42aad8ac96eec..097c4be7da9573 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 599672ee105e29..71b2c57242d5bf 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 becf0ffac56625..e8f1afabcf4a12 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 4a27a99361a2c3..827d7f6f1a60a8 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 077fe109f1cd55..53d60dc855692a 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 e1dc97dcb0c8c0..c9c5141b87d2c0 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 0e5171abad162a..bf2a73dc11fdff 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 de611d29a1042a..047ba524a01f7f 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 671b7e97424a9d..2a33a5bbc0e197 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 6813edda419898..73357c4bda49ac 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 2e53e404e359cd..1d19e999cbfc14 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 9a72311c220fd8..bf36fbf01f5e28 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 cdaa7cb7c5f22b..b8cd28825f9a2f 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 b754b3bec02b94..be98cad392d1aa 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 d090f57fe006ed..7770b48a2f799d 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 7cf2706f3f1e5b..352e7c04e1fe7c 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 817fa215d73b20..1b7ada43159005 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 97b2c0cfbf74de..0a3ca2403d7036 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 e42812b85c3400..a689644b3dd06b 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 df14d8ae146980..3b33bc90b057af 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 1e9c83407eb424..737e3b7c6429a2 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 5e277c7927ca0c..cde5c3bc6e02d8 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 e72329852db9a0..399155f105c31f 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 {