Throw original error when loading external config (#7973)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-04-10 21:27:50 +08:00
1 parent f0207e6850
commit aeeb2ff7a8
5 files changed
+28 -13

No files matched your search

+2 -8
View File
@@ -18,14 +18,8 @@ const getExplorerMemoized = mem(
if (result && result.config) {
if (typeof result.config === "string") {
const dir = path.dirname(result.filepath);
try {
const modulePath = resolve(result.config, { paths: [dir] });
result.config = eval("require")(modulePath);
} catch (error) {
// Original message contains `__filename`, can't pass tests
error.message = `Cannot find module '${result.config}' from '${dir}'`;
throw error;
}
const modulePath = resolve(result.config, { paths: [dir] });
result.config = eval("require")(modulePath);
}
if (typeof result.config !== "object") {
@@ -1,5 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`resolves external configuration from package.json (stderr) 1`] = `
"[error] Invalid configuration file \`syntax-error.js\`: THIS ERROR MESSAGE MUST COME FROM THIS syntax-error.js FILE
"
`;
exports[`resolves external configuration from package.json (stdout) 1`] = `""`;
exports[`resolves external configuration from package.json (write) 1`] = `Array []`;
exports[`show warning with kebab-case option key (stderr) 1`] = `
"[warn] Ignored unknown option { \\"print-width\\": 3 }. Did you mean printWidth?
"
@@ -27,11 +36,6 @@ exports[`throw error for unsupported extension (stdout) 1`] = `""`;
exports[`throw error for unsupported extension (write) 1`] = `Array []`;
exports[`throw error with invalid config format (stderr) 1`] = `
"[error] Invalid configuration file \`<cwd>/tests_integration/cli/config/invalid\`: Cannot find module '--invalid--' from '<cwd>/tests_integration/cli/config/invalid/file'
"
`;
exports[`throw error with invalid config format (stdout) 1`] = `""`;
exports[`throw error with invalid config format (write) 1`] = `Array []`;
@@ -16,6 +16,9 @@ describe("throw error for unsupported extension", () => {
describe("throw error with invalid config format", () => {
runPrettier("cli/config/invalid", ["--config", "file/.prettierrc"]).test({
status: "non-zero",
stderr: expect.stringMatching(
/Cannot (?:resolve|find) module '--invalid--' from/
),
});
});
@@ -49,6 +52,14 @@ describe("throw error with invalid config precedence option (configPrecedence)",
});
});
describe("resolves external configuration from package.json", () => {
runPrettier("cli/config-external-config-syntax-error", [
"syntax-error.js",
]).test({
status: 2,
});
});
// Tests below require --parser to prevent an error (no parser/filepath specified)
describe("show warning with unknown option", () => {
@@ -0,0 +1,3 @@
{
"prettier": "./syntax-error"
}
@@ -0,0 +1,3 @@
"use strict";
throw new SyntaxError("THIS ERROR MESSAGE MUST COME FROM THIS syntax-error.js FILE");