Skip to content

Commit

Permalink
new-for-builtins: Remove auto-fix for new String, new Boolean()
Browse files Browse the repository at this point in the history
…, and `new Number()` (#907)
  • Loading branch information
fisker committed Nov 24, 2020
1 parent 635601d commit dfc1ce0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
3 changes: 1 addition & 2 deletions docs/rules/new-for-builtins.md
Expand Up @@ -39,14 +39,14 @@ Disallows the use of `new` for following builtins.

> These should not use `new` as that would create object wrappers for the primitive values, which is not what you want. However, without `new` they can be useful for coercing a value to that type.
This rule is fixable, except `new String()`, `new Number()`, and `new Boolean()`, [they returns wrapped object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#String_primitives_and_String_objects#String_primitives_and_String_objects).

## Fail

```js
const list = Array(10);
```


```js
const now = Date();
```
Expand All @@ -57,7 +57,6 @@ const map = Map([
]);
```


## Pass

```js
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -111,7 +111,7 @@ Configure it in `package.json`.
- [filename-case](docs/rules/filename-case.md) - Enforce a case style for filenames.
- [import-index](docs/rules/import-index.md) - Enforce importing index files with `.`. *(fixable)*
- [import-style](docs/rules/import-style.md) - Enforce specific import styles per module.
- [new-for-builtins](docs/rules/new-for-builtins.md) - Enforce the use of `new` for all builtins, except `String`, `Number`, `Boolean`, `Symbol` and `BigInt`. *(fixable)*
- [new-for-builtins](docs/rules/new-for-builtins.md) - Enforce the use of `new` for all builtins, except `String`, `Number`, `Boolean`, `Symbol` and `BigInt`. *(partly fixable)*
- [no-abusive-eslint-disable](docs/rules/no-abusive-eslint-disable.md) - Enforce specifying rules to disable in `eslint-disable` comments.
- [no-array-instanceof](docs/rules/no-array-instanceof.md) - Require `Array.isArray()` instead of `instanceof Array`. *(fixable)*
- [no-console-spaces](docs/rules/no-console-spaces.md) - Do not use leading/trailing space between `console.log` parameters. *(fixable)*
Expand Down
15 changes: 10 additions & 5 deletions rules/new-for-builtins.js
Expand Up @@ -31,15 +31,20 @@ const create = context => {
const {name} = callee;

if (disallowNew.has(name) && !isShadowed(context.getScope(), callee)) {
context.report({
const problem = {
node,
messageId: 'disallow',
data: {name},
fix: fixer => fixer.removeRange([
data: {name}
};

if (name !== 'String' && name !== 'Boolean' && name !== 'Number') {
problem.fix = fixer => fixer.removeRange([
node.range[0],
node.callee.range[0]
])
});
]);
}

context.report(problem);
}
}
};
Expand Down
8 changes: 4 additions & 4 deletions test/new-for-builtins.js
Expand Up @@ -249,22 +249,22 @@ test({
{
code: 'const foo = new Boolean()',
errors: [disallowNewError('Boolean')],
output: 'const foo = Boolean()'
output: 'const foo = new Boolean()'
},
{
code: 'const foo = new Number()',
errors: [disallowNewError('Number')],
output: 'const foo = Number()'
output: 'const foo = new Number()'
},
{
code: 'const foo = new Number(\'123\')',
errors: [disallowNewError('Number')],
output: 'const foo = Number(\'123\')'
output: 'const foo = new Number(\'123\')'
},
{
code: 'const foo = new String()',
errors: [disallowNewError('String')],
output: 'const foo = String()'
output: 'const foo = new String()'
},
{
code: 'const foo = new Symbol()',
Expand Down

0 comments on commit dfc1ce0

Please sign in to comment.