Skip to content

Commit

Permalink
New: no-undefined rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Aug 1, 2016
1 parent 229f88f commit a042645
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ npm install --save-dev eslint eslint-plugin-mysticatea
"mysticatea/arrow-parens": "error",
"mysticatea/block-scoped-var": "error",
"mysticatea/no-literal-call": "error",
"mysticatea/no-undefined": "error",
"mysticatea/no-use-ignored-vars": "error",
"mysticatea/no-useless-rest-spread": "error",
"arrow-parens": "off",
Expand All @@ -42,5 +43,6 @@ npm install --save-dev eslint eslint-plugin-mysticatea
- [arrow-parens](docs/rules/arrow-parens.md) - Enforce parens of argument lists (excludes too redundant parens) (fixable).
- [block-scoped-var](docs/rules/block-scoped-var.md) - The complete emulation of block-scoping for `var`.
- [no-literal-call](docs/rules/no-literal-call.md) - Disallow a call of a literal.
- [no-undefined](docs/rules/no-undefined.md) - disallow the declarations of `undefined`.
- [no-use-ignored-vars](docs/rules/no-use-ignored-vars.md) - Disallow a use of ignored variables.
- [no-useless-rest-spread](docs/rules/no-useless-rest-spread.md) - Disallow unnecessary rest/spread operators (fixable).
35 changes: 35 additions & 0 deletions docs/rules/no-undefined.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# disallow the declarations of `undefined` (no-undefined)

In JavaScript, `undefined` is not a keyword. So people can declare variables as `undefined`.

This rule disallow such variables.

## Rule Details

Examples of **incorrect** code for this rule:

```js
/*eslint no-undefined: "error"*/

var undefined;

function undefined() {
}

function foo(undefined) {
}
```

Examples of **correct** code for this rule:

```js
/*eslint no-undefined: "error"*/

// allow a use of undefined.
if (a === undefined) {
}
```

## Options

Nothing.
65 changes: 65 additions & 0 deletions lib/rules/no-undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

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

module.exports = {
meta: {
docs: {
description: "disallow the declarations of `undefined`",
category: "Best Practices",
recommended: false,
},
fixable: false,
schema: [],
},

create: function(context) {
/**
* Checks whether the given variable has `undefined` as its name or not.
*
* @param {escope.Variable} variable - The variable to check.
* @returns {boolean} `true` if the varaible is `undefined`.
*/
function isUndefined(variable) {
return variable.name === "undefined"
}

/**
* Reports the given variable.
*
* @param {escope.Variable} variable - The variable to report.
* @returns {void}
*/
function report(variable) {
variable.defs.forEach(function(def) {
var id = def.name

context.report({
node: id,
message: "Unexpected 'undefined'.",
})
})
}

return {
Program: function() {
var globalScope = context.getScope()
var scopes = [globalScope]
var scope = null

while ((scope = scopes.shift()) != null) {
Array.prototype.push.apply(scopes, scope.childScopes)

scope.variables.filter(isUndefined).forEach(report)
}
},
}
},
}
29 changes: 29 additions & 0 deletions tests/lib/rules/no-undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

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

var rule = require("../../../lib/rules/no-undefined")
var RuleTester = require("eslint").RuleTester

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

;(new RuleTester()).run("no-undefined", rule, {
valid: [
"if (a === undefined) {}",
],
invalid: [
{code: "var undefined", errors: ["Unexpected 'undefined'."]},
{code: "function undefined() {}", errors: ["Unexpected 'undefined'."]},
{code: "function foo() { var undefined }", errors: ["Unexpected 'undefined'."]},
{code: "function foo(undefined) {}", errors: ["Unexpected 'undefined'."]},
],
})

0 comments on commit a042645

Please sign in to comment.