Skip to content

Commit

Permalink
Stop formatting unknown files with babel parser
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 17, 2023
1 parent b232005 commit 6022276
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
12 changes: 12 additions & 0 deletions changelog_unreleased/api/14718.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#### Stop format unknown files 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.
```
35 changes: 16 additions & 19 deletions src/main/normalize-format-options.js
Original file line number Diff line number Diff line change
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

0 comments on commit 6022276

Please sign in to comment.