Skip to content

Commit

Permalink
Flow: Improve comment types detection (#9563)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Nov 4, 2020
1 parent bbd1f5d commit 4295bb2
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 15 deletions.
22 changes: 22 additions & 0 deletions changelog_unreleased/flow/pr-9563.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#### Improve comment types detection (#9563 by @fisker)

<!-- prettier-ignore -->
```jsx
// Input
foo/*::<bar>*/(baz);
class Foo {
bar( data: Array<string>) {}
}

// Prettier master
foo/*:: <bar> */(baz);
class Foo {
bar(data: Array/*:: <string> */) {}
}

// Prettier stable
foo/*:: <bar> */(baz);
class Foo {
bar(data: Array<string>) {}
}
```
34 changes: 19 additions & 15 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2899,21 +2899,25 @@ function printPathNoParens(path, options, print, args) {

case "TypeParameterDeclaration":
case "TypeParameterInstantiation": {
const value = path.getValue();
const commentStart = options.originalText
.slice(0, locStart(value))
.lastIndexOf("/*");
// As noted in the TypeCastExpression comments above, we're able to use a normal whitespace regex here
// because we know for sure that this is a type definition.
const commentSyntax =
commentStart >= 0 &&
options.originalText.slice(commentStart).match(/^\/\*\s*::/);
if (commentSyntax) {
return concat([
"/*:: ",
printTypeParameters(path, options, print, "params"),
" */",
]);
const start = locStart(n);
const end = locEnd(n);
const commentStartIndex = options.originalText.lastIndexOf("/*", start);
const commentEndIndex = options.originalText.indexOf("*/", end);
if (commentStartIndex !== -1 && commentEndIndex !== -1) {
const comment = options.originalText
.slice(commentStartIndex + 2, commentEndIndex)
.trim();
if (
comment.startsWith("::") &&
!comment.includes("/*") &&
!comment.includes("*/")
) {
return concat([
"/*:: ",
printTypeParameters(path, options, print, "params"),
" */",
]);
}
}

return printTypeParameters(path, options, print, "params");
Expand Down
19 changes: 19 additions & 0 deletions tests/flow/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,25 @@ const x = (input /*: string */) /*: string */ => {};
================================================================================
`;
exports[`type_annotations-2.js format 1`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
| printWidth
=====================================input======================================
foo/*::<bar>*/(baz);
class Foo {
bar( data: Array<string>) {}
}
=====================================output=====================================
foo/*:: <bar> */(baz);
class Foo {
bar(data: Array<string>) {}
}
================================================================================
`;
exports[`union.js format 1`] = `
====================================options=====================================
parsers: ["flow", "babel"]
Expand Down
30 changes: 30 additions & 0 deletions tests/flow/comments/babel-only/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`type_annotations-3.js format 1`] = `
====================================options=====================================
parsers: ["babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
import type { OneType } from './oneFile.js'
/*::
import type { HelloType } from './otherFile.js'
*/
type PropTypes = {|
TestComponent: React.AbstractComponent<any>,
hello: HelloType,
|};
=====================================output=====================================
import type { OneType } from "./oneFile.js";
/*::
import type { HelloType } from './otherFile.js'
*/
type PropTypes = {|
TestComponent: React.AbstractComponent<any>,
hello: HelloType,
|};
================================================================================
`;
1 change: 1 addition & 0 deletions tests/flow/comments/babel-only/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(__dirname, ["babel-flow"]);
9 changes: 9 additions & 0 deletions tests/flow/comments/babel-only/type_annotations-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { OneType } from './oneFile.js'
/*::
import type { HelloType } from './otherFile.js'
*/

type PropTypes = {|
TestComponent: React.AbstractComponent<any>,
hello: HelloType,
|};
4 changes: 4 additions & 0 deletions tests/flow/comments/type_annotations-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo/*::<bar>*/(baz);
class Foo {
bar( data: Array<string>) {}
}

0 comments on commit 4295bb2

Please sign in to comment.