-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
59 lines (57 loc) · 1.75 KB
/
index.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
import ejs from 'ejs';
import EJS_INCLUDE_REGEX from 'ejs-include-regex';
import check from 'syntax-error';
export default function lint(text, opts = {}) {
const arr = new ejs.Template(text, opts).parseTemplateText();
// Initialize mode var
// This is used to indicate the status:
// Inside Scriptlet, mode=1 (scriptlet) or mode=2 (expression)
// Outside Scriptlet, mode=0
let mode;
// Initialize delimiter variable
const d = opts.delimiter || '%';
const js = arr
.map((str) => {
switch (str) {
case `<${d}`:
case `<${d}_`:
mode = 1;
return padWhitespace(str);
case `<${d}=`:
case `<${d}-`:
mode = 2;
return `;${padWhitespace(str)}`;
case `${d}>`:
case `-${d}>`:
case `_${d}>`:
str = padWhitespace(str) + (mode === 2 ? ';' : '');
mode = 0;
return str;
case (str.match(EJS_INCLUDE_REGEX) || {}).input:
// if old-style include
// - replace with whitespace if preprocessorInclude is set
// - otherwise, leave it intact so it errors out correctly
return opts.preprocessorInclude ? padWhitespace(str) : str;
default:
// If inside Scriptlet, pass through
if (mode) return str;
// else, pad with whitespace
return padWhitespace(str);
}
})
.join('');
const checkOptions = {
allowAwaitOutsideFunction: !!opts.await,
};
return check(js, undefined, checkOptions);
}
function padWhitespace(text) {
let res = '';
text.split('\n').forEach((line, i) => {
// Add newline
if (i !== 0) res += '\n';
// Pad with whitespace between each newline
for (let x = 0; x < line.length; x++) res += ' ';
});
return res;
}