prettier-plugin-expand-json 1.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 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 ADDED
@@ -0,0 +1,92 @@
1
+ [![](https://img.shields.io/npm/v/prettier-plugin-expand-json)](https://www.npmjs.com/package/prettier-plugin-expand-json)
2
+ [![](https://img.shields.io/github/actions/workflow/status/porada/prettier-plugin-expand-json/test.yaml)](https://github.com/porada/prettier-plugin-expand-json/actions/workflows/test.yaml)
3
+ [![](https://img.shields.io/codecov/c/github/porada/prettier-plugin-expand-json)](https://codecov.io/github/porada/prettier-plugin-expand-json)
4
+
5
+ # prettier-plugin-expand-json
6
+
7
+ Prettier plugin that expands all JSON arrays and objects into multi-line notation. Works with both JSON and JSONC files.
8
+
9
+ ## Example
10
+
11
+ <!-- prettier-ignore-start -->
12
+
13
+ ```json
14
+ {
15
+ "extends": "@standard-config/tsconfig",
16
+ "compilerOptions": { "exactOptionalPropertyTypes": true },
17
+ "files": ["src/index.ts", "src/index.d.ts"]
18
+ }
19
+ ```
20
+
21
+ <!-- prettier-ignore-end -->
22
+
23
+ …will always be formatted as:
24
+
25
+ ```json
26
+ {
27
+ "extends": "@standard-config/tsconfig",
28
+ "compilerOptions": {
29
+ "exactOptionalPropertyTypes": true
30
+ },
31
+ "files": [
32
+ "src/index.ts",
33
+ "src/index.d.ts"
34
+ ]
35
+ }
36
+ ```
37
+
38
+ ## Install
39
+
40
+ ```sh
41
+ npm install --save-dev prettier-plugin-expand-json
42
+ ```
43
+
44
+ ```sh
45
+ pnpm add --save-dev prettier-plugin-expand-json
46
+ ```
47
+
48
+ ## Usage
49
+
50
+ Reference `prettier-plugin-expand-json` in your [Prettier config](https://prettier.io/docs/configuration):
51
+
52
+ ```json
53
+ {
54
+ "plugins": [
55
+ "prettier-plugin-expand-json"
56
+ ]
57
+ }
58
+ ```
59
+
60
+ If you’re using multiple JSON-related plugins, make sure `prettier-plugin-expand-json` is listed last. This applies to each `overrides` entry as well.
61
+
62
+ ```json
63
+ {
64
+ "plugins": [
65
+ "prettier-plugin-sort-json",
66
+ "prettier-plugin-expand-json"
67
+ ]
68
+ }
69
+ ```
70
+
71
+ ```json
72
+ {
73
+ "plugins": [
74
+ "prettier-plugin-expand-json"
75
+ ],
76
+ "overrides": [
77
+ {
78
+ "files": "packages/**/package.json",
79
+ "options": {
80
+ "plugins": [
81
+ "prettier-plugin-pkg",
82
+ "prettier-plugin-expand-json"
83
+ ]
84
+ }
85
+ }
86
+ ]
87
+ }
88
+ ```
89
+
90
+ ## Recommended
91
+
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.
@@ -0,0 +1,8 @@
1
+ import { Plugin } from "prettier";
2
+
3
+ //#region src/index.d.ts
4
+ declare const parsers: Plugin['parsers'];
5
+ declare const printers: Plugin['printers'];
6
+ //#endregion
7
+ export { parsers, printers };
8
+ //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs ADDED
@@ -0,0 +1,54 @@
1
+ import { applyEdits, format } from "jsonc-parser";
2
+ import { parsers as parsers$1 } from "prettier/plugins/babel";
3
+ import { printers as printers$1 } from "prettier/plugins/estree";
4
+
5
+ //#region src/index.ts
6
+ function createParser(name) {
7
+ const parse = async (text, options) => {
8
+ const priorParser = findPriorParser(name, options, parse, preprocess);
9
+ return typeof priorParser?.parse === "function" ? await priorParser.parse(text, options) : parsers$1[name].parse(text, options);
10
+ };
11
+ const preprocess = async (text, options) => {
12
+ const priorParser = findPriorParser(name, options, parse, preprocess);
13
+ if (typeof priorParser?.preprocess === "function") text = await priorParser.preprocess(text, options);
14
+ const edits = format(text, void 0, {
15
+ insertSpaces: !options.useTabs,
16
+ tabSize: options.tabWidth
17
+ });
18
+ return applyEdits(text, edits);
19
+ };
20
+ return {
21
+ ...parsers$1[name],
22
+ astFormat: "estree-json",
23
+ parse,
24
+ preprocess
25
+ };
26
+ }
27
+ function findPriorParser(name, options, ownParser, ownPreprocessor) {
28
+ const plugins = options.plugins ?? [];
29
+ for (const plugin of plugins.toReversed()) {
30
+ if (!isParserPlugin(plugin)) continue;
31
+ const parser = plugin.parsers[name];
32
+ if (parser && parser.parse !== ownParser && parser.preprocess !== ownPreprocessor) return parser;
33
+ }
34
+ }
35
+ function isParserPlugin(plugin) {
36
+ if (!plugin) return false;
37
+ return typeof plugin === "object" && Object.hasOwn(plugin, "parsers");
38
+ }
39
+ const parsers = {
40
+ "json": createParser("json"),
41
+ "json-stringify": createParser("json-stringify"),
42
+ "jsonc": createParser("jsonc")
43
+ };
44
+ const { canAttachComment, isBlockComment, printComment } = printers$1.estree;
45
+ const printers = { "estree-json": {
46
+ ...printers$1["estree-json"],
47
+ canAttachComment,
48
+ isBlockComment,
49
+ printComment
50
+ } };
51
+
52
+ //#endregion
53
+ export { parsers, printers };
54
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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 = keyof typeof babelParsers;\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, preprocess);\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(text, options)\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, preprocess);\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(text, options);\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\townParser: Parser['parse'],\n\townPreprocessor: Parser['preprocess']\n): Parser | undefined {\n\tconst plugins = options.plugins ?? [];\n\n\tfor (const plugin of 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 (\n\t\t\tparser &&\n\t\t\tparser.parse !== ownParser &&\n\t\t\tparser.preprocess !== ownPreprocessor\n\t\t) {\n\t\t\treturn parser;\n\t\t}\n\t}\n\n\treturn undefined;\n}\n\nfunction isParserPlugin(plugin: unknown): plugin is ParserPlugin {\n\tif (!plugin) {\n\t\treturn false;\n\t}\n\n\treturn typeof plugin === 'object' && Object.hasOwn(plugin, 'parsers');\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,OAAO,WAAW;AAGrE,SAAO,OAAO,aAAa,UAAU,aAClC,MAAM,YAAY,MAAM,MAAM,QAAQ,GACtCA,UAAa,MAAM,MAAM,MAAM,QAAQ;;CAG3C,MAAM,aAAgD,OACrD,MACA,YACI;EACJ,MAAM,cAAc,gBAAgB,MAAM,SAAS,OAAO,WAAW;AAErE,MAAI,OAAO,aAAa,eAAe,WAEtC,QAAO,MAAM,YAAY,WAAW,MAAM,QAAQ;EAInD,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,WACA,iBACqB;CACrB,MAAM,UAAU,QAAQ,WAAW,EAAE;AAErC,MAAK,MAAM,UAAU,QAAQ,YAAY,EAAE;AAC1C,MAAI,CAAC,eAAe,OAAO,CAC1B;EAGD,MAAM,SAAS,OAAO,QAAQ;AAE9B,MACC,UACA,OAAO,UAAU,aACjB,OAAO,eAAe,gBAEtB,QAAO;;;AAOV,SAAS,eAAe,QAAyC;AAChE,KAAI,CAAC,OACJ,QAAO;AAGR,QAAO,OAAO,WAAW,YAAY,OAAO,OAAO,QAAQ,UAAU;;AAGtE,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 ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "prettier-plugin-expand-json",
3
+ "version": "1.0.0",
4
+ "description": "Prettier plugin that expands JSON arrays and objects into multi-line notation",
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-expand-json.git"
14
+ },
15
+ "keywords": [
16
+ "formatting",
17
+ "json",
18
+ "jsonc",
19
+ "prettier",
20
+ "prettier-plugin"
21
+ ],
22
+ "files": [
23
+ "dist/**"
24
+ ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "exports": "./dist/index.mjs",
28
+ "types": "./dist/index.d.mts",
29
+ "engines": {
30
+ "node": ">=22"
31
+ },
32
+ "packageManager": "pnpm@10.28.1",
33
+ "dependencies": {
34
+ "jsonc-parser": "^3.3.1"
35
+ },
36
+ "peerDependencies": {
37
+ "prettier": ">=3"
38
+ },
39
+ "devDependencies": {
40
+ "@standard-config/prettier": "1.3.0",
41
+ "@standard-config/tsconfig": "2.0.0",
42
+ "@vitest/coverage-v8": "4.0.17",
43
+ "husky": "9.1.7",
44
+ "oxlint": "1.41.0",
45
+ "oxlint-tsgolint": "0.11.1",
46
+ "prettier": "3.8.0",
47
+ "prettier-plugin-sort-json": "4.2.0",
48
+ "publint": "0.3.16",
49
+ "tsdown": "0.19.0",
50
+ "typescript": "5.9.3",
51
+ "vitest": "4.0.17"
52
+ },
53
+ "scripts": {
54
+ "build": "tsdown",
55
+ "fix": "pnpm format && pnpm lint",
56
+ "format": "prettier --write --ignore-unknown .",
57
+ "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",
60
+ "prepack": "pnpm run '/^(format:check|lint:check|test|typecheck)$/' && pnpm build",
61
+ "prepare": "husky",
62
+ "test": "vitest run",
63
+ "typecheck": "tsc --noEmit"
64
+ }
65
+ }