Skip to content

Commit 05290eb

Browse files
authored
feat(formatter): handle JSON5 and JSON with Comments (#611)
Fixes #601
1 parent a3e4464 commit 05290eb

File tree

8 files changed

+57
-4
lines changed

8 files changed

+57
-4
lines changed

example/.swcrc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
// Allows comments!
3+
"$schema": "https://json.schemastore.org/swcrc",
4+
"jsc": {
5+
"parser": {
6+
"syntax": "typescript",
7+
"tsx": true,
8+
"decorators": true,
9+
"dynamicImport": true
10+
},
11+
"transform": {
12+
"react": {
13+
"runtime": "automatic"
14+
}
15+
},
16+
"target": "es2022",
17+
"loose": false,
18+
"externalHelpers": true,
19+
"keepClassNames": true
20+
},
21+
"module": {
22+
"type": "es6",
23+
"strict": true,
24+
"strictMode": true,
25+
"lazy": false,
26+
"noInterop": false
27+
},
28+
"minify": false,
29+
"sourceMaps": true
30+
}

example/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ exports_files(
2121
".ruff.toml",
2222
".shellcheckrc",
2323
".scalafmt.conf",
24+
".swcrc",
2425
".vale.ini",
2526
".editorconfig",
2627
"ktlint-baseline.xml",

example/src/BUILD.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
load("@aspect_rules_lint//format:defs.bzl", "format_test")
22
load("@aspect_rules_swc//swc:defs.bzl", "swc")
33
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
4+
load("@bazel_skylib//lib:partial.bzl", "partial")
45
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
56
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
67
load("@rules_proto//proto:defs.bzl", "proto_library")
@@ -45,7 +46,10 @@ ts_project(
4546
name = "ts",
4647
srcs = ["file.ts"],
4748
declaration = True,
48-
transpiler = swc,
49+
transpiler = partial.make(
50+
swc,
51+
swcrc = "//:.swcrc",
52+
),
4953
deps = [
5054
":ts_dep",
5155
"//:node_modules/dayjs",

example/src/config.json5

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
// This is a comment - not allowed in JSON
3+
trailingComma: "allowed in JSON5", // Trailing commas are allowed
4+
singleQuotes: "valid in JSON5", // Single quotes are allowed
5+
unquotedKeys: true, // Unquoted object keys are allowed
6+
hex: 0x1f, // Hexadecimal numbers are allowed
7+
infinity: Infinity, // Infinity is allowed
8+
nan: NaN, // NaN is allowed
9+
trailing_decimal: 1, // Trailing decimal point is allowed
10+
"contains.periods": true, // Dots in keys don't need to be quoted
11+
"can mix": "quote styles", // Mixing quote styles is allowed
12+
}

example/src/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
// A comment
23
"compilerOptions": {
34
"declaration": true,
45
"esModuleInterop": true,

format/private/format.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function process_args_in_batches() {
112112
function ls-files {
113113
language="$1" && shift;
114114
# Copied file patterns from
115-
# https://github.com/github-linguist/linguist/blob/559a6426942abcae16b6d6b328147476432bf6cb/lib/linguist/languages.yml
115+
# https://github.com/github-linguist/linguist/blob/main/lib/linguist/languages.yml
116116
# using the ./mirror_linguist_languages.sh tool to transform to Bash code
117117
case "$language" in
118118
'C') patterns=('*.c' '*.cats' '*.h' '*.idc') ;;
@@ -123,6 +123,8 @@ function ls-files {
123123
'GraphQL') patterns=('*.graphql' '*.gql' '*.graphqls') ;;
124124
'HTML') patterns=('*.html' '*.hta' '*.htm' '*.html.hl' '*.inc' '*.xht' '*.xhtml') ;;
125125
'JSON') patterns=('.all-contributorsrc' '.arcconfig' '.auto-changelog' '.c8rc' '.htmlhintrc' '.imgbotconfig' '.nycrc' '.tern-config' '.tern-project' '.watchmanconfig' 'Pipfile.lock' 'composer.lock' 'deno.lock' 'flake.lock' 'mcmod.info' '*.json' '*.4DForm' '*.4DProject' '*.avsc' '*.geojson' '*.gltf' '*.har' '*.ice' '*.JSON-tmLanguage' '*.jsonl' '*.mcmeta' '*.tfstate' '*.tfstate.backup' '*.topojson' '*.webapp' '*.webmanifest' '*.yy' '*.yyp') ;;
126+
'JSON with Comments') patterns=( '*.jsonc' '*.code-snippets' '*.code-workspace' '*.sublime-build' '*.sublime-commands' '*.sublime-completions' '*.sublime-keymap' '*.sublime-macro' '*.sublime-menu' '*.sublime-mousemap' '*.sublime-project' '*.sublime-settings' '*.sublime-theme' '*.sublime-workspace' '*.sublime_metrics' '*.sublime_session' '.babelrc' '.devcontainer.json' '.eslintrc.json' '.jscsrc' '.jshintrc' '.jslintrc' '.swcrc' 'api-extractor.json' 'devcontainer.json' 'jsconfig.json' 'language-configuration.json' 'tsconfig.json' 'tslint.json') ;;
127+
'JSON5') patterns=('*.json5') ;;
126128
'Java') patterns=('*.java' '*.jav' '*.jsh') ;;
127129
'JavaScript') patterns=('Jakefile' '*.js' '*._js' '*.bones' '*.cjs' '*.es' '*.es6' '*.frag' '*.gs' '*.jake' '*.javascript' '*.jsb' '*.jscad' '*.jsfl' '*.jslib' '*.jsm' '*.jspre' '*.jss' '*.jsx' '*.mjs' '*.njs' '*.pac' '*.sjs' '*.ssjs' '*.xsjs' '*.xsjslib') ;;
128130
'Jsonnet') patterns=('*.jsonnet' '*.libsonnet') ;;
@@ -302,7 +304,7 @@ if [ "${BASH_SOURCE[0]}" -ef "$0" ]; then
302304

303305
# Handle additional languages for JavaScript and CSS
304306
if [[ "$lang" == "JavaScript" ]]; then
305-
for sublang in "JSON" "TSX" "TypeScript" "Vue"; do
307+
for sublang in "JSON" "JSON5" "JSON with Comments" "TSX" "TypeScript" "Vue"; do
306308
process_args_in_batches "$sublang" "$bin" "${flags:-""}" "$@"
307309
done
308310
fi

format/private/formatter_binary.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
# Per the formatter design, each language can only have a single formatter binary
44
# Keys in this map must match the `case "$language" in` block in format.sh
55
TOOLS = {
6-
# NB: includes TypeScript and JSON
6+
# NB: includes TypeScript and some others
77
"JavaScript": "prettier",
88
"Markdown": "prettier",
9+
# NB: includes LESS and SASS
910
"CSS": "prettier",
1011
"GraphQL": "prettier",
1112
"HTML": "prettier",

format/test/format_test.bats

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ bats_load_library "bats-assert"
1414
assert_output --partial "+ prettier --write example/src/hello.tsx"
1515
assert_output --partial "+ prettier --write example/src/hello.vue"
1616
assert_output --partial "+ prettier --write .bcr/metadata.template.json"
17+
assert_output --partial "+ prettier --write example/.swcrc"
18+
assert_output --partial "+ prettier --write example/src/config.json5"
1719
}
1820

1921
# File arguments: will filter with find

0 commit comments

Comments
 (0)