From 99679a58a0248f120faf2e8c3f37a670fd6005eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= <9273484+P0lip@users.noreply.github.com> Date: Tue, 1 Oct 2019 18:55:10 +0200 Subject: [PATCH] fix(dereferencing): set null as the value of the property (#29) --- .../__snapshots__/parse.spec.ts.snap | 28 +++++++++---------- .../parseWithPointers.spec.ts.snap | 28 +++++++++---------- src/__tests__/parse.spec.ts | 17 +++++------ src/__tests__/parseWithPointers.spec.ts | 11 ++++---- src/parseWithPointers.ts | 23 ++++++++------- 5 files changed, 54 insertions(+), 53 deletions(-) diff --git a/src/__tests__/__snapshots__/parse.spec.ts.snap b/src/__tests__/__snapshots__/parse.spec.ts.snap index 36cb80f..d03512a 100644 --- a/src/__tests__/__snapshots__/parse.spec.ts.snap +++ b/src/__tests__/__snapshots__/parse.spec.ts.snap @@ -4,36 +4,36 @@ exports[`parse dereferencing anchor refs insane edge case 1`] = ` Array [ Array [ Array [ - undefined, + null, Object { "test": Array [ - undefined, + null, ], }, Object { "abc": Object { "a": null, - "c": undefined, + "c": null, "foo": 2, - "x": undefined, + "x": null, }, }, ], Object { "test": Array [ Array [ - undefined, + null, Object { "test": Array [ - undefined, + null, ], }, Object { "abc": Object { "a": null, - "c": undefined, + "c": null, "foo": 2, - "x": undefined, + "x": null, }, }, ], @@ -43,27 +43,27 @@ Array [ "abc": Object { "a": null, "c": Array [ - undefined, + null, Object { "test": Array [ - undefined, + null, ], }, Object { "abc": Object { "a": null, - "c": undefined, + "c": null, "foo": 2, - "x": undefined, + "x": null, }, }, ], "foo": 2, "x": Object { "a": null, - "c": undefined, + "c": null, "foo": 2, - "x": undefined, + "x": null, }, }, }, diff --git a/src/__tests__/__snapshots__/parseWithPointers.spec.ts.snap b/src/__tests__/__snapshots__/parseWithPointers.spec.ts.snap index 7db950d..9ad09be 100644 --- a/src/__tests__/__snapshots__/parseWithPointers.spec.ts.snap +++ b/src/__tests__/__snapshots__/parseWithPointers.spec.ts.snap @@ -4,36 +4,36 @@ exports[`yaml parser dereferencing anchor refs insane edge case 1`] = ` Array [ Array [ Array [ - undefined, + null, Object { "test": Array [ - undefined, + null, ], }, Object { "abc": Object { "a": null, - "c": undefined, + "c": null, "foo": 2, - "x": undefined, + "x": null, }, }, ], Object { "test": Array [ Array [ - undefined, + null, Object { "test": Array [ - undefined, + null, ], }, Object { "abc": Object { "a": null, - "c": undefined, + "c": null, "foo": 2, - "x": undefined, + "x": null, }, }, ], @@ -43,27 +43,27 @@ Array [ "abc": Object { "a": null, "c": Array [ - undefined, + null, Object { "test": Array [ - undefined, + null, ], }, Object { "abc": Object { "a": null, - "c": undefined, + "c": null, "foo": 2, - "x": undefined, + "x": null, }, }, ], "foo": 2, "x": Object { "a": null, - "c": undefined, + "c": null, "foo": 2, - "x": undefined, + "x": null, }, }, }, diff --git a/src/__tests__/parse.spec.ts b/src/__tests__/parse.spec.ts index 0823d12..9b4cb21 100644 --- a/src/__tests__/parse.spec.ts +++ b/src/__tests__/parse.spec.ts @@ -53,7 +53,7 @@ to save space`, european-cities: austria: *austrian-cities `); - expect(result).toEqual({ + expect(result).toStrictEqual({ 'austrian-cities': ['Vienna', 'Graz', 'Linz', 'Salzburg'], 'european-cities': { austria: ['Vienna', 'Graz', 'Linz', 'Salzburg'], @@ -68,13 +68,13 @@ european-cities: name: *ref `); - expect(result).toEqual({ + expect(result).toStrictEqual({ definitions: { model: { foo: { name: { foo: { - name: undefined, + name: null, }, }, }, @@ -90,10 +90,10 @@ european-cities: - test: - *foo `); - expect(result).toEqual([ + expect(result).toStrictEqual([ [ { - test: [[{ test: [undefined] }]], + test: [[{ test: [null] }]], }, ], ]); @@ -113,10 +113,11 @@ european-cities: &cities all: *cities `); - expect(result).toEqual({ + expect(result).toStrictEqual({ 'austrian-cities': ['Vienna', 'Graz', 'Linz', 'Salzburg'], 'european-cities': { all: { + all: null, austria: ['Vienna', 'Graz', 'Linz', 'Salzburg'], }, austria: ['Vienna', 'Graz', 'Linz', 'Salzburg'], @@ -134,10 +135,10 @@ european-cities: &cities - *foo `); - expect(result).toEqual({ + expect(result).toStrictEqual({ a: [ { - b: [true, { c: [true, { c: undefined }, undefined] }, [{ b: [true, { c: undefined }, undefined] }]], + b: [true, { c: [true, { c: null }, null] }, [{ b: [true, { c: null }, null] }]], }, ], }); diff --git a/src/__tests__/parseWithPointers.spec.ts b/src/__tests__/parseWithPointers.spec.ts index 8d41e43..42f9085 100644 --- a/src/__tests__/parseWithPointers.spec.ts +++ b/src/__tests__/parseWithPointers.spec.ts @@ -170,7 +170,7 @@ european-cities: foo: { name: { foo: { - name: undefined, + name: null, }, }, }, @@ -189,7 +189,7 @@ european-cities: expect(result.data).toEqual([ [ { - test: [[{ test: [undefined] }]], + test: [[{ test: [null] }]], }, ], ]); @@ -209,10 +209,11 @@ european-cities: &cities all: *cities `); - expect(result.data).toEqual({ + expect(result.data).toStrictEqual({ 'austrian-cities': ['Vienna', 'Graz', 'Linz', 'Salzburg'], 'european-cities': { all: { + all: null, austria: ['Vienna', 'Graz', 'Linz', 'Salzburg'], }, austria: ['Vienna', 'Graz', 'Linz', 'Salzburg'], @@ -230,10 +231,10 @@ european-cities: &cities - *foo `); - expect(result.data).toEqual({ + expect(result.data).toStrictEqual({ a: [ { - b: [true, { c: [true, { c: undefined }, undefined] }, [{ b: [true, { c: undefined }, undefined] }]], + b: [true, { c: [true, { c: null }, null] }, [{ b: [true, { c: null }, null] }]], }, ], }); diff --git a/src/parseWithPointers.ts b/src/parseWithPointers.ts index 3dc3ca5..96e8b43 100644 --- a/src/parseWithPointers.ts +++ b/src/parseWithPointers.ts @@ -1,4 +1,4 @@ -import { DiagnosticSeverity, IDiagnostic, Optional } from '@stoplight/types'; +import { DiagnosticSeverity, IDiagnostic } from '@stoplight/types'; import { determineScalarType, load as loadAST, @@ -15,11 +15,10 @@ import { Kind, ScalarType, YAMLAnchorReference, - YAMLMap, + YAMLMapping, YAMLNode, YamlParserResult, YAMLScalar, - YAMLSequence, } from './types'; import { isObject } from './utils'; @@ -114,7 +113,7 @@ export const walkAST = ( return getScalarValue(node); case Kind.ANCHOR_REF: { if (isObject(node.value) && isCircularAnchorRef(node)) { - node.value = (dereferenceAnchor(node.value, node.referencesAnchor) as YAMLNode) || void 0; + node.value = dereferenceAnchor(node.value, node.referencesAnchor)!; } return node.value && walkAST(node.value, options, duplicatedMappingKeys); @@ -140,28 +139,28 @@ const isCircularAnchorRef = (anchorRef: YAMLAnchorReference) => { return false; }; -const dereferenceAnchor = (node: YAMLNode | null, anchorId: string): Optional => { +const dereferenceAnchor = (node: YAMLNode | null, anchorId: string): YAMLNode | null => { if (!isObject(node)) return node; - if (node.kind === Kind.ANCHOR_REF && node.referencesAnchor === anchorId) return; + 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 YAMLMap; + mappings: node.mappings.map(mapping => dereferenceAnchor(mapping, anchorId) as YAMLMapping), + }; case Kind.SEQ: return { ...node, - items: node.items.map(item => dereferenceAnchor(item, anchorId)), - } as YAMLSequence; + items: node.items.map(item => dereferenceAnchor(item, anchorId)!), + }; case Kind.MAPPING: - return { ...node, value: dereferenceAnchor(node.value, anchorId) as YAMLNode }; + return { ...node, value: dereferenceAnchor(node.value, anchorId) }; case Kind.SCALAR: return node; case Kind.ANCHOR_REF: if (isObject(node.value) && isCircularAnchorRef(node)) { - return; + return null; } return node;