-
-
Notifications
You must be signed in to change notification settings - Fork 791
feat(js_analyze): implement useRegexpExec
#8034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the nursery rule [`useRegexpExec`](https://biomejs.dev/linter/rules/use-regexp-exec/). Enforce `RegExp#exec` over `String#match` if no global flag is provided. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| use crate::services::typed::Typed; | ||
| use biome_analyze::{ | ||
| Rule, RuleDiagnostic, RuleDomain, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_js_syntax::JsCallExpression; | ||
| use biome_js_type_info::{Literal, ResolvedTypeData, TypeData}; | ||
| use biome_rowan::{AstNode, AstSeparatedList}; | ||
| use biome_rule_options::use_regexp_exec::UseRegexpExecOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Enforce `RegExp#exec` over `String#match` if no global flag is provided. | ||
| /// | ||
| /// String#match is defined to work the same as RegExp#exec when the regular expression does not include the g flag. | ||
| /// Keeping to consistently using one of the two can help improve code readability. | ||
| /// | ||
| /// RegExp#exec may also be slightly faster than String#match; this is the reason to choose it as the preferred usage. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```ts,file=invalid.ts,expect_diagnostic | ||
| /// 'something'.match(/thing/); | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```ts,file=valid.ts | ||
| /// /thing/.exec('something'); | ||
| /// ``` | ||
| /// | ||
| pub UseRegexpExec { | ||
| version: "next", | ||
| name: "useRegexpExec", | ||
| language: "js", | ||
| recommended: false, | ||
| sources: &[RuleSource::EslintTypeScript("prefer-regexp-exec").same(), RuleSource::EslintRegexp("prefer-regexp-exec").same()], | ||
| domains: &[RuleDomain::Project], | ||
| } | ||
| } | ||
|
|
||
| impl Rule for UseRegexpExec { | ||
| type Query = Typed<JsCallExpression>; | ||
| type State = (); | ||
| type Signals = Option<Self::State>; | ||
| type Options = UseRegexpExecOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let node = ctx.query(); | ||
|
|
||
| let binding = node.callee().ok()?.omit_parentheses(); | ||
| let callee = binding.as_js_static_member_expression()?; | ||
|
|
||
| let call_object = callee.object().ok()?; | ||
| if !ctx | ||
| .type_of_expression(&call_object) | ||
| .is_string_or_string_literal() | ||
| { | ||
| return None; | ||
| } | ||
|
|
||
| let call_name = callee.member().ok()?.as_js_name()?.to_trimmed_text(); | ||
| if call_name != "match" { | ||
| return None; | ||
| } | ||
|
|
||
| let args = node.arguments().ok()?.args(); | ||
| let first_arg = args.first()?.ok()?; | ||
| let express = first_arg.as_any_js_expression()?; | ||
|
|
||
| let value_type = ctx.type_of_expression(express); | ||
|
|
||
| if value_type | ||
| .resolved_data() | ||
| .map(ResolvedTypeData::as_raw_data) | ||
| .is_some_and(|ty| match ty { | ||
| TypeData::Literal(literal) => match literal.as_ref() { | ||
| Literal::RegExp(literal) => !literal.flags.contains('g'), | ||
| _ => false, | ||
| }, | ||
| _ => false, | ||
| }) | ||
| { | ||
| return Some(()); | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
| let node = ctx.query(); | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| node.range(), | ||
| markup! { | ||
| "Prefer "<Emphasis>"RegExp#exec()"</Emphasis>" over "<Emphasis>"String#match()"</Emphasis>" when searching within a string." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Use "<Emphasis>"RegExp#exec()"</Emphasis>" instead of "<Emphasis>"String#match()"</Emphasis>" for consistent and slightly faster regex matching." | ||
| }), | ||
| ) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| 'something'.match(/thing/); | ||
|
|
||
| 'some things are just things'.match(/thing/); | ||
|
|
||
| 'something'.match(new RegExp(/thing/)); | ||
|
|
||
| const text = 'something'; | ||
| const search = /thing/; | ||
| text.match(search); | ||
|
|
||
| const text1 = 'something'; | ||
| const search1 = new RegExp(/thing/); | ||
| text1.match(search1); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: invalid.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| 'something'.match(/thing/); | ||
|
|
||
| 'some things are just things'.match(/thing/); | ||
|
|
||
| 'something'.match(new RegExp(/thing/)); | ||
|
|
||
| const text = 'something'; | ||
| const search = /thing/; | ||
| text.match(search); | ||
|
|
||
| const text1 = 'something'; | ||
| const search1 = new RegExp(/thing/); | ||
| text1.match(search1); | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.js:1:1 lint/nursery/useRegexpExec ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Prefer RegExp#exec() over String#match() when searching within a string. | ||
|
|
||
| > 1 │ 'something'.match(/thing/); | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 2 │ | ||
| 3 │ 'some things are just things'.match(/thing/); | ||
|
|
||
| i Use RegExp#exec() instead of String#match() for consistent and slightly faster regex matching. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.js:3:1 lint/nursery/useRegexpExec ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Prefer RegExp#exec() over String#match() when searching within a string. | ||
|
|
||
| 1 │ 'something'.match(/thing/); | ||
| 2 │ | ||
| > 3 │ 'some things are just things'.match(/thing/); | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 4 │ | ||
| 5 │ 'something'.match(new RegExp(/thing/)); | ||
|
|
||
| i Use RegExp#exec() instead of String#match() for consistent and slightly faster regex matching. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.js:9:1 lint/nursery/useRegexpExec ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i Prefer RegExp#exec() over String#match() when searching within a string. | ||
|
|
||
| 7 │ const text = 'something'; | ||
| 8 │ const search = /thing/; | ||
| > 9 │ text.match(search); | ||
| │ ^^^^^^^^^^^^^^^^^^ | ||
| 10 │ | ||
| 11 │ const text1 = 'something'; | ||
|
|
||
| i Use RegExp#exec() instead of String#match() for consistent and slightly faster regex matching. | ||
|
|
||
|
|
||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* should not generate diagnostics */ | ||
| /thing/.exec('something'); | ||
|
|
||
| 'some things are just things'.match(/thing/g); | ||
|
|
||
| const text = 'something'; | ||
| const search = /thing/; | ||
| search.exec(text); | ||
|
|
||
| const text1 = 'something'; | ||
| const search1 = /thing/g; | ||
| text1.match(search1); | ||
|
|
||
| const obj = { | ||
| match: () => { } | ||
| } | ||
| obj.match(/thing/) | ||
|
|
||
| new RegExp(/thing/).exec('something'); | ||
|
|
||
| 'some things are just things'.match(new RegExp(/thing/g)); | ||
| 'some things are just things'.match(new RegExp(/thing/, "g")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: valid.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| /* should not generate diagnostics */ | ||
| /thing/.exec('something'); | ||
|
|
||
| 'some things are just things'.match(/thing/g); | ||
|
|
||
| const text = 'something'; | ||
| const search = /thing/; | ||
| search.exec(text); | ||
|
|
||
| const text1 = 'something'; | ||
| const search1 = /thing/g; | ||
| text1.match(search1); | ||
|
|
||
| const obj = { | ||
| match: () => { } | ||
| } | ||
| obj.match(/thing/) | ||
|
|
||
| new RegExp(/thing/).exec('something'); | ||
|
|
||
| 'some things are just things'.match(new RegExp(/thing/g)); | ||
| 'some things are just things'.match(new RegExp(/thing/, "g")); | ||
|
|
||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| use biome_deserialize_macros::{Deserializable, Merge}; | ||
| use serde::{Deserialize, Serialize}; | ||
| #[derive(Default, Clone, Debug, Deserialize, Deserializable, Eq, Merge, PartialEq, Serialize)] | ||
| #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] | ||
| #[serde(rename_all = "camelCase", deny_unknown_fields, default)] | ||
| pub struct UseRegexpExecOptions {} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these two sources implementing the exact same rule? Otherwise, maybe one or both need to be changed from
same()toinspired().There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are exactly the same