Skip to content

Commit

Permalink
fix(eslint-plugin-experience): support static generator
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Mar 25, 2024
1 parent 0708865 commit c0f7db8
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions projects/eslint-plugin-experience/rules/no-simple-for-of.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const ESLintUtils = require('@typescript-eslint/utils').ESLintUtils;

/**
* @type {import('eslint').Rule.RuleModule}
*/
Expand All @@ -9,16 +11,20 @@ module.exports = {
* @return {*}
*/
ForOfStatement(node) {
const isSimpleForOf = !findExpressions(
node,
new Set([
'AwaitExpression',
'YieldExpression',
'BreakStatement',
'ContinueStatement',
'ReturnStatement',
]),
);
console.log(isStaticGenerator(context, node));

const isSimpleForOf =
!node?.await &&
!findExpressions(
node,
new Set([
'AwaitExpression',
'YieldExpression',
'BreakStatement',
'ContinueStatement',
'ReturnStatement',
]),
);

if (isSimpleForOf) {
context.report({
Expand Down Expand Up @@ -57,3 +63,16 @@ function findExpressions(node, keys) {
false
);
}

function isStaticGenerator(context, node) {
const right = ESLintUtils.getParserServices(context)?.getTypeAtLocation(node.right);
const declaredProperties = right?.declaredProperties ?? [];
const hasNextAndIterator =
!!declaredProperties.find(member => member.escapedName === 'next') &&
!!declaredProperties.find(member => member.escapedName.startsWith('__@iterator'));

return (
hasNextAndIterator ||
right?.members?.keys()?.[Symbol.toStringTag] === 'Map Iterator'
);
}

0 comments on commit c0f7db8

Please sign in to comment.