Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better fix for
new-for-builtins
(#1022)
- Loading branch information
Showing
6 changed files
with
123 additions
and
27 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
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,26 @@ | ||
'use strict'; | ||
|
||
const {isOpeningParenToken, isClosingParenToken} = require('eslint-utils'); | ||
|
||
/** | ||
Determine if a constructor function is newed-up with parens. | ||
@param {Node} node - The `NewExpression` node to be checked. | ||
@param {SourceCode} sourceCode - The source code object. | ||
@returns {boolean} True if the constructor is called with parens. | ||
Copied from https://github.com/eslint/eslint/blob/cc4871369645c3409dc56ded7a555af8a9f63d51/lib/rules/no-extra-parens.js#L252 | ||
*/ | ||
function isNewExpressionWithParentheses(node, sourceCode) { | ||
if (node.arguments.length > 0) { | ||
return true; | ||
} | ||
|
||
const [penultimateToken, lastToken] = sourceCode.getLastTokens(node, 2); | ||
// The expression should end with its own parens, for example, `new new Foo()` is not a new expression with parens. | ||
return isOpeningParenToken(penultimateToken) && | ||
isClosingParenToken(lastToken) && | ||
node.callee.range[1] < node.range[1]; | ||
} | ||
|
||
module.exports = isNewExpressionWithParentheses; |
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
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
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,69 @@ | ||
# Snapshot report for `test/new-for-builtins.js` | ||
|
||
The actual snapshot is saved in `new-for-builtins.js.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## Invalid #1 | ||
1 | const object = (Object)(); | ||
|
||
> Output | ||
`␊ | ||
1 | const object = new (Object)();␊ | ||
` | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | const object = (Object)();␊ | ||
| ^^^^^^^^^^ Use `new Object()` instead of `Object()`.␊ | ||
` | ||
|
||
## Invalid #2 | ||
1 | const symbol = new (Symbol)(""); | ||
|
||
> Output | ||
`␊ | ||
1 | const symbol = (Symbol)("");␊ | ||
` | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | const symbol = new (Symbol)("");␊ | ||
| ^^^^^^^^^^^^^^^^ Use `Symbol()` instead of `new Symbol()`.␊ | ||
` | ||
|
||
## Invalid #3 | ||
1 | const symbol = new /* comment */ Symbol(""); | ||
|
||
> Output | ||
`␊ | ||
1 | const symbol = /* comment */ Symbol("");␊ | ||
` | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | const symbol = new /* comment */ Symbol("");␊ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Symbol()` instead of `new Symbol()`.␊ | ||
` | ||
|
||
## Invalid #4 | ||
1 | const symbol = new Symbol; | ||
|
||
> Output | ||
`␊ | ||
1 | const symbol = Symbol();␊ | ||
` | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | const symbol = new Symbol;␊ | ||
| ^^^^^^^^^^ Use `Symbol()` instead of `new Symbol()`.␊ | ||
` |
Binary file not shown.