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

Rule Proposal: `no-new-symbol` #4862

Closed
mysticatea opened this Issue Jan 6, 2016 · 3 comments

Comments

Projects
None yet
3 participants
@mysticatea
Copy link
Member

mysticatea commented Jan 6, 2016

new Symbol() always throws a TypeError, but people might guess Symbol is a constructor.
So this rule reports all new Symbol()s.

http://www.ecma-international.org/ecma-262/6.0/#sec-symbol-description

If NewTarget is not undefined, throw a TypeError exception.


The following patterns are considered problems:

/*eslint no-new-symbol: 2*/
/*eslint-env es6*/

const foo = new Symbol("foo");  /*error `new Symbol()` always throws a TypeError.*/

The following patterns are not considered problems:

/*eslint no-new-symbol: 2*/
/*eslint-env es6*/

const foo = Symbol("foo");

// Ignores shadowed Symbol.
function bar(Symbol) {
    const baz = new Symbol("baz");
}
@nzakas

This comment has been minimized.

Copy link
Member

nzakas commented Jan 6, 2016

Good idea, this is very confusing

@alberto

This comment has been minimized.

Copy link
Member

alberto commented Jan 7, 2016

I'm not familiar with escope. Is it enough to check that the Symbol in references doesn't come from the global scope?

@mysticatea

This comment has been minimized.

Copy link
Member Author

mysticatea commented Jan 7, 2016

I guess a logic is:

var variable = globalScope.set.get("Symbol");
if (variable && variable.defs.length === 0) {
    variable.references.filter(isCalleeOfNew).forEach(report);
}
  • variable.defs is not empty if there is a declaration like function Symbol() { } in the source code.
  • variable.references contains all references to this variable.
  • Tests need {env: {es6: true}} to define Symbol variable.

@alberto alberto closed this in ceaf324 Jan 7, 2016

ilyavolodin added a commit that referenced this issue Jan 7, 2016

Merge pull request #4876 from eslint/issue4862
New: Add no-new-symbol rule (fixes #4862)

@eslint eslint bot locked and limited conversation to collaborators Feb 6, 2018

@eslint eslint bot added the archived due to age label Feb 6, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.