Tweak gap regex for Flow (#13703)

This commit is contained in:
Georgii Dolzhykov authored and GitHub committed 2022-10-24 12:20:44 +03:00
1 parent d09bc86c9f
commit 7682464887
9 files changed
+96 -42

No files matched your search

+2 -2
View File
@@ -1,4 +1,4 @@
#### Remove support for Flow comments (#13687 by @thorn0)
#### [BREAKING] Remove support for Flow comments (#13687, #13703 by @thorn0)
Being a kind of preprocessor, [Flow comments](https://flow.org/blog/2015/02/20/Flow-Comments/) AKA [comment types](https://flow.org/en/docs/types/comments/) are processed on the token level and can't be represented in an AST in the general case. Flow builds the AST as if these special comment tokens didn't exist. Example:
@@ -9,7 +9,7 @@ Being a kind of preprocessor, [Flow comments](https://flow.org/blog/2015/02/20/F
This is parsed as `if (x) +y;` by Flow and as `x + y;` by JS parsers that don't support Flow.
Previously, for some special cases, Prettier tried to detect that this syntax was used and to preserve it. As an attempt to solve an unsolvable problem, this limited support was fragile and riddled with bugs, so it has been removed. Now if the `parser` option is set to `flow` or `babel-flow`, the special comments will be parsed and reprinted like normal code. If a parser that doesn't support Flow is used, they will be treated like usual comments.
Previously, for some special cases, Prettier tried to detect that this syntax was used and to preserve it. As an attempt to solve an unsolvable problem, this limited support was fragile and riddled with bugs, so it has been removed. Now if the `parser` option is set to `flow` or `babel-flow`, Flow comments will be parsed and reprinted like normal code. If a parser that doesn't support Flow is used, they will be treated like usual comments.
<!-- prettier-ignore -->
```js
@@ -82,6 +82,15 @@ function willPrintOwnComments({ node, parent }) {
);
}
function isGap(text, { parser }) {
if (parser === "flow" || parser === "babel-flow") {
// Example: (a /* b */ /* : c */)
// gap ^^^^
text = text.replace(/[\s(]/g, "");
return text === "" || text === "/*" || text === "/*::";
}
}
export * as handleComments from "./handle-comments.js";
export { printComment } from "../print/comment.js";
export {
@@ -90,4 +99,5 @@ export {
hasPrettierIgnore,
isBlockComment,
willPrintOwnComments,
isGap,
};
+7 -12
View File
@@ -350,20 +350,15 @@ function breakTies(tiesToBreak, text, options) {
if (tieCount === 0) {
return;
}
const { precedingNode, followingNode, enclosingNode } = tiesToBreak[0];
const gapRegExp =
(options.printer.getGapRegex &&
options.printer.getGapRegex(enclosingNode)) ||
/^[\s(]*$/;
const { precedingNode, followingNode } = tiesToBreak[0];
let gapEndPos = options.locStart(followingNode);
// Iterate backwards through tiesToBreak, examining the gaps
// between the tied comments. In order to qualify as leading, a
// comment must be separated from followingNode by an unbroken series of
// gaps (or other comments). Gaps should only contain whitespace or open
// parentheses.
// Iterate backwards through tiesToBreak, examining the gaps between the tied
// comments. In order to qualify as leading, a comment must be separated from
// followingNode by an unbroken series of gaps (or other comments). By
// default, gaps should only contain whitespace or open parentheses.
// printer.isGap can be used to define custom logic for checking gaps.
let indexOfFirstLeadingComment;
for (
indexOfFirstLeadingComment = tieCount;
@@ -380,7 +375,7 @@ function breakTies(tiesToBreak, text, options) {
const gap = text.slice(options.locEnd(comment), gapEndPos);
if (gapRegExp.test(gap)) {
if (options.printer.isGap?.(gap, options) ?? /^[\s(]*$/.test(gap)) {
gapEndPos = options.locStart(comment);
} else {
// The gap string contained something other than whitespace or open
@@ -30,6 +30,28 @@ const run = (cmd: string): Promise<void> => {};
================================================================================
`;
exports[`cast.js format 1`] = `
====================================options=====================================
parsers: ["flow", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
(a /* b */ /*: c */);
(a /* b */ : c );
(a /* b */ /* : c */);
(a /* b */ /*:: : c */);
(a /* b */ /* :: : c */);
=====================================output=====================================
(a: /* b */ c);
(a: /* b */ c);
(a: /* b */ c);
(a: /* b */ c);
(a: /* b */ c);
================================================================================
`;
exports[`class.js format 1`] = `
====================================options=====================================
parsers: ["flow", "babel-flow"]
@@ -66,6 +88,26 @@ function foo<T>(bar: T[], baz: T): S {}
================================================================================
`;
exports[`functions2.js format 1`] = `
====================================options=====================================
parsers: ["flow", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
function f1(a /* b */ /*: c */){}
function f2(a /* b */ : c ){}
function f3(a /* b */ /* : c */){}
function f4(a /* b */ /*:: : c */){}
=====================================output=====================================
function f1(a: /* b */ c) {}
function f2(a: /* b */ c) {}
function f3(a: /* b */ c) {}
function f4(a: /* b */ c) {}
================================================================================
`;
exports[`generics.js format 1`] = `
====================================options=====================================
parsers: ["flow", "babel-flow"]
@@ -313,6 +355,32 @@ class Foo {
================================================================================
`;
exports[`type_annotations-3.js format 1`] = `
====================================options=====================================
parsers: ["flow", "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,
|};
================================================================================
`;
exports[`union.js format 1`] = `
====================================options=====================================
parsers: ["flow", "babel-flow"]
@@ -1,27 +0,0 @@
// 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 +0,0 @@
run_spec(import.meta, ["babel-flow"]);
+5
View File
@@ -0,0 +1,5 @@
(a /* b */ /*: c */);
(a /* b */ : c );
(a /* b */ /* : c */);
(a /* b */ /*:: : c */);
(a /* b */ /* :: : c */);
+4
View File
@@ -0,0 +1,4 @@
function f1(a /* b */ /*: c */){}
function f2(a /* b */ : c ){}
function f3(a /* b */ /* : c */){}
function f4(a /* b */ /*:: : c */){}