prettier-plugin-expand-json 1.0.0 → 1.0.2
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/README.md +8 -4
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +18 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +14 -11
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
# prettier-plugin-expand-json
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Expand JSON arrays and objects into multi-line notation with Prettier—for JSON and JSONC files.
|
|
8
8
|
|
|
9
9
|
## Example
|
|
10
10
|
|
|
@@ -12,9 +12,9 @@ Prettier plugin that expands all JSON arrays and objects into multi-line notatio
|
|
|
12
12
|
|
|
13
13
|
```json
|
|
14
14
|
{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
"extends": "@standard-config/tsconfig",
|
|
16
|
+
"compilerOptions": { "exactOptionalPropertyTypes": true },
|
|
17
|
+
"files": ["src/index.ts", "src/index.d.ts"]
|
|
18
18
|
}
|
|
19
19
|
```
|
|
20
20
|
|
|
@@ -90,3 +90,7 @@ If you’re using multiple JSON-related plugins, make sure `prettier-plugin-expa
|
|
|
90
90
|
## Recommended
|
|
91
91
|
|
|
92
92
|
Try [**@standard-config/prettier**](https://github.com/standard-config/prettier) if you’re looking for a consistent, TypeScript-first Prettier config. It comes with this plugin pre-configured.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
MIT © [Dom Porada](https://dom.engineering)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;cA0Ga,OAAA,EAAS,MAAA;AAAA,cAUT,QAAA,EAAU,MAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -5,12 +5,12 @@ import { printers as printers$1 } from "prettier/plugins/estree";
|
|
|
5
5
|
//#region src/index.ts
|
|
6
6
|
function createParser(name) {
|
|
7
7
|
const parse = async (text, options) => {
|
|
8
|
-
const priorParser = findPriorParser(name, options, parse
|
|
9
|
-
return typeof priorParser?.parse === "function" ? await priorParser.parse(text, options) : parsers$1[name].parse(text, options);
|
|
8
|
+
const priorParser = findPriorParser(name, options, parse);
|
|
9
|
+
return typeof priorParser?.parse === "function" ? await priorParser.parse(text, omitCurrentParser(name, options, parse)) : parsers$1[name].parse(text, options);
|
|
10
10
|
};
|
|
11
11
|
const preprocess = async (text, options) => {
|
|
12
|
-
const priorParser = findPriorParser(name, options, parse
|
|
13
|
-
if (typeof priorParser?.preprocess === "function") text = await priorParser.preprocess(text, options);
|
|
12
|
+
const priorParser = findPriorParser(name, options, parse);
|
|
13
|
+
if (typeof priorParser?.preprocess === "function") text = await priorParser.preprocess(text, omitCurrentParser(name, options, parse));
|
|
14
14
|
const edits = format(text, void 0, {
|
|
15
15
|
insertSpaces: !options.useTabs,
|
|
16
16
|
tabSize: options.tabWidth
|
|
@@ -24,18 +24,28 @@ function createParser(name) {
|
|
|
24
24
|
preprocess
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
-
function findPriorParser(name, options,
|
|
28
|
-
const
|
|
29
|
-
for (const plugin of plugins.toReversed()) {
|
|
27
|
+
function findPriorParser(name, options, currentParse) {
|
|
28
|
+
for (const plugin of options.plugins.toReversed()) {
|
|
30
29
|
if (!isParserPlugin(plugin)) continue;
|
|
31
30
|
const parser = plugin.parsers[name];
|
|
32
|
-
if (parser && parser.parse !==
|
|
31
|
+
if (parser && parser.parse !== currentParse) return parser;
|
|
33
32
|
}
|
|
34
33
|
}
|
|
35
34
|
function isParserPlugin(plugin) {
|
|
35
|
+
/* v8 ignore if -- @preserve */
|
|
36
36
|
if (!plugin) return false;
|
|
37
37
|
return typeof plugin === "object" && Object.hasOwn(plugin, "parsers");
|
|
38
38
|
}
|
|
39
|
+
function omitCurrentParser(name, options, currentParse) {
|
|
40
|
+
return {
|
|
41
|
+
...options,
|
|
42
|
+
plugins: options.plugins.filter((plugin) => {
|
|
43
|
+
if (!isParserPlugin(plugin)) return true;
|
|
44
|
+
const parser = plugin.parsers[name];
|
|
45
|
+
return !(parser && parser.parse === currentParse);
|
|
46
|
+
})
|
|
47
|
+
};
|
|
48
|
+
}
|
|
39
49
|
const parsers = {
|
|
40
50
|
"json": createParser("json"),
|
|
41
51
|
"json-stringify": createParser("json-stringify"),
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["babelParsers","estreePrinters"],"sources":["../src/index.ts"],"sourcesContent":["import type { Parser, ParserOptions, Plugin } from 'prettier';\nimport { applyEdits, format } from 'jsonc-parser';\nimport { parsers as babelParsers } from 'prettier/plugins/babel';\nimport { printers as estreePrinters } from 'prettier/plugins/estree';\n\ntype ParserName =
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["babelParsers","estreePrinters"],"sources":["../src/index.ts"],"sourcesContent":["import type { Parser, ParserOptions, Plugin } from 'prettier';\nimport { applyEdits, format } from 'jsonc-parser';\nimport { parsers as babelParsers } from 'prettier/plugins/babel';\nimport { printers as estreePrinters } from 'prettier/plugins/estree';\n\ntype ParserName = 'json' | 'json-stringify' | 'jsonc';\n\ntype ParserPlugin = Omit<Plugin, 'parsers'> & {\n\tparsers: NonNullable<Plugin['parsers']>;\n};\n\nfunction createParser(name: ParserName): Parser {\n\tconst parse: Parser['parse'] = async (\n\t\ttext: string,\n\t\toptions: ParserOptions\n\t) => {\n\t\tconst priorParser = findPriorParser(name, options, parse);\n\n\t\t/* oxlint-disable-next-line typescript/no-unsafe-return */\n\t\treturn typeof priorParser?.parse === 'function'\n\t\t\t? await priorParser.parse(\n\t\t\t\t\ttext,\n\t\t\t\t\tomitCurrentParser(name, options, parse)\n\t\t\t\t)\n\t\t\t: babelParsers[name].parse(text, options);\n\t};\n\n\tconst preprocess: NonNullable<Parser['preprocess']> = async (\n\t\ttext: string,\n\t\toptions: ParserOptions\n\t) => {\n\t\tconst priorParser = findPriorParser(name, options, parse);\n\n\t\tif (typeof priorParser?.preprocess === 'function') {\n\t\t\t/* oxlint-disable-next-line eslint/no-param-reassign */\n\t\t\ttext = await priorParser.preprocess(\n\t\t\t\ttext,\n\t\t\t\tomitCurrentParser(name, options, parse)\n\t\t\t);\n\t\t}\n\n\t\t// This is where the actual expansion happens\n\t\tconst edits = format(text, undefined, {\n\t\t\tinsertSpaces: !options.useTabs,\n\t\t\ttabSize: options.tabWidth,\n\t\t});\n\n\t\treturn applyEdits(text, edits);\n\t};\n\n\treturn {\n\t\t...babelParsers[name],\n\t\tastFormat: 'estree-json',\n\t\tparse,\n\t\tpreprocess,\n\t};\n}\n\nfunction findPriorParser(\n\tname: ParserName,\n\toptions: ParserOptions,\n\tcurrentParse: Parser['parse']\n): Parser | undefined {\n\tfor (const plugin of options.plugins.toReversed()) {\n\t\tif (!isParserPlugin(plugin)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst parser = plugin.parsers[name];\n\n\t\tif (parser && parser.parse !== currentParse) {\n\t\t\treturn parser;\n\t\t}\n\t}\n\n\t/* v8 ignore next -- @preserve */\n\treturn undefined;\n}\n\nfunction isParserPlugin(plugin: unknown): plugin is ParserPlugin {\n\t/* v8 ignore if -- @preserve */\n\tif (!plugin) {\n\t\treturn false;\n\t}\n\n\treturn typeof plugin === 'object' && Object.hasOwn(plugin, 'parsers');\n}\n\nfunction omitCurrentParser(\n\tname: ParserName,\n\toptions: ParserOptions,\n\tcurrentParse: Parser['parse']\n): ParserOptions {\n\treturn {\n\t\t...options,\n\t\tplugins: options.plugins.filter((plugin) => {\n\t\t\tif (!isParserPlugin(plugin)) {\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\tconst parser = plugin.parsers[name];\n\t\t\treturn !(parser && parser.parse === currentParse);\n\t\t}),\n\t};\n}\n\nexport const parsers: Plugin['parsers'] = {\n\t'json': createParser('json'),\n\t'json-stringify': createParser('json-stringify'),\n\t'jsonc': createParser('jsonc'),\n};\n\n// Necessary for comment support in JSONC files\nconst { canAttachComment, isBlockComment, printComment } =\n\testreePrinters.estree;\n\nexport const printers: Plugin['printers'] = {\n\t'estree-json': {\n\t\t...estreePrinters['estree-json'],\n\t\tcanAttachComment,\n\t\tisBlockComment,\n\t\tprintComment,\n\t},\n};\n"],"mappings":";;;;;AAWA,SAAS,aAAa,MAA0B;CAC/C,MAAM,QAAyB,OAC9B,MACA,YACI;EACJ,MAAM,cAAc,gBAAgB,MAAM,SAAS,MAAM;AAGzD,SAAO,OAAO,aAAa,UAAU,aAClC,MAAM,YAAY,MAClB,MACA,kBAAkB,MAAM,SAAS,MAAM,CACvC,GACAA,UAAa,MAAM,MAAM,MAAM,QAAQ;;CAG3C,MAAM,aAAgD,OACrD,MACA,YACI;EACJ,MAAM,cAAc,gBAAgB,MAAM,SAAS,MAAM;AAEzD,MAAI,OAAO,aAAa,eAAe,WAEtC,QAAO,MAAM,YAAY,WACxB,MACA,kBAAkB,MAAM,SAAS,MAAM,CACvC;EAIF,MAAM,QAAQ,OAAO,MAAM,QAAW;GACrC,cAAc,CAAC,QAAQ;GACvB,SAAS,QAAQ;GACjB,CAAC;AAEF,SAAO,WAAW,MAAM,MAAM;;AAG/B,QAAO;EACN,GAAGA,UAAa;EAChB,WAAW;EACX;EACA;EACA;;AAGF,SAAS,gBACR,MACA,SACA,cACqB;AACrB,MAAK,MAAM,UAAU,QAAQ,QAAQ,YAAY,EAAE;AAClD,MAAI,CAAC,eAAe,OAAO,CAC1B;EAGD,MAAM,SAAS,OAAO,QAAQ;AAE9B,MAAI,UAAU,OAAO,UAAU,aAC9B,QAAO;;;AAQV,SAAS,eAAe,QAAyC;;AAEhE,KAAI,CAAC,OACJ,QAAO;AAGR,QAAO,OAAO,WAAW,YAAY,OAAO,OAAO,QAAQ,UAAU;;AAGtE,SAAS,kBACR,MACA,SACA,cACgB;AAChB,QAAO;EACN,GAAG;EACH,SAAS,QAAQ,QAAQ,QAAQ,WAAW;AAC3C,OAAI,CAAC,eAAe,OAAO,CAC1B,QAAO;GAGR,MAAM,SAAS,OAAO,QAAQ;AAC9B,UAAO,EAAE,UAAU,OAAO,UAAU;IACnC;EACF;;AAGF,MAAa,UAA6B;CACzC,QAAQ,aAAa,OAAO;CAC5B,kBAAkB,aAAa,iBAAiB;CAChD,SAAS,aAAa,QAAQ;CAC9B;AAGD,MAAM,EAAE,kBAAkB,gBAAgB,iBACzCC,WAAe;AAEhB,MAAa,WAA+B,EAC3C,eAAe;CACd,GAAGA,WAAe;CAClB;CACA;CACA;CACA,EACD"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-expand-json",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Expand JSON arrays and objects into multi-line notation with Prettier",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Dom Porada",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"exports": "./dist/index.mjs",
|
|
28
28
|
"types": "./dist/index.d.mts",
|
|
29
29
|
"engines": {
|
|
30
|
-
"node": ">=
|
|
30
|
+
"node": ">=20"
|
|
31
31
|
},
|
|
32
32
|
"packageManager": "pnpm@10.28.1",
|
|
33
33
|
"dependencies": {
|
|
@@ -37,26 +37,29 @@
|
|
|
37
37
|
"prettier": ">=3"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@standard-config/
|
|
40
|
+
"@standard-config/eslint": "1.0.0",
|
|
41
|
+
"@standard-config/prettier": "1.5.0",
|
|
41
42
|
"@standard-config/tsconfig": "2.0.0",
|
|
42
|
-
"@vitest/coverage-v8": "4.0.
|
|
43
|
+
"@vitest/coverage-v8": "4.0.18",
|
|
44
|
+
"eslint": "9.39.2",
|
|
43
45
|
"husky": "9.1.7",
|
|
46
|
+
"jiti": "2.6.1",
|
|
44
47
|
"oxlint": "1.41.0",
|
|
45
48
|
"oxlint-tsgolint": "0.11.1",
|
|
46
|
-
"prettier": "3.8.
|
|
49
|
+
"prettier": "3.8.1",
|
|
47
50
|
"prettier-plugin-sort-json": "4.2.0",
|
|
48
|
-
"publint": "0.3.
|
|
49
|
-
"tsdown": "0.
|
|
51
|
+
"publint": "0.3.17",
|
|
52
|
+
"tsdown": "0.20.1",
|
|
50
53
|
"typescript": "5.9.3",
|
|
51
|
-
"vitest": "4.0.
|
|
54
|
+
"vitest": "4.0.18"
|
|
52
55
|
},
|
|
53
56
|
"scripts": {
|
|
54
57
|
"build": "tsdown",
|
|
55
58
|
"fix": "pnpm format && pnpm lint",
|
|
56
59
|
"format": "prettier --write --ignore-unknown .",
|
|
57
60
|
"format:check": "prettier --check --ignore-unknown .",
|
|
58
|
-
"lint": "oxlint --fix --type-aware --type-check --deny-warnings --report-unused-disable-directives",
|
|
59
|
-
"lint:check": "oxlint --type-aware --type-check --deny-warnings --report-unused-disable-directives",
|
|
61
|
+
"lint": "oxlint --fix --type-aware --type-check --deny-warnings --report-unused-disable-directives && eslint . --fix",
|
|
62
|
+
"lint:check": "oxlint --type-aware --type-check --deny-warnings --report-unused-disable-directives && eslint .",
|
|
60
63
|
"prepack": "pnpm run '/^(format:check|lint:check|test|typecheck)$/' && pnpm build",
|
|
61
64
|
"prepare": "husky",
|
|
62
65
|
"test": "vitest run",
|