|
5 | 5 | import * as assert from 'assert';
|
6 | 6 | import { expect } from 'chai';
|
7 | 7 | import { ArrayASTNode, ObjectASTNode, PropertyASTNode } from '../src/languageservice/jsonASTTypes';
|
8 |
| -import { parse } from './../src/languageservice/parser/yamlParser07'; |
| 8 | +import { parse, YAMLDocument } from './../src/languageservice/parser/yamlParser07'; |
| 9 | +import { aliasDepth } from '../src/languageservice/parser/ast-converter'; |
9 | 10 |
|
10 | 11 | describe('YAML parser', () => {
|
11 | 12 | describe('YAML parser', function () {
|
@@ -243,6 +244,73 @@ describe('YAML parser', () => {
|
243 | 244 | );
|
244 | 245 | assert(parsedDocument.documents[0].errors.length === 0, JSON.stringify(parsedDocument.documents[0].errors));
|
245 | 246 | });
|
| 247 | + |
| 248 | + it('parse aliases up to a depth', () => { |
| 249 | + // If maxRefCount is set to 1, it will only resolve one layer of aliases, which means |
| 250 | + // `b` below will inherit `a`, but `c` will not inherit `a`. |
| 251 | + let parsedDocument: YAMLDocument; |
| 252 | + try { |
| 253 | + aliasDepth.maxRefCount = 1; |
| 254 | + parsedDocument = parse(` |
| 255 | +a: &a |
| 256 | + foo: "web" |
| 257 | +b: &b |
| 258 | + <<: *a |
| 259 | +c: &c |
| 260 | + <<: *b |
| 261 | +`); |
| 262 | + } finally { |
| 263 | + aliasDepth.maxRefCount = 1000; |
| 264 | + } |
| 265 | + |
| 266 | + const anode: ObjectASTNode = (parsedDocument.documents[0].root.children[0] as PropertyASTNode).valueNode as ObjectASTNode; |
| 267 | + const aval = anode.properties[0].valueNode; |
| 268 | + |
| 269 | + const bnode: ObjectASTNode = (parsedDocument.documents[0].root.children[1] as PropertyASTNode).valueNode as ObjectASTNode; |
| 270 | + const bvalprops: PropertyASTNode = (bnode.properties[0].valueNode as ObjectASTNode).properties[0]; |
| 271 | + const bval = bvalprops.valueNode; |
| 272 | + |
| 273 | + const cnode: ObjectASTNode = (parsedDocument.documents[0].root.children[2] as PropertyASTNode).valueNode as ObjectASTNode; |
| 274 | + const cvalprops: PropertyASTNode = (cnode.properties[0].valueNode as ObjectASTNode).properties[0]; |
| 275 | + const cval = cvalprops.valueNode; |
| 276 | + |
| 277 | + assert(aval?.value === 'web'); |
| 278 | + assert(bval?.value === 'web'); |
| 279 | + assert(cval?.value === undefined); |
| 280 | + }); |
| 281 | + |
| 282 | + it('parse aliases up to a depth for multiple objects', () => { |
| 283 | + // In the below configuration, `c` will not inherit `a` because of depth issues |
| 284 | + // but the following object `o` will still resolve correctly. |
| 285 | + let parsedDocument: YAMLDocument; |
| 286 | + try { |
| 287 | + aliasDepth.maxRefCount = 1; |
| 288 | + parsedDocument = parse(` |
| 289 | +a: &a |
| 290 | + foo: "web" |
| 291 | +b: &b |
| 292 | + <<: *a |
| 293 | +c: &c |
| 294 | + <<: *b |
| 295 | +
|
| 296 | +o: &o |
| 297 | + <<: *a |
| 298 | +`); |
| 299 | + } finally { |
| 300 | + aliasDepth.maxRefCount = 1000; |
| 301 | + } |
| 302 | + |
| 303 | + const onode: ObjectASTNode = (parsedDocument.documents[0].root.children[3] as PropertyASTNode).valueNode as ObjectASTNode; |
| 304 | + const ovalprops: PropertyASTNode = (onode.properties[0].valueNode as ObjectASTNode).properties[0]; |
| 305 | + const oval = ovalprops.valueNode; |
| 306 | + |
| 307 | + const cnode: ObjectASTNode = (parsedDocument.documents[0].root.children[2] as PropertyASTNode).valueNode as ObjectASTNode; |
| 308 | + const cvalprops: PropertyASTNode = (cnode.properties[0].valueNode as ObjectASTNode).properties[0]; |
| 309 | + const cval = cvalprops.valueNode; |
| 310 | + |
| 311 | + assert(cval?.value === undefined); |
| 312 | + assert(oval?.value === 'web'); |
| 313 | + }); |
246 | 314 | });
|
247 | 315 |
|
248 | 316 | describe('YAML parser bugs', () => {
|
|
0 commit comments