Files
eslint-plugin-undef-init/lib/rules/undef-init.js
T
2018-08-20 10:44:07 +05:30

59 lines
1.8 KiB
JavaScript

/**
* @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,
},
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");
}
});
}
}
};
}
};