-
-
Notifications
You must be signed in to change notification settings - Fork 489
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
feat(linter): add no-octal-escape
rule
#8151
Open
baseballyama
wants to merge
9
commits into
oxc-project:main
Choose a base branch
from
baseballyama:feat/linter/no-octal-escape
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+530
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
526a5ae
wip
baseballyama dfc8f4f
feat(linter): add `no-octal-escape` rule
baseballyama c8b129e
perf
baseballyama 89c26a5
fix lint
baseballyama aafe273
use lazy_static!
baseballyama ac8ea22
pending
baseballyama 8345d3c
fix
baseballyama d267cdb
remove .idea
baseballyama 1dbfd9d
merge 8353
baseballyama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
use crate::{context::LintContext, rule::Rule, AstNode}; | ||
use lazy_static::lazy_static; | ||
use oxc_ast::AstKind; | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::Span; | ||
use regex::Regex; | ||
|
||
fn no_octal_escape_diagnostic(span: Span, sequence: &str) -> OxcDiagnostic { | ||
OxcDiagnostic::warn(format!("Don't use octal: '\\{sequence}'. Use '\\u....' instead.")) | ||
.with_label(span) | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoOctalEscape; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// Disallows the use of octal escape sequences in string literals. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// Octal escape sequences are deprecated and can lead to unexpected behavior. Modern code should use Unicode or hexadecimal escape sequences instead. | ||
/// | ||
/// ### Examples | ||
/// | ||
/// Examples of **incorrect** code for this rule: | ||
/// ```js | ||
/// const str = "\251"; | ||
/// const str = "\1"; | ||
/// ``` | ||
/// | ||
/// Examples of **correct** code for this rule: | ||
/// ```js | ||
/// const str = "\u00A9"; | ||
/// const str = "\xA9"; | ||
/// ``` | ||
NoOctalEscape, | ||
eslint, | ||
correctness, | ||
pending | ||
); | ||
|
||
impl Rule for NoOctalEscape { | ||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
if let AstKind::StringLiteral(literal) = node.kind() { | ||
if let Some(raw) = &literal.raw { | ||
if let Some(captures) = OCTAL_ESCAPE_PATTERN.captures(raw) { | ||
if let Some(sequence) = captures.get(1) { | ||
ctx.diagnostic(no_octal_escape_diagnostic(literal.span, sequence.as_str())); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
lazy_static! { | ||
static ref OCTAL_ESCAPE_PATTERN: Regex = | ||
Regex::new(r"(?s)^(?:[^\\]|\\.)*?\\([0-3][0-7]{1,2}|[4-7][0-7]|(08|09)|[1-7])").unwrap(); | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
r#"var foo = "\x51";"#, | ||
r#"var foo = "foo \\251 bar";"#, | ||
r"var foo = /([abc]) \1/g;", | ||
r"var foo = '\0';", | ||
r"'\0'", | ||
r"'\8'", | ||
r"'\9'", | ||
r"'\0 '", | ||
r"' \0'", | ||
r"'a\0'", | ||
r"'\0a'", | ||
r"'a\8a'", | ||
r"'\0\8'", | ||
r"'\8\0'", | ||
r"'\80'", | ||
r"'\81'", | ||
r"'\\'", | ||
r"'\\0'", | ||
r"'\\08'", | ||
r"'\\1'", | ||
r"'\\01'", | ||
r"'\\12'", | ||
r"'\\\0'", | ||
r"'\\\8'", | ||
r"'\0\\'", | ||
"'0'", | ||
"'1'", | ||
"'8'", | ||
"'01'", | ||
"'08'", | ||
"'80'", | ||
"'12'", | ||
r"'\a'", | ||
r"'\n'", | ||
]; | ||
|
||
let fail = vec![ | ||
r#"var foo = "foo \01 bar";"#, | ||
r#"var foo = "foo \000 bar";"#, | ||
r#"var foo = "foo \377 bar";"#, | ||
r#"var foo = "foo \378 bar";"#, | ||
r#"var foo = "foo \37a bar";"#, | ||
r#"var foo = "foo \381 bar";"#, | ||
r#"var foo = "foo \3a1 bar";"#, | ||
r#"var foo = "foo \251 bar";"#, | ||
r#"var foo = "foo \258 bar";"#, | ||
r#"var foo = "foo \25a bar";"#, | ||
r#"var foo = "\3s51";"#, | ||
r#"var foo = "\77";"#, | ||
r#"var foo = "\78";"#, | ||
r#"var foo = "\5a";"#, | ||
r#"var foo = "\751";"#, | ||
r#"var foo = "foo \400 bar";"#, | ||
r#"var foo = "\t\1";"#, | ||
r#"var foo = "\\\751";"#, | ||
r"'\0\1'", | ||
r"'\0 \1'", | ||
// oxc itself throws the error below: | ||
// Octal escape sequences are not allowed. Use the syntax '\x01'. | ||
// r"\0\01'", | ||
r"'\0 \01'", | ||
r"'\0a\1'", | ||
r"'\0a\01'", | ||
r"'\0\08'", | ||
r"'\1'", | ||
r"'\2'", | ||
r"'\7'", | ||
r"'\00'", | ||
r"'\01'", | ||
r"'\02'", | ||
r"'\07'", | ||
r"'\08'", | ||
r"'\09'", | ||
r"'\10'", | ||
r"'\12'", | ||
r"' \1'", | ||
r"'\1 '", | ||
r"'a\1'", | ||
r"'\1a'", | ||
r"'a\1a'", | ||
r"' \01'", | ||
r"'\01 '", | ||
r"'a\01'", | ||
r"'\01a'", | ||
r"'a\01a'", | ||
r"'a\08a'", | ||
r"'\n\1'", | ||
r"'\n\01'", | ||
r"'\n\08'", | ||
r"'\\\1'", | ||
r"'\\\01'", | ||
r"'\\\08'", | ||
r"'\\n\1'", | ||
r"'\01\02'", | ||
r"'\02\01'", | ||
r"'\01\2'", | ||
r"'\2\01'", | ||
r"'\08\1'", | ||
r"'foo \1 bar \2'", | ||
]; | ||
|
||
Tester::new(NoOctalEscape::NAME, NoOctalEscape::PLUGIN, pass, fail).test_and_snapshot(); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
i think should be possible to have a fixer for this?
could we mark it as pending (unless it's not possible to fix?)
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.
I think this can be auto-fixed, but we need to be careful because it’s impossible to mechanically determine whether the missing backquote was intentional or if
u.....
should be used instead.I’ve marked it as pending for now, but the fixer should be implemented as a dangerous auto-fix.
ac4226f