-
-
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.
fix(eslint-plugin): [no-for-in-array] refine report location (#8874)
* [no-for-in-array] refine report location * move code to util * lint check * update snapshot
- Loading branch information
1 parent
eef257b
commit fdeba42
Showing
4 changed files
with
121 additions
and
14 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), | ||
}; | ||
} |
8 changes: 2 additions & 6 deletions
8
packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-for-in-array.shot
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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