diff --git a/src/ast.ts b/src/ast.ts index db0409b..7d65e48 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -77,7 +77,7 @@ export interface YAMLWithMark extends BaseYAMLNode { type: "YAMLWithMark" anchor: YAMLAnchor | null tag: YAMLTag | null - value: YAMLContent + value: YAMLContent | null parent: YAMLDocument | YAMLPair | YAMLSequence } diff --git a/src/convert.ts b/src/convert.ts index 9cf2ca8..5bde6b4 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -340,7 +340,7 @@ function convertMapping( for (const n of node.children) { ast.pairs.push(convertMappingItem(n, tokens, code, ast, doc)) } - return convertAnchorAndTag(node, tokens, code, parent, ast, doc) + return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast) } /** @@ -364,7 +364,7 @@ function convertFlowMapping( for (const n of node.children) { ast.pairs.push(convertMappingItem(n, tokens, code, ast, doc)) } - return convertAnchorAndTag(node, tokens, code, parent, ast, doc) + return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast) } /** @@ -443,7 +443,7 @@ function convertSequence( for (const n of node.children) { ast.entries.push(...convertSequenceItem(n, tokens, code, ast, doc)) } - return convertAnchorAndTag(node, tokens, code, parent, ast, doc) + return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast) } /** @@ -481,7 +481,7 @@ function convertFlowSequence( ast.entries.push(map) } } - return convertAnchorAndTag(node, tokens, code, parent, ast, doc) + return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast) } /** @@ -510,40 +510,52 @@ function convertPlain( doc: YAMLDocument, ): YAMLPlainScalar | YAMLWithMark { const loc = getConvertLocation(node) - const strValue = node.value - let value: string | number | boolean | null - - if (isTrue(strValue)) { - value = true - } else if (isFalse(strValue)) { - value = false - } else if (isNull(strValue)) { - value = null - } else if (needParse(strValue)) { - value = yaml.parse(strValue) || strValue - } else { - value = strValue - } - const ast: YAMLPlainScalar = { - type: "YAMLScalar", - style: "plain", - strValue, - value, - parent, - ...loc, - } + if (loc.range[0] < loc.range[1]) { + const strValue = node.value + let value: string | number | boolean | null - const type = typeof value - if (type === "boolean") { - addToken(tokens, "Boolean", clone(loc), code) - } else if (type === "number" && isFinite(Number(value))) { - addToken(tokens, "Numeric", clone(loc), code) - } else if (value === null) { - addToken(tokens, "Null", clone(loc), code) - } else { - addToken(tokens, "Identifier", clone(loc), code) + if (isTrue(strValue)) { + value = true + } else if (isFalse(strValue)) { + value = false + } else if (isNull(strValue)) { + value = null + } else if (needParse(strValue)) { + value = yaml.parse(strValue) || strValue + } else { + value = strValue + } + const ast: YAMLPlainScalar = { + type: "YAMLScalar", + style: "plain", + strValue, + value, + parent, + ...loc, + } + + const type = typeof value + if (type === "boolean") { + addToken(tokens, "Boolean", clone(loc), code) + } else if (type === "number" && isFinite(Number(value))) { + addToken(tokens, "Numeric", clone(loc), code) + } else if (value === null) { + addToken(tokens, "Null", clone(loc), code) + } else { + addToken(tokens, "Identifier", clone(loc), code) + } + return convertAnchorAndTag(node, tokens, code, parent, ast, doc, loc) } - return convertAnchorAndTag(node, tokens, code, parent, ast, doc) + + return convertAnchorAndTag( + node, + tokens, + code, + parent, + null, + doc, + loc, + ) /** * Checks if the given string needs to be parsed @@ -592,7 +604,7 @@ function convertQuoteDouble( ...loc, } addToken(tokens, "String", clone(loc), code) - return convertAnchorAndTag(node, tokens, code, parent, ast, doc) + return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast) } /** @@ -617,7 +629,7 @@ function convertQuoteSingle( ...loc, } addToken(tokens, "String", clone(loc), code) - return convertAnchorAndTag(node, tokens, code, parent, ast, doc) + return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast) } /** @@ -698,7 +710,7 @@ function convertBlockLiteral( // ?? addToken(tokens, "BlockLiteral", clone(loc), code) } - return convertAnchorAndTag(node, tokens, code, parent, ast, doc) + return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast) } /** @@ -779,7 +791,7 @@ function convertBlockFolded( // ?? addToken(tokens, "BlockFolded", clone(loc), code) } - return convertAnchorAndTag(node, tokens, code, parent, ast, doc) + return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast) } /** @@ -825,7 +837,7 @@ function convertAlias( // ?? addToken(tokens, "Identifier", clone(loc), code) } - return convertAnchorAndTag(node, tokens, code, parent, ast, doc) + return convertAnchorAndTag(node, tokens, code, parent, ast, doc, ast) } /** @@ -836,8 +848,9 @@ function convertAnchorAndTag( tokens: Token[], code: string, parent: YAMLDocument | YAMLPair | YAMLSequence, - value: V, + value: V | null, doc: YAMLDocument, + valueLoc: Locations, ): YAMLWithMark | V { if (node.anchor || node.tag) { const ast: YAMLWithMark = { @@ -846,10 +859,12 @@ function convertAnchorAndTag( tag: null, value, parent, - range: clone(value.range), - loc: clone(value.loc), + range: clone(valueLoc.range), + loc: clone(valueLoc.loc), + } + if (value) { + value.parent = ast } - value.parent = ast if (node.anchor) { const anchor = convertAnchor(node.anchor, tokens, code, ast, doc) @@ -868,7 +883,7 @@ function convertAnchorAndTag( return ast } - return value + return value as any } /** diff --git a/src/utils.ts b/src/utils.ts index bfc2a0b..9dddeb2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -91,6 +91,9 @@ const resolver = { }, YAMLWithMark(node: YAMLWithMark) { if (node.tag) { + if (node.value == null) { + return getTaggedValue(node.tag, "", "") + } if (node.value.type === "YAMLScalar") { if (node.value.style === "plain") { return getTaggedValue( @@ -111,6 +114,9 @@ const resolver = { } } } + if (node.value == null) { + return null + } return getStaticYAMLValue(node.value) }, } diff --git a/tests/fixtures/parser/yaml-test-suite/FH7J-output.json b/tests/fixtures/parser/yaml-test-suite/FH7J-output.json index 3173cf1..3cea2cc 100644 --- a/tests/fixtures/parser/yaml-test-suite/FH7J-output.json +++ b/tests/fixtures/parser/yaml-test-suite/FH7J-output.json @@ -29,26 +29,7 @@ } } }, - "value": { - "type": "YAMLScalar", - "style": "plain", - "strValue": "", - "value": "", - "range": [ - 7, - 7 - ], - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 7 - } - } - }, + "value": null, "range": [ 2, 7 @@ -91,26 +72,7 @@ } } }, - "value": { - "type": "YAMLScalar", - "style": "plain", - "strValue": "", - "value": "", - "range": [ - 18, - 18 - ], - "loc": { - "start": { - "line": 3, - "column": 8 - }, - "end": { - "line": 3, - "column": 8 - } - } - }, + "value": null, "range": [ 12, 18 @@ -204,26 +166,7 @@ } } }, - "value": { - "type": "YAMLScalar", - "style": "plain", - "strValue": "", - "value": "", - "range": [ - 33, - 33 - ], - "loc": { - "start": { - "line": 4, - "column": 10 - }, - "end": { - "line": 4, - "column": 10 - } - } - }, + "value": null, "range": [ 28, 33 @@ -297,26 +240,7 @@ } } }, - "value": { - "type": "YAMLScalar", - "style": "plain", - "strValue": "", - "value": "", - "range": [ - 41, - 41 - ], - "loc": { - "start": { - "line": 5, - "column": 7 - }, - "end": { - "line": 5, - "column": 7 - } - } - }, + "value": null, "range": [ 36, 41 @@ -353,26 +277,7 @@ } } }, - "value": { - "type": "YAMLScalar", - "style": "plain", - "strValue": "", - "value": "", - "range": [ - 50, - 50 - ], - "loc": { - "start": { - "line": 5, - "column": 16 - }, - "end": { - "line": 5, - "column": 16 - } - } - }, + "value": null, "range": [ 44, 50 @@ -508,24 +413,6 @@ } } }, - { - "type": "Identifier", - "value": "", - "range": [ - 7, - 7 - ], - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 7 - } - } - }, { "type": "Punctuator", "value": "-", @@ -580,24 +467,6 @@ } } }, - { - "type": "Identifier", - "value": "", - "range": [ - 18, - 18 - ], - "loc": { - "start": { - "line": 3, - "column": 8 - }, - "end": { - "line": 3, - "column": 8 - } - } - }, { "type": "Punctuator", "value": ":", @@ -706,24 +575,6 @@ } } }, - { - "type": "Identifier", - "value": "", - "range": [ - 33, - 33 - ], - "loc": { - "start": { - "line": 4, - "column": 10 - }, - "end": { - "line": 4, - "column": 10 - } - } - }, { "type": "Punctuator", "value": "-", @@ -778,24 +629,6 @@ } } }, - { - "type": "Identifier", - "value": "", - "range": [ - 41, - 41 - ], - "loc": { - "start": { - "line": 5, - "column": 7 - }, - "end": { - "line": 5, - "column": 7 - } - } - }, { "type": "Punctuator", "value": ":", @@ -849,24 +682,6 @@ "column": 16 } } - }, - { - "type": "Identifier", - "value": "", - "range": [ - 50, - 50 - ], - "loc": { - "start": { - "line": 5, - "column": 16 - }, - "end": { - "line": 5, - "column": 16 - } - } } ], "range": [ diff --git a/tests/fixtures/parser/yaml-test-suite/LE5A-output.json b/tests/fixtures/parser/yaml-test-suite/LE5A-output.json index 03ec775..58ff418 100644 --- a/tests/fixtures/parser/yaml-test-suite/LE5A-output.json +++ b/tests/fixtures/parser/yaml-test-suite/LE5A-output.json @@ -182,26 +182,7 @@ } } }, - "value": { - "type": "YAMLScalar", - "style": "plain", - "strValue": "", - "value": "", - "range": [ - 49, - 49 - ], - "loc": { - "start": { - "line": 5, - "column": 7 - }, - "end": { - "line": 5, - "column": 7 - } - } - }, + "value": null, "range": [ 44, 49 @@ -539,24 +520,6 @@ "column": 7 } } - }, - { - "type": "Identifier", - "value": "", - "range": [ - 49, - 49 - ], - "loc": { - "start": { - "line": 5, - "column": 7 - }, - "end": { - "line": 5, - "column": 7 - } - } } ], "range": [ diff --git a/tests/fixtures/parser/yaml-test-suite/WZ62-output.json b/tests/fixtures/parser/yaml-test-suite/WZ62-output.json index 978a7d0..d8eb1f3 100644 --- a/tests/fixtures/parser/yaml-test-suite/WZ62-output.json +++ b/tests/fixtures/parser/yaml-test-suite/WZ62-output.json @@ -51,26 +51,7 @@ } } }, - "value": { - "type": "YAMLScalar", - "style": "plain", - "strValue": "", - "value": "", - "range": [ - 15, - 15 - ], - "loc": { - "start": { - "line": 2, - "column": 13 - }, - "end": { - "line": 2, - "column": 13 - } - } - }, + "value": null, "range": [ 10, 15 @@ -124,26 +105,7 @@ } } }, - "value": { - "type": "YAMLScalar", - "style": "plain", - "strValue": "", - "value": "", - "range": [ - 24, - 24 - ], - "loc": { - "start": { - "line": 3, - "column": 7 - }, - "end": { - "line": 3, - "column": 7 - } - } - }, + "value": null, "range": [ 19, 24 @@ -319,24 +281,6 @@ } } }, - { - "type": "Identifier", - "value": "", - "range": [ - 15, - 15 - ], - "loc": { - "start": { - "line": 2, - "column": 13 - }, - "end": { - "line": 2, - "column": 13 - } - } - }, { "type": "Punctuator", "value": ",", @@ -391,24 +335,6 @@ } } }, - { - "type": "Identifier", - "value": "", - "range": [ - 24, - 24 - ], - "loc": { - "start": { - "line": 3, - "column": 7 - }, - "end": { - "line": 3, - "column": 7 - } - } - }, { "type": "Punctuator", "value": ":",