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(javascript): do not attach to block if it's not behind right func paren #5435

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}*/
) {}