Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: object-shorthand loses type parameters when auto-fixing #18438

Merged
merged 8 commits into from
May 16, 2024
17 changes: 8 additions & 9 deletions lib/rules/object-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,20 @@ module.exports = {
const arrowToken = sourceCode.getTokenBefore(node.value.body, astUtils.isArrowToken);
const fnBody = sourceCode.text.slice(arrowToken.range[1], node.value.range[1]);

let shouldAddParensAroundParameters = false;
let tokenBeforeParams;
let shouldAddParensAroundParameters = false;

if (node.value.params.length === 0) {
tokenBeforeParams = sourceCode.getFirstToken(node.value, astUtils.isOpeningParenToken);
// First token should not be `async`
if (node.value.async) {
tokenBeforeParams = sourceCode.getFirstToken(node.value, token => !(token.type === "Identifier" && token.value === "async"));
} else {
tokenBeforeParams = sourceCode.getTokenBefore(node.value.params[0]);
tokenBeforeParams = sourceCode.getFirstToken(node.value);
}
shulaoda marked this conversation as resolved.
Show resolved Hide resolved

if (node.value.params.length === 1) {
const hasParen = astUtils.isOpeningParenToken(tokenBeforeParams);
const isTokenOutsideNode = tokenBeforeParams.range[0] < node.range[0];

shouldAddParensAroundParameters = !hasParen || isTokenOutsideNode;
shouldAddParensAroundParameters = !astUtils.isOpeningParenToken(
sourceCode.getTokenBefore(node.value.params[0])
);
}
shulaoda marked this conversation as resolved.
Show resolved Hide resolved

const sliceStart = shouldAddParensAroundParameters
Expand All @@ -312,7 +312,6 @@ module.exports = {
fixRange,
methodPrefix + newParamText + fnBody
);

}

/**
Expand Down