forked from stylelint/postcss-css-in-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template-tokenize.js
62 lines (48 loc) · 1.22 KB
/
template-tokenize.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
'use strict';
const tokenize = require('postcss/lib/tokenize');
function templateTokenize(input, options = {}) {
let pos = input.quasis[0].start;
const quasis = input.quasis.filter((quasi) => quasi.start !== quasi.end);
const tokenizer = tokenize(input, options);
function tokenInExpressions(token, returned) {
const start = pos;
pos += token[1].length;
if (
!quasis.some((quasi) => start >= quasi.start && pos <= quasi.end) ||
(returned.length && token[0] === returned[0][0])
) {
return true;
}
if (returned.length) {
back(token);
}
}
function back(token) {
pos -= token[1].length;
return tokenizer.back(token);
}
function nextToken(opts) {
const returned = [];
let token;
let line;
let column;
while ((token = tokenizer.nextToken(opts)) && tokenInExpressions(token, returned)) {
line = token[4] || token[2] || line;
column = token[5] || token[3] || column;
returned.push(token);
}
if (returned.length) {
token = [
returned[0][0],
returned.map((parentToken) => parentToken[1]).join(''),
returned[0][2],
returned[0][3],
line,
column,
];
}
return token;
}
return { ...tokenizer, back, nextToken };
}
module.exports = templateTokenize;