mirror of
https://github.com/bendtherules/eslint-plugin-undef-init.git
synced 2026-08-18 13:52:15 +00:00
59 lines
1.8 KiB
JavaScript
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");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
};
|
|
}
|
|
};
|