Skip to content

Commit

Permalink
fix: object-shorthand loses type parameters when auto-fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed May 10, 2024
1 parent 069aa68 commit c4003f5
Show file tree
Hide file tree
Showing 5 changed files with 2,237 additions and 10 deletions.
13 changes: 3 additions & 10 deletions lib/rules/object-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,11 @@ module.exports = {
const fnBody = sourceCode.text.slice(arrowToken.range[1], node.value.range[1]);

let shouldAddParensAroundParameters = false;
let tokenBeforeParams;

if (node.value.params.length === 0) {
tokenBeforeParams = sourceCode.getFirstToken(node.value, astUtils.isOpeningParenToken);
} else {
tokenBeforeParams = sourceCode.getTokenBefore(node.value.params[0]);
}
const tokenBeforeParams = sourceCode.getFirstToken(node.value, token => astUtils.isOpeningAngleToken(token) || astUtils.isOpeningParenToken(token));

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

shouldAddParensAroundParameters = !hasParen || isTokenOutsideNode;
}
Expand All @@ -312,7 +306,6 @@ module.exports = {
fixRange,
methodPrefix + newParamText + fnBody
);

}

/**
Expand Down
22 changes: 22 additions & 0 deletions lib/rules/utils/ast-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,24 @@ function isColonToken(token) {
return token.value === ":" && token.type === "Punctuator";
}

/**
* Checks if the given token is an opening angle token or not.
* @param {Token} token The token to check.
* @returns {boolean} `true` if the token is an opening angle token.
*/
function isOpeningAngleToken(token) {
return token.value === "<" && token.type === "Punctuator";
}

/**
* Checks if the given token is a closing angle token or not.
* @param {Token} token The token to check.
* @returns {boolean} `true` if the token is a closing angle token.
*/
function isClosingAngleToken(token) {
return token.value === ">" && token.type === "Punctuator";
}

/**
* Checks if the given token is an opening parenthesis token or not.
* @param {Token} token The token to check.
Expand Down Expand Up @@ -1167,6 +1185,7 @@ module.exports = {
equalTokens,

isArrowToken,
isClosingAngleToken,
isClosingBraceToken,
isClosingBracketToken,
isClosingParenToken,
Expand All @@ -1176,17 +1195,20 @@ module.exports = {
isDotToken,
isQuestionDotToken,
isKeywordToken,
isNotClosingAngleToken: negate(isClosingAngleToken),
isNotClosingBraceToken: negate(isClosingBraceToken),
isNotClosingBracketToken: negate(isClosingBracketToken),
isNotClosingParenToken: negate(isClosingParenToken),
isNotColonToken: negate(isColonToken),
isNotCommaToken: negate(isCommaToken),
isNotDotToken: negate(isDotToken),
isNotQuestionDotToken: negate(isQuestionDotToken),
isNotOpeningAngleToken: negate(isOpeningAngleToken),
isNotOpeningBraceToken: negate(isOpeningBraceToken),
isNotOpeningBracketToken: negate(isOpeningBracketToken),
isNotOpeningParenToken: negate(isOpeningParenToken),
isNotSemicolonToken: negate(isSemicolonToken),
isOpeningAngleToken,
isOpeningBraceToken,
isOpeningBracketToken,
isOpeningParenToken,
Expand Down

0 comments on commit c4003f5

Please sign in to comment.