diff --git a/src/language-js/parse/flow.js b/src/language-js/parse/flow.js index b33c75045..4174fb769 100644 --- a/src/language-js/parse/flow.js +++ b/src/language-js/parse/flow.js @@ -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); diff --git a/src/language-js/parse/hermes.js b/src/language-js/parse/hermes.js index 42e13f49b..d141f32e9 100644 --- a/src/language-js/parse/hermes.js +++ b/src/language-js/parse/hermes.js @@ -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: { diff --git a/tests/format/filename-matters/flow-ambient/__snapshots__/format.test.js.snap b/tests/format/filename-matters/flow-ambient/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..bb35664f6 --- /dev/null +++ b/tests/format/filename-matters/flow-ambient/__snapshots__/format.test.js.snap @@ -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; + +================================================================================ +`; diff --git a/tests/format/filename-matters/flow-ambient/ambient.js.flow b/tests/format/filename-matters/flow-ambient/ambient.js.flow new file mode 100644 index 000000000..6821c110c --- /dev/null +++ b/tests/format/filename-matters/flow-ambient/ambient.js.flow @@ -0,0 +1 @@ +const foo: string; diff --git a/tests/format/filename-matters/flow-ambient/format.test.js b/tests/format/filename-matters/flow-ambient/format.test.js new file mode 100644 index 000000000..e382c0e71 --- /dev/null +++ b/tests/format/filename-matters/flow-ambient/format.test.js @@ -0,0 +1,11 @@ +runFormatTest( + { + snippets: [ + // Unknown filename + "const foo: string;", + ], + importMeta: import.meta, + }, + ["flow"], + { errors: { hermes: true } }, +); diff --git a/tests/format/misc/errors/flow/ambient/__snapshots__/format.test.js.snap b/tests/format/misc/errors/flow/ambient/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..1ad7bb27a --- /dev/null +++ b/tests/format/misc/errors/flow/ambient/__snapshots__/format.test.js.snap @@ -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 |" +`; diff --git a/tests/format/misc/errors/flow/ambient/ambient.js b/tests/format/misc/errors/flow/ambient/ambient.js new file mode 100644 index 000000000..6821c110c --- /dev/null +++ b/tests/format/misc/errors/flow/ambient/ambient.js @@ -0,0 +1 @@ +const foo: string; diff --git a/tests/format/misc/errors/flow/ambient/ambient.unknown b/tests/format/misc/errors/flow/ambient/ambient.unknown new file mode 100644 index 000000000..6821c110c --- /dev/null +++ b/tests/format/misc/errors/flow/ambient/ambient.unknown @@ -0,0 +1 @@ +const foo: string; diff --git a/tests/format/misc/errors/flow/ambient/format.test.js b/tests/format/misc/errors/flow/ambient/format.test.js new file mode 100644 index 000000000..55c339883 --- /dev/null +++ b/tests/format/misc/errors/flow/ambient/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["flow"]);