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

Add support for flow typecast comments #5280

Merged
merged 7 commits into from Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/language-js/printer-estree.js
Expand Up @@ -155,7 +155,7 @@ function genericPrint(path, options, printPath, args) {
const node = path.getValue();
if (hasFlowShorthandAnnotationComment(node)) {
parts.push(" /*");
parts.push(node.trailingComments[0].value);
parts.push(node.trailingComments[0].value.trimLeft());
parts.push("*/");
node.trailingComments[0].printed = true;
parts.push(")");
Expand Down
10 changes: 9 additions & 1 deletion src/language-js/utils.js
Expand Up @@ -8,7 +8,15 @@ function hasFlowShorthandAnnotationComment(node) {
node.extra &&
node.extra.parenthesized &&
node.trailingComments &&
node.trailingComments[0].value.trimLeft().startsWith(":")
// We intentionally only match spaces and tabs here instead of any whitespace because
j-f1 marked this conversation as resolved.
Show resolved Hide resolved
// Flow annotation comments cannot be split across lines. For example:
//
// (this /*
// : any */).foo = 5;
//
// is not picked up by Flow, so removing the newline would create a type annotation
// that the user did not intend to create.
j-f1 marked this conversation as resolved.
Show resolved Hide resolved
node.trailingComments[0].value.match(/^[ \t]*:/)
j-f1 marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/flow_comments/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -91,12 +91,26 @@ new (window.Request/*: Class<Request> */)("");

(this/*: any */).foo = 5;

(this/* : any */).foo = 5;

// This next example illustrates that Prettier will not remove a line break
// and unintentionally create a type annotation that was not there before.
(this/*
: any */).foo = 5;

const x = (input /*: string */) /*: string */ => {};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new (window.Request /*: Class<Request> */)("");

(this /*: any */).foo = 5;

(this /*: any */).foo = 5;

// This next example illustrates that Prettier will not remove a line break
// and unintentionally create a type annotation that was not there before.
this /*
: any */.foo = 5;

const x = (input /*: string */) /*: string */ => {};

`;
7 changes: 7 additions & 0 deletions tests/flow_comments/type_annotations.js
Expand Up @@ -2,4 +2,11 @@ new (window.Request/*: Class<Request> */)("");

(this/*: any */).foo = 5;

(this/* : any */).foo = 5;

// This next example illustrates that Prettier will not remove a line break
// and unintentionally create a type annotation that was not there before.
(this/*
: any */).foo = 5;

const x = (input /*: string */) /*: string */ => {};