@ttskch/prettier-plugin-tailwindcss-anywhere 0.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 +21 -0
- package/README.md +113 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.js +35 -0
- package/dist/src/parser.d.ts +10 -0
- package/dist/src/parser.js +28 -0
- package/dist/src/printer.d.ts +2 -0
- package/dist/src/printer.js +9 -0
- package/package.json +55 -0
- package/src/index.ts +63 -0
- package/src/parser.ts +51 -0
- package/src/printer.ts +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Takashi Kanemoto
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# prettier-plugin-tailwindcss-anywhere
|
|
2
|
+
|
|
3
|
+
[](https://github.com/ttskch/prettier-plugin-tailwindcss-anywhere/actions/workflows/ci.yaml?query=branch:main)
|
|
4
|
+
[](https://codecov.io/gh/ttskch/prettier-plugin-tailwindcss-anywhere)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
A Prettier plugin for sorting TailwindCSS classes **in any HTML-like language, like Twig etc**⚡
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
npm install -D prettier \
|
|
14
|
+
prettier-plugin-tailwindcss \
|
|
15
|
+
@ttskch/prettier-plugin-tailwindcss-anywhere
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
This plugin is intended to be used in conjunction with [prettier-plugin-tailwindcss](https://github.com/tailwindlabs/prettier-plugin-tailwindcss).
|
|
21
|
+
|
|
22
|
+
For example, by setting `.prettierrc` as follows, you can sort classes even in files of template engines such as [Twig](https://twig.symfony.com/) that cannot be processed by prettier-plugin-tailwindcss.
|
|
23
|
+
|
|
24
|
+
```json5
|
|
25
|
+
// .prettierrc
|
|
26
|
+
{
|
|
27
|
+
"plugins": ["prettier-plugin-tailwindcss", "@ttskch/prettier-plugin-tailwindcss-anywhere"],
|
|
28
|
+
"overrides": [
|
|
29
|
+
{
|
|
30
|
+
"files": "*.html.twig",
|
|
31
|
+
"options": {
|
|
32
|
+
"parser": "anywhere",
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Before
|
|
40
|
+
|
|
41
|
+
```twig
|
|
42
|
+
{% extends 'base.html.twig %}
|
|
43
|
+
{% block content %}
|
|
44
|
+
<div class="space-y-4 flex-col flex">
|
|
45
|
+
<div class="p-4 rounded-lg bg-blue-500 text-white rounded-lg p-4">
|
|
46
|
+
Hello, {{ name }}!
|
|
47
|
+
</div>
|
|
48
|
+
</div>
|
|
49
|
+
{% endblock %}
|
|
50
|
+
{% endblock %}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### After
|
|
54
|
+
|
|
55
|
+
```diff
|
|
56
|
+
{% extends 'base.html.twig %}
|
|
57
|
+
{% block content %}
|
|
58
|
+
- <div class="space-y-4 flex-col flex">
|
|
59
|
+
+ <div class="flex flex-col space-y-4">
|
|
60
|
+
- <div class="p-4 rounded-lg bg-blue-500 text-white rounded-lg p-4">
|
|
61
|
+
+ <div class="rounded-lg bg-blue-500 p-4 text-white">
|
|
62
|
+
Hello, {{ name }}!
|
|
63
|
+
</div>
|
|
64
|
+
</div>
|
|
65
|
+
{% endblock %}
|
|
66
|
+
{% endblock %}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Options
|
|
70
|
+
|
|
71
|
+
### `regex`
|
|
72
|
+
|
|
73
|
+
The `regex` option allows you to specify the part that corresponds to the value of the class attribute using a regular expression.
|
|
74
|
+
|
|
75
|
+
For example, if you have the following text:
|
|
76
|
+
|
|
77
|
+
```twig
|
|
78
|
+
<div class="space-y-4 flex-col flex {% if foo %}is-foo{% endif %}">
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
In this case, you can specify the following `regex` to sort only the part before `{% if foo %}`.
|
|
82
|
+
|
|
83
|
+
```json5
|
|
84
|
+
{
|
|
85
|
+
"options": {
|
|
86
|
+
"parser": "anywhere",
|
|
87
|
+
"plugins": ["@ttskch/prettier-plugin-tailwindcss-anywhere"],
|
|
88
|
+
"regex": 'class="([^{}"]*)(?:"| {)' // <-- here
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```diff
|
|
94
|
+
- <div class="space-y-4 flex-col flex {% if foo %}is-foo{% endif %}">
|
|
95
|
+
+ <div class="flex flex-col space-y-4 {% if foo %}is-foo{% endif %}">
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
> [!NOTE]
|
|
99
|
+
> Note that you need to surround the part you want to see as the class attribute value with `()` so that the plugin can backreference it.
|
|
100
|
+
|
|
101
|
+
## Getting involved
|
|
102
|
+
|
|
103
|
+
```shell
|
|
104
|
+
pnpm install
|
|
105
|
+
|
|
106
|
+
# Develop...
|
|
107
|
+
|
|
108
|
+
pnpm check
|
|
109
|
+
pnpm test
|
|
110
|
+
pnpm build
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Thanks! 🎉
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Options, Parser, Printer, SupportLanguage, SupportOption } from "prettier";
|
|
2
|
+
export declare const languages: Partial<SupportLanguage>[];
|
|
3
|
+
export declare const parsers: Record<string, Parser>;
|
|
4
|
+
export declare const printers: Record<string, Printer>;
|
|
5
|
+
export declare const options: Record<string, SupportOption>;
|
|
6
|
+
export declare const defaultOptions: Options;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { parse } from "./parser.js";
|
|
2
|
+
import { print } from "./printer.js";
|
|
3
|
+
export const languages = [
|
|
4
|
+
{
|
|
5
|
+
name: "Any HTML-like Languages",
|
|
6
|
+
parsers: ["anywhere"],
|
|
7
|
+
aceMode: "text",
|
|
8
|
+
},
|
|
9
|
+
];
|
|
10
|
+
export const parsers = {
|
|
11
|
+
anywhere: {
|
|
12
|
+
parse,
|
|
13
|
+
astFormat: "anywhere",
|
|
14
|
+
locStart(node) {
|
|
15
|
+
return node.start;
|
|
16
|
+
},
|
|
17
|
+
locEnd(node) {
|
|
18
|
+
return node.end;
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
export const printers = {
|
|
23
|
+
anywhere: {
|
|
24
|
+
print,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
export const options = {
|
|
28
|
+
regex: {
|
|
29
|
+
type: "string",
|
|
30
|
+
category: "Anywhere",
|
|
31
|
+
default: 'class="([^{}"]*)"',
|
|
32
|
+
description: "regex to match class attribute",
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
export const defaultOptions = {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ParserOptions } from "prettier";
|
|
2
|
+
type AnywhereNode = {
|
|
3
|
+
type: "anywhere";
|
|
4
|
+
body: string;
|
|
5
|
+
source: string;
|
|
6
|
+
start: number;
|
|
7
|
+
end: number;
|
|
8
|
+
};
|
|
9
|
+
export declare const parse: (text: string, options: ParserOptions) => Promise<AnywhereNode>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { format } from "prettier";
|
|
2
|
+
import * as prettierPluginTailwindcss from "prettier-plugin-tailwindcss";
|
|
3
|
+
export const parse = async (text, options) => {
|
|
4
|
+
let formattedText = text;
|
|
5
|
+
const regex = options.regex;
|
|
6
|
+
const matches = text.matchAll(new RegExp(regex, "g"));
|
|
7
|
+
const map = new Map();
|
|
8
|
+
for (const match of matches) {
|
|
9
|
+
const original = match[0];
|
|
10
|
+
const value = match[1];
|
|
11
|
+
const fixedValue = (await format(`<div class="${value}"></div>`, {
|
|
12
|
+
parser: "html",
|
|
13
|
+
plugins: [prettierPluginTailwindcss],
|
|
14
|
+
})).match(/class="([^"]*)"/)?.[1];
|
|
15
|
+
const fixed = original.replace(value, fixedValue);
|
|
16
|
+
map.set(original, fixed);
|
|
17
|
+
}
|
|
18
|
+
for (const [original, fixed] of map) {
|
|
19
|
+
formattedText = formattedText.replace(original, fixed);
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
type: "anywhere",
|
|
23
|
+
body: formattedText,
|
|
24
|
+
end: text.length,
|
|
25
|
+
source: text,
|
|
26
|
+
start: 0,
|
|
27
|
+
};
|
|
28
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ttskch/prettier-plugin-tailwindcss-anywhere",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "A Prettier plugin for sorting TailwindCSS classes in any HTML-like language, like Twig etc.",
|
|
5
|
+
"keywords": ["prettier", "tailwindcss", "formatter", "html", "twig"],
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">= 20.9 < 21"
|
|
9
|
+
},
|
|
10
|
+
"packageManager": "pnpm@9.12.3",
|
|
11
|
+
"main": "dist/src/index.js",
|
|
12
|
+
"files": ["dist", "src"],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/src/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"tslib": "~2.7"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@biomejs/biome": "1.9.4",
|
|
23
|
+
"@types/node": "~20",
|
|
24
|
+
"@types/prettier": "^3.0.0",
|
|
25
|
+
"@vitest/coverage-v8": "~2.1",
|
|
26
|
+
"prettier": "^3.0",
|
|
27
|
+
"prettier-plugin-tailwindcss": "^0.6",
|
|
28
|
+
"rimraf": "~6.0",
|
|
29
|
+
"ts-api-utils": "~1.3",
|
|
30
|
+
"typescript": "~5.5",
|
|
31
|
+
"vitest": "~2.1"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"prettier": "^3.0",
|
|
35
|
+
"prettier-plugin-tailwindcss": "^0.6"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"start": "node dist/src/main.js",
|
|
39
|
+
"clean": "rimraf coverage dist tmp",
|
|
40
|
+
"prebuild": "biome lint",
|
|
41
|
+
"build": "tsc -d -p tsconfig.json",
|
|
42
|
+
"build:watch": "tsc -w -d -p tsconfig.json",
|
|
43
|
+
"build:release": "npm run clean && tsc -d -p tsconfig.release.json",
|
|
44
|
+
"check": "biome check",
|
|
45
|
+
"check:fix": "biome check --write",
|
|
46
|
+
"test": "vitest run unit",
|
|
47
|
+
"test:coverage": "vitest run unit --coverage.enabled --coverage.all",
|
|
48
|
+
"test:watch": "vitest unit"
|
|
49
|
+
},
|
|
50
|
+
"author": "Takashi Kanemoto <ttskch@gmail.com>",
|
|
51
|
+
"license": "MIT",
|
|
52
|
+
"volta": {
|
|
53
|
+
"node": "20.17.0"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Options,
|
|
3
|
+
Parser,
|
|
4
|
+
Printer,
|
|
5
|
+
SupportLanguage,
|
|
6
|
+
SupportOption,
|
|
7
|
+
} from "prettier";
|
|
8
|
+
import { parse } from "./parser.js";
|
|
9
|
+
import { print } from "./printer.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @see https://prettier.io/docs/en/plugins#languages
|
|
13
|
+
*/
|
|
14
|
+
export const languages: Partial<SupportLanguage>[] = [
|
|
15
|
+
{
|
|
16
|
+
name: "Any HTML-like Languages",
|
|
17
|
+
parsers: ["anywhere"],
|
|
18
|
+
aceMode: "text",
|
|
19
|
+
},
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @see https://prettier.io/docs/en/plugins#parsers
|
|
24
|
+
*/
|
|
25
|
+
export const parsers: Record<string, Parser> = {
|
|
26
|
+
anywhere: {
|
|
27
|
+
parse,
|
|
28
|
+
astFormat: "anywhere",
|
|
29
|
+
// there's only a single node
|
|
30
|
+
locStart(node) {
|
|
31
|
+
return node.start;
|
|
32
|
+
},
|
|
33
|
+
locEnd(node) {
|
|
34
|
+
return node.end;
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @see https://prettier.io/docs/en/plugins#printers
|
|
41
|
+
*/
|
|
42
|
+
export const printers: Record<string, Printer> = {
|
|
43
|
+
anywhere: {
|
|
44
|
+
print,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @see https://prettier.io/docs/en/plugins.html#options
|
|
50
|
+
*/
|
|
51
|
+
export const options: Record<string, SupportOption> = {
|
|
52
|
+
regex: {
|
|
53
|
+
type: "string",
|
|
54
|
+
category: "Anywhere",
|
|
55
|
+
default: 'class="([^{}"]*)"',
|
|
56
|
+
description: "regex to match class attribute",
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @see https://prettier.io/docs/en/plugins#defaultoptions
|
|
62
|
+
*/
|
|
63
|
+
export const defaultOptions: Options = {};
|
package/src/parser.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ParserOptions } from "prettier";
|
|
2
|
+
import { format } from "prettier";
|
|
3
|
+
import * as prettierPluginTailwindcss from "prettier-plugin-tailwindcss";
|
|
4
|
+
|
|
5
|
+
type AnywhereNode = {
|
|
6
|
+
type: "anywhere";
|
|
7
|
+
body: string;
|
|
8
|
+
source: string;
|
|
9
|
+
start: number;
|
|
10
|
+
end: number;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const parse = async (
|
|
14
|
+
text: string,
|
|
15
|
+
options: ParserOptions,
|
|
16
|
+
): Promise<AnywhereNode> => {
|
|
17
|
+
let formattedText = text;
|
|
18
|
+
const regex = options.regex as string;
|
|
19
|
+
|
|
20
|
+
const matches = text.matchAll(new RegExp(regex, "g"));
|
|
21
|
+
|
|
22
|
+
const map = new Map();
|
|
23
|
+
|
|
24
|
+
for (const match of matches) {
|
|
25
|
+
const original = match[0];
|
|
26
|
+
const value = match[1];
|
|
27
|
+
|
|
28
|
+
const fixedValue = (
|
|
29
|
+
await format(`<div class="${value}"></div>`, {
|
|
30
|
+
parser: "html",
|
|
31
|
+
plugins: [prettierPluginTailwindcss],
|
|
32
|
+
})
|
|
33
|
+
).match(/class="([^"]*)"/)?.[1];
|
|
34
|
+
|
|
35
|
+
const fixed = original.replace(value, fixedValue);
|
|
36
|
+
|
|
37
|
+
map.set(original, fixed);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
for (const [original, fixed] of map) {
|
|
41
|
+
formattedText = formattedText.replace(original, fixed);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
type: "anywhere",
|
|
46
|
+
body: formattedText,
|
|
47
|
+
end: text.length,
|
|
48
|
+
source: text,
|
|
49
|
+
start: 0,
|
|
50
|
+
};
|
|
51
|
+
};
|
package/src/printer.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AstPath, Doc } from "prettier";
|
|
2
|
+
|
|
3
|
+
export const print = (path: AstPath): Doc => {
|
|
4
|
+
const node = path.node;
|
|
5
|
+
|
|
6
|
+
switch (node.type) {
|
|
7
|
+
case "anywhere": {
|
|
8
|
+
return node.body;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
throw new Error(`Unknown node type: ${node.type}`);
|
|
13
|
+
};
|