Skip to content
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

new-for-builtins: Remove auto-fix for new String, new Boolean(), and new Number() #907

Merged
merged 4 commits into from Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}
}
fisker marked this conversation as resolved.
Show resolved Hide resolved

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