Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 96ffee4

Browse files
committed
Add support for prettier 3
We still support prettier2, but we now accept prettier3 as a peer dependency as well. Fixes #87 Fixes #194
1 parent e3a4ae0 commit 96ffee4

File tree

20 files changed

+186
-72
lines changed

20 files changed

+186
-72
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ jobs:
1111
matrix:
1212
os: [windows-latest, ubuntu-latest]
1313
node-version: [16.x, 17.x]
14+
prettier-major: [2, 3]
1415

15-
name: OS ${{ matrix.os }} / NodeJS ${{ matrix.node-version }}
16+
name: OS ${{ matrix.os }} / NodeJS ${{ matrix.node-version }} / Prettier ${{ matrix.prettier-major }}
17+
18+
env:
19+
PRETTIER_MAJOR: ${{ matrix.prettier-major }}
1620

1721
steps:
1822
- uses: actions/checkout@v2

.husky/pre-commit

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
npx pretty-quick --staged --pattern 'src/**/*.ts'
4+
cleanup() {
5+
rm node_modules/prettier
6+
}
7+
8+
# format with prettier2
9+
ln -s ./prettier2 node_modules/prettier
10+
11+
# cleanup when done
12+
trap cleanup EXIT
13+
14+
# pretty-quick on code base
15+
pretty-quick --staged --pattern 'src/**/*.ts'

