From ab9792cc00566dd6a3585aeba022a2d7c2de8d85 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Sat, 22 Apr 2023 22:18:48 +0800 Subject: [PATCH] Update dependency flow-parser to v0.204.0 (#14710) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: fisker Cheung --- package.json | 2 +- scripts/build/config.js | 12 ++++++-- src/language-js/print/flow.js | 17 +++++++++-- src/language-js/print/function.js | 4 +-- .../cursor/__snapshots__/jsfmt.spec.js.snap | 30 +++++++++++++++++++ .../flow/cursor/function-predicate-2.js | 1 + .../function-return-type-and-predicate-2.js | 1 + yarn.lock | 10 +++---- 8 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 tests/format/flow/cursor/function-predicate-2.js create mode 100644 tests/format/flow/cursor/function-return-type-and-predicate-2.js diff --git a/package.json b/package.json index b80f1acd5..2c72b7de9 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "file-entry-cache": "6.0.1", "find-cache-dir": "4.0.0", "find-parent-dir": "0.3.1", - "flow-parser": "0.203.1", + "flow-parser": "0.204.0", "get-stdin": "9.0.0", "graphql": "16.6.0", "hermes-eslint": "0.10.1", diff --git a/scripts/build/config.js b/scripts/build/config.js index 688975506..d08a0c63e 100644 --- a/scripts/build/config.js +++ b/scripts/build/config.js @@ -106,10 +106,16 @@ const pluginFiles = [ replaceModule: [ { module: require.resolve("flow-parser"), - process: (text) => - text + process(text) { + const { fsModuleNameVariableName } = text.match( + /,(?\w+)="fs",/ + ).groups; + + return text + .replaceAll(`require(${fsModuleNameVariableName})`, "{}") .replaceAll('require("fs")', "{}") - .replaceAll('require("constants")', "{}"), + .replaceAll('require("constants")', "{}"); + }, }, ], }, diff --git a/src/language-js/print/flow.js b/src/language-js/print/flow.js index 93f0550c1..c07cf501b 100644 --- a/src/language-js/print/flow.js +++ b/src/language-js/print/flow.js @@ -64,7 +64,6 @@ function printFlow(path, options, print) { printDeclareToken(path), "function ", print("id"), - node.predicate ? " " : "", print("predicate"), semi, ]; @@ -258,9 +257,21 @@ function printFlow(path, options, print) { return printTypeParameters(path, options, print, "params"); case "InferredPredicate": - return "%checks"; case "DeclaredPredicate": - return ["%checks(", print("value"), ")"]; + // Note: Leading comment print should be improved https://github.com/prettier/prettier/pull/14710#issuecomment-1512522282 + return [ + // The return type will already add the colon, but otherwise we + // need to do it ourselves + path.key === "predicate" && + path.parent.type !== "DeclareFunction" && + !path.parent.returnType + ? ": " + : " ", + "%checks", + ...(node.type === "DeclaredPredicate" + ? ["(", print("value"), ")"] + : []), + ]; } } diff --git a/src/language-js/print/function.js b/src/language-js/print/function.js index fd40444c3..2e3ddebda 100644 --- a/src/language-js/print/function.js +++ b/src/language-js/print/function.js @@ -444,9 +444,7 @@ function printReturnType(path, print) { const parts = [returnType]; if (node.predicate) { - // The return type will already add the colon, but otherwise we - // need to do it ourselves - parts.push(node.returnType ? " " : ": ", print("predicate")); + parts.push(print("predicate")); } return parts; diff --git a/tests/format/flow/cursor/__snapshots__/jsfmt.spec.js.snap b/tests/format/flow/cursor/__snapshots__/jsfmt.spec.js.snap index 8d61b676a..d68187237 100644 --- a/tests/format/flow/cursor/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/flow/cursor/__snapshots__/jsfmt.spec.js.snap @@ -30,6 +30,21 @@ function a()<|>: %checks(a) {} ================================================================================ `; +exports[`function-predicate-2.js format 1`] = ` +====================================options===================================== +cursorOffset: 12 +parsers: ["flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +function a()<|>: %checks {} + +=====================================output===================================== +function a()<|>: %checks {} + +================================================================================ +`; + exports[`function-return-type.js format 1`] = ` ====================================options===================================== parsers: ["flow"] @@ -59,6 +74,21 @@ function a()<|>: boolean %checks(a) {} ================================================================================ `; +exports[`function-return-type-and-predicate-2.js format 1`] = ` +====================================options===================================== +cursorOffset: 12 +parsers: ["flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +function a()<|>: boolean %checks {} + +=====================================output===================================== +function a()<|>: boolean %checks {} + +================================================================================ +`; + exports[`function-type-parameter-bound.js format 1`] = ` ====================================options===================================== cursorOffset: 12 diff --git a/tests/format/flow/cursor/function-predicate-2.js b/tests/format/flow/cursor/function-predicate-2.js new file mode 100644 index 000000000..60c31a2a1 --- /dev/null +++ b/tests/format/flow/cursor/function-predicate-2.js @@ -0,0 +1 @@ +function a()<|>: %checks {} diff --git a/tests/format/flow/cursor/function-return-type-and-predicate-2.js b/tests/format/flow/cursor/function-return-type-and-predicate-2.js new file mode 100644 index 000000000..b4fb5dba9 --- /dev/null +++ b/tests/format/flow/cursor/function-return-type-and-predicate-2.js @@ -0,0 +1 @@ +function a()<|>: boolean %checks {} diff --git a/yarn.lock b/yarn.lock index ae934c374..bd65b922c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3989,10 +3989,10 @@ __metadata: languageName: node linkType: hard -"flow-parser@npm:0.203.1": - version: 0.203.1 - resolution: "flow-parser@npm:0.203.1" - checksum: ed9beb3d83e352ed646bd9c06fa2a96a0b71cfd6f83d7ccf75a7c16dbe844128bcefefa6c8d0f09ef1337d0e0a9a9c8dadfd25bb149ed657ce91ec63a0a5113b +"flow-parser@npm:0.204.0": + version: 0.204.0 + resolution: "flow-parser@npm:0.204.0" + checksum: 295a85bbb3e9f6f46766d751ab9af9977b278b3a41648a4f1a5a8e123b5388822fd120bb16b2ee68a43199b309e0472732f1f2ff04cc841d4472aab3a67d8848 languageName: node linkType: hard @@ -6773,7 +6773,7 @@ __metadata: file-entry-cache: 6.0.1 find-cache-dir: 4.0.0 find-parent-dir: 0.3.1 - flow-parser: 0.203.1 + flow-parser: 0.204.0 get-stdin: 9.0.0 graphql: 16.6.0 hermes-eslint: 0.10.1