-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): implement noOctalEscape (#3986)
- Loading branch information
1 parent
a3193e7
commit 33d0958
Showing
12 changed files
with
1,587 additions
and
48 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
111 changes: 65 additions & 46 deletions
111
crates/biome_configuration/src/analyzer/linter/rules.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
crates/biome_js_analyze/src/lint/nursery/no_octal_escape.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use biome_analyze::{ | ||
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_syntax::JsStringLiteralExpression; | ||
use biome_rowan::AstNode; | ||
|
||
declare_lint_rule! { | ||
/// Disallow octal escape sequences in string literals | ||
/// | ||
/// As of the ECMAScript 5 specification, octal escape sequences in string literals are deprecated and should not be used. | ||
/// Unicode escape sequences should be used instead. | ||
/// | ||
/// ### Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// var foo = "Copyright \251"; | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// var foo = "Copyright \u00A9"; // unicode | ||
/// | ||
/// var foo = "Copyright \xA9"; // hexadecimal | ||
/// ``` | ||
/// | ||
pub NoOctalEscape { | ||
version: "next", | ||
name: "noOctalEscape", | ||
language: "js", | ||
sources: &[RuleSource::Eslint("no-octal-escape")], | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoOctalEscape { | ||
type Query = Ast<JsStringLiteralExpression>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let token = node.value_token().ok()?; | ||
let text = token.text(); | ||
|
||
let bytes = text.as_bytes(); | ||
let mut byte_it = bytes.iter(); | ||
while let Some(&byte) = byte_it.next() { | ||
if byte == b'\\' { | ||
if let Some(&next_byte) = byte_it.next() { | ||
if (b'0'..=b'7').contains(&next_byte) { | ||
return Some(()); | ||
} | ||
} | ||
} | ||
} | ||
None | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
let token = node.value_token().ok()?; | ||
let text = token.text(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.range(), | ||
markup! { | ||
"Don't use "<Emphasis>"octal"</Emphasis> | ||
}, | ||
) | ||
.note(markup! { | ||
"Don't use octal escape sequences: " {text} | ||
}) | ||
.note(markup! { | ||
"Use unicode or hexidecimal escape sequences instead." | ||
}), | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
crates/biome_js_analyze/tests/specs/nursery/noOctalEscape/invalid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
var foo = "foo \01 bar"; | ||
var foo = "foo \000 bar"; | ||
var foo = "foo \377 bar"; | ||
var foo = "foo \378 bar"; | ||
var foo = "foo \37a bar"; | ||
var foo = "foo \381 bar"; | ||
var foo = "foo \3a1 bar"; | ||
var foo = "foo \251 bar"; | ||
var foo = "foo \258 bar"; | ||
var foo = "foo \25a bar"; | ||
var foo = "\3s51"; | ||
var foo = "\77"; | ||
var foo = "\78"; | ||
var foo = "\5a"; | ||
var foo = "\751"; | ||
var foo = "foo \400 bar"; | ||
var foo = "foo \400 bar"; | ||
var foo = "\t\1"; | ||
var foo = "\\\751"; | ||
'\0\1' | ||
'\0 \1' | ||
'\0\01' | ||
'\0 \01' | ||
'\0a\1' | ||
'\0a\01' | ||
'\0\08' | ||
'\1' | ||
'\2' | ||
'\7' | ||
'\00' | ||
'\01' | ||
'\02' | ||
'\07' | ||
'\08' | ||
'\09' | ||
'\10' | ||
'\12' | ||
' \1' | ||
'\1 ' | ||
'a\1' | ||
'\1a' | ||
'a\1a' | ||
' \01' | ||
'\01 ' | ||
'a\01' | ||
'\01a' | ||
'a\01a' | ||
'a\08a' | ||
'\n\1' | ||
'\n\01' | ||
'\n\08' | ||
'\\\1' | ||
'\\\01' | ||
'\\\08' | ||
'\\n\1' | ||
'\01\02' | ||
'\02\01' | ||
'\01\2' | ||
'\2\01' | ||
'\08\1' | ||
'foo \1 bar \2' | ||
|
||
|
||
|
Oops, something went wrong.