Add filename to flow parse options (#18727)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-01-23 17:32:22 +08:00
1 parent a1c271f39d
commit 0268fdbce5
9 files changed
+88 -7

No files matched your search

+7 -2
View File
@@ -55,8 +55,13 @@ function createParseError(error) {
});
}
function parse(text) {
const ast = flowParser.parse(text, parseOptions);
function parse(text, options) {
const filepath = options?.filepath;
const ast = flowParser.parse(text, {
filename: typeof filepath === "string" ? filepath : "prettier.js.flow",
...parseOptions,
});
const [error] = ast.errors;
if (error) {
throw createParseError(error);
+4 -5
View File
@@ -4,21 +4,20 @@ import postprocess from "./postprocess/index.js";
import createParser from "./utilities/create-parser.js";
function createParseError(error) {
let { message, loc } = error;
const { loc } = error;
/* c8 ignore next 3 */
if (!loc) {
return error;
}
const { line, column } = loc;
let [message] = error.message.split("\n");
message = message.split("\n")[0];
const suffix = `(${line}:${column})`;
const suffix = ` (${line}:${column})`;
if (message.endsWith(suffix)) {
message = message.slice(0, -suffix.length);
}
message = message.trim();
return createError(message, {
loc: {
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`ambient.js.flow [hermes] format 1`] = `
"missing initializer in const declaration (1:6)
> 1 | const foo: string;
| ^
2 |
Cause: missing initializer in const declaration (1:6)
const foo: string;
^~~~~~~~~~~"
`;
exports[`ambient.js.flow format 1`] = `
====================================options=====================================
parsers: ["flow"]
printWidth: 80
| printWidth
=====================================input======================================
const foo: string;
=====================================output=====================================
const foo: string;
================================================================================
`;
exports[`snippet: #0 [hermes] format 1`] = `
"missing initializer in const declaration (1:6)
> 1 | const foo: string;
| ^
Cause: missing initializer in const declaration (1:6)
const foo: string;
^~~~~~~~~~~"
`;
exports[`snippet: #0 format 1`] = `
====================================options=====================================
parsers: ["flow"]
printWidth: 80
| printWidth
=====================================input======================================
const foo: string;
=====================================output=====================================
const foo: string;
================================================================================
`;
@@ -0,0 +1 @@
const foo: string;
@@ -0,0 +1,11 @@
runFormatTest(
{
snippets: [
// Unknown filename
"const foo: string;",
],
importMeta: import.meta,
},
["flow"],
{ errors: { hermes: true } },
);
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`ambient.js [flow] format 1`] = `
"Const must be initialized (1:7)
> 1 | const foo: string;
| ^^^^^^^^^^^
2 |"
`;
exports[`ambient.unknown [flow] format 1`] = `
"Const must be initialized (1:7)
> 1 | const foo: string;
| ^^^^^^^^^^^
2 |"
`;
@@ -0,0 +1 @@
const foo: string;
@@ -0,0 +1 @@
const foo: string;
@@ -0,0 +1 @@
runFormatTest(import.meta, ["flow"]);