-
-
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(lint): add rule
useStrictMode
(#3370)
Co-authored-by: Victorien Elvinger <[email protected]>
- Loading branch information
Showing
18 changed files
with
260 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
101 changes: 101 additions & 0 deletions
101
crates/biome_js_analyze/src/lint/nursery/use_strict_mode.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,101 @@ | ||
use crate::JsRuleAction; | ||
use biome_analyze::{ | ||
context::RuleContext, declare_lint_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_factory::make::{ | ||
js_directive, js_directive_list, jsx_string_literal, jsx_string_literal_single_quotes, | ||
}; | ||
use biome_js_syntax::{JsFileSource, JsScript}; | ||
use biome_rowan::{AstNode, AstNodeList, BatchMutationExt}; | ||
|
||
declare_lint_rule! { | ||
/// Enforce the use of the directive `"use strict"` in script files. | ||
/// | ||
/// The JavaScript [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) prohibits some obsolete JavaScript syntaxes and makes some slight semantic chnmages to allow more optimizations by JavaScript engines. | ||
/// EcmaScript modules are always in strict mode, while JavaScript scripts are by default in non-strict mode, also known as _sloppy mode_. | ||
/// A developer can add the `"use strict"` directive at the start of a script file to enable the strict mode in that file. | ||
/// | ||
/// Biome considers a CommonJS (`.cjs`) file as a script file. | ||
/// By default, Biome recognizes a JavaScript file (`.js`) as a module file, except if `"type": "commonjs"` is specified in `package.json`. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```cjs,expect_diagnostic | ||
/// var a = 1; | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```cjs | ||
/// "use strict"; | ||
/// | ||
/// var a = 1; | ||
/// ``` | ||
/// | ||
pub UseStrictMode { | ||
version: "1.8.0", | ||
name: "useStrictMode", | ||
language: "js", | ||
recommended: true, | ||
fix_kind: FixKind::Safe, | ||
} | ||
} | ||
|
||
impl Rule for UseStrictMode { | ||
type Query = Ast<JsScript>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let file_source = ctx.source_type::<JsFileSource>(); | ||
|
||
if node.directives().is_empty() && file_source.is_script() { | ||
Some(()) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.range(), | ||
markup! { | ||
"Unexpected absence of the directive "<Emphasis>"\"use strict\"."</Emphasis> | ||
}, | ||
) | ||
.note(markup! { | ||
"Strict mode allows to opt-in some optimisations of the runtime engines, and it eliminates some JavaScript silent errors by changing them to throw errors." | ||
}) | ||
.note(markup!{ | ||
"Check the "<Hyperlink href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode">"for more information regarding strict mode."</Hyperlink> | ||
}), | ||
) | ||
} | ||
|
||
fn action(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<JsRuleAction> { | ||
let node = ctx.query().clone(); | ||
let mut mutation = ctx.root().begin(); | ||
let value = if ctx.as_preferred_quote().is_double() { | ||
jsx_string_literal("use strict") | ||
} else { | ||
jsx_string_literal_single_quotes("use strict") | ||
}; | ||
let directives = js_directive_list(vec![js_directive(value).build()]); | ||
let new_node = node.clone().with_directives(directives); | ||
mutation.replace_node(node, new_node); | ||
Some(JsRuleAction::new( | ||
ActionCategory::QuickFix, | ||
ctx.metadata().applicability(), | ||
markup!("Insert a top level"<Emphasis>"\"use strict\" "</Emphasis>".").to_owned(), | ||
mutation, | ||
)) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/useStrictMode/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,5 @@ | ||
|
||
|
||
function f() { | ||
return "lorem ipsum" | ||
} |
42 changes: 42 additions & 0 deletions
42
crates/biome_js_analyze/tests/specs/nursery/useStrictMode/invalid.js.snap
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,42 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```jsx | ||
|
||
|
||
function f() { | ||
return "lorem ipsum" | ||
} | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:3:1 lint/nursery/useStrictMode FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Unexpected absence of the directive "use strict". | ||
> 3 │ function f() { | ||
│ ^^^^^^^^^^^^^^ | ||
> 4 │ return "lorem ipsum" | ||
> 5 │ } | ||
│ ^ | ||
6 │ | ||
i Strict mode allows to opt-in some optimisations of the runtime engines, and it eliminates some JavaScript silent errors by changing them to throw errors. | ||
i Check the for more information regarding strict mode. | ||
i Safe fix: Insert a top level"use strict" . | ||
1 1 │ | ||
2 2 │ | ||
3 │ + "use·strict" | ||
4 │ + | ||
3 5 │ function f() { | ||
4 6 │ return "lorem ipsum" | ||
``` |
3 changes: 3 additions & 0 deletions
3
crates/biome_js_analyze/tests/specs/nursery/useStrictMode/invalid.package.json
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,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
2 changes: 2 additions & 0 deletions
2
crates/biome_js_analyze/tests/specs/nursery/useStrictMode/valid.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,2 @@ | ||
/* should not generate diagnostics */ | ||
// var a = 1; |
9 changes: 9 additions & 0 deletions
9
crates/biome_js_analyze/tests/specs/nursery/useStrictMode/valid.js.snap
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,9 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```jsx | ||
/* should not generate diagnostics */ | ||
// var a = 1; | ||
``` |
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
Oops, something went wrong.