prettier-plugin-json-jsonc 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +168 -2
- package/dist/index.d.mts +18 -0
- package/dist/index.mjs +366 -0
- package/package.json +58 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright © Dom Porada <dom@dom.engineering> (https://dom.engineering)
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,169 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://github.com/porada/prettier-plugin-json-jsonc">
|
|
3
|
+
<picture>
|
|
4
|
+
<source
|
|
5
|
+
srcset="https://github.com/porada/prettier-plugin-json-jsonc/blob/main/.github/assets/prettier-plugin-json-jsonc-dark-scheme@3x.png?raw=true"
|
|
6
|
+
media="(prefers-color-scheme: dark)"
|
|
7
|
+
/>
|
|
8
|
+
<source
|
|
9
|
+
srcset="https://github.com/porada/prettier-plugin-json-jsonc/blob/main/.github/assets/prettier-plugin-json-jsonc-light-scheme@3x.png?raw=true"
|
|
10
|
+
media="(prefers-color-scheme: light)"
|
|
11
|
+
/>
|
|
12
|
+
<img
|
|
13
|
+
src="https://github.com/porada/prettier-plugin-json-jsonc/blob/main/.github/assets/prettier-plugin-json-jsonc-light-scheme@3x.png?raw=true"
|
|
14
|
+
width="520"
|
|
15
|
+
alt=""
|
|
16
|
+
/>
|
|
17
|
+
</picture>
|
|
18
|
+
</a>
|
|
19
|
+
</p>
|
|
2
20
|
|
|
3
|
-
|
|
21
|
+
<h1 align="center">prettier-plugin-json-jsonc</h1>
|
|
22
|
+
|
|
23
|
+
<p align="center">Additional JSON and JSONC formatting options for Prettier.</p>
|
|
24
|
+
|
|
25
|
+
<p align="center">
|
|
26
|
+
<a href="https://npmx.dev/package/prettier-plugin-json-jsonc"
|
|
27
|
+
><img
|
|
28
|
+
src="https://img.shields.io/npm/v/prettier-plugin-json-jsonc?style=flat-square"
|
|
29
|
+
alt=""
|
|
30
|
+
/></a>
|
|
31
|
+
<a href="https://github.com/porada/prettier-plugin-json-jsonc/actions/workflows/test.yaml"
|
|
32
|
+
><img
|
|
33
|
+
src="https://img.shields.io/github/actions/workflow/status/porada/prettier-plugin-json-jsonc/test.yaml?style=flat-square"
|
|
34
|
+
alt=""
|
|
35
|
+
/></a>
|
|
36
|
+
<a href="https://codecov.io/github/porada/prettier-plugin-json-jsonc"
|
|
37
|
+
><img
|
|
38
|
+
src="https://img.shields.io/codecov/c/github/porada/prettier-plugin-json-jsonc?style=flat-square"
|
|
39
|
+
alt=""
|
|
40
|
+
/></a>
|
|
41
|
+
</p>
|
|
42
|
+
|
|
43
|
+
<div> </div>
|
|
44
|
+
|
|
45
|
+
## Install
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
npm install --save-dev prettier-plugin-json-jsonc
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
pnpm add --save-dev prettier-plugin-json-jsonc
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
Reference `prettier-plugin-json-jsonc` in your [Prettier config](https://prettier.io/docs/configuration):
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"plugins": [
|
|
62
|
+
"prettier-plugin-json-jsonc"
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
If you’re using any other JSON plugins, make sure `prettier-plugin-json-jsonc` is listed last. This applies to each `overrides` entry as well.
|
|
68
|
+
|
|
69
|
+
```json
|
|
70
|
+
{
|
|
71
|
+
"plugins": [
|
|
72
|
+
"prettier-plugin-sort-json",
|
|
73
|
+
"prettier-plugin-json-jsonc"
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
{
|
|
80
|
+
"overrides": [
|
|
81
|
+
{
|
|
82
|
+
"files": "**/package.json",
|
|
83
|
+
"options": {
|
|
84
|
+
"plugins": [
|
|
85
|
+
"prettier-plugin-pkg",
|
|
86
|
+
"prettier-plugin-json-jsonc"
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
]
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Options
|
|
95
|
+
|
|
96
|
+
In addition to Prettier’s [built-in options](https://prettier.io/docs/options) that affect JSON formatting, `prettier-plugin-json-jsonc` offers additional configuration options.
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
interface PluginOptions {
|
|
100
|
+
/**
|
|
101
|
+
* Choose how non-empty arrays and objects wrap. `collapse` uses one line
|
|
102
|
+
* when they fit. `expand` puts each item or property on its own line.
|
|
103
|
+
* `inherit` follows the current parser’s wrapping rules.
|
|
104
|
+
*
|
|
105
|
+
* @default 'inherit'
|
|
106
|
+
*/
|
|
107
|
+
jsonCollectionWrap?: 'collapse' | 'expand' | 'inherit' | undefined;
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## FAQ
|
|
112
|
+
|
|
113
|
+
### How do `expand` and `collapse` affect JSON wrapping?
|
|
114
|
+
|
|
115
|
+
With `expand`, each array item and object property starts on a separate line. Empty arrays and objects stay compact.
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"extends": "@standard-config/tsconfig",
|
|
120
|
+
"compilerOptions": {
|
|
121
|
+
"types": [
|
|
122
|
+
"node",
|
|
123
|
+
"vitest/globals"
|
|
124
|
+
]
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
With `collapse`, collections are formatted on a single line when they fit within the configured `printWidth`.
|
|
130
|
+
|
|
131
|
+
<!-- prettier-ignore -->
|
|
132
|
+
```json
|
|
133
|
+
{
|
|
134
|
+
"extends": "@standard-config/tsconfig",
|
|
135
|
+
"compilerOptions": { "types": ["node", "vitest/globals"] }
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Please note that both `expand` and `collapse` override the configured [`objectWrap`](https://prettier.io/docs/options#object-wrap) value.
|
|
140
|
+
|
|
141
|
+
### Does this plugin work with files like `package.json` and `composer.json`?
|
|
142
|
+
|
|
143
|
+
Yes. Prettier uses a separate parser called `json-stringify` to match the formatting of the tools that update these files. This plugin supports that parser, too.
|
|
144
|
+
|
|
145
|
+
That said, `json-stringify` always expands arrays and objects, and `jsonCollectionWrap` can’t override that behavior. You can use the `json` parser to make Prettier apply the option anyway, but please note that external tools may reformat the entire file when updating it.
|
|
146
|
+
|
|
147
|
+
```json
|
|
148
|
+
{
|
|
149
|
+
"overrides": [
|
|
150
|
+
{
|
|
151
|
+
"files": "**/package.json",
|
|
152
|
+
"options": {
|
|
153
|
+
"parser": "json",
|
|
154
|
+
"jsonCollectionWrap": "collapse"
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
]
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Related
|
|
162
|
+
|
|
163
|
+
- [**@standard-config/prettier**](https://github.com/standard-config/prettier)
|
|
164
|
+
- [**prettier-plugin-markdown-html**](https://github.com/porada/prettier-plugin-markdown-html)
|
|
165
|
+
- [**prettier-plugin-yaml**](https://github.com/porada/prettier-plugin-yaml)
|
|
166
|
+
|
|
167
|
+
## License
|
|
168
|
+
|
|
169
|
+
MIT © [Dom Porada](https://dom.engineering)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Plugin } from "prettier";
|
|
2
|
+
interface PluginOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Choose how non-empty arrays and objects wrap. `collapse` uses one line
|
|
5
|
+
* when they fit. `expand` puts each item or property on its own line.
|
|
6
|
+
* `inherit` follows the current parser’s wrapping rules.
|
|
7
|
+
*
|
|
8
|
+
* @default 'inherit'
|
|
9
|
+
*/
|
|
10
|
+
jsonCollectionWrap?: 'collapse' | 'expand' | 'inherit' | undefined;
|
|
11
|
+
}
|
|
12
|
+
declare module 'prettier' {
|
|
13
|
+
interface Options extends PluginOptions {}
|
|
14
|
+
}
|
|
15
|
+
export declare const parsers: Plugin['parsers'];
|
|
16
|
+
export declare const printers: Plugin['printers'];
|
|
17
|
+
export declare const options: Plugin['options'];
|
|
18
|
+
export type { PluginOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { parsers as parsers$1 } from "prettier/plugins/babel";
|
|
2
|
+
import * as pluginEstree from "prettier/plugins/estree";
|
|
3
|
+
import { printers as printers$1 } from "prettier/plugins/estree";
|
|
4
|
+
import { doc } from "prettier";
|
|
5
|
+
// src/plugin-hooks/index.ts
|
|
6
|
+
const PARSER_DELEGATION = Symbol.for("prettier-plugin-json-jsonc.parser-delegation");
|
|
7
|
+
const PARSER_MARKER = Symbol.for("prettier-plugin-json-jsonc.parser");
|
|
8
|
+
const PARSER_ORIGIN = Symbol.for("prettier-plugin-json-jsonc.parser-origin");
|
|
9
|
+
/**
|
|
10
|
+
* Marks a parser so resolver chains can recognize this plugin’s wrappers.
|
|
11
|
+
*/
|
|
12
|
+
function markParserAsOwn(parser) {
|
|
13
|
+
Object.defineProperty(parser, PARSER_MARKER, { value: true });
|
|
14
|
+
Object.defineProperty(parser, PARSER_ORIGIN, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
value: {
|
|
17
|
+
parse: parser.parse,
|
|
18
|
+
preprocess: parser.preprocess
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return parser;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Checks whether a parser defines or inherits this plugin’s marker.
|
|
25
|
+
*/
|
|
26
|
+
function isOwnParser(parser) {
|
|
27
|
+
return Reflect.get(parser, PARSER_MARKER) === true;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Invokes a parser with options available through both Prettier’s current
|
|
31
|
+
* two-argument and legacy three-argument parse signatures.
|
|
32
|
+
*/
|
|
33
|
+
function callParserWithCompatibility(parser, text, options) {
|
|
34
|
+
return parser.parse.call(parser, text, options, options);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Creates a resolver for prior compatible parsers. Each options object has its
|
|
38
|
+
* own cache of hook lookups.
|
|
39
|
+
*/
|
|
40
|
+
function createPriorParserResolver(name, expectedAstFormat, currentParser) {
|
|
41
|
+
const resolverStateByOptions = /* @__PURE__ */ new WeakMap();
|
|
42
|
+
return async (options, hook) => {
|
|
43
|
+
const context = options[PARSER_DELEGATION];
|
|
44
|
+
const delegation = context?.delegation;
|
|
45
|
+
if (delegation?.hook === hook && delegation.parserName === name) return delegation.resolveNext();
|
|
46
|
+
let state = resolverStateByOptions.get(options);
|
|
47
|
+
if (!state) {
|
|
48
|
+
state = {
|
|
49
|
+
entryOptions: {
|
|
50
|
+
locEnd: currentParser.locEnd,
|
|
51
|
+
locStart: currentParser.locStart,
|
|
52
|
+
plugins: options.plugins
|
|
53
|
+
},
|
|
54
|
+
lifecycleState: {},
|
|
55
|
+
name: typeof options.parser === "string" ? options.parser : name,
|
|
56
|
+
parserByPluginIndex: /* @__PURE__ */ new Map(),
|
|
57
|
+
parserName: name,
|
|
58
|
+
plugins: options.plugins,
|
|
59
|
+
priorParserByHook: /* @__PURE__ */ new Map()
|
|
60
|
+
};
|
|
61
|
+
resolverStateByOptions.set(options, state);
|
|
62
|
+
}
|
|
63
|
+
if (!context) {
|
|
64
|
+
const { entryOptions, lifecycleState } = state;
|
|
65
|
+
if (options.locEnd !== entryOptions.locEnd) lifecycleState.locEnd = options.locEnd;
|
|
66
|
+
if (options.locStart !== entryOptions.locStart) lifecycleState.locStart = options.locStart;
|
|
67
|
+
if (options.plugins !== entryOptions.plugins) lifecycleState.plugins = options.plugins;
|
|
68
|
+
entryOptions.locEnd = options.locEnd;
|
|
69
|
+
entryOptions.locStart = options.locStart;
|
|
70
|
+
entryOptions.plugins = options.plugins;
|
|
71
|
+
}
|
|
72
|
+
const cachedParser = state.priorParserByHook.get(hook);
|
|
73
|
+
if (cachedParser) return await cachedParser;
|
|
74
|
+
const parser = findPriorParser(state, hook, currentParser, expectedAstFormat);
|
|
75
|
+
state.priorParserByHook.set(hook, parser);
|
|
76
|
+
return await parser;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Finds the nearest prior compatible parser with a distinct implementation of
|
|
81
|
+
* the requested hook.
|
|
82
|
+
*/
|
|
83
|
+
async function findPriorParser(state, hook, currentParser, expectedAstFormat, beforeIndex = state.plugins.length, omittedPluginIndexes = /* @__PURE__ */ new Set(), rootEntry = true) {
|
|
84
|
+
const { name } = state;
|
|
85
|
+
let isEntry = rootEntry;
|
|
86
|
+
for (let index = beforeIndex - 1; index >= 0; index -= 1) {
|
|
87
|
+
const plugin = state.plugins[index];
|
|
88
|
+
if (!hasParsers(plugin) || !Object.hasOwn(plugin.parsers, name)) continue;
|
|
89
|
+
const parserOrInitializer = plugin.parsers[name];
|
|
90
|
+
if (!parserOrInitializer) continue;
|
|
91
|
+
const parser = await resolveParser(state, index, parserOrInitializer);
|
|
92
|
+
const isSelectedParser = isEntry;
|
|
93
|
+
isEntry = false;
|
|
94
|
+
if (isOwnParser(parser)) {
|
|
95
|
+
omittedPluginIndexes.add(index);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
assertCompatibleParser(name, parser, expectedAstFormat, currentParser);
|
|
99
|
+
const parserHook = parser[hook];
|
|
100
|
+
const origin = Reflect.get(parser, PARSER_ORIGIN);
|
|
101
|
+
if (isSelectedParser && origin !== void 0 || parserHook === currentParser[hook] || origin !== void 0 && parserHook === origin[hook]) {
|
|
102
|
+
omittedPluginIndexes.add(index);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (hook === "preprocess" && typeof parserHook !== "function") return;
|
|
106
|
+
const omittedPlugins = new Set([...omittedPluginIndexes].map((omittedIndex) => state.plugins[omittedIndex]));
|
|
107
|
+
let nextParser;
|
|
108
|
+
return {
|
|
109
|
+
delegation: {
|
|
110
|
+
hook,
|
|
111
|
+
parserName: state.parserName,
|
|
112
|
+
resolveNext: async () => {
|
|
113
|
+
nextParser ??= findPriorParser(state, hook, currentParser, expectedAstFormat, index, /* @__PURE__ */ new Set([...omittedPluginIndexes, index]), false);
|
|
114
|
+
return await nextParser;
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
entryOptions: state.entryOptions,
|
|
118
|
+
lifecycleState: state.lifecycleState,
|
|
119
|
+
parser,
|
|
120
|
+
get plugins() {
|
|
121
|
+
const plugins = state.lifecycleState.plugins ?? state.plugins;
|
|
122
|
+
return plugins.some((plugin) => omittedPlugins.has(plugin)) ? plugins.filter((plugin) => !omittedPlugins.has(plugin)) : plugins;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Shares a cached parser promise across hook lookups at the same plugin index.
|
|
129
|
+
*/
|
|
130
|
+
async function resolveParser(state, index, parserOrInitializer) {
|
|
131
|
+
const cachedParser = state.parserByPluginIndex.get(index);
|
|
132
|
+
if (cachedParser) return await cachedParser;
|
|
133
|
+
const parser = initializeParser(parserOrInitializer);
|
|
134
|
+
state.parserByPluginIndex.set(index, parser);
|
|
135
|
+
return await parser;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Returns a direct parser or initializes a lazy parser.
|
|
139
|
+
*/
|
|
140
|
+
async function initializeParser(parserOrInitializer) {
|
|
141
|
+
if (typeof parserOrInitializer === "function") return await parserOrInitializer();
|
|
142
|
+
return parserOrInitializer;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Throws when a parser’s AST format is incompatible with this plugin.
|
|
146
|
+
*/
|
|
147
|
+
function assertCompatibleParser(name, parser, expectedAstFormat, currentParser) {
|
|
148
|
+
const compatibleAstFormats = /* @__PURE__ */ new Set([expectedAstFormat, currentParser.astFormat]);
|
|
149
|
+
if (!compatibleAstFormats.has(parser.astFormat)) {
|
|
150
|
+
const expectedAstFormats = [...compatibleAstFormats].map((astFormat) => `\`${astFormat}\``).join(" or ");
|
|
151
|
+
throw new TypeError(`[prettier-plugin-json-jsonc] Unsupported AST format for the \`${name}\` parser. Expected ${expectedAstFormats}, received \`${parser.astFormat}\``);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Checks whether a value exposes a parser map.
|
|
156
|
+
*/
|
|
157
|
+
function hasParsers(plugin) {
|
|
158
|
+
if (!plugin || typeof plugin !== "object") return false;
|
|
159
|
+
const { parsers } = plugin;
|
|
160
|
+
return typeof parsers === "object" && parsers !== null;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Runs the prior hook with its settings on the shared options object. Explicit
|
|
164
|
+
* changes to the plugin list or location functions survive errors. A successful
|
|
165
|
+
* parse also keeps any default location functions the hook leaves in place.
|
|
166
|
+
*/
|
|
167
|
+
async function withPriorParserOptions(options, priorParser, callback) {
|
|
168
|
+
const parserOptions = options;
|
|
169
|
+
const previousContext = parserOptions[PARSER_DELEGATION];
|
|
170
|
+
previousContext?.syncOptions();
|
|
171
|
+
const { astFormat, locEnd, locStart, plugins } = options;
|
|
172
|
+
const { entryOptions, lifecycleState } = priorParser;
|
|
173
|
+
const delegatedLocEnd = lifecycleState.locEnd ?? priorParser.parser.locEnd;
|
|
174
|
+
const delegatedLocStart = lifecycleState.locStart ?? priorParser.parser.locStart;
|
|
175
|
+
const delegatedPlugins = priorParser.plugins;
|
|
176
|
+
options.astFormat = priorParser.parser.astFormat;
|
|
177
|
+
options.locEnd = delegatedLocEnd;
|
|
178
|
+
options.locStart = delegatedLocStart;
|
|
179
|
+
options.plugins = delegatedPlugins;
|
|
180
|
+
const context = {
|
|
181
|
+
delegation: priorParser.delegation,
|
|
182
|
+
snapshot: {
|
|
183
|
+
locEnd: delegatedLocEnd,
|
|
184
|
+
locStart: delegatedLocStart,
|
|
185
|
+
plugins: delegatedPlugins
|
|
186
|
+
},
|
|
187
|
+
syncOptions: () => {
|
|
188
|
+
if (options.locEnd !== context.snapshot.locEnd) lifecycleState.locEnd = options.locEnd;
|
|
189
|
+
if (options.locStart !== context.snapshot.locStart) lifecycleState.locStart = options.locStart;
|
|
190
|
+
if (options.plugins !== context.snapshot.plugins) lifecycleState.plugins = options.plugins;
|
|
191
|
+
context.snapshot = {
|
|
192
|
+
locEnd: options.locEnd,
|
|
193
|
+
locStart: options.locStart,
|
|
194
|
+
plugins: options.plugins
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
parserOptions[PARSER_DELEGATION] = context;
|
|
199
|
+
let parsedLocations;
|
|
200
|
+
try {
|
|
201
|
+
const result = await callback(options);
|
|
202
|
+
if (context.delegation?.hook === "parse") parsedLocations = {
|
|
203
|
+
locEnd: options.locEnd,
|
|
204
|
+
locStart: options.locStart
|
|
205
|
+
};
|
|
206
|
+
return result;
|
|
207
|
+
} finally {
|
|
208
|
+
context.syncOptions();
|
|
209
|
+
options.astFormat = astFormat;
|
|
210
|
+
options.locEnd = lifecycleState.locEnd ?? parsedLocations?.locEnd ?? locEnd;
|
|
211
|
+
options.locStart = lifecycleState.locStart ?? parsedLocations?.locStart ?? locStart;
|
|
212
|
+
if (options.plugins === delegatedPlugins) options.plugins = plugins;
|
|
213
|
+
if (previousContext) {
|
|
214
|
+
previousContext.snapshot = {
|
|
215
|
+
locEnd: options.locEnd,
|
|
216
|
+
locStart: options.locStart,
|
|
217
|
+
plugins: options.plugins
|
|
218
|
+
};
|
|
219
|
+
parserOptions[PARSER_DELEGATION] = previousContext;
|
|
220
|
+
} else {
|
|
221
|
+
if (entryOptions) {
|
|
222
|
+
entryOptions.locEnd = options.locEnd;
|
|
223
|
+
entryOptions.locStart = options.locStart;
|
|
224
|
+
entryOptions.plugins = options.plugins;
|
|
225
|
+
}
|
|
226
|
+
Reflect.deleteProperty(parserOptions, PARSER_DELEGATION);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// src/print-json/index.ts
|
|
231
|
+
/**
|
|
232
|
+
* Checks whether a node has comments that are neither leading nor trailing.
|
|
233
|
+
*/
|
|
234
|
+
function hasDanglingComments(node) {
|
|
235
|
+
const { comments } = node;
|
|
236
|
+
return comments?.some(({ leading, trailing }) => !leading && !trailing) === true;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Identifies arrays and objects that contain entries or dangling comments.
|
|
240
|
+
*/
|
|
241
|
+
function getNonEmptyJSONContainerType(node) {
|
|
242
|
+
/* v8 ignore if -- @preserve */
|
|
243
|
+
if (typeof node !== "object" || node === null || !("type" in node)) return;
|
|
244
|
+
const hasContents = hasDanglingComments(node);
|
|
245
|
+
switch (node.type) {
|
|
246
|
+
case "ArrayExpression": return node.elements.length > 0 || hasContents ? "array" : void 0;
|
|
247
|
+
case "ObjectExpression": return node.properties.length > 0 || hasContents ? "object" : void 0;
|
|
248
|
+
default: return;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Unwraps a fill document so the enclosing group controls its line breaks.
|
|
253
|
+
*/
|
|
254
|
+
function unpackFill(currentDoc) {
|
|
255
|
+
return typeof currentDoc !== "string" && !Array.isArray(currentDoc) && currentDoc.type === "fill" ? currentDoc.parts : currentDoc;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Forces a group document to break without changing nested groups.
|
|
259
|
+
*/
|
|
260
|
+
function forceDirectGroupToBreak(currentDoc) {
|
|
261
|
+
return typeof currentDoc !== "string" && !Array.isArray(currentDoc) && currentDoc.type === "group" ? {
|
|
262
|
+
...currentDoc,
|
|
263
|
+
break: true
|
|
264
|
+
} : currentDoc;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Updates Prettier’s container group while preserving trailing documents.
|
|
268
|
+
*/
|
|
269
|
+
function mapNativeContainerGroup(printed, callback) {
|
|
270
|
+
let structuralGroup = printed;
|
|
271
|
+
if (Array.isArray(printed)) [structuralGroup] = printed;
|
|
272
|
+
/* v8 ignore if -- @preserve */
|
|
273
|
+
if (typeof structuralGroup !== "object" || structuralGroup === null || Array.isArray(structuralGroup) || structuralGroup.type !== "group") return printed;
|
|
274
|
+
const mappedGroup = callback(structuralGroup);
|
|
275
|
+
return Array.isArray(printed) ? [mappedGroup, ...printed.slice(1)] : mappedGroup;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Applies the selected wrapping mode. `inherit` preserves Prettier’s output.
|
|
279
|
+
*/
|
|
280
|
+
function applyJSONCollectionWrapping(printed, node, collectionWrap) {
|
|
281
|
+
const containerType = getNonEmptyJSONContainerType(node);
|
|
282
|
+
if (!containerType || collectionWrap === "inherit") return printed;
|
|
283
|
+
if (collectionWrap === "collapse") return mapNativeContainerGroup(printed, (group) => ({
|
|
284
|
+
...group,
|
|
285
|
+
break: false
|
|
286
|
+
}));
|
|
287
|
+
return mapNativeContainerGroup(printed, (group) => ({
|
|
288
|
+
...group,
|
|
289
|
+
break: true,
|
|
290
|
+
contents: containerType === "array" ? doc.utils.mapDoc(group.contents, unpackFill) : forceDirectGroupToBreak(group.contents)
|
|
291
|
+
}));
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Prints with Prettier before applying the selected wrapping mode.
|
|
295
|
+
*/
|
|
296
|
+
const printJSON = (path, options, print, args) => {
|
|
297
|
+
const { jsonCollectionWrap = "inherit" } = options;
|
|
298
|
+
return applyJSONCollectionWrapping(printers$1.estree.print(path, options, print, args), path.node, jsonCollectionWrap);
|
|
299
|
+
};
|
|
300
|
+
// src/index.ts
|
|
301
|
+
const estreeOptions = pluginEstree.options;
|
|
302
|
+
const { printers: estreePrinters } = pluginEstree;
|
|
303
|
+
const PLUGIN_ESTREE_FORMAT = "prettier-plugin-json-jsonc.estree";
|
|
304
|
+
/**
|
|
305
|
+
* Creates a parser that defers to the prior compatible parser before Prettier’s
|
|
306
|
+
* native parser.
|
|
307
|
+
*/
|
|
308
|
+
function createParser(name) {
|
|
309
|
+
async function parse(text, options) {
|
|
310
|
+
const resolvedPriorParser = await resolvePriorParser(options, "parse");
|
|
311
|
+
const priorParser = resolvedPriorParser?.parser;
|
|
312
|
+
if (resolvedPriorParser && priorParser && typeof priorParser.parse === "function" && priorParser.parse !== parse) return withPriorParserOptions(options, resolvedPriorParser, (delegatedOptions) => callParserWithCompatibility(priorParser, text, delegatedOptions));
|
|
313
|
+
return await parsers$1[name].parse(text, options);
|
|
314
|
+
}
|
|
315
|
+
const preprocess = async (text, options) => {
|
|
316
|
+
const resolvedPriorParser = await resolvePriorParser(options, "preprocess");
|
|
317
|
+
const priorParser = resolvedPriorParser?.parser;
|
|
318
|
+
const priorPreprocess = priorParser?.preprocess;
|
|
319
|
+
if (resolvedPriorParser && priorParser && typeof priorPreprocess === "function" && priorPreprocess !== preprocess) return withPriorParserOptions(options, resolvedPriorParser, (delegatedOptions) => priorPreprocess.call(priorParser, text, delegatedOptions));
|
|
320
|
+
return text;
|
|
321
|
+
};
|
|
322
|
+
const parser = {
|
|
323
|
+
...parsers$1[name],
|
|
324
|
+
astFormat: name === "json-stringify" ? parsers$1[name].astFormat : PLUGIN_ESTREE_FORMAT,
|
|
325
|
+
parse,
|
|
326
|
+
preprocess
|
|
327
|
+
};
|
|
328
|
+
const resolvePriorParser = createPriorParserResolver(name, parsers$1[name].astFormat, parser);
|
|
329
|
+
return markParserAsOwn(parser);
|
|
330
|
+
}
|
|
331
|
+
const parsers = {
|
|
332
|
+
"json": createParser("json"),
|
|
333
|
+
"json-stringify": createParser("json-stringify"),
|
|
334
|
+
"jsonc": createParser("jsonc")
|
|
335
|
+
};
|
|
336
|
+
const printers = {
|
|
337
|
+
[PLUGIN_ESTREE_FORMAT]: {
|
|
338
|
+
...estreePrinters.estree,
|
|
339
|
+
print: printJSON
|
|
340
|
+
},
|
|
341
|
+
"estree-json": estreePrinters["estree-json"]
|
|
342
|
+
};
|
|
343
|
+
const options = {
|
|
344
|
+
...estreeOptions,
|
|
345
|
+
jsonCollectionWrap: {
|
|
346
|
+
category: "JSON",
|
|
347
|
+
description: "Choose how non-empty arrays and objects wrap.",
|
|
348
|
+
type: "choice",
|
|
349
|
+
default: "inherit",
|
|
350
|
+
choices: [
|
|
351
|
+
{
|
|
352
|
+
description: "Use one line when the collection fits.",
|
|
353
|
+
value: "collapse"
|
|
354
|
+
},
|
|
355
|
+
{
|
|
356
|
+
description: "Put each item or property on its own line.",
|
|
357
|
+
value: "expand"
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
description: "Follow Prettier’s wrapping behavior for the current parser.",
|
|
361
|
+
value: "inherit"
|
|
362
|
+
}
|
|
363
|
+
]
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
export { options, parsers, printers };
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-json-jsonc",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Additional JSON and JSONC formatting options for Prettier",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Dom Porada",
|
|
8
|
+
"email": "dom@dom.engineering",
|
|
9
|
+
"url": "https://dom.engineering"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/porada/prettier-plugin-json-jsonc.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"formatting",
|
|
17
|
+
"json",
|
|
18
|
+
"jsonc",
|
|
19
|
+
"prettier",
|
|
20
|
+
"prettier-plugin",
|
|
21
|
+
"prettier-plugin-expand-json",
|
|
22
|
+
"prettier-plugin-json",
|
|
23
|
+
"prettier-plugin-jsonc"
|
|
24
|
+
],
|
|
25
|
+
"files": [
|
|
26
|
+
"dist/**"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"exports": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"prettier": ">=3"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@standard-config/oxlint": "^2.14.0",
|
|
40
|
+
"@standard-config/oxlint-stylistic": "^2.14.0",
|
|
41
|
+
"@standard-config/prettier": "^1.23.1",
|
|
42
|
+
"@standard-config/tsconfig": "^3.2.0",
|
|
43
|
+
"@vitest/coverage-v8": "5.1.2",
|
|
44
|
+
"prettier": "^3.10.2",
|
|
45
|
+
"prettier-plugin-sort-json": "^4.3.0",
|
|
46
|
+
"publint": "^0.3.25",
|
|
47
|
+
"typescript": "^6.0.3",
|
|
48
|
+
"vite-plus": "^1.9.0"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "vp pack",
|
|
52
|
+
"fix": "pnpm build && pnpm format && pnpm lint && pnpm format",
|
|
53
|
+
"format": "prettier --ignore-unknown --log-level=warn --write .",
|
|
54
|
+
"format:check": "prettier --check --ignore-unknown .",
|
|
55
|
+
"lint": "vp lint --fix",
|
|
56
|
+
"lint:check": "vp lint --deny-warnings --type-check",
|
|
57
|
+
"postprepare": "pnpm build",
|
|
58
|
+
"test": "vp test",
|
|
59
|
+
"typecheck": "tsc --noEmit"
|
|
60
|
+
}
|
|
6
61
|
}
|