Skip to content

Commit

Permalink
refactor: judge the first token and use it's index
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda authored and dalaoshu committed May 12, 2024
1 parent ccdaca2 commit 2d64687
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/rules/object-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,15 @@ module.exports = {
const arrowToken = sourceCode.getTokenBefore(node.value.body, astUtils.isArrowToken);
const fnBody = sourceCode.text.slice(arrowToken.range[1], node.value.range[1]);

let tokenBeforeParams;
let shouldAddParensAroundParameters = false;
const 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.getFirstToken(node.value);
}

if (node.value.params.length === 1) {
const hasParen = astUtils.isOpeningParenToken(sourceCode.getTokenBefore(node.value.params[0]));
Expand All @@ -296,7 +303,7 @@ module.exports = {

const sliceStart = shouldAddParensAroundParameters
? node.value.params[0].range[0]
: (node.value.typeParameters || tokenBeforeParams).range[0];
: tokenBeforeParams.range[0];
const sliceEnd = sourceCode.getTokenBefore(arrowToken).range[1];

const oldParamText = sourceCode.text.slice(sliceStart, sliceEnd);
Expand Down

0 comments on commit 2d64687

Please sign in to comment.