mirror of
https://github.com/denoland/deno.git
synced 2024-11-26 16:09:27 -05:00
support load yaml/yml prettier config (#3370)
This commit is contained in:
parent
b6b813cbfc
commit
00aa409ff2
4 changed files with 25 additions and 6 deletions
|
@ -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<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}.`);
|
||||
|
@ -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."
|
||||
|
|
|
@ -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) {
|
||||
|
@ -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"
|
||||
}
|
||||
];
|
||||
|
||||
|
|
2
std/prettier/testdata/config_file_yaml/.prettierrc.yaml
vendored
Normal file
2
std/prettier/testdata/config_file_yaml/.prettierrc.yaml
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
singleQuote: true
|
||||
trailingComma: "all"
|
2
std/prettier/testdata/config_file_yml/.prettierrc.yml
vendored
Normal file
2
std/prettier/testdata/config_file_yml/.prettierrc.yml
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
singleQuote: true
|
||||
trailingComma: "all"
|
Loading…
Reference in a new issue