Skip to content
Permalink
Browse files

New: Add no-new-symbol rule (fixes #4862)

  • Loading branch information...
alberto committed Jan 7, 2016
1 parent 2ccbc01 commit ceaf324010c80c1925b8807867e5cbe0336112db
Showing with 122 additions and 0 deletions.
  1. +1 −0 conf/eslint.json
  2. +1 −0 docs/rules/README.md
  3. +46 −0 docs/rules/no-new-symbol.md
  4. +33 −0 lib/rules/no-new-symbol.js
  5. +41 −0 tests/lib/rules/no-new-symbol.js
@@ -68,6 +68,7 @@
"no-new-func": 0,
"no-new-object": 0,
"no-new-require": 0,
"no-new-symbol": 0,
"no-new-wrappers": 0,
"no-obj-calls": 2,
"no-octal": 2,
@@ -221,6 +221,7 @@ These rules are only relevant to ES6 environments.
* [no-confusing-arrow](no-confusing-arrow.md) - disallow arrow functions where they could be confused with comparisons
* [no-const-assign](no-const-assign.md) - disallow modifying variables that are declared using `const`
* [no-dupe-class-members](no-dupe-class-members.md) - disallow duplicate name in class members
* [no-new-symbol](no-new-symbol.md) - disallow use of the `new` operator with the `Symbol` object
* [no-this-before-super](no-this-before-super.md) - disallow use of `this`/`super` before calling `super()` in constructors.
* [no-var](no-var.md) - require `let` or `const` instead of `var`
* [no-useless-constructor](no-useless-constructor.md) - disallow unnecessary constructor
@@ -0,0 +1,46 @@
# Disallow Symbol Constructor (no-new-symbol)

The `Symbol` constructor is not intended to be used with the `new` operator, but to be called as a function.

```js
var foo = new Symbol("foo");
```

This throws a `TypeError` exception.

## Rule Details

This rule is aimed at preventing the accidental calling of `Symbol` with the `new` operator.

The following patterns are considered problems:

```js
/*eslint no-new-symbol: 2*/
/*eslint-env es6*/
var foo = new Symbol('foo'); /*error `Symbol` cannot be called as a constructor. */
```

The following patterns are not considered problems:

```js
/*eslint no-new-symbol: 2*/
/*eslint-env es6*/
var foo = Symbol('foo');
// Ignores shadowed Symbol.
function bar(Symbol) {
const baz = new Symbol("baz");
}
```

## When Not to Use It

This rule should not be used in ES3/5 environments.

## Further Reading

* [Symbol Objects specification](http://www.ecma-international.org/ecma-262/6.0/#sec-symbol-objects)
@@ -0,0 +1,33 @@
/**
* @fileoverview Rule to disallow use of the new operator with the `Symbol` object
* @author Alberto Rodríguez
* @copyright 2016 Alberto Rodríguez. All rights reserved.
* See LICENSE file in root directory for full license.
*/

"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

return {
"Program:exit": function() {
var globalScope = context.getScope();
var variable = globalScope.set.get("Symbol");
if (variable && variable.defs.length === 0) {
variable.references.forEach(function(ref) {
var node = ref.identifier;
if (node.parent && node.parent.type === "NewExpression") {
context.report(node, "`Symbol` cannot be called as a constructor.");
}
});
}
}
};

};

module.exports.schema = [];
@@ -0,0 +1,41 @@
/**
* @fileoverview Tests for the no-new-symbol rule
* @author Alberto Rodríguez
* @copyright 2016 Alberto Rodríguez. All rights reserved.
* See LICENSE file in root directory for full license.
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/no-new-symbol"),
RuleTester = require("../../../lib/testers/rule-tester");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run("no-new-symbol", rule, {
valid: [
{code: "var foo = Symbol('foo');", env: {es6: true} },
{code: "function bar(Symbol) { var baz = new Symbol('baz');}", env: {es6: true} },
{code: "function Symbol() {} new Symbol();", env: {es6: true} }
],
invalid: [
{
code: "var foo = new Symbol('foo');",
env: {es6: true},
errors: [{ message: "`Symbol` cannot be called as a constructor."}]
},
{
code: "function bar() { return function Symbol() {}; } var baz = new Symbol('baz');",
env: {es6: true},
errors: [{ message: "`Symbol` cannot be called as a constructor."}]
}
]
});

0 comments on commit ceaf324

Please sign in to comment.
You can’t perform that action at this time.