New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix FP S4138 ('prefer-for-of'): Check if node does have an iterator to use for-of #3385
Fix FP S4138 ('prefer-for-of'): Check if node does have an iterator to use for-of #3385
Comments
Goal: don't report if arrayLike does not support for-of (flagged by TS) We are sure that HTMLFormElement has the [Symbol.iterator] generator function (see typedef in Refs below), but when loading the required libs in the tsconfig
Therefore there must be another property that typescript looks for in order to allow the usage of for-of. Using the typechecker, we can find that arrayLikes that implement the iterable protocol have a property
typescript flags for-of when the arrayLike is missing:
We need to find this (or these) other field(s)! Refs:
{
"compilerOptions": {
"lib": [
"DOM.Iterable",
"DOM"
]
}
}
function foo(form: HTMLFormElement) {
const disabledFields: Element[] = [];
for(const input of form) {
if(input.hasAttribute('disabled')) {
disabledFields.push(input);
}
}
return disabledFields;
} |
I tested that when adding this to the tsconfig the error disappears:
Source: https://bobbyhadz.com/blog/typescript-type-iterableiterator-is-not-an-array-type Still, should we rely on user's tsconfig in order to raise or not raise? |
The proposal from the community user who reported the issue is for Sonar product not to lead towards changes that would conflict with TypeScript, even if it is because of an error in the
|
Closing this as we will modify the message in rspec: SonarSource/rspec#1303 |
Can Sonar ask TypeScript if there is an iterator, and not require one if TypeScript says no? |
The rule S4138 can raise issues when
for...of
is not available for the iterable.The typescript-eslint rule implementation does not check the type, so we could add a extend the current decorator to filter the flagged nodes for type.
Reported in https://community.sonarsource.com/t/fp-seeking-for-of-where-typescript-prohibits-it/67142
The text was updated successfully, but these errors were encountered: