Skip to content

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
minht11 committed Apr 22, 2024
1 parent e665f76 commit dc1f690
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 76 deletions.
10 changes: 10 additions & 0 deletions 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.

Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ use biome_js_factory::make::js_regex_literal_expression;
use biome_js_semantic::SemanticModel;
use biome_js_syntax::{
global_identifier, static_value::StaticValue, AnyJsCallArgument, AnyJsExpression,
AnyJsLiteralExpression, JsCallArguments, JsCallExpression, JsComputedMemberExpression,
JsNewExpression, JsSyntaxKind, JsSyntaxToken,
};
use biome_rowan::{
declare_node_union, AstNode, AstSeparatedList, BatchMutationExt, SyntaxError, TokenText,
AnyJsLiteralExpression, JsCallArguments, JsComputedMemberExpression, JsNewOrCallExpression,
JsSyntaxKind, JsSyntaxToken,
};
use biome_rowan::{AstNode, AstSeparatedList, BatchMutationExt, SyntaxError, TokenText};

use crate::{services::semantic::Semantic, JsRuleAction};

Expand Down Expand Up @@ -54,10 +52,6 @@ declare_rule! {
}
}

declare_node_union! {
pub JsNewOrCallExpression = JsNewExpression | JsCallExpression
}

pub struct UseRegexLiteralsState {
pattern: String,
flags: Option<String>,
Expand Down
141 changes: 74 additions & 67 deletions crates/biome_js_analyze/src/lint/nursery/use_consistent_new_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make;
use biome_js_syntax::{
global_identifier, AnyJsExpression, JsCallExpression, JsNewExpression, JsSyntaxKind,
};
use biome_rowan::{
chain_trivia_pieces, declare_node_union, AstNode, BatchMutationExt, TriviaPieceKind,
global_identifier, AnyJsExpression, JsCallExpression, JsNewExpression, JsNewOrCallExpression,
JsSyntaxKind,
};
use biome_rowan::{chain_trivia_pieces, AstNode, BatchMutationExt, TriviaPieceKind};

declare_rule! {
/// Enforce the use of new for all builtins, except String, Number, Boolean, Symbol and BigInt.
/// Enforce the use of new for all builtins, except `String`, `Number`, `Boolean`, `Symbol` and `BigInt`.
///
/// They work the same, but new should be preferred for consistency with other constructors.
/// `new Builtin()` and `Builtin()` work the same, but new should be preferred for consistency with other constructors.
/// Enforces the use of new for following builtins:
///
/// - Object
Expand Down Expand Up @@ -65,8 +64,14 @@ declare_rule! {
/// ### Invalid
///
/// ```js,expect_diagnostic
/// const list = Array(10);
/// const text = new String(10);
/// ```
///
/// ```js,expect_diagnostic
/// const now = Date();
/// ```
///
/// ```js,expect_diagnostic
/// const map = Map([
/// ['foo', 'bar']
/// ]);
Expand All @@ -75,8 +80,14 @@ declare_rule! {
/// ### Valid
///
/// ```js
/// const list = new Array(10);
/// const text = String(10);
/// ```
///
/// ```js
/// const now = new Date();
/// ```
///
/// ```js
/// const map = new Map([
/// ['foo', 'bar']
/// ]);
Expand All @@ -85,70 +96,12 @@ declare_rule! {
pub UseConsistentNewBuiltin {
version: "next",
name: "useConsistentNewBuiltin",
sources: &[RuleSource::EslintUnicorn("new-for-builtins")],
sources: &[RuleSource::EslintUnicorn("new-for-builtins"), RuleSource::Eslint("no-new-wrappers")],
recommended: false,
fix_kind: FixKind::Unsafe,
}
}

/// Sorted array of builtins that require new keyword.
const BUILTINS_REQUIRING_NEW: &[&str] = &[
"Array",
"ArrayBuffer",
"BigInt64Array",
"BigUint64Array",
"DataView",
"Date",
"Error",
"FinalizationRegistry",
"Float32Array",
"Float64Array",
"Function",
"Int16Array",
"Int32Array",
"Int8Array",
"Map",
"Object",
"Promise",
"Proxy",
"RegExp",
"Set",
"SharedArrayBuffer",
"Uint16Array",
"Uint32Array",
"Uint8Array",
"Uint8ClampedArray",
"WeakMap",
"WeakRef",
"WeakSet",
];

/// Sorted array of builtins that should not use new keyword.
const BUILTINS_NOT_REQUIRING_NEW: &[&str] = &["Boolean", "Number", "String"];

enum BuiltinCreationRule {
MustUseNew,
MustNotUseNew,
}

impl BuiltinCreationRule {
fn forbidden_builtins_list(&self) -> &[&str] {
match self {
BuiltinCreationRule::MustUseNew => BUILTINS_REQUIRING_NEW,
BuiltinCreationRule::MustNotUseNew => BUILTINS_NOT_REQUIRING_NEW,
}
}
}

pub struct UseConsistentNewBuiltinState {
name: String,
creation_rule: BuiltinCreationRule,
}

declare_node_union! {
pub JsNewOrCallExpression = JsNewExpression | JsCallExpression
}

impl Rule for UseConsistentNewBuiltin {
type Query = Semantic<JsNewOrCallExpression>;
type State = UseConsistentNewBuiltinState;
Expand Down Expand Up @@ -231,6 +184,60 @@ impl Rule for UseConsistentNewBuiltin {
}
}

/// Sorted array of builtins that require new keyword.
const BUILTINS_REQUIRING_NEW: &[&str] = &[
"Array",
"ArrayBuffer",
"BigInt64Array",
"BigUint64Array",
"DataView",
"Date",
"Error",
"FinalizationRegistry",
"Float32Array",
"Float64Array",
"Function",
"Int16Array",
"Int32Array",
"Int8Array",
"Map",
"Object",
"Promise",
"Proxy",
"RegExp",
"Set",
"SharedArrayBuffer",
"Uint16Array",
"Uint32Array",
"Uint8Array",
"Uint8ClampedArray",
"WeakMap",
"WeakRef",
"WeakSet",
];

/// Sorted array of builtins that should not use new keyword.
const BUILTINS_NOT_REQUIRING_NEW: &[&str] = &["Boolean", "Number", "String"];

enum BuiltinCreationRule {
MustUseNew,
MustNotUseNew,
}

impl BuiltinCreationRule {
fn forbidden_builtins_list(&self) -> &[&str] {
match self {
BuiltinCreationRule::MustUseNew => BUILTINS_REQUIRING_NEW,
BuiltinCreationRule::MustNotUseNew => BUILTINS_NOT_REQUIRING_NEW,
}
}
}

pub struct UseConsistentNewBuiltinState {
name: String,
creation_rule: BuiltinCreationRule,
}

fn extract_callee_and_rule(
node: &JsNewOrCallExpression,
) -> Option<(AnyJsExpression, BuiltinCreationRule)> {
Expand Down
4 changes: 4 additions & 0 deletions crates/biome_js_syntax/src/expr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const GLOBAL_THIS: &str = "globalThis";
const UNDEFINED: &str = "undefined";
const WINDOW: &str = "window";

declare_node_union! {
pub JsNewOrCallExpression = JsNewExpression | JsCallExpression
}

impl JsReferenceIdentifier {
/// Returns `true` if this identifier refers to the `undefined` symbol.
///
Expand Down

0 comments on commit dc1f690

Please sign in to comment.