-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c65fcc
commit 0e407fe
Showing
2 changed files
with
36 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'; | ||
import { nullThrows } from '@typescript-eslint/utils/eslint-utils'; | ||
|
||
/** | ||
* Gets the location of the head of the given for statement variant for reporting. | ||
* | ||
* - `for (const foo in bar) expressionOrBlock` | ||
* ^^^^^^^^^^^^^^^^^^^^^^ | ||
* | ||
* - `for (const foo of bar) expressionOrBlock` | ||
* ^^^^^^^^^^^^^^^^^^^^^^ | ||
* | ||
* - `for await (const foo of bar) expressionOrBlock` | ||
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
* | ||
* - `for (let i = 0; i < 10; i++) expressionOrBlock` | ||
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
*/ | ||
export function getForStatementHeadLoc( | ||
sourceCode: TSESLint.SourceCode, | ||
node: | ||
| TSESTree.ForInStatement | ||
| TSESTree.ForOfStatement | ||
| TSESTree.ForStatement, | ||
): TSESTree.SourceLocation { | ||
const closingParens = nullThrows( | ||
sourceCode.getTokenBefore(node.body, token => token.value === ')'), | ||
'for statement must have a closing parenthesis.', | ||
); | ||
return { | ||
start: structuredClone(node.loc.start), | ||
end: structuredClone(closingParens.loc.end), | ||
}; | ||
} |