Skip to content

Commit

Permalink
Stop formatting unknown code with babel parser (#14718)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 21, 2023
1 parent 7f292bd commit b5043bc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 66 deletions.
12 changes: 12 additions & 0 deletions changelog_unreleased/api/14718.md
@@ -0,0 +1,12 @@
#### Stop formatting unknown code with `babel` parser (#14718 by @fisker)

```console
await prettier.format("foo")

// Prettier stable
No parser and no filepath given, using 'babel' the parser now but this will throw an error in the future. Please specify a parser or a filepath so one can be inferred.
'foo;\n'

// Prettier main
UndefinedParserError: No parser and no file path given, couldn't infer a parser.
```
11 changes: 0 additions & 11 deletions src/cli/format.js
Expand Up @@ -84,11 +84,6 @@ async function listDifferent(context, input, options, filename) {
}

try {
if (!options.filepath && !options.parser) {
throw new errors.UndefinedParserError(
"No parser and no file path given, couldn't infer a parser."
);
}
if (!(await prettier.check(input, options)) && !context.argv.write) {
context.logger.log(filename);
process.exitCode = 1;
Expand All @@ -101,12 +96,6 @@ async function listDifferent(context, input, options, filename) {
}

async function format(context, input, opt) {
if (!opt.parser && !opt.filepath) {
throw new errors.UndefinedParserError(
"No parser and no file path given, couldn't infer a parser."
);
}

if (context.argv.debugPrintDoc) {
const doc = await prettier.__debug.printToDoc(input, opt);
return { formatted: (await prettier.__debug.formatDoc(doc)) + "\n" };
Expand Down
35 changes: 16 additions & 19 deletions src/main/normalize-format-options.js
Expand Up @@ -20,28 +20,11 @@ const formatOptionsHiddenDefaults = {
async function normalizeFormatOptions(options, opts = {}) {
const rawOptions = { ...options };

const supportOptions = getSupportInfo({
plugins: options.plugins,
showDeprecated: true,
}).options;

const defaults = {
...formatOptionsHiddenDefaults,
...Object.fromEntries(
supportOptions
.filter((optionInfo) => optionInfo.default !== undefined)
.map((option) => [option.name, option.default])
),
};
if (!rawOptions.parser) {
if (!rawOptions.filepath) {
const logger = opts.logger || console;
logger.warn(
"No parser and no filepath given, using 'babel' the parser now " +
"but this will throw an error in the future. " +
"Please specify a parser or a filepath so one can be inferred."
throw new UndefinedParserError(
"No parser and no file path given, couldn't infer a parser."
);
rawOptions.parser = "babel";
} else {
rawOptions.parser = inferParser(rawOptions, {
physicalFile: rawOptions.filepath,
Expand All @@ -54,6 +37,20 @@ async function normalizeFormatOptions(options, opts = {}) {
}
}

const supportOptions = getSupportInfo({
plugins: options.plugins,
showDeprecated: true,
}).options;

const defaults = {
...formatOptionsHiddenDefaults,
...Object.fromEntries(
supportOptions
.filter((optionInfo) => optionInfo.default !== undefined)
.map((option) => [option.name, option.default])
),
};

const parserPlugin = getParserPluginByParserName(
rawOptions.plugins,
rawOptions.parser
Expand Down
12 changes: 0 additions & 12 deletions tests/integration/__tests__/__snapshots__/infer-parser.js.snap
Expand Up @@ -76,18 +76,6 @@ exports[`--write with unknown path and no parser multiple files (write) 1`] = `
exports[`--write with unknown path and no parser specific file (stderr) 1`] = `"[error] No parser could be inferred for file "<cli>/infer-parser/FOO"."`;
exports[`API with no path and no parser prettier.check 1`] = `
[
"No parser and no filepath given, using 'babel' the parser now but this will throw an error in the future. Please specify a parser or a filepath so one can be inferred.",
]
`;
exports[`API with no path and no parser prettier.format 1`] = `
[
"No parser and no filepath given, using 'babel' the parser now but this will throw an error in the future. Please specify a parser or a filepath so one can be inferred.",
]
`;
exports[`Interpreters (stdout) 1`] = `
"{ "ignored": false, "inferredParser": "babel" }
"
Expand Down
30 changes: 6 additions & 24 deletions tests/integration/__tests__/infer-parser.js
Expand Up @@ -166,34 +166,16 @@ describe("--write and --list-different with unknown path and no parser", () => {
});

describe("API with no path and no parser", () => {
const _console = global.console;
const result = { called: 0, arguments: [] };

beforeEach(() => {
global.console = {
warn(...args) {
result.called += 1;
result.arguments = args;
},
};
});

afterEach(() => {
result.called = 0;
result.arguments = [];
global.console = _console;
});

test("prettier.format", async () => {
expect(await prettier.format(" foo ( )")).toBe("foo();\n");
expect(result.called).toBe(1);
expect(result.arguments).toMatchSnapshot();
await expect(prettier.format(" foo ( )")).rejects.toThrow(
/No parser and no file path given, couldn't infer a parser\./
);
});

test("prettier.check", async () => {
expect(await prettier.check(" foo ( )")).toBe(false);
expect(result.called).toBe(1);
expect(result.arguments).toMatchSnapshot();
await expect(prettier.check(" foo ( )")).rejects.toThrow(
/No parser and no file path given, couldn't infer a parser\./
);
});
});

Expand Down

0 comments on commit b5043bc

Please sign in to comment.