-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
1 parent
229f88f
commit a042645
Showing
4 changed files
with
131 additions
and
0 deletions.
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,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. |
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,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) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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,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'."]}, | ||
], | ||
}) |