Skip to content

Commit

Permalink
feat(eslint-plugin-experience): add `@taiga-ui/experience/no-simple-f…
Browse files Browse the repository at this point in the history
…or-of`
  • Loading branch information
splincode committed Nov 13, 2023
1 parent 9f129f2 commit e4cf0b4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ module.exports = {
message:
"Use `map(x => x?.foo?.bar)` instead of `pluck('foo', 'bar')`",
},
{
selector: 'ForOfStatement',
message: 'Use `forEach` instead of `for-of`',
},
],
},
},
Expand Down
1 change: 1 addition & 0 deletions projects/eslint-plugin-experience/configs/taiga.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
],
'@taiga-ui/experience/strict-tui-doc-example': 'error',
'@taiga-ui/experience/no-assert-without-ng-dev-mode': 'error',
'@taiga-ui/experience/no-simple-for-of': 'error',
'@taiga-ui/experience/decorator-key-sort': [
'error',
{
Expand Down
1 change: 1 addition & 0 deletions projects/eslint-plugin-experience/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = {
rules: {
'injection-token-description': require('./rules/injection-token-description'),
'no-deep-imports': require('./rules/no-deep-imports'),
'no-simple-for-of': require('./rules/no-simple-for-of'),
'prefer-inject-decorator': require('./rules/prefer-inject-decorator'),
'prefer-self-destroy-service': require('./rules/prefer-self-destroy-service'),
'no-typeof': require('./rules/no-typeof'),
Expand Down
47 changes: 47 additions & 0 deletions projects/eslint-plugin-experience/rules/no-simple-for-of.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
schema: [],
},
create(context) {
return {
/**
* @type {import('eslint').Rule.Node}
* @return {*}
*/
ForOfStatement(node) {
const isSimpleForOf = !findExpressions(node, [
'AwaitExpression',
'BreakStatement',
'ContinueStatement',
]);

if (isSimpleForOf) {
context.report({
node: node,
message: `Don't use simple "for-of" instead of "forEach"`,
});
}
},
};
},
};

function findExpressions(node, keys) {
if (keys.includes(node?.type) || keys.includes(node?.expression?.type)) {
return true;
}

if (Array.isArray(node?.body)) {
return node?.body?.some?.(item => findExpressions(item, keys));
} else if (!!node?.body) {
return findExpressions(node?.body, keys);
} else if (!!node?.consequent) {
return findExpressions(node?.consequent, keys);
}

return false;
}

0 comments on commit e4cf0b4

Please sign in to comment.