.mocharc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"require": ["./build/shims", "ts-node/register/transpile-only", "tsconfig-paths/register"],
2+
"require": ["./build/shims", "./test/test-setup", "ts-node/register/transpile-only", "tsconfig-paths/register"],
33
"reporter": "spec"
44
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
"prerelease": "scripts/prerelease",
4141
"prettier": "prettier --plugin . --parser=liquid-html",
4242
"test": "node_modules/.bin/mocha '{src,test}/**/*.spec.ts'",
43+
"test:3": "cross-env PRETTIER_MAJOR=3 yarn test",
4344
"test:idempotence": "cross-env TEST_IDEMPOTENCE=true node_modules/.bin/mocha 'test/**/*.spec.ts'",
45+
"test:idempotence:3": "cross-env PRETTIER_MAJOR=3 yarn test:idempotence",
4446
"type-check": "tsc --noEmit"
4547
},
4648
"peerDependencies": {
@@ -58,8 +60,10 @@
5860
"cross-env": "^7.0.3",
5961
"husky": "^7.0.0",
6062
"mocha": "^9.1.3",
63+
"module-alias": "^2.2.3",
6164
"nyc": "^15.1.0",
62-
"prettier": "^2.6.1",
65+
"prettier2": "npm:prettier@^2.6.1",
66+
"prettier3": "npm:prettier@^3.0.0",
6367
"pretty-quick": "^3.1.3",
6468
"source-map-support": "^0.5.21",
6569
"ts-node": "^10.4.0",

src/index.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {
2-
Plugin,
32
RequiredOptions,
43
SupportLanguage,
54
SupportOptions,
5+
version,
66
} from 'prettier';
7+
import type { Plugin as Plugin2 } from 'prettier';
8+
import type { Plugin as Plugin3 } from 'prettier3';
79
import { parsers, liquidHtmlLanguageName } from '~/parser';
8-
import { printers } from '~/printer';
10+
import { printers2, printers3 } from '~/printer';
911
import { LiquidHtmlNode } from '~/types';
1012

1113
const languages: SupportLanguage[] = [
@@ -50,16 +52,26 @@ const options: SupportOptions = {
5052
},
5153
};
5254

53-
const defaultOptions: Partial<RequiredOptions> = {
55+
const defaultOptions = {
5456
printWidth: 120,
5557
};
5658

57-
const plugin: Plugin<LiquidHtmlNode> = {
59+
const plugin2: Plugin2<LiquidHtmlNode> = {
5860
languages,
59-
parsers,
60-
printers,
61+
parsers: parsers as Plugin2['parsers'],
62+
printers: printers2,
6163
options,
6264
defaultOptions,
6365
};
6466

65-
export = plugin;
67+
const plugin3: Plugin3<LiquidHtmlNode> = {
68+
languages,
69+
parsers: parsers as Plugin3['parsers'],
70+
printers: printers3 as any,
71+
options,
72+
defaultOptions,
73+
};
74+
75+
const prettierMajor = version.split('.')[0]!;
76+
77+
export = prettierMajor === '2' ? plugin2 : plugin3;

src/parser/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
Parsers,
32
liquidHtmlParser,
43
liquidHtmlAstFormat,
54
liquidHtmlLanguageName,
@@ -9,6 +8,6 @@ export * from '~/parser/stage-2-ast';
98

109
export { liquidHtmlLanguageName, liquidHtmlAstFormat };
1110

12-
export const parsers: Parsers = {
11+
export const parsers = {
1312
[liquidHtmlLanguageName]: liquidHtmlParser,
1413
};

src/parser/parser.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
import { Parser, ParserOptions } from 'prettier';
21
import { locEnd, locStart } from '~/utils';
32
import { toLiquidHtmlAST, LiquidHtmlNode } from '~/parser/stage-2-ast';
43

5-
export function parse(
6-
text: string,
7-
_parsers: Parsers,
8-
_opts: ParserOptions<LiquidHtmlNode>,
9-
): LiquidHtmlNode {
4+
export function parse(text: string): LiquidHtmlNode {
105
return toLiquidHtmlAST(text);
116
}
127

138
export const liquidHtmlAstFormat = 'liquid-html-ast';
149

1510
export const liquidHtmlLanguageName = 'liquid-html';
1611

17-
export const liquidHtmlParser: Parser<LiquidHtmlNode> = {
12+
export const liquidHtmlParser = {
1813
parse,
1914
astFormat: liquidHtmlAstFormat,
2015
locStart,
2116
locEnd,
2217
};
23-
24-
export interface Parsers {
25-
[languageName: string]: Parser;
26-
}

src/parser/stage-1-cst.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export interface ConcreteNumberLiteral
438438
export interface ConcreteLiquidLiteral
439439
extends ConcreteBasicNode<ConcreteNodeTypes.LiquidLiteral> {
440440
keyword: keyof typeof LiquidLiteralValues;
441-
value: typeof LiquidLiteralValues[keyof typeof LiquidLiteralValues];
441+
value: (typeof LiquidLiteralValues)[keyof typeof LiquidLiteralValues];
442442
}
443443

444444
export interface ConcreteLiquidRange

src/printer/embed.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Printer, doc } from 'prettier';
1+
import { doc } from 'prettier';
2+
import type { Printer as Printer2 } from 'prettier';
3+
import type { Doc as Doc3, Printer as Printer3 } from 'prettier3';
24
import { RawMarkupKinds } from '~/parser';
35
import { LiquidHtmlNode, LiquidParserOptions, NodeTypes } from '~/types';
46

@@ -13,7 +15,10 @@ export const ParserMap: { [key in RawMarkupKinds]: string | null } = {
1315
[RawMarkupKinds.text]: null,
1416
};
1517

16-
export const embed: Printer<LiquidHtmlNode>['embed'] = (
18+
// Prettier 2 and 3 have a slightly different API for embed.
19+
//
20+
// https://github.com/prettier/prettier/wiki/How-to-migrate-my-plugin-to-support-Prettier-v3%3F
21+
export const embed2: Printer2<LiquidHtmlNode>['embed'] = (
1722
path,
1823
_print,
1924
textToDoc,
@@ -27,7 +32,8 @@ export const embed: Printer<LiquidHtmlNode>['embed'] = (
2732
return doc.utils.stripTrailingHardline(
2833
textToDoc(node.value, {
2934
...options,
30-
singleQuote: (options as LiquidParserOptions).embeddedSingleQuote,
35+
singleQuote: (options as any as LiquidParserOptions)
36+
.embeddedSingleQuote,
3137
parser,
3238
__embeddedInHtml: true,
3339
}),
@@ -38,3 +44,26 @@ export const embed: Printer<LiquidHtmlNode>['embed'] = (
3844
return null;
3945
}
4046
};
47+
48+
export const embed3: Printer3<LiquidHtmlNode>['embed'] = (path, options) => {
49+
return (textToDoc) => {
50+
const node = path.node as LiquidHtmlNode;
51+
switch (node.type) {
52+
case NodeTypes.RawMarkup: {
53+
const parser = ParserMap[node.kind];
54+
if (parser && node.value.trim() !== '') {
55+
return textToDoc(node.value, {
56+
...options,
57+
singleQuote: (options as LiquidParserOptions).embeddedSingleQuote,
58+
parser,
59+
__embeddedInHtml: true,
60+
}).then((document) =>
61+
doc.utils.stripTrailingHardline(document),
62+
) as Promise<Doc3>;
63+
}
64+
}
65+
default:
66+
return undefined;
67+
}
68+
};
69+
};

src/printer/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { printerLiquidHtml } from '~/printer/printer-liquid-html';
1+
import {
2+
printerLiquidHtml2,
3+
printerLiquidHtml3,
4+
} from '~/printer/printer-liquid-html';
25
import { liquidHtmlAstFormat } from '~/parser';
36

4-
export const printers = {
5-
[liquidHtmlAstFormat]: printerLiquidHtml,
7+
export const printers2 = {
8+
[liquidHtmlAstFormat]: printerLiquidHtml2,
9+
};
10+
11+
export const printers3 = {
12+
[liquidHtmlAstFormat]: printerLiquidHtml3,
613
};

0 commit comments

Comments
 (0)