1 | /**
|
---|
2 | * @fileoverview Rule to flag use constant conditions
|
---|
3 | * @author Christian Schulz <http://rndm.de>
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { isConstant } = require("./utils/ast-utils");
|
---|
9 |
|
---|
10 | //------------------------------------------------------------------------------
|
---|
11 | // Helpers
|
---|
12 | //------------------------------------------------------------------------------
|
---|
13 |
|
---|
14 | //------------------------------------------------------------------------------
|
---|
15 | // Rule Definition
|
---|
16 | //------------------------------------------------------------------------------
|
---|
17 |
|
---|
18 | /** @type {import('../shared/types').Rule} */
|
---|
19 | module.exports = {
|
---|
20 | meta: {
|
---|
21 | type: "problem",
|
---|
22 |
|
---|
23 | docs: {
|
---|
24 | description: "Disallow constant expressions in conditions",
|
---|
25 | recommended: true,
|
---|
26 | url: "https://eslint.org/docs/latest/rules/no-constant-condition"
|
---|
27 | },
|
---|
28 |
|
---|
29 | schema: [
|
---|
30 | {
|
---|
31 | type: "object",
|
---|
32 | properties: {
|
---|
33 | checkLoops: {
|
---|
34 | type: "boolean",
|
---|
35 | default: true
|
---|
36 | }
|
---|
37 | },
|
---|
38 | additionalProperties: false
|
---|
39 | }
|
---|
40 | ],
|
---|
41 |
|
---|
42 | messages: {
|
---|
43 | unexpected: "Unexpected constant condition."
|
---|
44 | }
|
---|
45 | },
|
---|
46 |
|
---|
47 | create(context) {
|
---|
48 | const options = context.options[0] || {},
|
---|
49 | checkLoops = options.checkLoops !== false,
|
---|
50 | loopSetStack = [];
|
---|
51 | const sourceCode = context.sourceCode;
|
---|
52 |
|
---|
53 | let loopsInCurrentScope = new Set();
|
---|
54 |
|
---|
55 | //--------------------------------------------------------------------------
|
---|
56 | // Helpers
|
---|
57 | //--------------------------------------------------------------------------
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Tracks when the given node contains a constant condition.
|
---|
61 | * @param {ASTNode} node The AST node to check.
|
---|
62 | * @returns {void}
|
---|
63 | * @private
|
---|
64 | */
|
---|
65 | function trackConstantConditionLoop(node) {
|
---|
66 | if (node.test && isConstant(sourceCode.getScope(node), node.test, true)) {
|
---|
67 | loopsInCurrentScope.add(node);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Reports when the set contains the given constant condition node
|
---|
73 | * @param {ASTNode} node The AST node to check.
|
---|
74 | * @returns {void}
|
---|
75 | * @private
|
---|
76 | */
|
---|
77 | function checkConstantConditionLoopInSet(node) {
|
---|
78 | if (loopsInCurrentScope.has(node)) {
|
---|
79 | loopsInCurrentScope.delete(node);
|
---|
80 | context.report({ node: node.test, messageId: "unexpected" });
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Reports when the given node contains a constant condition.
|
---|
86 | * @param {ASTNode} node The AST node to check.
|
---|
87 | * @returns {void}
|
---|
88 | * @private
|
---|
89 | */
|
---|
90 | function reportIfConstant(node) {
|
---|
91 | if (node.test && isConstant(sourceCode.getScope(node), node.test, true)) {
|
---|
92 | context.report({ node: node.test, messageId: "unexpected" });
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Stores current set of constant loops in loopSetStack temporarily
|
---|
98 | * and uses a new set to track constant loops
|
---|
99 | * @returns {void}
|
---|
100 | * @private
|
---|
101 | */
|
---|
102 | function enterFunction() {
|
---|
103 | loopSetStack.push(loopsInCurrentScope);
|
---|
104 | loopsInCurrentScope = new Set();
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Reports when the set still contains stored constant conditions
|
---|
109 | * @returns {void}
|
---|
110 | * @private
|
---|
111 | */
|
---|
112 | function exitFunction() {
|
---|
113 | loopsInCurrentScope = loopSetStack.pop();
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Checks node when checkLoops option is enabled
|
---|
118 | * @param {ASTNode} node The AST node to check.
|
---|
119 | * @returns {void}
|
---|
120 | * @private
|
---|
121 | */
|
---|
122 | function checkLoop(node) {
|
---|
123 | if (checkLoops) {
|
---|
124 | trackConstantConditionLoop(node);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | //--------------------------------------------------------------------------
|
---|
129 | // Public
|
---|
130 | //--------------------------------------------------------------------------
|
---|
131 |
|
---|
132 | return {
|
---|
133 | ConditionalExpression: reportIfConstant,
|
---|
134 | IfStatement: reportIfConstant,
|
---|
135 | WhileStatement: checkLoop,
|
---|
136 | "WhileStatement:exit": checkConstantConditionLoopInSet,
|
---|
137 | DoWhileStatement: checkLoop,
|
---|
138 | "DoWhileStatement:exit": checkConstantConditionLoopInSet,
|
---|
139 | ForStatement: checkLoop,
|
---|
140 | "ForStatement > .test": node => checkLoop(node.parent),
|
---|
141 | "ForStatement:exit": checkConstantConditionLoopInSet,
|
---|
142 | FunctionDeclaration: enterFunction,
|
---|
143 | "FunctionDeclaration:exit": exitFunction,
|
---|
144 | FunctionExpression: enterFunction,
|
---|
145 | "FunctionExpression:exit": exitFunction,
|
---|
146 | YieldExpression: () => loopsInCurrentScope.clear()
|
---|
147 | };
|
---|
148 |
|
---|
149 | }
|
---|
150 | };
|
---|