forked from RyanZim/EJS-Lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (53 loc) · 1.5 KB
/
index.js
File metadata and controls
56 lines (53 loc) · 1.5 KB
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
'use strict';
const rewire = require('rewire');
const ejs = rewire('ejs');
const EJS_INCLUDE_REGEX = require('ejs-include-regex');
const check = require('syntax-error');
function lint (text, opts) {
opts = opts || {};
// Use rewire to access the ejs internal function "Template"
const Template = ejs.__get__('Template');
const arr = new Template(text, opts).parseTemplateText();
// Initialize mode var
// This is used to indicate the status:
// Inside Scriptlet, mode=1
// 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 + '>':
case '_' + d + '>':
mode = 0;
return padWhitespace(str);
case (str.match(EJS_INCLUDE_REGEX) || {}).input:
// if old-style include, replace with whitespace
return padWhitespace(str);
default:
// If inside Scriptlet, pass through
if (mode === 1) return str;
// else, pad with whitespace
return padWhitespace(str);
}
}).join('');
return check(js);
}
module.exports = lint;
// Backwards compat:
module.exports.lint = lint;
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;
}