Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: object-shorthand providing invalid fixes for typescript (#12260)
* Fix: object-shorthand providing invalid fixes for typescript

* Chore: refactor to not rely on async modifier check

* Chore: ensure return type isn't wiped for no arguments

* Chore: add ts code test to prevent regressions
  • Loading branch information
bradzacher authored and ilyavolodin committed Sep 14, 2019
1 parent 1c921c6 commit 365331a
Show file tree
Hide file tree
Showing 3 changed files with 9,062 additions and 9 deletions.
44 changes: 35 additions & 9 deletions lib/rules/object-shorthand.js
Expand Up @@ -244,6 +244,7 @@ module.exports = {
const keyText = sourceCode.text.slice(firstKeyToken.range[0], lastKeyToken.range[1]);
let keyPrefix = "";

// key: /* */ () => {}
if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) {
return null;
}
Expand All @@ -255,24 +256,49 @@ module.exports = {
keyPrefix += "*";
}

const fixRange = [firstKeyToken.range[0], node.range[1]];
const methodPrefix = keyPrefix + keyText;

if (node.value.type === "FunctionExpression") {
const functionToken = sourceCode.getTokens(node.value).find(token => token.type === "Keyword" && token.value === "function");
const tokenBeforeParams = node.value.generator ? sourceCode.getTokenAfter(functionToken) : functionToken;

return fixer.replaceTextRange(
[firstKeyToken.range[0], node.range[1]],
keyPrefix + keyText + sourceCode.text.slice(tokenBeforeParams.range[1], node.value.range[1])
fixRange,
methodPrefix + sourceCode.text.slice(tokenBeforeParams.range[1], node.value.range[1])
);
}
const arrowToken = sourceCode.getTokenBefore(node.value.body, { filter: token => token.value === "=>" });
const tokenBeforeArrow = sourceCode.getTokenBefore(arrowToken);
const hasParensAroundParameters = tokenBeforeArrow.type === "Punctuator" && tokenBeforeArrow.value === ")";
const oldParamText = sourceCode.text.slice(sourceCode.getFirstToken(node.value, node.value.async ? 1 : 0).range[0], tokenBeforeArrow.range[1]);
const newParamText = hasParensAroundParameters ? oldParamText : `(${oldParamText})`;

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;

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

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

shouldAddParensAroundParameters = !hasParen || isTokenOutsideNode;
}

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

const oldParamText = sourceCode.text.slice(sliceStart, sliceEnd);
const newParamText = shouldAddParensAroundParameters ? `(${oldParamText})` : oldParamText;

return fixer.replaceTextRange(
[firstKeyToken.range[0], node.range[1]],
keyPrefix + keyText + newParamText + sourceCode.text.slice(arrowToken.range[1], node.value.range[1])
fixRange,
methodPrefix + newParamText + fnBody
);

}
Expand Down

0 comments on commit 365331a

Please sign in to comment.