/
lint.js
115 lines (115 loc) · 3.49 KB
/
lint.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var yaml = require("js-yaml");
var _ = require("lodash");
var ast = require("yaml-ast-parser");
var lineColumn = require("line-column");
var ast_1 = require("./ast");
var Neq = (function () {
function Neq(path, value) {
this.path = path;
this.value = value;
}
Neq.prototype.test = function (object) {
return {
matched: _.get(object, this.path) === this.value,
paths: [this.path],
};
};
return Neq;
}());
exports.Neq = Neq;
var Exists = (function () {
function Exists(path) {
this.path = path;
}
Exists.prototype.test = function (object) {
return {
matched: !_.get(object, this.path),
paths: [this.path],
};
};
return Exists;
}());
exports.Exists = Exists;
var DOC_SEPARATOR_LENGTH = 3;
function lintMultidoc(inYaml, rules) {
var docs = inYaml.split("---").slice(1);
var offset = inYaml.indexOf("---") + 3;
var lineColumnFinder = lineColumn(inYaml);
return _.map(docs, function (doc, index) {
var vetted = lint(doc, rules, lineColumnFinder, offset);
offset += doc.length + DOC_SEPARATOR_LENGTH;
return ({
index: index,
findings: vetted,
});
});
}
exports.lintMultidoc = lintMultidoc;
function lint(inYaml, rules, lineColumnFinder, positionOffset) {
var offset = positionOffset || 0;
lineColumnFinder = lineColumnFinder || lineColumn(inYaml);
if (!inYaml) {
return [noDocError(inYaml)];
}
var root;
try {
root = yaml.safeLoad(inYaml);
}
catch (err) {
return [loadYamlError(err, inYaml, lineColumnFinder, offset)];
}
if (!root) {
return [noDocError(inYaml)];
}
var yamlAST = ast.safeLoad(inYaml, null);
if (_.isEmpty(rules)) {
return [];
}
var ruleTriggers = [];
_.forEach(rules, function (rule) {
var result = rule.test.test(root);
if (result.matched) {
var positions = _.flatMap(result.paths, function (path) { return ast_1.astPosition(yamlAST, path, lineColumnFinder, offset); });
if (_.isEmpty(positions)) {
var shorterPaths = _.map(result.paths, function (p) { return p.split(".").slice(0, -1).join("."); });
positions = _.flatMap(shorterPaths, function (path) { return ast_1.astPosition(yamlAST, path, lineColumnFinder, offset); });
}
ruleTriggers.push({
type: rule.type,
rule: rule.name,
received: _.map(result.paths, function (p) { return _.get(root, p); })[0],
message: rule.message,
positions: positions,
});
}
});
return ruleTriggers;
}
exports.lint = lint;
function loadYamlError(err, inYaml, lineColumnFinder, offset) {
return {
type: "error",
rule: "validYaml",
received: inYaml,
positions: [
{
start: {
column: lineColumnFinder.fromIndex(err.mark.position + offset).col - 1,
line: lineColumnFinder.fromIndex(err.mark.position + offset).line - 1,
position: err.mark.position + offset,
},
},
],
message: err.message,
};
}
function noDocError(inYaml) {
return {
type: "warn",
rule: "notEmpty",
received: inYaml,
message: "No document provided",
};
}