Skip to content

Commit 79d8ecf

Browse files
author
Stan Wayne
committed
Compact yet pretty json for Vintage Story
1 parent 0358481 commit 79d8ecf

File tree

7 files changed

+116
-149
lines changed

7 files changed

+116
-149
lines changed

README.md

Lines changed: 7 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,12 @@
1-
[![Prettier Banner](https://unpkg.com/prettier[email protected]/images/prettier-banner-light.svg)](https://prettier.io)
1+
This version of prettier produces more compact JSON that is still readable by using one-line objects when it is not longer than 'printWidth' in config (80 by default).
22

3-
<h2 align="center">Opinionated Code Formatter</h2>
3+
I probably broke a few core principles of prettier here, but whatever, the intended usage is limited to modding Vintage Story, so it's easier to work with JSON files there.
44

5-
<p align="center">
6-
<em>
7-
JavaScript
8-
· TypeScript
9-
· Flow
10-
· JSX
11-
· JSON
12-
</em>
13-
<br />
14-
<em>
15-
CSS
16-
· SCSS
17-
· Less
18-
</em>
19-
<br />
20-
<em>
21-
HTML
22-
· Vue
23-
· Angular
24-
</em>
25-
<br />
26-
<em>
27-
GraphQL
28-
· Markdown
29-
· YAML
30-
</em>
31-
<br />
32-
<em>
33-
<a href="https://prettier.io/docs/en/plugins.html">
34-
Your favorite language?
35-
</a>
36-
</em>
37-
</p>
5+
It does two runs for formatting:
6+
1. Default prettier without any changes.
7+
2. Custom check for current node length. If it's less than printWidth - we print the node using basic JSON.stringify with some spaces to keep it readable.
388

39-
<p align="center">
40-
<a href="https://github.com/prettier/prettier/actions?query=workflow%3AProd+branch%3Amain">
41-
<img alt="Github Actions Build Status" src="https://img.shields.io/github/actions/workflow/status/prettier/prettier/prod-test.yml?label=Prod&style=flat-square"></a>
42-
<a href="https://github.com/prettier/prettier/actions?query=workflow%3ADev+branch%3Amain">
43-
<img alt="Github Actions Build Status" src="https://img.shields.io/github/actions/workflow/status/prettier/prettier/dev-test.yml?label=Dev&style=flat-square"></a>
44-
<a href="https://github.com/prettier/prettier/actions?query=workflow%3ALint+branch%3Amain">
45-
<img alt="Github Actions Build Status" src="https://img.shields.io/github/actions/workflow/status/prettier/prettier/lint.yml?label=Lint&style=flat-square"></a>
46-
<a href="https://codecov.io/gh/prettier/prettier">
47-
<img alt="Codecov Coverage Status" src="https://img.shields.io/codecov/c/github/prettier/prettier.svg?style=flat-square"></a>
48-
<a href="https://twitter.com/acdlite/status/974390255393505280">
49-
<img alt="Blazing Fast" src="https://img.shields.io/badge/speed-blazing%20%F0%9F%94%A5-brightgreen.svg?style=flat-square"></a>
50-
<br/>
51-
<a href="https://www.npmjs.com/package/prettier">
52-
<img alt="npm version" src="https://img.shields.io/npm/v/prettier.svg?style=flat-square"></a>
53-
<a href="https://www.npmjs.com/package/prettier">
54-
<img alt="weekly downloads from npm" src="https://img.shields.io/npm/dw/prettier.svg?style=flat-square"></a>
55-
<a href="#badge">
56-
<img alt="code style: prettier" src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"></a>
57-
<a href="https://twitter.com/PrettierCode">
58-
<img alt="Follow Prettier on Twitter" src="https://img.shields.io/badge/%40PrettierCode-9f9f9f?style=flat-square&logo=x&labelColor=555"></a>
59-
</p>
9+
Let me know if you have any ideas or if something doesn't work.
6010

61-
## Intro
11+
Enjoy!
6212

63-
Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
64-
65-
### Input
66-
67-
<!-- prettier-ignore -->
68-
```js
69-
foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
70-
```
71-
72-
### Output
73-
74-
```js
75-
foo(
76-
reallyLongArg(),
77-
omgSoManyParameters(),
78-
IShouldRefactorThis(),
79-
isThereSeriouslyAnotherOne(),
80-
);
81-
```
82-
83-
Prettier can be run [in your editor](https://prettier.io/docs/en/editors.html) on-save, in a [pre-commit hook](https://prettier.io/docs/en/precommit.html), or in [CI environments](https://prettier.io/docs/en/cli.html#list-different) to ensure your codebase has a consistent style without devs ever having to post a nit-picky comment on a code review ever again!
84-
85-
---
86-
87-
**[Documentation](https://prettier.io/docs/en/)**
88-
89-
<!-- prettier-ignore -->
90-
[Install](https://prettier.io/docs/en/install.html) ·
91-
[Options](https://prettier.io/docs/en/options.html) ·
92-
[CLI](https://prettier.io/docs/en/cli.html) ·
93-
[API](https://prettier.io/docs/en/api.html)
94-
95-
**[Playground](https://prettier.io/playground/)**
96-
97-
---
98-
99-
## Badge
100-
101-
Show the world you're using _Prettier_[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
102-
103-
```md
104-
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
105-
```
106-
107-
## Contributing
108-
109-
See [CONTRIBUTING.md](CONTRIBUTING.md).

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
{
22
"name": "prettier",
3-
"version": "3.3.0-dev",
4-
"description": "Prettier is an opinionated code formatter",
3+
"version": "3.3.0-vintage-story",
4+
"description": "Custom version of prettier to produce pretty yet compact json files. Meant to be used only with json while modding Vintage Story.",
55
"bin": "./bin/prettier.cjs",
6-
"repository": "prettier/prettier",
7-
"funding": "https://github.com/prettier/prettier?sponsor=1",
8-
"homepage": "https://prettier.io",
9-
"author": "James Long",
6+
"repository": "rhoun/prettier-vs",
7+
"homepage": "https://github.com/rhoun/prettier-vs",
8+
"author": "Rhoun",
109
"type": "module",
1110
"license": "MIT",
1211
"main": "./src/index.cjs",

src/language-js/print/estree.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
startsWithNoLookaheadToken,
2626
} from "../utils/index.js";
2727
import isBlockComment from "../utils/is-block-comment.js";
28+
import { customPrint, textLength } from "../vs-customs.js";
2829
import { printArray } from "./array.js";
2930
import { printArrowFunction } from "./arrow-function.js";
3031
import {
@@ -83,15 +84,26 @@ import { printTypeAnnotationProperty } from "./type-annotation.js";
8384
* @param {*} options
8485
* @param {*} print
8586
* @param {*} [args]
87+
* @param text raw file text
8688
* @returns {Doc}
8789
*/
88-
function printEstree(path, options, print, args) {
90+
function printEstree(path, options, print, args, text = null) {
8991
const { node } = path;
9092

9193
if (isLiteral(node)) {
9294
return printLiteral(path, options);
9395
}
9496

97+
if (
98+
node &&
99+
text &&
100+
node?.type !== "ArrayExpression" &&
101+
textLength(text, node.start, node.end) <=
102+
options.printWidth - (path.ancestors?.length ?? 0)
103+
) {
104+
return customPrint(node, text);
105+
}
106+
95107
const semi = options.semi ? ";" : "";
96108
/** @type{Doc[]} */
97109
let parts = [];
@@ -150,8 +162,8 @@ function printEstree(path, options, print, args) {
150162

151163
parts.push(
152164
group(
153-
indent([softline, printBindExpressionCallee(path, options, print)]),
154-
),
165+
indent([softline, printBindExpressionCallee(path, options, print)])
166+
)
155167
);
156168

157169
return parts;
@@ -199,13 +211,13 @@ function printEstree(path, options, print, args) {
199211
// avoid printing `await (await` on one line
200212
const parentAwaitOrBlock = path.findAncestor(
201213
(node) =>
202-
node.type === "AwaitExpression" || node.type === "BlockStatement",
214+
node.type === "AwaitExpression" || node.type === "BlockStatement"
203215
);
204216
if (
205217
parentAwaitOrBlock?.type !== "AwaitExpression" ||
206218
!startsWithNoLookaheadToken(
207219
parentAwaitOrBlock.argument,
208-
(leftmostNode) => leftmostNode === node,
220+
(leftmostNode) => leftmostNode === node
209221
)
210222
) {
211223
return group(parts);
@@ -305,7 +317,7 @@ function printEstree(path, options, print, args) {
305317

306318
if (hasComment(node.argument)) {
307319
parts.push(
308-
group(["(", indent([softline, print("argument")]), softline, ")"]),
320+
group(["(", indent([softline, print("argument")]), softline, ")"])
309321
);
310322
} else {
311323
parts.push(print("argument"));
@@ -353,7 +365,7 @@ function printEstree(path, options, print, args) {
353365
",",
354366
hasValue && !isParentForLoop ? hardline : line,
355367
p,
356-
]),
368+
])
357369
),
358370
];
359371

@@ -385,7 +397,7 @@ function printEstree(path, options, print, args) {
385397
const commentOnOwnLine =
386398
hasComment(
387399
node.consequent,
388-
CommentCheckFlags.Trailing | CommentCheckFlags.Line,
400+
CommentCheckFlags.Trailing | CommentCheckFlags.Line
389401
) || needsHardlineAfterDanglingComment(node);
390402
const elseOnSameLine =
391403
node.consequent.type === "BlockStatement" && !commentOnOwnLine;
@@ -394,7 +406,7 @@ function printEstree(path, options, print, args) {
394406
if (hasComment(node, CommentCheckFlags.Dangling)) {
395407
parts.push(
396408
printDanglingComments(path, options),
397-
commentOnOwnLine ? hardline : " ",
409+
commentOnOwnLine ? hardline : " "
398410
);
399411
}
400412

@@ -404,9 +416,9 @@ function printEstree(path, options, print, args) {
404416
adjustClause(
405417
node.alternate,
406418
print("alternate"),
407-
node.alternate.type === "IfStatement",
408-
),
409-
),
419+
node.alternate.type === "IfStatement"
420+
)
421+
)
410422
);
411423
}
412424

@@ -490,7 +502,7 @@ function printEstree(path, options, print, args) {
490502
"while (",
491503
group([indent([softline, print("test")]), softline]),
492504
")",
493-
semi,
505+
semi
494506
);
495507

496508
return parts;
@@ -532,7 +544,7 @@ function printEstree(path, options, print, args) {
532544
(comment.trailing &&
533545
hasNewline(options.originalText, locStart(comment), {
534546
backwards: true,
535-
})),
547+
}))
536548
);
537549
const param = print("param");
538550

@@ -566,8 +578,8 @@ function printEstree(path, options, print, args) {
566578
print(),
567579
!isLast && isNextLineEmpty(node, options) ? hardline : "",
568580
],
569-
"cases",
570-
),
581+
"cases"
582+
)
571583
),
572584
])
573585
: "",
@@ -586,7 +598,7 @@ function printEstree(path, options, print, args) {
586598
}
587599

588600
const consequent = node.consequent.filter(
589-
(node) => node.type !== "EmptyStatement",
601+
(node) => node.type !== "EmptyStatement"
590602
);
591603

592604
if (consequent.length > 0) {
@@ -595,7 +607,7 @@ function printEstree(path, options, print, args) {
595607
parts.push(
596608
consequent.length === 1 && consequent[0].type === "BlockStatement"
597609
? [" ", cons]
598-
: indent([hardline, cons]),
610+
: indent([hardline, cons])
599611
);
600612
}
601613

src/language-js/print/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { printTypescript } from "./typescript.js";
1818
* @typedef {import("../../document/builders.js").Doc} Doc
1919
*/
2020

21-
function printWithoutParentheses(path, options, print, args) {
21+
function printWithoutParentheses(path, options, print, args, text) {
2222
if (isIgnored(path)) {
2323
return printIgnored(path, options);
2424
}
@@ -30,7 +30,7 @@ function printWithoutParentheses(path, options, print, args) {
3030
printTypescript,
3131
printEstree,
3232
]) {
33-
const doc = printer(path, options, print, args);
33+
const doc = printer(path, options, print, args, text);
3434
if (doc !== undefined) {
3535
return doc;
3636
}
@@ -58,14 +58,15 @@ const shouldPrintDirectly = createTypeCheckFunction([
5858
* @param {*} options
5959
* @param {*} print
6060
* @param {*} [args]
61+
* @param text raw file text
6162
* @returns {Doc}
6263
*/
63-
function print(path, options, print, args) {
64+
function print(path, options, print, args, text = null) {
6465
if (path.isRoot) {
6566
options.__onHtmlBindingRoot?.(path.node, options);
6667
}
6768

68-
const doc = printWithoutParentheses(path, options, print, args);
69+
const doc = printWithoutParentheses(path, options, print, args, text);
6970
if (!doc) {
7071
return "";
7172
}

src/language-js/vs-customs.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export function customPrint(node, docText) {
2+
const part = docText.slice(node.start, node.end);
3+
4+
if (node.type === "UnaryExpression") {
5+
return part;
6+
}
7+
8+
let result;
9+
try {
10+
if (
11+
(part.startsWith("{") && part.endsWith("}")) ||
12+
(part.startsWith("[") && part.endsWith("]"))
13+
) {
14+
result = JSON.stringify(JSON.parse(part), null, 1);
15+
} else {
16+
result = JSON.stringify(JSON.parse(`{${part}}`), null, 1).slice(2, -2);
17+
}
18+
} catch {
19+
//just so we don't entirely fail if I didn't handle some cases
20+
result = part;
21+
}
22+
23+
result = result
24+
.split("\n")
25+
.map((line) => line.trim())
26+
.join(" ");
27+
28+
return result;
29+
}
30+
31+
export function textLength(text, start, end) {
32+
const extracted = text.slice(start, end);
33+
const formatted = extracted.replaceAll(/\s+/g, " ");
34+
return formatted.length;
35+
}

0 commit comments

Comments
 (0)