1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-27 16:10:57 -05:00

support load yaml/yml prettier config (#3370)

This commit is contained in:
罗文 2019-11-19 10:15:52 +08:00 committed by Ry Dahl
parent b6b813cbfc
commit 00aa409ff2
4 changed files with 25 additions and 6 deletions

View file

@ -26,6 +26,7 @@
import { parse } from "../flags/mod.ts"; import { parse } from "../flags/mod.ts";
import * as path from "../path/mod.ts"; import * as path from "../path/mod.ts";
import * as toml from "../encoding/toml.ts"; import * as toml from "../encoding/toml.ts";
import * as yaml from "../encoding/yaml.ts";
import { ExpandGlobOptions, WalkInfo, expandGlob } from "../fs/mod.ts"; import { ExpandGlobOptions, WalkInfo, expandGlob } from "../fs/mod.ts";
import { prettier, prettierPlugins } from "./prettier.ts"; import { prettier, prettierPlugins } from "./prettier.ts";
const { args, cwd, exit, readAll, readFile, stdin, stdout, writeFile } = Deno; const { args, cwd, exit, readAll, readFile, stdin, stdout, writeFile } = Deno;
@ -393,7 +394,7 @@ async function autoResolveConfig(): Promise<PrettierBuildInOptions> {
async function resolveConfig( async function resolveConfig(
filepath: string filepath: string
): Promise<PrettierBuildInOptions> { ): Promise<PrettierBuildInOptions> {
let config: PrettierOptions = undefined; let config: PrettierBuildInOptions = undefined;
function generateError(msg: string): Error { function generateError(msg: string): Error {
return new Error(`Invalid prettier configuration file: ${msg}.`); return new Error(`Invalid prettier configuration file: ${msg}.`);
@ -404,18 +405,22 @@ async function resolveConfig(
switch (path.extname(filepath)) { switch (path.extname(filepath)) {
case ".json": case ".json":
try { try {
config = JSON.parse(raw) as PrettierOptions; config = JSON.parse(raw) as PrettierBuildInOptions;
} catch (err) { } catch (err) {
throw generateError(err.message); throw generateError(err.message);
} }
break; break;
case ".yml": case ".yml":
case ".yaml": case ".yaml":
// TODO: Unimplemented loading yaml / yml configuration file yet. try {
config = yaml.parse(raw) as PrettierBuildInOptions;
} catch (err) {
throw generateError(err.message);
}
break; break;
case ".toml": case ".toml":
try { try {
config = toml.parse(raw) as PrettierOptions; config = toml.parse(raw) as PrettierBuildInOptions;
} catch (err) { } catch (err) {
throw generateError(err.message); throw generateError(err.message);
} }
@ -434,7 +439,7 @@ async function resolveConfig(
); );
if (output && output.default) { if (output && output.default) {
config = output.default; config = output.default as PrettierBuildInOptions;
} else { } else {
throw new Error( throw new Error(
"Prettier of JS version should have default exports." "Prettier of JS version should have default exports."

View file

@ -383,7 +383,9 @@ test(async function testPrettierWithAutoConfig(): Promise<void> {
"config_file_json", "config_file_json",
"config_file_toml", "config_file_toml",
"config_file_js", "config_file_js",
"config_file_ts" "config_file_ts",
"config_file_yaml",
"config_file_yml"
]; ];
for (const configName of configs) { for (const configName of configs) {
@ -441,6 +443,14 @@ test(async function testPrettierWithSpecifiedConfig(): Promise<void> {
{ {
dir: "config_file_ts", dir: "config_file_ts",
name: ".prettierrc.ts" name: ".prettierrc.ts"
},
{
dir: "config_file_yaml",
name: ".prettierrc.yaml"
},
{
dir: "config_file_yml",
name: ".prettierrc.yml"
} }
]; ];

View file

@ -0,0 +1,2 @@
singleQuote: true
trailingComma: "all"

View file

@ -0,0 +1,2 @@
singleQuote: true
trailingComma: "all"