Skip to content

Commit

Permalink
fix(javascript): do not attach to block if it's not behind right func…
Browse files Browse the repository at this point in the history
… paren (#5435)
  • Loading branch information
ikatyang committed Nov 10, 2018
1 parent 182a92e commit e0f74cb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/common/util.js
Expand Up @@ -196,9 +196,8 @@ function isNextLineEmpty(text, node, locEnd) {
return isNextLineEmptyAfterIndex(text, locEnd(node));
}

function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, idx) {
let oldIdx = null;
let idx = locEnd(node);
while (idx !== oldIdx) {
oldIdx = idx;
idx = skipSpaces(text, idx);
Expand All @@ -209,6 +208,13 @@ function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
return idx;
}

function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
return getNextNonSpaceNonCommentCharacterIndexWithStartIndex(
text,
locEnd(node)
);
}

function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
return text.charAt(
getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd)
Expand Down Expand Up @@ -679,6 +685,7 @@ module.exports = {
getParentExportDeclaration,
getPenultimate,
getLast,
getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
getNextNonSpaceNonCommentCharacterIndex,
getNextNonSpaceNonCommentCharacter,
skip,
Expand Down
22 changes: 20 additions & 2 deletions src/language-js/comments.js
Expand Up @@ -630,8 +630,26 @@ function handleLastFunctionArgComments(
followingNode &&
followingNode.type === "BlockStatement"
) {
addBlockStatementFirstComment(followingNode, comment);
return true;
const functionParamRightParenIndex = (() => {
if (enclosingNode.params.length !== 0) {
return privateUtil.getNextNonSpaceNonCommentCharacterIndexWithStartIndex(
text,
options.locEnd(privateUtil.getLast(enclosingNode.params))
);
}
const functionParamLeftParenIndex = privateUtil.getNextNonSpaceNonCommentCharacterIndexWithStartIndex(
text,
options.locEnd(enclosingNode.id)
);
return privateUtil.getNextNonSpaceNonCommentCharacterIndexWithStartIndex(
text,
functionParamLeftParenIndex + 1
);
})();
if (options.locStart(comment) > functionParamRightParenIndex) {
addBlockStatementFirstComment(followingNode, comment);
return true;
}
}

return false;
Expand Down
14 changes: 14 additions & 0 deletions tests/flow_comments/babylon_only/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -26,3 +26,17 @@ class Component extends React.Component /*:: <Props, State> */ {
}
`;

exports[`functions.js - babylon-verify 1`] = `
export function updateStoreFromURL(
store /*: Store*/,
{search, hash} /*: {search: string, hash: string}*/
) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export function updateStoreFromURL(
store /*: Store*/,
{ search, hash } /*: {search: string, hash: string}*/
) {}
`;
5 changes: 5 additions & 0 deletions tests/flow_comments/babylon_only/functions.js
@@ -0,0 +1,5 @@

export function updateStoreFromURL(
store /*: Store*/,
{search, hash} /*: {search: string, hash: string}*/
) {}

0 comments on commit e0f74cb

Please sign in to comment.