-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lint/noInvalidNewBuiltin): add rule (#375)
- Loading branch information
Showing
15 changed files
with
446 additions
and
30 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
107 changes: 107 additions & 0 deletions
107
crates/biome_js_analyze/src/semantic_analyzers/nursery/no_invalid_new_builtin.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,107 @@ | ||
use crate::{semantic_services::Semantic, JsRuleAction}; | ||
use biome_analyze::{context::RuleContext, declare_rule, ActionCategory, Rule, RuleDiagnostic}; | ||
use biome_console::markup; | ||
use biome_diagnostics::Applicability; | ||
use biome_js_factory::make; | ||
use biome_js_syntax::{ | ||
global_identifier, static_value::StaticValue, AnyJsExpression, JsCallExpression, | ||
JsNewExpression, | ||
}; | ||
use biome_rowan::{chain_trivia_pieces, AstNode, BatchMutationExt}; | ||
|
||
declare_rule! { | ||
/// Disallow `new` operators with global non-constructor functions. | ||
/// | ||
/// Some global functions cannot be called using the new operator and | ||
/// will throw a `TypeError` if you attempt to do so. These functions are: | ||
/// | ||
/// - [`Symbol`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol/Symbol) | ||
/// - [`BigInt`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/BigInt/BigInt) | ||
/// | ||
/// Source: https://eslint.org/docs/latest/rules/no-new-native-nonconstructor/ | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// var foo = new Symbol('foo'); | ||
/// var bar = new BigInt(9007199254740991); | ||
/// ``` | ||
/// | ||
/// ## Valid | ||
/// | ||
/// ```js | ||
/// var foo = Symbol('foo'); | ||
/// var bar = BigInt(9007199254740991); | ||
/// | ||
/// function baz(Symbol) { | ||
/// const qux = new Symbol("baz"); | ||
/// } | ||
/// function quux(BigInt) { | ||
/// const corge = new BigInt(9007199254740991); | ||
/// } | ||
/// ``` | ||
pub(crate) NoInvalidNewBuiltin { | ||
version: "next", | ||
name: "noInvalidNewBuiltin", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for NoInvalidNewBuiltin { | ||
type Query = Semantic<JsNewExpression>; | ||
type State = StaticValue; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let callee = ctx.query().callee().ok()?; | ||
let (reference, name) = global_identifier(&callee)?; | ||
match name.text() { | ||
"Symbol" | "BigInt" => ctx.model().binding(&reference).is_none().then_some(name), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, builtin_name: &Self::State) -> Option<RuleDiagnostic> { | ||
let builtin_name = builtin_name.text(); | ||
Some(RuleDiagnostic::new( | ||
rule_category!(), | ||
ctx.query().range(), | ||
markup! { | ||
<Emphasis>{builtin_name}</Emphasis>" cannot be called as a constructor." | ||
}, | ||
).note(markup! { | ||
"Calling "<Emphasis>{builtin_name}</Emphasis>" with the "<Emphasis>"new"</Emphasis>" operator throws a "<Emphasis>"TypeError"</Emphasis>"." | ||
})) | ||
} | ||
|
||
fn action(ctx: &RuleContext<Self>, _: &Self::State) -> Option<JsRuleAction> { | ||
let node = ctx.query(); | ||
let call_expression = convert_new_expression_to_call_expression(node)?; | ||
let mut mutation = ctx.root().begin(); | ||
mutation.replace_node_discard_trivia::<AnyJsExpression>( | ||
node.clone().into(), | ||
call_expression.into(), | ||
); | ||
Some(JsRuleAction { | ||
category: ActionCategory::QuickFix, | ||
applicability: Applicability::MaybeIncorrect, | ||
message: markup! { "Remove "<Emphasis>"new"</Emphasis>"." }.to_owned(), | ||
mutation, | ||
}) | ||
} | ||
} | ||
|
||
fn convert_new_expression_to_call_expression(expr: &JsNewExpression) -> Option<JsCallExpression> { | ||
let new_token = expr.new_token().ok()?; | ||
let mut callee = expr.callee().ok()?; | ||
if new_token.has_leading_comments() || new_token.has_trailing_comments() { | ||
callee = callee.prepend_trivia_pieces(chain_trivia_pieces( | ||
new_token.leading_trivia().pieces(), | ||
new_token.trailing_trivia().pieces(), | ||
))?; | ||
} | ||
Some(make::js_call_expression(callee, expr.arguments()?).build()) | ||
} |
11 changes: 11 additions & 0 deletions
11
crates/biome_js_analyze/tests/specs/nursery/noInvalidNewBuiltin/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,11 @@ | ||
var foo = new Symbol("foo"); | ||
function bar() { | ||
return function Symbol() {}; | ||
} | ||
var baz = new Symbol("baz"); | ||
|
||
var foo = new BigInt(9007199254740991); | ||
function bar() { | ||
return function BigInt() {}; | ||
} | ||
var baz = new BigInt(9007199254740991); |
103 changes: 103 additions & 0 deletions
103
crates/biome_js_analyze/tests/specs/nursery/noInvalidNewBuiltin/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,103 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```js | ||
var foo = new Symbol("foo"); | ||
function bar() { | ||
return function Symbol() {}; | ||
} | ||
var baz = new Symbol("baz"); | ||
|
||
var foo = new BigInt(9007199254740991); | ||
function bar() { | ||
return function BigInt() {}; | ||
} | ||
var baz = new BigInt(9007199254740991); | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:1:11 lint/nursery/noInvalidNewBuiltin FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Symbol cannot be called as a constructor. | ||
> 1 │ var foo = new Symbol("foo"); | ||
│ ^^^^^^^^^^^^^^^^^ | ||
2 │ function bar() { | ||
3 │ return function Symbol() {}; | ||
i Calling Symbol with the new operator throws a TypeError. | ||
i Suggested fix: Remove new. | ||
1 │ var·foo·=·new·Symbol("foo"); | ||
│ ---- | ||
``` | ||
|
||
``` | ||
invalid.js:5:11 lint/nursery/noInvalidNewBuiltin FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Symbol cannot be called as a constructor. | ||
3 │ return function Symbol() {}; | ||
4 │ } | ||
> 5 │ var baz = new Symbol("baz"); | ||
│ ^^^^^^^^^^^^^^^^^ | ||
6 │ | ||
7 │ var foo = new BigInt(9007199254740991); | ||
i Calling Symbol with the new operator throws a TypeError. | ||
i Suggested fix: Remove new. | ||
5 │ var·baz·=·new·Symbol("baz"); | ||
│ ---- | ||
``` | ||
|
||
``` | ||
invalid.js:7:11 lint/nursery/noInvalidNewBuiltin FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! BigInt cannot be called as a constructor. | ||
5 │ var baz = new Symbol("baz"); | ||
6 │ | ||
> 7 │ var foo = new BigInt(9007199254740991); | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
8 │ function bar() { | ||
9 │ return function BigInt() {}; | ||
i Calling BigInt with the new operator throws a TypeError. | ||
i Suggested fix: Remove new. | ||
7 │ var·foo·=·new·BigInt(9007199254740991); | ||
│ ---- | ||
``` | ||
|
||
``` | ||
invalid.js:11:11 lint/nursery/noInvalidNewBuiltin FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! BigInt cannot be called as a constructor. | ||
9 │ return function BigInt() {}; | ||
10 │ } | ||
> 11 │ var baz = new BigInt(9007199254740991); | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
12 │ | ||
i Calling BigInt with the new operator throws a TypeError. | ||
i Suggested fix: Remove new. | ||
11 │ var·baz·=·new·BigInt(9007199254740991); | ||
│ ---- | ||
``` | ||
|
||
|
17 changes: 17 additions & 0 deletions
17
crates/biome_js_analyze/tests/specs/nursery/noInvalidNewBuiltin/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,17 @@ | ||
var foo = Symbol("foo"); | ||
function bar(Symbol) { | ||
var baz = new Symbol("baz"); | ||
} | ||
function Symbol() {} | ||
new Symbol(); | ||
new foo(Symbol); | ||
new foo(bar, Symbol); | ||
|
||
var foo = BigInt(9007199254740991); | ||
function bar(BigInt) { | ||
var baz = new BigInt(9007199254740991); | ||
} | ||
function BigInt() {} | ||
new BigInt(); | ||
new foo(BigInt); | ||
new foo(bar, BigInt); |
27 changes: 27 additions & 0 deletions
27
crates/biome_js_analyze/tests/specs/nursery/noInvalidNewBuiltin/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,27 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```js | ||
var foo = Symbol("foo"); | ||
function bar(Symbol) { | ||
var baz = new Symbol("baz"); | ||
} | ||
function Symbol() {} | ||
new Symbol(); | ||
new foo(Symbol); | ||
new foo(bar, Symbol); | ||
|
||
var foo = BigInt(9007199254740991); | ||
function bar(BigInt) { | ||
var baz = new BigInt(9007199254740991); | ||
} | ||
function BigInt() {} | ||
new BigInt(); | ||
new foo(BigInt); | ||
new foo(bar, BigInt); | ||
|
||
``` | ||
|
||
|
Oops, something went wrong.