-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
feat: new no-new-native-nonconstructor
rule
#16368
Merged
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9ccc5c6
feat: add `no-new-noconstructor` rule
sosukesuzuki 6cde95a
docs: add docs for `no-new-noconstructor`
sosukesuzuki 9263431
fix: pass constructor name to message
sosukesuzuki ab55170
docs: add `related_rules`
sosukesuzuki 48ef5e5
chore: add new rule
sosukesuzuki 1467efe
fix: rename `noconstructor` -> `nonconstructor`
sosukesuzuki e1138b4
fix: set `recommended: false`
sosukesuzuki b79aac4
fix: rename `no-new-nonconstructor` -> `no-new-native-nonconstructor`
sosukesuzuki 0349065
docs: fix correct example
sosukesuzuki f15fd20
Update docs/src/rules/no-new-native-nonconstructor.md
sosukesuzuki 5eed2d6
docs: update links
sosukesuzuki 410b348
docs: address reviews
sosukesuzuki aa6f701
fix: `no constructor` -> `non-constructor`
sosukesuzuki 14489ad
Apply suggestions from code review
sosukesuzuki c9f11ed
Merge branch 'main' into add-no-new-noconstructor
btmills File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,74 @@ | ||
--- | ||
title: no-new-native-nonconstructor | ||
layout: doc | ||
rule_type: problem | ||
related_rules: | ||
- no-new-symbol | ||
sosukesuzuki marked this conversation as resolved.
Show resolved
Hide resolved
mdjermanovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
further_reading: | ||
- https://tc39.es/ecma262/#sec-symbol-object | ||
- https://tc39.es/ecma262/#sec-bigint-object | ||
sosukesuzuki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--- | ||
|
||
|
||
|
||
Certain functions are not intended to be used with the `new` operator, but to be called as a function. | ||
sosukesuzuki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```js | ||
var foo = new Symbol("foo"); | ||
``` | ||
|
||
```js | ||
var bar = new BigInt(9007199254740991) | ||
sosukesuzuki marked this conversation as resolved.
Show resolved
Hide resolved
mdjermanovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
These throw a `TypeError` exception. | ||
sosukesuzuki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Rule Details | ||
|
||
This rule is aimed at preventing the accidental calling of certain functions with the `new` operator. These functions are: | ||
sosukesuzuki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* `Symbol` | ||
* `BigInt` | ||
|
||
## Examples | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
::: incorrect | ||
|
||
```js | ||
/*eslint no-new-native-nonconstructor: "error"*/ | ||
/*eslint-env es2022*/ | ||
|
||
var foo = new Symbol('foo'); | ||
var bar = new BigInt(9007199254740991); | ||
``` | ||
|
||
::: | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
::: correct | ||
|
||
```js | ||
/*eslint no-new-symbol: "error"*/ | ||
sosukesuzuki marked this conversation as resolved.
Show resolved
Hide resolved
mdjermanovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/*eslint-env es2022*/ | ||
|
||
var foo = Symbol('foo'); | ||
var bar = new BigInt(9007199254740991); | ||
mdjermanovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Ignores shadowed Symbol. | ||
function baz(Symbol) { | ||
const qux = new Symbol("baz"); | ||
} | ||
function quux(BigInt) { | ||
const corge = new BigInt(9007199254740991); | ||
} | ||
|
||
``` | ||
|
||
::: | ||
|
||
## When Not To Use It | ||
|
||
This rule should not be used in ES3/5 environments. |
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,64 @@ | ||
/** | ||
* @fileoverview Rule to disallow use of the new operator with global no constructor functions | ||
* @author Sosuke Suzuki | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
const nonConstructorGlobalFunctionNames = ["Symbol", "BigInt"]; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
/** @type {import('../shared/types').Rule} */ | ||
module.exports = { | ||
meta: { | ||
type: "problem", | ||
|
||
docs: { | ||
description: "Disallow `new` operators with global no constructor functions", | ||
sosukesuzuki marked this conversation as resolved.
Show resolved
Hide resolved
mdjermanovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
recommended: false, | ||
url: "https://eslint.org/docs/rules/no-new-native-nonconstructor" | ||
}, | ||
|
||
schema: [], | ||
|
||
messages: { | ||
noNewNonconstructor: "`{{name}}` cannot be called as a constructor." | ||
} | ||
}, | ||
|
||
create(context) { | ||
|
||
return { | ||
"Program:exit"() { | ||
const globalScope = context.getScope(); | ||
|
||
for (const nonConstructorName of nonConstructorGlobalFunctionNames) { | ||
const variable = globalScope.set.get(nonConstructorName); | ||
|
||
if (variable && variable.defs.length === 0) { | ||
variable.references.forEach(ref => { | ||
const node = ref.identifier; | ||
const parent = node.parent; | ||
|
||
if (parent && parent.type === "NewExpression" && parent.callee === node) { | ||
context.report({ | ||
node, | ||
messageId: "noNewNonconstructor", | ||
data: { name: nonConstructorName } | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
} | ||
}; |
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,68 @@ | ||
/** | ||
* @fileoverview Tests for the no-new-native-nonconstructor rule | ||
* @author Sosuke Suzuki | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/no-new-native-nonconstructor"), | ||
{ RuleTester } = require("../../../lib/rule-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ env: { es2022: true } }); | ||
|
||
ruleTester.run("no-new-native-nonconstructor", rule, { | ||
valid: [ | ||
|
||
// Symbol | ||
"var foo = Symbol('foo');", | ||
"function bar(Symbol) { var baz = new Symbol('baz');}", | ||
"function Symbol() {} new Symbol();", | ||
"new foo(Symbol);", | ||
"new foo(bar, Symbol);", | ||
|
||
// BigInt | ||
"var foo = BigInt(9007199254740991);", | ||
"function bar(BigInt) { var baz = new BigInt(9007199254740991);}", | ||
"function BigInt() {} new BigInt();", | ||
"new foo(BigInt);", | ||
"new foo(bar, BigInt);" | ||
], | ||
invalid: [ | ||
|
||
// Symbol | ||
{ | ||
code: "var foo = new Symbol('foo');", | ||
errors: [{ | ||
message: "`Symbol` cannot be called as a constructor." | ||
}] | ||
}, | ||
{ | ||
code: "function bar() { return function Symbol() {}; } var baz = new Symbol('baz');", | ||
errors: [{ | ||
message: "`Symbol` cannot be called as a constructor." | ||
}] | ||
}, | ||
|
||
// BigInt | ||
{ | ||
code: "var foo = new BigInt(9007199254740991);", | ||
errors: [{ | ||
message: "`BigInt` cannot be called as a constructor." | ||
}] | ||
}, | ||
{ | ||
code: "function bar() { return function BigInt() {}; } var baz = new BigInt(9007199254740991);", | ||
errors: [{ | ||
message: "`BigInt` cannot be called as a constructor." | ||
}] | ||
} | ||
] | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should fix the docs site build.