Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support load yaml/yml prettier config #3370

Merged
merged 1 commit into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions std/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 std/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 std/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 std/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"