GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor: extract no unmodified loop condition (#20679) · eslint/eslint@8120e30

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/rules
    • no-unmodified-loop-condition.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/rules/no-unmodified-loop-condition.js‎

Copy file name to clipboardExpand all lines: lib/rules/no-unmodified-loop-condition.js
+30-19Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,35 @@ function getEncloseFunctionDeclaration(reference) {
128128
return null;
129129
}
130130

131+
/**
132+
* Checks whether a given modifier is in a loop.
133+
*
134+
* Besides checking for the condition being in the loop, this also checks
135+
* whether the function that this modifier is belonging to is called
136+
* in the loop.
137+
* @param {LoopConditionInfo} condition The condition to check.
138+
* @param {Reference} modifier The modifier to check.
139+
* @returns {boolean} `true` if the modifier is in a loop.
140+
*/
141+
function hasModifierInLoop(condition, modifier) {
142+
if (condition.isInLoop(modifier)) {
143+
return true;
144+
}
145+
146+
const funcNode = getEncloseFunctionDeclaration(modifier);
147+
148+
if (!funcNode) {
149+
return false;
150+
}
151+
152+
const funcVar = astUtils.getVariableByName(
153+
modifier.from.upper,
154+
funcNode.id.name,
155+
);
156+
157+
return Boolean(funcVar && funcVar.references.some(condition.isInLoop));
158+
}
159+
131160
/**
132161
* Updates the "modified" flags of given loop conditions with given modifiers.
133162
* @param {LoopConditionInfo[]} conditions The loop conditions to be updated.
@@ -140,26 +169,8 @@ function updateModifiedFlag(conditions, modifiers) {
140169

141170
for (let j = 0; !condition.modified && j < modifiers.length; ++j) {
142171
const modifier = modifiers[j];
143-
let funcNode, funcVar;
144172

145-
/*
146-
* Besides checking for the condition being in the loop, we want to
147-
* check the function that this modifier is belonging to is called
148-
* in the loop.
149-
* FIXME: This should probably be extracted to a function.
150-
*/
151-
const inLoop =
152-
condition.isInLoop(modifier) ||
153-
Boolean(
154-
(funcNode = getEncloseFunctionDeclaration(modifier)) &&
155-
(funcVar = astUtils.getVariableByName(
156-
modifier.from.upper,
157-
funcNode.id.name,
158-
)) &&
159-
funcVar.references.some(condition.isInLoop),
160-
);
161-
162-
condition.modified = inLoop;
173+
condition.modified = hasModifierInLoop(condition, modifier);
163174
}
164175
}
165176
}

0 commit comments

Comments
0 (0)
You are visitor 01402605 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.