mirror of
https://github.com/bendtherules/eslint-plugin-undef-init.git
synced 2026-08-18 22:02:50 +00:00
Add undef-init rule (with yo eslint:rule) and tests for it
This commit is contained in:
@@ -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");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user