UNPKG

4.5 kBJavaScriptView Raw
1/**
2 * @fileoverview A rule to control the style of variable initializations.
3 * @author Colin Ihrig
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Helpers
10//------------------------------------------------------------------------------
11
12/**
13 * Checks whether or not a given node is a for loop.
14 * @param {ASTNode} block - A node to check.
15 * @returns {boolean} `true` when the node is a for loop.
16 */
17function isForLoop(block) {
18 return block.type === "ForInStatement" ||
19 block.type === "ForOfStatement" ||
20 block.type === "ForStatement";
21}
22
23/**
24 * Checks whether or not a given declarator node has its initializer.
25 * @param {ASTNode} node - A declarator node to check.
26 * @returns {boolean} `true` when the node has its initializer.
27 */
28function isInitialized(node) {
29 const declaration = node.parent;
30 const block = declaration.parent;
31
32 if (isForLoop(block)) {
33 if (block.type === "ForStatement") {
34 return block.init === declaration;
35 }
36 return block.left === declaration;
37 }
38 return Boolean(node.init);
39}
40
41//------------------------------------------------------------------------------
42// Rule Definition
43//------------------------------------------------------------------------------
44
45module.exports = {
46 meta: {
47 docs: {
48 description: "require or disallow initialization in variable declarations",
49 category: "Variables",
50 recommended: false,
51 url: "https://eslint.org/docs/rules/init-declarations"
52 },
53
54 schema: {
55 anyOf: [
56 {
57 type: "array",
58 items: [
59 {
60 enum: ["always"]
61 }
62 ],
63 minItems: 0,
64 maxItems: 1
65 },
66 {
67 type: "array",
68 items: [
69 {
70 enum: ["never"]
71 },
72 {
73 type: "object",
74 properties: {
75 ignoreForLoopInit: {
76 type: "boolean"
77 }
78 },
79 additionalProperties: false
80 }
81 ],
82 minItems: 0,
83 maxItems: 2
84 }
85 ]
86 }
87 },
88
89 create(context) {
90
91 const MODE_ALWAYS = "always",
92 MODE_NEVER = "never";
93
94 const mode = context.options[0] || MODE_ALWAYS;
95 const params = context.options[1] || {};
96
97 //--------------------------------------------------------------------------
98 // Public API
99 //--------------------------------------------------------------------------
100
101 return {
102 "VariableDeclaration:exit"(node) {
103
104 const kind = node.kind,
105 declarations = node.declarations;
106
107 for (let i = 0; i < declarations.length; ++i) {
108 const declaration = declarations[i],
109 id = declaration.id,
110 initialized = isInitialized(declaration),
111 isIgnoredForLoop = params.ignoreForLoopInit && isForLoop(node.parent);
112
113 if (id.type !== "Identifier") {
114 continue;
115 }
116
117 if (mode === MODE_ALWAYS && !initialized) {
118 context.report({
119 node: declaration,
120 message: "Variable '{{idName}}' should be initialized on declaration.",
121 data: {
122 idName: id.name
123 }
124 });
125 } else if (mode === MODE_NEVER && kind !== "const" && initialized && !isIgnoredForLoop) {
126 context.report({
127 node: declaration,
128 message: "Variable '{{idName}}' should not be initialized on declaration.",
129 data: {
130 idName: id.name
131 }
132 });
133 }
134 }
135 }
136 };
137 }
138};