Skip to content

Commit

Permalink
fix: Refactor AST traversal to prevent stack overflow in ESLint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Dec 10, 2024
1 parent 56d18c9 commit b4a8a4f
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions packages/eslint-plugin/src/rules/start-function-prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,25 @@ export default {

// Check for await usage before and within the region
let foundAwait = false;
// Use ESLint's selector to find await expressions

// Use ESLint's built-in traversal
const awaitExpressions = [];
context.getSourceCode().ast.body.forEach(node => {
if (node.type === 'ExportNamedDeclaration' &&
node.declaration?.type === 'FunctionDeclaration' &&
node.declaration.id.name === 'start') {
// Walk the function body looking for AwaitExpression nodes
const walk = node => {
if (node.type === 'AwaitExpression') {
awaitExpressions.push(node);
}
for (const key in node) {
if (typeof node[key] === 'object' && node[key] !== null) {
if (Array.isArray(node[key])) {
node[key].forEach(walk);
} else {
walk(node[key]);
}
context.getSourceCode().visitorKeys.BlockStatement.forEach(key => {
const bodyContent = functionNode.body[key];
if (Array.isArray(bodyContent)) {
bodyContent.forEach(statement => {
if (statement.type === 'ExpressionStatement' &&
statement.expression.type === 'AwaitExpression') {
const awaitLine = statement.loc.start.line;
const relativeLine = awaitLine - functionNode.loc.start.line;
if (relativeLine <= regionEndLine) {
foundAwait = true;
}
}
};
walk(node.declaration.body);
});
}
});

foundAwait = awaitExpressions.some(node => {
const awaitLine = node.loc.start.line;
const relativeLine = awaitLine - functionNode.loc.start.line;
return relativeLine <= regionEndLine;
});

if (foundAwait) {
context.report({
node: functionNode,
Expand Down

0 comments on commit b4a8a4f

Please sign in to comment.