Skip to content

Commit

Permalink
support load yaml/yml prettier config (denoland/deno#3370)
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy authored and caspervonb committed Jan 31, 2021
1 parent bee01a1 commit 12ee882
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
15 changes: 10 additions & 5 deletions prettier/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import { parse } from "../flags/mod.ts";
import * as path from "../path/mod.ts";
import * as toml from "../encoding/toml.ts";
import * as yaml from "../encoding/yaml.ts";
import { ExpandGlobOptions, WalkInfo, expandGlob } from "../fs/mod.ts";
import { prettier, prettierPlugins } from "./prettier.ts";
const { args, cwd, exit, readAll, readFile, stdin, stdout, writeFile } = Deno;
Expand Down Expand Up @@ -393,7 +394,7 @@ async function autoResolveConfig(): Promise<PrettierBuildInOptions> {
async function resolveConfig(
filepath: string
): Promise<PrettierBuildInOptions> {
let config: PrettierOptions = undefined;
let config: PrettierBuildInOptions = undefined;

function generateError(msg: string): Error {
return new Error(`Invalid prettier configuration file: ${msg}.`);
Expand All @@ -404,18 +405,22 @@ async function resolveConfig(
switch (path.extname(filepath)) {
case ".json":
try {
config = JSON.parse(raw) as PrettierOptions;
config = JSON.parse(raw) as PrettierBuildInOptions;
} catch (err) {
throw generateError(err.message);
}
break;
case ".yml":
case ".yaml":
// TODO: Unimplemented loading yaml / yml configuration file yet.
try {
config = yaml.parse(raw) as PrettierBuildInOptions;
} catch (err) {
throw generateError(err.message);
}
break;
case ".toml":
try {
config = toml.parse(raw) as PrettierOptions;
config = toml.parse(raw) as PrettierBuildInOptions;
} catch (err) {
throw generateError(err.message);
}
Expand All @@ -434,7 +439,7 @@ async function resolveConfig(
);

if (output && output.default) {
config = output.default;
config = output.default as PrettierBuildInOptions;
} else {
throw new Error(
"Prettier of JS version should have default exports."
Expand Down
12 changes: 11 additions & 1 deletion prettier/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ test(async function testPrettierWithAutoConfig(): Promise<void> {
"config_file_json",
"config_file_toml",
"config_file_js",
"config_file_ts"
"config_file_ts",
"config_file_yaml",
"config_file_yml"
];

for (const configName of configs) {
Expand Down Expand Up @@ -441,6 +443,14 @@ test(async function testPrettierWithSpecifiedConfig(): Promise<void> {
{
dir: "config_file_ts",
name: ".prettierrc.ts"
},
{
dir: "config_file_yaml",
name: ".prettierrc.yaml"
},
{
dir: "config_file_yml",
name: ".prettierrc.yml"
}
];

Expand Down
2 changes: 2 additions & 0 deletions prettier/testdata/config_file_yaml/.prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
singleQuote: true
trailingComma: "all"
2 changes: 2 additions & 0 deletions prettier/testdata/config_file_yml/.prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
singleQuote: true
trailingComma: "all"

0 comments on commit 12ee882

Please sign in to comment.