]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/init-declarations.js
d994bbc1ea0590ea54d780dc14e8d84425c262c2
2 * @fileoverview A rule to control the style of variable initializations.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
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.
17 function isForLoop(block
) {
18 return block
.type
=== "ForInStatement" ||
19 block
.type
=== "ForOfStatement" ||
20 block
.type
=== "ForStatement";
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.
28 function isInitialized(node
) {
29 const declaration
= node
.parent
;
30 const block
= declaration
.parent
;
32 if (isForLoop(block
)) {
33 if (block
.type
=== "ForStatement") {
34 return block
.init
=== declaration
;
36 return block
.left
=== declaration
;
38 return Boolean(node
.init
);
41 //------------------------------------------------------------------------------
43 //------------------------------------------------------------------------------
50 description
: "require or disallow initialization in variable declarations",
52 url
: "https://eslint.org/docs/rules/init-declarations"
80 additionalProperties
: false
89 initialized
: "Variable '{{idName}}' should be initialized on declaration.",
90 notInitialized
: "Variable '{{idName}}' should not be initialized on declaration."
96 const MODE_ALWAYS
= "always",
99 const mode
= context
.options
[0] || MODE_ALWAYS
;
100 const params
= context
.options
[1] || {};
102 //--------------------------------------------------------------------------
104 //--------------------------------------------------------------------------
107 "VariableDeclaration:exit"(node
) {
109 const kind
= node
.kind
,
110 declarations
= node
.declarations
;
112 for (let i
= 0; i
< declarations
.length
; ++i
) {
113 const declaration
= declarations
[i
],
115 initialized
= isInitialized(declaration
),
116 isIgnoredForLoop
= params
.ignoreForLoopInit
&& isForLoop(node
.parent
);
119 if (mode
=== MODE_ALWAYS
&& !initialized
) {
120 messageId
= "initialized";
121 } else if (mode
=== MODE_NEVER
&& kind
!== "const" && initialized
&& !isIgnoredForLoop
) {
122 messageId
= "notInitialized";
125 if (id
.type
=== "Identifier" && messageId
) {