This repository has been archived by the owner on Dec 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
81 lines (75 loc) · 2.45 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const Liquid = require("liquid-node");
const engine = new Liquid.Engine;
const replaceProblemWithSpace = (chunk, err) => {
const problemReg = /at (.*) /;
const replacer = err.message.match(problemReg)[1];
const replacee = replacer.replace(/.*/g, ' ');
var replacedstring = chunk.split(/\n/g);
var newlinestring = replacedstring[err.location.line-1];
newlinestring = newlinestring.substring(0, err.location.col-1) + replacee + newlinestring.substring(err.location.col-1 + replacer.length, newlinestring.length);
replacedstring[err.location.line-1] = newlinestring;
return replacedstring.join('\n');
};
const parseChunk = (chunk, errors) => {
return engine
.parse(chunk)
.catch((err) => {
if(err.name === "Liquid.SyntaxError") {
const problemReg = /at (.*) /;
const length = err.message.match(problemReg)[1].length;
err.location.lenght = length;
errors.push(err);
}
chunk = replaceProblemWithSpace(chunk, err);
return parseChunk(chunk, errors);
});
};
const linter = {
lintFile: (filepath, callback) => {
const errors = [];
const allchecks = [];
const testString = fs.readFileSync(filepath).toString();
allchecks.push(parseChunk(testString, errors));
Promise.all(allchecks)
.then(() => callback(errors.reverse()));
},
lintFilePromise: (filepath) => {
const errors = [];
const allchecks = [];
return fs.readFileAsync(filepath)
.then((buffer) => {
allchecks.push(parseChunk(buffer.toString(), errors));
return Promise.all(allchecks)
.then(() => errors.reverse());
});
},
lintString: (string, callback) => {
const errors = [];
const allchecks = [];
allchecks.push(parseChunk(string, errors));
Promise.all(allchecks)
.then(() => callback(errors.reverse()));
},
lintStringPromise: (string) => {
const errors = [];
const allchecks = [];
allchecks.push(parseChunk(string, errors));
return Promise.all(allchecks)
.then(() => errors.reverse());
},
loadTags: (obj) => {
const opts = {
blocks: obj.blocks || [],
tags: obj.tags || []
};
for (var i = 0; i < opts.blocks.length; i++) {
engine.registerTag(opts.blocks[i], Liquid.Block);
}
for (var j = 0; j < opts.tags.length; j++) {
engine.registerTag(opts.tags[j], Liquid.Tag);
}
},
};
module.exports = linter;