From 12ee8826f557522d8eda085eb1d6b93e97329818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=96=87?= Date: Tue, 19 Nov 2019 10:15:52 +0800 Subject: [PATCH] support load yaml/yml prettier config (denoland/deno#3370) --- prettier/main.ts | 15 ++++++++++----- prettier/main_test.ts | 12 +++++++++++- .../testdata/config_file_yaml/.prettierrc.yaml | 2 ++ prettier/testdata/config_file_yml/.prettierrc.yml | 2 ++ 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 prettier/testdata/config_file_yaml/.prettierrc.yaml create mode 100644 prettier/testdata/config_file_yml/.prettierrc.yml diff --git a/prettier/main.ts b/prettier/main.ts index cd8800d231e6..e3f2270c38a7 100755 --- a/prettier/main.ts +++ b/prettier/main.ts @@ -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; @@ -393,7 +394,7 @@ async function autoResolveConfig(): Promise { async function resolveConfig( filepath: string ): Promise { - let config: PrettierOptions = undefined; + let config: PrettierBuildInOptions = undefined; function generateError(msg: string): Error { return new Error(`Invalid prettier configuration file: ${msg}.`); @@ -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); } @@ -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." diff --git a/prettier/main_test.ts b/prettier/main_test.ts index a63890741290..dd5197e4db2f 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -383,7 +383,9 @@ test(async function testPrettierWithAutoConfig(): Promise { "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) { @@ -441,6 +443,14 @@ test(async function testPrettierWithSpecifiedConfig(): Promise { { dir: "config_file_ts", name: ".prettierrc.ts" + }, + { + dir: "config_file_yaml", + name: ".prettierrc.yaml" + }, + { + dir: "config_file_yml", + name: ".prettierrc.yml" } ]; diff --git a/prettier/testdata/config_file_yaml/.prettierrc.yaml b/prettier/testdata/config_file_yaml/.prettierrc.yaml new file mode 100644 index 000000000000..76e9f3104943 --- /dev/null +++ b/prettier/testdata/config_file_yaml/.prettierrc.yaml @@ -0,0 +1,2 @@ +singleQuote: true +trailingComma: "all" diff --git a/prettier/testdata/config_file_yml/.prettierrc.yml b/prettier/testdata/config_file_yml/.prettierrc.yml new file mode 100644 index 000000000000..76e9f3104943 --- /dev/null +++ b/prettier/testdata/config_file_yml/.prettierrc.yml @@ -0,0 +1,2 @@ +singleQuote: true +trailingComma: "all"