const getDisabledFieldsSet = function(form: HTMLFormElement) {
const disabledFields: Element[] = [];
//Using for-of to open this loop, e.g.
//for(let input of form) {
//fails w/err: Type 'HTMLFormElement' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
//However, Sonar insists on using for-of.
//Expected a `for-of` loop instead of a `for` loop with this simple iteration.sonarlint(typescript:S4138).
//It does not appear possible to simultaneously satisfy both checkers.
for(let i = 0; i < form.length; i++) {
const input = form[i];
if(input.hasAttribute('disabled')) {
disabledFields.push(input);
}
}
return disabledFields;
}
Sonar should not report a TypeScript issue seeking a for-of field in cases where TypeScript prohibits it, even if the prohibition is a TypeScript issue associated with not properly including the “dom.iterable” option in the “lib” compiler option.
Thank you for notifying us of this issue. Indeed HTMLFormElement can be accessed through its indices as an array, but it does not have an iterator function which prevents it from using for .. of ...
You can however augment HTMLFormElement using Array.from() like that:
Then Sonar’s message requiring use of for...of should explain how you have to add extra lines to the code to convert it into an object type that allows its use, or better yet just drop the false-positive detection.