-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparseWithPointers.ts
377 lines (322 loc) · 10.4 KB
/
parseWithPointers.ts
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { DiagnosticSeverity, IDiagnostic, Optional } from '@stoplight/types';
import {
determineScalarType,
load as loadAST,
parseYamlBoolean,
parseYamlFloat,
parseYamlInteger,
YAMLException,
} from '@stoplight/yaml-ast-parser';
import { buildJsonPath } from './buildJsonPath';
import { SpecialMappingKeys } from './consts';
import { lineForPosition } from './lineForPosition';
import {
IParseOptions,
Kind,
ScalarType,
YAMLAnchorReference,
YAMLMapping,
YAMLNode,
YamlParserResult,
YAMLScalar,
} from './types';
import { isObject } from './utils';
export const parseWithPointers = <T>(value: string, options?: IParseOptions): YamlParserResult<T | undefined> => {
const lineMap = computeLineMap(value);
const ast = loadAST(value, {
...options,
ignoreDuplicateKeys: true,
}) as YAMLNode;
const parsed: YamlParserResult<T | undefined> = {
ast,
lineMap,
data: undefined,
diagnostics: [],
metadata: options,
};
if (!ast) return parsed;
parsed.data = walkAST(ast, options, lineMap, parsed.diagnostics) as T;
if (ast.errors) {
parsed.diagnostics.push(...transformErrors(ast.errors, lineMap));
}
if (parsed.diagnostics.length > 0) {
parsed.diagnostics.sort((itemA, itemB) => itemA.range.start.line - itemB.range.start.line);
}
if (Array.isArray(parsed.ast.errors)) {
parsed.ast.errors.length = 0;
}
return parsed;
};
const KEYS = Symbol('object_keys');
export const walkAST = (
node: YAMLNode | null,
options: Optional<IParseOptions>,
lineMap: number[],
diagnostics: IDiagnostic[],
): unknown => {
if (node) {
switch (node.kind) {
case Kind.MAP: {
const preserveKeyOrder = options !== void 0 && options.preserveKeyOrder === true;
const container = createMapContainer(preserveKeyOrder);
// note, we don't handle null aka '~' keys on purpose
const seenKeys: string[] = [];
const handleMergeKeys = options !== void 0 && options.mergeKeys === true;
const yamlMode = options !== void 0 && options.json === false;
const handleDuplicates = options !== void 0 && options.ignoreDuplicateKeys === false;
for (const mapping of node.mappings) {
if (!validateMappingKey(mapping, lineMap, diagnostics, yamlMode)) continue;
const key = String(getScalarValue(mapping.key));
if ((yamlMode || handleDuplicates) && (!handleMergeKeys || key !== SpecialMappingKeys.MergeKey)) {
if (seenKeys.includes(key)) {
if (yamlMode) {
throw new Error('Duplicate YAML mapping key encountered');
}
if (handleDuplicates) {
diagnostics.push(createYAMLException(mapping.key, lineMap, 'duplicate key'));
}
} else {
seenKeys.push(key);
}
}
// https://yaml.org/type/merge.html merge keys, not a part of YAML spec
if (handleMergeKeys && key === SpecialMappingKeys.MergeKey) {
const reduced = reduceMergeKeys(walkAST(mapping.value, options, lineMap, diagnostics), preserveKeyOrder);
if (preserveKeyOrder && reduced !== null) {
for (const reducedKey of Object.keys(reduced)) {
pushKey(container, reducedKey);
}
}
Object.assign(container, reduced);
} else {
container[key] = walkAST(mapping.value, options, lineMap, diagnostics);
if (preserveKeyOrder) {
pushKey(container, key);
}
}
}
if (KEYS in container) {
(container as Partial<{ [KEYS]: Array<Symbol | string> }>)[KEYS]!.push(KEYS);
}
return container;
}
case Kind.SEQ:
return node.items.map(item => walkAST(item, options, lineMap, diagnostics));
case Kind.SCALAR:
return getScalarValue(node);
case Kind.ANCHOR_REF: {
if (isObject(node.value) && isCircularAnchorRef(node)) {
node.value = dereferenceAnchor(node.value, node.referencesAnchor)!;
}
return node.value && walkAST(node.value, options, lineMap, diagnostics);
}
default:
return null;
}
}
return node;
};
const isCircularAnchorRef = (anchorRef: YAMLAnchorReference) => {
const { referencesAnchor } = anchorRef;
let node: YAMLNode | undefined = anchorRef;
// tslint:disable-next-line:no-conditional-assignment
while ((node = node.parent)) {
if ('anchorId' in node && node.anchorId === referencesAnchor) {
return true;
}
}
return false;
};
const dereferenceAnchor = (node: YAMLNode | null, anchorId: string): YAMLNode | null => {
if (!isObject(node)) return node;
if (node.kind === Kind.ANCHOR_REF && node.referencesAnchor === anchorId) return null;
switch (node.kind) {
case Kind.MAP:
return {
...node,
mappings: node.mappings.map(mapping => dereferenceAnchor(mapping, anchorId) as YAMLMapping),
};
case Kind.SEQ:
return {
...node,
items: node.items.map(item => dereferenceAnchor(item, anchorId)!),
};
case Kind.MAPPING:
return { ...node, value: dereferenceAnchor(node.value, anchorId) };
case Kind.SCALAR:
return node;
case Kind.ANCHOR_REF:
if (isObject(node.value) && isCircularAnchorRef(node)) {
return null;
}
return node;
default:
return node;
}
};
function getScalarValue(node: YAMLScalar): number | null | boolean | string | void {
switch (determineScalarType(node)) {
case ScalarType.null:
return null;
case ScalarType.string:
return String(node.value);
case ScalarType.bool:
return parseYamlBoolean(node.value);
case ScalarType.int:
return parseYamlInteger(node.value);
case ScalarType.float:
return parseYamlFloat(node.value);
}
}
// builds up the line map, for use by linesForPosition
const computeLineMap = (input: string) => {
const lineMap: number[] = [];
let i = 0;
for (; i < input.length; i++) {
if (input[i] === '\n') {
lineMap.push(i + 1);
}
}
lineMap.push(i + 1);
return lineMap;
};
function getLineLength(lineMap: number[], line: number) {
if (line === 0) {
return Math.max(0, lineMap[0] - 1);
}
return Math.max(0, lineMap[line] - lineMap[line - 1] - 1);
}
const transformErrors = (errors: YAMLException[], lineMap: number[]): IDiagnostic[] => {
const validations: IDiagnostic[] = [];
for (const error of errors) {
const validation: IDiagnostic = {
code: error.name,
message: error.reason,
severity: error.isWarning ? DiagnosticSeverity.Warning : DiagnosticSeverity.Error,
range: {
start: {
line: error.mark.line,
character: error.mark.column,
},
end: {
line: error.mark.line,
character: error.mark.toLineEnd ? getLineLength(lineMap, error.mark.line) : error.mark.column,
},
},
};
validations.push(validation);
}
return validations;
};
const reduceMergeKeys = (items: unknown, preserveKeyOrder: boolean): object | null => {
if (Array.isArray(items)) {
// reduceRight is on purpose here! We need to respect the order - the key cannot be overridden
const reduced = items.reduceRight(
preserveKeyOrder
? (merged, item) => {
const keys = Object.keys(item);
for (let i = keys.length - 1; i >= 0; i--) {
unshiftKey(merged, keys[i]);
}
return Object.assign(merged, item);
}
: (merged, item) => Object.assign(merged, item),
createMapContainer(preserveKeyOrder),
);
if (preserveKeyOrder) {
reduced[KEYS].push(KEYS);
}
return reduced;
}
return typeof items !== 'object' || items === null ? null : Object(items);
};
const traps = {
ownKeys(target: object) {
return target[KEYS];
},
};
function createMapContainer(preserveKeyOrder: boolean): { [key in PropertyKey]: unknown } {
if (preserveKeyOrder) {
const container = new Proxy({}, traps);
Reflect.defineProperty(container, KEYS, {
value: [],
});
return container;
}
return {};
}
function deleteKey(container: object, key: string) {
const index = key in container ? container[KEYS].indexOf(key) : -1;
if (index !== -1) {
container[KEYS].splice(index, 1);
}
}
function unshiftKey(container: object, key: string) {
deleteKey(container, key);
container[KEYS].unshift(key);
}
function pushKey(container: object, key: string) {
deleteKey(container, key);
container[KEYS].push(key);
}
function validateMappingKey(
mapping: YAMLMapping,
lineMap: number[],
diagnostics: IDiagnostic[],
yamlMode: boolean,
): boolean {
if (mapping.key.kind !== Kind.SCALAR) {
if (!yamlMode) {
diagnostics.push(
createYAMLIncompatibilityException(mapping.key, lineMap, 'mapping key must be a string scalar', yamlMode),
);
}
// no exception is thrown, yet the mapping is excluded regardless of mode, as we cannot represent the value anyway
return false;
}
if (!yamlMode) {
const type = typeof getScalarValue(mapping.key);
if (type !== 'string') {
diagnostics.push(
createYAMLIncompatibilityException(
mapping.key,
lineMap,
`mapping key must be a string scalar rather than ${mapping.key.valueObject === null ? 'null' : type}`,
yamlMode,
),
);
}
}
return true;
}
function createYAMLIncompatibilityException(
node: YAMLNode,
lineMap: number[],
message: string,
yamlMode: boolean,
): IDiagnostic {
const exception = createYAMLException(node, lineMap, message);
exception.code = 'YAMLIncompatibleValue';
exception.severity = yamlMode ? DiagnosticSeverity.Hint : DiagnosticSeverity.Error;
return exception;
}
function createYAMLException(node: YAMLNode, lineMap: number[], message: string): IDiagnostic {
const startLine = lineForPosition(node.startPosition, lineMap);
const endLine = lineForPosition(node.endPosition, lineMap);
return {
code: 'YAMLException',
message,
severity: DiagnosticSeverity.Error,
path: buildJsonPath(node),
range: {
start: {
line: startLine,
character: startLine === 0 ? node.startPosition : node.startPosition - lineMap[startLine - 1],
},
end: {
line: endLine,
character: endLine === 0 ? node.endPosition : node.endPosition - lineMap[endLine - 1],
},
},
};
}