UNPKG

4.68 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const types_1 = require("@stoplight/types");
4const jp = require("jsonpath");
5const lodash_1 = require("lodash");
6const message_1 = require("./rulesets/message");
7exports.lintNode = (node, rule, then, apply, opts, parsedResult) => {
8 const givenPath = node.path[0] === '$' ? node.path.slice(1) : node.path;
9 const conditioning = exports.whatShouldBeLinted(givenPath, node.value, rule);
10 if (!conditioning.lint) {
11 return [];
12 }
13 const { parsed } = parsedResult;
14 const targetValue = conditioning.value;
15 const targets = [];
16 if (then && then.field) {
17 if (then.field === '@key') {
18 for (const key of Object.keys(targetValue)) {
19 targets.push({
20 path: key,
21 value: key,
22 });
23 }
24 }
25 else if (then.field[0] === '$') {
26 const nodes = jp.nodes(targetValue, then.field);
27 for (const n of nodes) {
28 targets.push({
29 path: n.path,
30 value: n.value,
31 });
32 }
33 }
34 else {
35 targets.push({
36 path: typeof then.field === 'string' ? then.field.split('.') : then.field,
37 value: lodash_1.get(targetValue, then.field),
38 });
39 }
40 }
41 else {
42 targets.push({
43 path: [],
44 value: targetValue,
45 });
46 }
47 if (!targets.length) {
48 targets.push({
49 path: [],
50 value: undefined,
51 });
52 }
53 let results = [];
54 for (const target of targets) {
55 const targetPath = givenPath.concat(target.path);
56 const targetResults = apply(target.value, then.functionOptions || {}, {
57 given: givenPath,
58 target: targetPath,
59 }, {
60 original: node.value,
61 given: node.value,
62 resolved: opts.resolvedTarget,
63 }) || [];
64 const severity = rule.severity !== undefined ? rule.severity : types_1.DiagnosticSeverity.Warning;
65 results = results.concat(targetResults.map(result => {
66 const path = result.path || targetPath;
67 const location = parsedResult.getLocationForJsonPath(parsed, path, true);
68 return Object.assign({ code: rule.name, get summary() {
69 return this.message;
70 }, message: rule.message === undefined
71 ? rule.summary || result.message
72 : message_1.message(rule.message, {
73 error: result.message,
74 property: path.length > 0 ? path[path.length - 1] : '',
75 description: rule.description,
76 }), path,
77 severity, source: parsedResult.source }, (location || {
78 range: {
79 start: {
80 character: 0,
81 line: 0,
82 },
83 end: {
84 character: 0,
85 line: 0,
86 },
87 },
88 }));
89 }));
90 }
91 return results;
92};
93exports.whatShouldBeLinted = (path, originalValue, rule) => {
94 const leaf = path[path.length - 1];
95 const when = rule.when;
96 if (!when) {
97 return {
98 lint: true,
99 value: originalValue,
100 };
101 }
102 const pattern = when.pattern;
103 const field = when.field;
104 const isKey = field === '@key';
105 if (!pattern) {
106 if (isKey) {
107 return {
108 lint: false,
109 value: originalValue,
110 };
111 }
112 return {
113 lint: lodash_1.has(originalValue, field),
114 value: originalValue,
115 };
116 }
117 if (isKey && pattern) {
118 return keyAndOptionalPattern(leaf, pattern, originalValue);
119 }
120 const fieldValue = String(lodash_1.get(originalValue, when.field));
121 return {
122 lint: fieldValue.match(pattern) !== null,
123 value: originalValue,
124 };
125};
126function keyAndOptionalPattern(key, pattern, value) {
127 if (typeof key === 'number' && typeof value === 'object') {
128 for (const k of Object.keys(value)) {
129 if (String(k).match(pattern)) {
130 return {
131 lint: true,
132 value,
133 };
134 }
135 }
136 }
137 else if (String(key).match(pattern)) {
138 return {
139 lint: true,
140 value,
141 };
142 }
143 return {
144 lint: false,
145 value,
146 };
147}
148//# sourceMappingURL=linter.js.map
\No newline at end of file