Skip to content

Commit

Permalink
Merge pull request #10 from rivethealth/pauldraper/prettier
Browse files Browse the repository at this point in the history
Upgrade to Prettier 3
  • Loading branch information
pauldraper authored Jan 31, 2024
2 parents 4253b79 + 9dd9702 commit 0696c8d
Show file tree
Hide file tree
Showing 34 changed files with 328 additions and 107 deletions.
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ js_library(
srcs = ["prettierrc.yml"],
root = ":root",
visibility = [":__subpackages__"],
deps = ["@npm//prettier-plugin-organize-imports:lib"],
)

alias(
Expand All @@ -66,6 +67,7 @@ test_suite(
"//jest:test",
"//nodejs:test",
"//npm:test",
"//prettier:test",
"//rollup:test",
# "//ts-proto/test",
"//typescript:test",
Expand Down
2 changes: 1 addition & 1 deletion angular/test/bazel/material/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
Expand Down
4 changes: 2 additions & 2 deletions bazel/worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export async function workerMain(workerFactory: WorkerFactory) {
error instanceof CliError
? error.message
: error instanceof Error
? error.stack
: String(error),
? error.stack
: String(error),
);
process.exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion nodejs/fs-linker/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ function writeFileSync(

export function patchFs(
vfs: Vfs,
delegate: { -readonly [K in keyof typeof fs]: typeof fs[K] },
delegate: { -readonly [K in keyof typeof fs]: (typeof fs)[K] },
) {
delegate.access = access(vfs, delegate.access);
delegate.accessSync = accessSync(vfs, delegate.accessSync);
Expand Down
1 change: 1 addition & 0 deletions nodejs/runner.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export NODE_PACKAGE_MANIFEST="$RUNFILES_DIR"/%{package_manifest}
exec -a "$0" "$RUNFILES_DIR"/%{node} \
-r "$(abspath "$RUNFILES_DIR"/%{runtime})" \
-r "$(abspath "$RUNFILES_DIR"/%{module_linker})" \
--experimental-import-meta-resolve \
--experimental-loader "$(abspath "$RUNFILES_DIR"/%{esm_loader})" \
--experimental-specifier-resolution=node \
--preserve-symlinks \
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@angular/platform-browser-dynamic": "^16",
"@babel/core": "7.18.13",
"@babel/plugin-transform-modules-commonjs": "7.18.6",
"@prettier/plugin-xml": "^3",
"@rollup/plugin-commonjs": "~16.0.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.0",
Expand All @@ -33,8 +34,8 @@
"long": "~4.0.0",
"node-fetch": "^2.0.0",
"npm-registry-fetch": "~14.0.2",
"prettier": "~2.5.0",
"prettier-plugin-organize-imports": "^3.0.0",
"prettier": "^3",
"prettier-plugin-organize-imports": "^3",
"protobufjs": "~6.11.2",
"reflect-metadata": "0.1.13",
"rollup": "^2.78.0",
Expand All @@ -52,7 +53,6 @@
"@types/long": "~4.0.0",
"@types/node": "^18",
"@types/node-fetch": "^2.0.0",
"@types/prettier": "~2.4.0",
"@types/react": "~15.0.0",
"@types/tar-stream": "~2.2.0"
},
Expand Down
6 changes: 6 additions & 0 deletions prettier/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ alias(
actual = "@better_rules_javascript_npm//prettier:lib",
visibility = ["//visibility:public"],
)

test_suite(
name = "test",
tests = ["//angular/test"],
visibility = ["//:__subpackages__"],
)
12 changes: 11 additions & 1 deletion prettier/format/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@ js_library(
deps = ["//tools/typescript:tsconfig"],
)

js_library(
name = "lib_js",
srcs = glob([
"src/**/*.js",
"src/**/*.mjs",
]),
root = ":root",
)

ts_library(
name = "lib",
srcs = glob(["src/**/*.ts"]),
compile_deps = ["@better_rules_javascript_npm//@types/prettier:lib"],
compile_deps = ["@better_rules_javascript_npm//prettier:lib"],
compiler = "@better_rules_javascript//typescript:tsc",
config = "tsconfig.json",
config_dep = ":tsconfig",
root = ":root",
visibility = ["//visibility:public"],
deps = [
":lib_js",
"@better_rules_javascript//bazel/worker:lib",
"@better_rules_javascript_npm//@types/argparse:lib",
"@better_rules_javascript_npm//@types/node:lib",
Expand Down
3 changes: 3 additions & 0 deletions prettier/format/src/import.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function load(specifier: string): Promise<any>;

export function resolve(specifier: string, parent?: URL): Promise<string>;
11 changes: 11 additions & 0 deletions prettier/format/src/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
async function load() {
const { load } = await import("./import.mjs");
return Reflect.apply(load, this, arguments);
}

async function resolve() {
const { resolve } = await import("./import.mjs");
return Reflect.apply(resolve, this, arguments);
}

module.exports = { load, resolve };
7 changes: 7 additions & 0 deletions prettier/format/src/import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function load(specifier) {
return import(specifier);
}

export function resolve(specifier, parent) {
return import.meta.resolve(specifier, parent);
}
41 changes: 33 additions & 8 deletions prettier/format/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
import { workerMain } from "@better-rules-javascript/bazel-worker";
import { ArgumentParser } from "argparse";
import prettier from "prettier";
import { dirname } from "node:path";
import { pathToFileURL } from "node:url";
import { Options } from "prettier";
import { load, resolve } from "./import";

interface Args {
config?: string;
}

workerMain(async (a) => {
const parser = new ArgumentParser();
parser.add_argument("--config");
const args = parser.parse_args(a);
const parser = new ArgumentParser({ prog: "prettier-format" });
parser.add_argument("--config", { help: "Configuration path" });
const args: Args = parser.parse_args(a);

const { resolveConfig } = await import("prettier");
const { PrettierWorker } = await import("./worker");

const options =
args.config &&
prettier.resolveConfig.sync(args.config, { config: args.config });
const options: Options | undefined =
args.config === undefined
? undefined
: (await resolveConfig(args.config, { config: args.config })) ||
undefined;
if (options?.plugins) {
const contextUrl = pathToFileURL(dirname(args.config!));
options.plugins = await Promise.all(
options.plugins.map(async (plugin) => {
// in theory, should be able to just resolve the path, but for some reason
// that dereferences symlinks
if (typeof plugin === "string") {
const path = await resolve(plugin, contextUrl);
const p = await load(path);
plugin = p.default ?? p;
}
return plugin;
}),
);
}
const worker = new PrettierWorker(options);

return async (a) => {
try {
worker.run(a);
await worker.run(a);
} catch (error) {
return {
exitCode: 1,
Expand Down
14 changes: 7 additions & 7 deletions prettier/format/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { ArgumentParser } from "argparse";
import * as fs from "node:fs";
import prettier from "prettier";
import { readFile, writeFile } from "node:fs/promises";
import { Options, format } from "prettier";

export class PrettierWorker {
constructor(private readonly options: any) {}
constructor(private readonly options: Options | undefined) {}

run(a: string[]) {
async run(a: string[]) {
const parser = new ArgumentParser();
parser.add_argument("input");
parser.add_argument("output");
const args = parser.parse_args(a);
const input = fs.readFileSync(args.input, "utf8");
const output = prettier.format(input, {
const input = await readFile(args.input, "utf8");
const output = await format(input, {
...this.options,
filepath: args.input,
});
fs.writeFileSync(args.output, output, "utf8");
await writeFile(args.output, output, "utf8");
}
}
2 changes: 1 addition & 1 deletion prettier/format/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"esModuleInterop": true
"module": "es2020"
},
"extends": "@better-rules-javascript/tools-typescript/tsconfig.common.json"
}
11 changes: 2 additions & 9 deletions prettier/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ load("//javascript:rules.bzl", "js_export")
load("//nodejs:rules.bzl", "nodejs_binary")
load("//util:path.bzl", "runfile_path")

def configure_prettier(name, config, config_dep, dep = "@better_rules_javascript//prettier:prettier_lib", plugins = [], visibility = None):
js_export(
name = "%s.prettier" % name,
dep = dep,
deps = plugins,
visibility = ["//visibility:private"],
)

def configure_prettier(name, config, config_dep, dep = "@better_rules_javascript//prettier:prettier_lib", visibility = None):
js_export(
name = "%s.main" % name,
dep = "@better_rules_javascript//prettier/format:lib",
deps = [":%s.prettier" % name],
deps = [dep],
extra_deps = [config_dep],
visibility = ["//visibility:private"],
)
Expand Down
50 changes: 50 additions & 0 deletions prettier/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
load("//commonjs:rules.bzl", "cjs_root")
load("//javascript:rules.bzl", "js_library")
load("//jest:rules.bzl", "jest_test")
load("//typescript:rules.bzl", "ts_library")

js_library(
name = "jest_config",
srcs = ["jest.config.js"],
root = ":root",
deps = ["//test:lib"],
)

cjs_root(
name = "root",
package_name = "@better-rules-javascript/prettier-test",
)

jest_test(
name = "test",
size = "large",
config = "jest.config.js",
config_dep = ":jest_config",
data = glob(["bazel/**/*"]) + ["@files//:common"],
dep = ":test_lib",
jest = "@npm//jest:lib",
node = "@better_rules_javascript//tools/nodejs",
tags = ["local"],
visibility = ["//visibility:public"],
)

ts_library(
name = "test_lib",
srcs = glob(["src/**/*.ts"]),
compiler = "//typescript:tsc",
config = "tsconfig.json",
config_dep = ":tsconfig",
root = ":root",
deps = [
"//test:lib",
"@npm//@types/jest:lib",
"@npm//@types/node:lib",
],
)

js_library(
name = "tsconfig",
srcs = ["tsconfig.json"],
root = ":root",
deps = ["//tools/typescript:tsconfig"],
)
16 changes: 16 additions & 0 deletions prettier/test/bazel/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
workspace(name = "better_rules_javascript_test")

# JavaScript

local_repository(
name = "better_rules_javascript",
path = "../../../external/files/files",
)

load("@better_rules_javascript//test:workspace0.bzl", "test_repositories0")

test_repositories0()

load("@better_rules_javascript//test:workspace1.bzl", "test_repositories1")

test_repositories1()
30 changes: 30 additions & 0 deletions prettier/test/bazel/plugin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("@better_rules_javascript//commonjs:rules.bzl", "cjs_root")
load("@better_rules_javascript//javascript:rules.bzl", "js_library")
load("@better_rules_javascript//prettier:rules.bzl", "configure_prettier")
load("@rules_file//generate:rules.bzl", "format")

format(
name = "format",
srcs = ["example.xml"],
formatter = ":prettier",
)

js_library(
name = "prettier_config",
srcs = ["prettierrc.yml"],
root = "root",
deps = ["@npm//@prettier/plugin-xml:lib"],
)

cjs_root(
name = "root",
package_name = "@better-rules-javascript-test/prettier-plugin",
descriptors = ["package.json"],
)

configure_prettier(
name = "prettier",
config = "prettierrc.yml",
config_dep = ":prettier_config",
dep = "@npm//prettier:lib",
)
File renamed without changes.
1 change: 1 addition & 0 deletions prettier/test/bazel/plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
File renamed without changes.
3 changes: 3 additions & 0 deletions prettier/test/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { jestConfig } = require("@better-rules-javascript/test/jest-config");

module.exports = jestConfig();
15 changes: 0 additions & 15 deletions prettier/test/plugin/BUILD.bazel

This file was deleted.

11 changes: 11 additions & 0 deletions prettier/test/src/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { spawnOptions } from "@better-rules-javascript/test";
import * as childProcess from "node:child_process";

test("Format", () => {
const result = childProcess.spawnSync("bazel", ["run", "plugin:format"], {
cwd: "prettier/test/bazel",
stdio: "inherit",
...spawnOptions(),
});
expect(result.status).toBe(0);
});
3 changes: 3 additions & 0 deletions prettier/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@better-rules-javascript/tools-typescript/tsconfig.common.json"
}
Loading

0 comments on commit 0696c8d

Please sign in to comment.