mirror of
https://github.com/bendtherules/eslint-plugin-undef-init.git
synced 2026-08-18 13:52:15 +00:00
Add undef-init rule (with yo eslint:rule) and tests for it
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
# Always initialize variables during declaration. Set it explicitly to undefined, if required. (undef-init)
|
||||||
|
|
||||||
|
Please describe the origin of the rule here.
|
||||||
|
|
||||||
|
|
||||||
|
## Rule Details
|
||||||
|
|
||||||
|
This rule aims to...
|
||||||
|
|
||||||
|
Examples of **incorrect** code for this rule:
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
// fill me in
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Examples of **correct** code for this rule:
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
// fill me in
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Options
|
||||||
|
|
||||||
|
If there are any options, describe them here. Otherwise, delete this section.
|
||||||
|
|
||||||
|
## When Not To Use It
|
||||||
|
|
||||||
|
Give a short description of when it would be appropriate to turn off this rule.
|
||||||
|
|
||||||
|
## Further Reading
|
||||||
|
|
||||||
|
If there are other links that describe the issue this rule addresses, please include them here in a bulleted list.
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = {
|
||||||
|
'undef-init': "Always initialize variables during declaration. Set it explicitly to undefined, if required.",
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
/**
|
||||||
|
* @fileoverview Always initialize variables during declaration. Set it explicitly to undefined, if required.
|
||||||
|
* @author Abhas Bhattacharya
|
||||||
|
*/
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const ruleMessage = require('../ruleNameMessageMap')['undef-init'];
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// Rule Definition
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
meta: {
|
||||||
|
docs: {
|
||||||
|
description: ruleMessage,
|
||||||
|
category: "Fill me in",
|
||||||
|
recommended: false
|
||||||
|
},
|
||||||
|
fixable: "code",
|
||||||
|
schema: [
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
create: function (context) {
|
||||||
|
|
||||||
|
// variables should be defined here
|
||||||
|
const defaultAllowInForConditon = {
|
||||||
|
ForInStatement: true,
|
||||||
|
ForOfStatement: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
// Public
|
||||||
|
//----------------------------------------------------------------------
|
||||||
|
|
||||||
|
return {
|
||||||
|
"VariableDeclarator": function (node) {
|
||||||
|
|
||||||
|
if (node.init === null) {
|
||||||
|
// Parent in always VariableDeclarator, whose parent can be directly one of the for in / for of statements
|
||||||
|
if (node.parent.parent.type === 'ForInStatement' && defaultAllowInForConditon.ForInStatement) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (node.parent.parent.type === 'ForOfStatement' && defaultAllowInForConditon.ForOfStatement) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.report({
|
||||||
|
node:node,
|
||||||
|
message:ruleMessage,
|
||||||
|
fix: function(fixer) {
|
||||||
|
return fixer.insertTextAfter(node, " = undefined");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
/**
|
||||||
|
* @fileoverview Always initialize variables during declaration. Set it explicitly to undefined, if required.
|
||||||
|
* @author Abhas Bhattacharya
|
||||||
|
*/
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// Requirements
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
var rule = require("../../../lib/rules/undef-init"),
|
||||||
|
RuleTester = require("eslint").RuleTester,
|
||||||
|
ruleMessage = require("../../../lib/ruleNameMessageMap")['undef-init'];
|
||||||
|
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// Tests
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
var ruleTester = new RuleTester({
|
||||||
|
parserOptions: { ecmaVersion: 7 }
|
||||||
|
});
|
||||||
|
ruleTester.run("undef-init", rule, {
|
||||||
|
|
||||||
|
valid: [
|
||||||
|
"var a = undefined;",
|
||||||
|
"var a = 123;",
|
||||||
|
"let a = [];",
|
||||||
|
"var a = 1; var b = a;",
|
||||||
|
"for (var a of {}) {a;}",
|
||||||
|
"for (var a in []) {a;}",
|
||||||
|
],
|
||||||
|
|
||||||
|
invalid: [
|
||||||
|
{
|
||||||
|
code: "var a;",
|
||||||
|
errors: [{
|
||||||
|
message: ruleMessage,
|
||||||
|
}],
|
||||||
|
output: "var a = undefined;",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "let abc; something_else()",
|
||||||
|
errors: [{
|
||||||
|
message: ruleMessage,
|
||||||
|
}],
|
||||||
|
output: "let abc = undefined; something_else()"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "let a1, a2, a3;",
|
||||||
|
errors: [
|
||||||
|
{
|
||||||
|
message: ruleMessage,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: ruleMessage,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: ruleMessage,
|
||||||
|
}
|
||||||
|
],
|
||||||
|
output: "let a1 = undefined, a2 = undefined, a3 = undefined;",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "var a1, a2= 123, a3;",
|
||||||
|
errors: [
|
||||||
|
{
|
||||||
|
message: ruleMessage,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: ruleMessage,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
output: "var a1 = undefined, a2= 123, a3 = undefined;",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "for (a of {}){var c;}",
|
||||||
|
errors: [
|
||||||
|
{
|
||||||
|
message: ruleMessage,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
output: "for (a of {}){var c = undefined;}",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "for (var a of {}){var c;}",
|
||||||
|
errors: [
|
||||||
|
{
|
||||||
|
message: ruleMessage,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
output: "for (var a of {}){var c = undefined;}",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "for (var a in []){var c;}",
|
||||||
|
errors: [
|
||||||
|
{
|
||||||
|
message: ruleMessage,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
output: "for (var a in []){var c = undefined;}",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user