Skip to content

Commit

Permalink
fix: update suggestion behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
nopeless committed Jun 22, 2023
1 parent 3669608 commit 1eac2dd
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 75 deletions.
89 changes: 59 additions & 30 deletions lib/rules/no-promise-executor-return.js
Expand Up @@ -70,7 +70,7 @@ function expressionIsVoid(node) {
}

/**
* Fixes the linting error by prepending "void " to the given node's body.
* Fixes the linting error by prepending "void " to the given node
* @param {ASTNode} node The node to fix.
* @param {Object} fixer The fixer object provided by ESLint.
* @returns {Array<Object>|Object} - An array of fix objects or fix to apply to the node.
Expand All @@ -82,13 +82,14 @@ function voidPrependFixer(node, fixer) {
* the expression is () => void () => {}
* therefore, check if the expression is a function
*/
if (node.body.type === "ArrowFunctionExpression") {
if (node.type === "ArrowFunctionExpression") {
return [
fixer.insertTextBefore(node.body, "void ("),
fixer.insertTextAfter(node.body, ")")
fixer.insertTextBefore(node, "void ("),
fixer.insertTextAfter(node, ")")
];
}
return fixer.insertTextBefore(node.body, "void ");

return fixer.insertTextBefore(node, "void ");
}

/**
Expand Down Expand Up @@ -140,8 +141,11 @@ module.exports = {

messages: {
returnsValue: "Return values from promise executor functions cannot be read.",
useVoid: "Return values from promise executor functions cannot be read. If you prefer to use arrow functions without `{...}`, prepend `void` to the expression.",

// arrow and function suggestions
prependVoid: "Prepend `void` to the expression.",

// only arrow suggestions
wrapBraces: "Wrap the expression in `{}`."
}
},
Expand All @@ -161,37 +165,40 @@ module.exports = {
upper: funcInfo,
shouldCheck:
functionTypesToCheck.has(node.type) &&
isPromiseExecutor(node, sourceCode.getScope(node))
isPromiseExecutor(node, sourceCode.getScope(node))
};

if (

// Is a Promise executor
if (// Is a Promise executor
funcInfo.shouldCheck &&
node.type === "ArrowFunctionExpression" &&
node.expression &&
node.type === "ArrowFunctionExpression" &&
node.expression &&

// Except void
!(allowVoid && expressionIsVoid(node.body))
// Except void
!(allowVoid && expressionIsVoid(node.body))
) {
const suggest = [];

// prevent useless refactors
if (allowVoid) {
suggest.push({
messageId: "prependVoid",
fix(fixer) {
return voidPrependFixer(node.body, fixer);
}
});
}

suggest.push({
messageId: "wrapBraces",
fix(fixer) {
return curlyWrapFixer(sourceCode, node, fixer);
}
});

context.report({
node: node.body,
messageId: "useVoid",
suggest: [
{
messageId: "prependVoid",
fix(fixer) {
return voidPrependFixer(node, fixer);
}
},
{
messageId: "wrapBraces",
fix(fixer) {
return curlyWrapFixer(sourceCode, node, fixer);
}
}
]
messageId: "returnsValue",
suggest
});
}
},
Expand All @@ -201,9 +208,31 @@ module.exports = {
},

ReturnStatement(node) {
if (funcInfo.shouldCheck && node.argument) {
if (!(funcInfo.shouldCheck && node.argument)) {
return;
}

// node is `return <expression>`
if (!allowVoid) {
context.report({ node, messageId: "returnsValue" });
return;
}

if (expressionIsVoid(node.argument)) {
return;
}

// allowVoid && !expressionIsVoid
context.report({
node,
messageId: "returnsValue",
suggest: [{
messageId: "prependVoid",
fix(fixer) {
return voidPrependFixer(node.argument, fixer);
}
}]
});
}
};
}
Expand Down

0 comments on commit 1eac2dd

Please sign in to comment.