Skip to content

Commit 5f2314b

Browse files
committed
Support Prettier's native overrides instead of fileExtOverrides
I forgot we need to stay compatible with better_rules_javascript's own self-formatting with Prettier, and it doesn't like the `require` syntax I was using. Therefore, we'll just support Prettier's native `overrides` by resolving for each file, and accept that it will do extra disk reads.
1 parent ba23003 commit 5f2314b

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ jobs:
1818
- name: Configure bazel
1919
run: .github/configure-bazel
2020
- name: Test
21-
run:
22-
bazel test tools/bazel:bazelrc.diff tools/lint:format_test
21+
run: bazel test tools/bazel:bazelrc.diff tools/lint:format_test
2322
tools/lint:lint_test
2423
# needs rules_file to work with generate directories tools/javascript:js_diff
2524
test:

prettier/format/src/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { workerMain } from "@better-rules-javascript/bazel-worker";
22
import { ArgumentParser } from "argparse";
3-
import * as path from "node:path";
3+
import { dirname } from "node:path";
44
import { pathToFileURL } from "node:url";
5-
import { Options } from "prettier";
5+
import { Options, resolveConfig } from "prettier";
66
import { load, resolve } from "./import";
7-
import { readFile, writeFile } from "node:fs/promises";
7+
import { PrettierWorker } from "./worker";
88

99
interface Args {
1010
config?: string;
@@ -15,16 +15,13 @@ workerMain(async (a) => {
1515
parser.add_argument("--config", { help: "Configuration path" });
1616
const args: Args = parser.parse_args(a);
1717

18-
const { resolveConfig } = await import("prettier");
19-
const { PrettierWorker } = await import("./worker");
20-
2118
const options: Options | undefined =
2219
args.config === undefined
2320
? undefined
2421
: (await resolveConfig(args.config, { config: args.config })) ||
2522
undefined;
2623
if (options?.plugins) {
27-
const contextUrl = pathToFileURL(path.dirname(args.config!));
24+
const contextUrl = pathToFileURL(dirname(args.config!));
2825
options.plugins = await Promise.all(
2926
options.plugins.map(async (plugin) => {
3027
// in theory, should be able to just resolve the path, but for some reason
@@ -38,8 +35,7 @@ workerMain(async (a) => {
3835
}),
3936
);
4037
}
41-
const fileExtOverrides = (await require(path.resolve(args.config))).fileExtOverrides;
42-
const worker = new PrettierWorker(options, fileExtOverrides);
38+
const worker = new PrettierWorker(args.config, options?.plugins);
4339

4440
return async (a) => {
4541
try {

prettier/format/src/worker.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
import { ArgumentParser } from "argparse";
22
import { readFile, writeFile } from "node:fs/promises";
3-
import { Options, format } from "prettier";
3+
import { resolve } from "node:path";
4+
import { Options, format, resolveConfig } from "prettier";
45

56
export class PrettierWorker {
67
constructor(
7-
readonly options: Options | undefined,
8-
readonly fileExtOverrides: { [fileExt: string]: object } | undefined,
8+
readonly configPath: string | undefined,
9+
readonly plugins: any[] | undefined,
910
) {}
1011

1112
async run(a: string[]) {
1213
const parser = new ArgumentParser();
1314
parser.add_argument("input");
1415
parser.add_argument("output");
1516
const args = parser.parse_args(a);
17+
18+
const resolvedConfig = await resolveConfig(resolve(args.input), {
19+
config: this.configPath,
20+
});
21+
const options: Options = {
22+
...resolvedConfig,
23+
plugins: this.plugins,
24+
};
25+
1626
const input = await readFile(args.input, "utf8");
1727
const output = await format(input, {
18-
...this.options,
19-
...Object.assign(
20-
{},
21-
...Object.entries(this.fileExtOverrides ?? {})
22-
.filter(([fileExt, overrides]) => args.input.endsWith(fileExt))
23-
.map(([fileExt, overrides]) => overrides)
24-
),
28+
...options,
2529
filepath: args.input,
2630
});
2731
await writeFile(args.output, output, "utf8");

util/starlark/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class StarlarkFile {
3737
constructor(readonly statements: StarlarkStatement[]) {}
3838
}
3939

40-
function printArray(value: StarlarkArray, indent: string | undefined): string {
40+
function printArray(value: StarlarkArray, indent?: string | undefined): string {
4141
let output = "";
4242
output += "[";
4343
output += indent === undefined ? " " : "\n";
@@ -56,15 +56,15 @@ function printArray(value: StarlarkArray, indent: string | undefined): string {
5656
return output;
5757
}
5858

59-
function printDict(value: StarlarkDict, indent: string | undefined): string {
59+
function printDict(value: StarlarkDict, indent?: string | undefined): string {
6060
let output = "";
6161
output += "{";
6262
output += indent === undefined ? " " : "\n";
6363
for (const [k, v] of value.elements) {
6464
if (indent !== undefined) {
6565
output += indent + " ";
6666
}
67-
output += printValue(k, undefined);
67+
output += printValue(k);
6868
output += ": ";
6969
output += printValue(v, indent === undefined ? indent : indent + " ");
7070
output += ",";
@@ -85,7 +85,7 @@ function printString(value: StarlarkString): string {
8585
return JSON.stringify(value.value);
8686
}
8787

88-
function printValue(value: StarlarkValue, indent: string | undefined): string {
88+
function printValue(value: StarlarkValue, indent?: string | undefined): string {
8989
if (value instanceof StarlarkArray) {
9090
return printArray(value, indent);
9191
}

0 commit comments

Comments
 (0)