-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtemplate-tokenize.js
57 lines (51 loc) · 1.27 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
"use strict";
const tokenize = require("postcss/lib/tokenize");
function templateTokenize (input) {
let pos = input.quasis[0].start;
const quasis = input.quasis.filter(quasi => quasi.start !== quasi.end);
const tokenizer = tokenize.apply(this, arguments);
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;
} else if (returned.length) {
back(token);
}
}
function back (token) {
pos -= token[1].length;
return tokenizer.back.apply(tokenizer, arguments);
}
function nextToken () {
const args = arguments;
const returned = [];
let token;
let line;
let column;
while (
(token = tokenizer.nextToken.apply(tokenizer, args)) &&
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(token => token[1]).join(""),
returned[0][2],
returned[0][3],
line,
column,
];
}
return token;
}
return Object.assign({}, tokenizer, {
back,
nextToken,
});
}
module.exports = templateTokenize;