@taiga-ui/prettier-config 0.437.0 → 0.439.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/index.js +39 -22
- package/package.json +1 -1
- package/plugins/prettier-plugin-embedded-ts.js +207 -0
- package/options/svg.js +0 -7
package/index.js
CHANGED
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
const
|
|
2
|
-
attributeGroups: [
|
|
3
|
-
'$ANGULAR_STRUCTURAL_DIRECTIVE',
|
|
4
|
-
'$ANGULAR_ELEMENT_REF',
|
|
5
|
-
'$ID',
|
|
6
|
-
'$DEFAULT',
|
|
7
|
-
'$CLASS',
|
|
8
|
-
'$ANGULAR_ANIMATION',
|
|
9
|
-
'$ANGULAR_ANIMATION_INPUT',
|
|
10
|
-
'$ANGULAR_INPUT',
|
|
11
|
-
'$ANGULAR_TWO_WAY_BINDING',
|
|
12
|
-
'$ANGULAR_OUTPUT',
|
|
13
|
-
],
|
|
14
|
-
attributeSort: 'ASC',
|
|
15
|
-
};
|
|
1
|
+
const path = require('node:path');
|
|
16
2
|
|
|
17
3
|
module.exports = {
|
|
18
4
|
$schema: 'https://json.schemastore.org/prettierrc',
|
|
@@ -51,7 +37,7 @@ module.exports = {
|
|
|
51
37
|
parser: 'json-stringify',
|
|
52
38
|
plugins: [
|
|
53
39
|
require.resolve(
|
|
54
|
-
|
|
40
|
+
path.resolve(
|
|
55
41
|
__dirname,
|
|
56
42
|
'plugins',
|
|
57
43
|
'prettier-plugin-sort-package.js',
|
|
@@ -87,23 +73,54 @@ module.exports = {
|
|
|
87
73
|
files: ['*.html'],
|
|
88
74
|
options: {
|
|
89
75
|
parser: 'angular',
|
|
76
|
+
attributeGroups: [
|
|
77
|
+
'$ANGULAR_STRUCTURAL_DIRECTIVE',
|
|
78
|
+
'$ANGULAR_ELEMENT_REF',
|
|
79
|
+
'$ID',
|
|
80
|
+
'$DEFAULT',
|
|
81
|
+
'$CLASS',
|
|
82
|
+
'$ANGULAR_ANIMATION',
|
|
83
|
+
'$ANGULAR_ANIMATION_INPUT',
|
|
84
|
+
'$ANGULAR_INPUT',
|
|
85
|
+
'$ANGULAR_TWO_WAY_BINDING',
|
|
86
|
+
'$ANGULAR_OUTPUT',
|
|
87
|
+
],
|
|
88
|
+
attributeSort: 'ASC',
|
|
90
89
|
printWidth: 120,
|
|
91
|
-
...attributeOptions,
|
|
92
90
|
},
|
|
93
91
|
},
|
|
94
92
|
{
|
|
95
|
-
files: ['*.js'
|
|
93
|
+
files: ['*.js'],
|
|
96
94
|
options: {
|
|
97
|
-
...attributeOptions,
|
|
98
95
|
parser: 'typescript',
|
|
99
96
|
printWidth: 90,
|
|
100
97
|
},
|
|
101
98
|
},
|
|
99
|
+
{
|
|
100
|
+
files: ['*.ts'],
|
|
101
|
+
options: {
|
|
102
|
+
parser: 'typescript-embedded-ts',
|
|
103
|
+
plugins: [
|
|
104
|
+
require.resolve(
|
|
105
|
+
path.resolve(
|
|
106
|
+
__dirname,
|
|
107
|
+
'plugins',
|
|
108
|
+
'prettier-plugin-embedded-ts.js',
|
|
109
|
+
),
|
|
110
|
+
),
|
|
111
|
+
],
|
|
112
|
+
printWidth: 90,
|
|
113
|
+
},
|
|
114
|
+
},
|
|
102
115
|
{
|
|
103
116
|
files: '*.svg',
|
|
104
|
-
options:
|
|
105
|
-
|
|
106
|
-
|
|
117
|
+
options: {
|
|
118
|
+
parser: 'html',
|
|
119
|
+
plugins: [require.resolve('prettier-plugin-organize-attributes')],
|
|
120
|
+
attributeGroups: ['^(id|name)$', '^x$', '^y$', '^xmlns$', '$DEFAULT'],
|
|
121
|
+
printWidth: 120,
|
|
122
|
+
singleAttributePerLine: false,
|
|
123
|
+
},
|
|
107
124
|
},
|
|
108
125
|
],
|
|
109
126
|
};
|
package/package.json
CHANGED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
const prettier = require('prettier');
|
|
2
|
+
const estreePlugin = require('prettier/plugins/estree');
|
|
3
|
+
const typescriptPlugin = require('prettier/plugins/typescript');
|
|
4
|
+
|
|
5
|
+
const {hardline, indent} = prettier.doc.builders;
|
|
6
|
+
const baseParser = typescriptPlugin.parsers.typescript;
|
|
7
|
+
const basePrinter = estreePlugin.printers.estree;
|
|
8
|
+
|
|
9
|
+
const EMBEDDED_PARSER = 'typescript';
|
|
10
|
+
const PARSER_NAME = 'typescript-embedded-ts';
|
|
11
|
+
const AST_FORMAT = 'estree-embedded-ts';
|
|
12
|
+
|
|
13
|
+
const MARKERS = new Set(['TS', 'TYPESCRIPT']);
|
|
14
|
+
|
|
15
|
+
const parsers = {
|
|
16
|
+
[PARSER_NAME]: {
|
|
17
|
+
...baseParser,
|
|
18
|
+
astFormat: AST_FORMAT,
|
|
19
|
+
async parse(text, options) {
|
|
20
|
+
const ast = await baseParser.parse(text, options);
|
|
21
|
+
|
|
22
|
+
markEmbeddedTemplateLiterals(ast, text);
|
|
23
|
+
|
|
24
|
+
return ast;
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const printers = {
|
|
30
|
+
[AST_FORMAT]: {
|
|
31
|
+
...basePrinter,
|
|
32
|
+
embed(path, options) {
|
|
33
|
+
const {node} = path;
|
|
34
|
+
|
|
35
|
+
if (options.embeddedLanguageFormatting === 'off') {
|
|
36
|
+
return basePrinter.embed?.(path, options);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (
|
|
40
|
+
node?.type === 'TemplateLiteral' &&
|
|
41
|
+
node.__embeddedParser === EMBEDDED_PARSER &&
|
|
42
|
+
node.expressions?.length === 0 &&
|
|
43
|
+
node.quasis?.length === 1
|
|
44
|
+
) {
|
|
45
|
+
return async (textToDoc) => {
|
|
46
|
+
const source = node.quasis[0].value.raw;
|
|
47
|
+
const doc = await textToDoc(source, {
|
|
48
|
+
...options,
|
|
49
|
+
parser: EMBEDDED_PARSER,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return ['`', indent([hardline, doc]), hardline, '`'];
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return basePrinter.embed?.(path, options);
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function markEmbeddedTemplateLiterals(ast, text) {
|
|
62
|
+
const comments = collectComments(ast)
|
|
63
|
+
.filter(isSupportedMarker)
|
|
64
|
+
.sort((a, b) => getEnd(a) - getEnd(b));
|
|
65
|
+
|
|
66
|
+
if (!comments.length) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
visit(ast, (node) => {
|
|
71
|
+
if (
|
|
72
|
+
node?.type !== 'TemplateLiteral' ||
|
|
73
|
+
node.expressions?.length !== 0 ||
|
|
74
|
+
node.quasis?.length !== 1
|
|
75
|
+
) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const start = getStart(node);
|
|
80
|
+
|
|
81
|
+
if (start == null) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const marker = findDirectMarkerBefore(comments, start, text);
|
|
86
|
+
|
|
87
|
+
if (marker) {
|
|
88
|
+
node.__embeddedParser = EMBEDDED_PARSER;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function findDirectMarkerBefore(comments, nodeStart, text) {
|
|
94
|
+
let candidate = null;
|
|
95
|
+
|
|
96
|
+
for (const comment of comments) {
|
|
97
|
+
const end = getEnd(comment);
|
|
98
|
+
|
|
99
|
+
if (end == null || end > nodeStart) {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const between = text.slice(end, nodeStart);
|
|
104
|
+
|
|
105
|
+
if (/^\s*$/.test(between)) {
|
|
106
|
+
candidate = comment;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return candidate;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function isSupportedMarker(comment) {
|
|
114
|
+
if (!comment || comment.type !== 'Block') {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return MARKERS.has(comment.value.trim().toUpperCase());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function collectComments(ast) {
|
|
122
|
+
const comments = [];
|
|
123
|
+
const seen = new Set();
|
|
124
|
+
|
|
125
|
+
const push = (comment) => {
|
|
126
|
+
if (!comment || typeof comment !== 'object') {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const key = `${getStart(comment)}:${getEnd(comment)}:${comment.type}:${comment.value}`;
|
|
131
|
+
|
|
132
|
+
if (seen.has(key)) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
seen.add(key);
|
|
137
|
+
comments.push(comment);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
if (Array.isArray(ast.comments)) {
|
|
141
|
+
ast.comments.forEach(push);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
visit(ast, (node) => {
|
|
145
|
+
if (Array.isArray(node.comments)) {
|
|
146
|
+
node.comments.forEach(push);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (Array.isArray(node.leadingComments)) {
|
|
150
|
+
node.leadingComments.forEach(push);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (Array.isArray(node.trailingComments)) {
|
|
154
|
+
node.trailingComments.forEach(push);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return comments;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function visit(value, fn) {
|
|
162
|
+
walk(value);
|
|
163
|
+
|
|
164
|
+
function walk(node) {
|
|
165
|
+
if (!node || typeof node !== 'object') {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (Array.isArray(node)) {
|
|
170
|
+
for (const item of node) {
|
|
171
|
+
walk(item);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
fn(node);
|
|
178
|
+
|
|
179
|
+
for (const [key, child] of Object.entries(node)) {
|
|
180
|
+
if (
|
|
181
|
+
key === 'loc' ||
|
|
182
|
+
key === 'range' ||
|
|
183
|
+
key === 'comments' ||
|
|
184
|
+
key === 'leadingComments' ||
|
|
185
|
+
key === 'trailingComments' ||
|
|
186
|
+
key === 'parent'
|
|
187
|
+
) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
walk(child);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function getStart(node) {
|
|
197
|
+
return typeof node?.start === 'number' ? node.start : node?.range?.[0];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function getEnd(node) {
|
|
201
|
+
return typeof node?.end === 'number' ? node.end : node?.range?.[1];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
module.exports = {
|
|
205
|
+
parsers,
|
|
206
|
+
printers,
|
|
207
|
+
};
|