-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
238 additions
and
1 deletion.
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,61 @@ | ||
'use strict'; | ||
|
||
const { makeRuleDocsURL } = require('../utils'); | ||
|
||
const CONSTRUCTOR_NAMES = | ||
[ | ||
'AggregateError', | ||
'Array', | ||
'Error', | ||
'EvalError', | ||
'Function', | ||
'Object', | ||
'RangeError', | ||
'ReferenceError', | ||
'RegExp', | ||
'SyntaxError', | ||
'TypeError', | ||
'URIError', | ||
]; | ||
|
||
const meta = | ||
{ | ||
type: 'suggestion', | ||
docs: | ||
{ | ||
description: 'Disallow unnecessary usages of the new syntax', | ||
url: makeRuleDocsURL('no-extra-new'), | ||
}, | ||
schema: [], | ||
messages: { unexpected: 'Unnecessary usage of the new syntax.' }, | ||
}; | ||
|
||
function create(context) | ||
{ | ||
const { sourceCode } = context; | ||
const ruleListeners = | ||
{ | ||
'Program:exit': | ||
node => | ||
{ | ||
const globalScope = sourceCode.getScope(node); | ||
for (const constructorName of CONSTRUCTOR_NAMES) | ||
{ | ||
const variable = globalScope.set.get(constructorName); | ||
if (variable && variable.defs.length === 0) | ||
{ | ||
for (const ref of variable.references) | ||
{ | ||
const idNode = ref.identifier; | ||
const { parent } = idNode; | ||
if (parent && parent.type === 'NewExpression' && parent.callee === idNode) | ||
context.report({ node: parent, messageId: 'unexpected' }); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
return ruleListeners; | ||
} | ||
|
||
module.exports = { meta, create }; |
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,52 @@ | ||
# `no-extra-new` | ||
|
||
The rule `no-extra-new` disallows unnecessary usages of the the `new` syntax. | ||
|
||
The following built-in constructors can be called equally with or without `new` syntax: | ||
* `AggregateError` | ||
* `Array` | ||
* `Error` | ||
* `EvalError` | ||
* `Function` | ||
* `Object` | ||
* `RangeError` | ||
* `ReferenceError` | ||
* `RegExp` | ||
* `SyntaxError` | ||
* `TypeError` | ||
* `URIError` | ||
|
||
## Examples | ||
|
||
### Examples of **incorrect** code for this rule | ||
|
||
```js | ||
/* eslint @origin-1/no-extra-new: "error" */ | ||
|
||
var array = new Array(42); | ||
var fn = new Function; | ||
var obj = new Object('foo'); | ||
var regExp = new RegExp('\\d', 'g'); | ||
throw new TypeError(); | ||
``` | ||
|
||
### Examples of **correct** code for this rule | ||
|
||
```js | ||
/* eslint @origin-1/no-extra-new: "error" */ | ||
|
||
var array = Array(42); | ||
var fn = Function(); | ||
var obj = Object('foo'); | ||
var regExp = RegExp('\\d', 'g'); | ||
throw TypeError(); | ||
|
||
function throwNew(Error) | ||
{ | ||
throw new Error(); // New expressions with a variable as callee are allowed. | ||
} | ||
``` | ||
|
||
## Superseded core ESLint rules | ||
|
||
* [`no-new-object`](https://eslint.org/docs/latest/rules/no-new-object) |
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,122 @@ | ||
'use strict'; | ||
|
||
const rule = require('../../../lib/rules/no-extra-new'); | ||
const { RuleTester } = require('eslint'); | ||
|
||
const ruleTester = new RuleTester(); | ||
const tests = | ||
{ | ||
valid: | ||
[ | ||
'var myObject = {};', | ||
'var myObject = new CustomObject();', | ||
'var foo = new foo.Object();', | ||
` | ||
var Object = function Object() {}; | ||
new Object(); | ||
`, | ||
` | ||
var x = something ? MyClass : Object; | ||
var y = new x();`, | ||
{ | ||
code: | ||
` | ||
class Object | ||
{ | ||
constructor() { } | ||
} | ||
new Object(); | ||
`, | ||
parserOptions: { ecmaVersion: 6 }, | ||
}, | ||
{ | ||
code: | ||
` | ||
import { Object } from './'; | ||
new Object(); | ||
`, | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
}, | ||
` | ||
var Error = CustomError; | ||
function foo() | ||
{ | ||
throw new Error(); | ||
} | ||
`, | ||
{ | ||
code: | ||
` | ||
function foo() | ||
{ | ||
throw new Error(); | ||
} | ||
const Error = CustomError; | ||
`, | ||
parserOptions: { ecmaVersion: 6 }, | ||
}, | ||
{ | ||
code: 'throw new AggregateError(errors);', | ||
env: { es2020: true }, | ||
}, | ||
{ | ||
code: '(new Function(foo))();', | ||
globals: { Function: 'off' }, | ||
}, | ||
` | ||
/* global RegExp:off */ | ||
new RegExp(pattern, flags); | ||
`, | ||
], | ||
invalid: | ||
[ | ||
{ | ||
code: 'var foo = new Object();', | ||
errors: [{ messageId: 'unexpected', type: 'NewExpression' }], | ||
}, | ||
{ | ||
code: 'new Object();', | ||
errors: [{ messageId: 'unexpected', type: 'NewExpression' }], | ||
}, | ||
{ | ||
code: 'const a = new Object();', | ||
parserOptions: { ecmaVersion: 6 }, | ||
errors: [{ messageId: 'unexpected', type: 'NewExpression' }], | ||
}, | ||
{ | ||
code: | ||
` | ||
new Array; | ||
new Error; | ||
new EvalError; | ||
new Function; | ||
new Object; | ||
new RangeError; | ||
new ReferenceError; | ||
new RegExp; | ||
new SyntaxError; | ||
new TypeError; | ||
new URIError; | ||
`, | ||
errors: Array(11).fill({ messageId: 'unexpected', type: 'NewExpression' }), | ||
}, | ||
{ | ||
code: 'throw new AggregateError(errors);', | ||
env: { es2021: true }, | ||
errors: [{ messageId: 'unexpected', type: 'NewExpression' }], | ||
}, | ||
{ | ||
code: | ||
` | ||
function foo(Error) | ||
{ | ||
throw new Error; | ||
} | ||
throw new Error; | ||
`, | ||
errors: [{ messageId: 'unexpected', type: 'NewExpression' }], | ||
}, | ||
], | ||
}; | ||
|
||
ruleTester.run('no-extra-new', rule, tests); |