This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
forked from gucong3000/postcss-jsx
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtemplate-parser-helper.js
257 lines (204 loc) · 5.1 KB
/
template-parser-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
'use strict';
const Literal = require('./literal');
const postcssParse = require('postcss/lib/parse');
// eslint-disable-next-line regexp/no-useless-non-capturing-group, regexp/no-useless-flag -- TODO: fix
const reNewLine = /(?:\r?\n|\r)/gm;
const isLiteral = (token) => token[0] === 'word' && /^\$\{[\s\S]*\}$/.test(token[1]);
function literal(start) {
if (!isLiteral(start)) {
return;
}
const tokens = [];
let hasWord;
let type;
let token;
while ((token = this.tokenizer.nextToken())) {
tokens.push(token);
type = token[0];
if (type.length === 1) {
break;
} else if (type === 'word') {
hasWord = true;
}
}
while (tokens.length) {
this.tokenizer.back(tokens.pop());
}
if (type === '{' || (type === ':' && !hasWord)) {
return;
}
const node = new Literal({
text: start[1],
});
this.init(node, start[2], start[3]);
const input = this.input;
if (input.templateLiteralStyles) {
const offset = input.quasis[0].start;
const nodeIndex = getNodeIndex(node, input);
const startIndex = offset + nodeIndex;
const endIndex = startIndex + node.text.length;
const templateLiteralStyles = input.templateLiteralStyles.filter(
(style) => style.startIndex <= endIndex && startIndex < style.endIndex,
);
if (templateLiteralStyles.length) {
const nodes = parseTemplateLiteralStyles(templateLiteralStyles, input, [
nodeIndex,
nodeIndex + node.text.length,
]);
if (nodes.length) {
node.nodes = nodes;
nodes.forEach((n) => (n.parent = node));
}
}
}
return node;
}
function freeSemicolon(token) {
this.spaces += token[1];
const nodes = this.current.nodes;
const prev = nodes && nodes[nodes.length - 1];
if (prev && /^(?:rule|literal)$/.test(prev.type) && !prev.raws.ownSemicolon) {
prev.raws.ownSemicolon = this.spaces;
this.spaces = '';
}
}
module.exports = {
freeSemicolon,
literal,
};
function parseTemplateLiteralStyles(styles, input, range) {
const offset = input.quasis[0].start;
const source = input.css;
const opts = { ...input.parseOptions };
delete opts.templateLiteralStyles;
delete opts.expressions;
delete opts.quasis;
const parseStyle = docFixer(offset, source, opts);
const nodes = [];
let index = range[0];
styles
.sort((a, b) => a.startIndex - b.startIndex)
.forEach((style) => {
const root = parseStyle(style);
if (!root || !root.nodes.length) {
return;
}
root.raws.beforeStart = source.slice(index, style.startIndex - offset);
root.raws.afterEnd = '';
if (style.endIndex) {
index = style.endIndex - offset;
} else {
index = style.startIndex - offset + (style.content || root.source.input.css).length;
}
nodes.push(root);
});
if (nodes.length) {
nodes[nodes.length - 1].raws.afterEnd = source.slice(index, range[1]);
}
return nodes;
}
class LocalFixer {
constructor(offset, lines, style, templateParse) {
const startIndex = style.startIndex - offset;
let line = 0;
let column = startIndex;
lines.some((lineEndIndex, lineNumber) => {
if (lineEndIndex >= startIndex) {
line = lineNumber--;
if (lineNumber in lines) {
column = startIndex - lines[lineNumber] - 1;
}
return true;
}
return false;
});
this.line = line;
this.column = column;
this.style = style;
this.templateParse = templateParse;
}
object(object) {
if (object) {
if (object.line === 1) {
object.column += this.column;
}
object.line += this.line;
}
}
node(node) {
this.object(node.source.start);
this.object(node.source.end);
}
root(root) {
this.node(root);
root.walk((node) => {
this.node(node);
});
}
error(error) {
if (error && error.name === 'CssSyntaxError') {
this.object(error);
this.object(error.input);
error.message = error.message.replace(/:\d+:\d+:/, `:${error.line}:${error.column}:`);
}
return error;
}
parse(opts) {
const style = this.style;
const syntax = style.syntax;
let root = style.root;
try {
root = this.templateParse(style.content, {
...opts,
map: false,
...style.opts,
});
} catch (error) {
if (style.ignoreErrors) {
return;
}
if (!style.skipConvert) {
this.error(error);
}
throw error;
}
if (!style.skipConvert) {
this.root(root);
}
root.source.inline = Boolean(style.inline);
root.source.lang = style.lang;
root.source.syntax = syntax;
return root;
}
}
function docFixer(offset, source, opts) {
let match;
const lines = [];
reNewLine.lastIndex = 0;
while ((match = reNewLine.exec(source))) {
lines.push(match.index);
}
lines.push(source.length);
return function parseStyle(style) {
const parse = style.syntax ? style.syntax.parse : postcssParse;
return new LocalFixer(offset, lines, style, parse).parse(opts);
};
}
function getNodeIndex(node, input) {
const source = input.css;
let match;
let line = 1;
let lastIndex = -1;
reNewLine.lastIndex = 0;
while ((match = reNewLine.exec(source))) {
if (line === node.source.start.line) {
return lastIndex + node.source.start.column;
}
lastIndex = match.index;
line++;
}
if (line === node.source.start.line) {
return lastIndex + node.source.start.column;
}
return source.length;
}