From f41755082f8a82d9f38bb9d91a98d7c501a4feb8 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 9 Sep 2020 01:12:54 +0800 Subject: [PATCH 1/2] test: add failing tests --- src/__snapshots__/attach.test.ts.snap | 143 ++++++++++++++++++++++++++ src/attach.test.ts | 24 +++++ 2 files changed, 167 insertions(+) diff --git a/src/__snapshots__/attach.test.ts.snap b/src/__snapshots__/attach.test.ts.snap index 95d1a46..fd6805b 100644 --- a/src/__snapshots__/attach.test.ts.snap +++ b/src/__snapshots__/attach.test.ts.snap @@ -44,6 +44,47 @@ mappingValue (2:1 ~ 2:2) `; +exports[`"?\\n # comment 1\\nb:\\n # comment 2" 1`] = ` +Error: Unexpected node type undefined +`; + +exports[`"?\\n # comment 1\\nb:\\n # comment 2" 2`] = ` +comment (4:3 ~ 4:14) +2 | ··#·comment·1¶ +3 | b:¶ +4 | ··#·comment·2¶ + | ^^^^^^^^^^^ +mappingValue (3:2 ~ 4:14) + + + +`; + +exports[`"?\\n # comment 1\\nbb:\\n # comment 2" 1`] = ` +comment (2:3 ~ 2:14) +1 | ?¶ +2 | ··#·comment·1¶ + | ^^^^^^^^^^^ +3 | bb:¶ +4 | ··#·comment·2¶ +mappingItem (3:1 ~ 4:14) + + + +`; + +exports[`"?\\n # comment 1\\nbb:\\n # comment 2" 2`] = ` +comment (4:3 ~ 4:14) +2 | ··#·comment·1¶ +3 | bb:¶ +4 | ··#·comment·2¶ + | ^^^^^^^^^^^ +mappingValue (3:3 ~ 4:14) + + + +`; + exports[`"\\n# 123\\n%YAML 1.2\\n# 456\\n# 789\\n---\\n" 1`] = ` comment (2:1 ~ 2:6) 1 | ¶ @@ -1537,6 +1578,108 @@ mappingValue (2:7 ~ 4:11) `; +exports[`"- a\\n # comment 1\\n- b\\n # comment 2" 1`] = ` +comment (2:3 ~ 2:14) +1 | -·a¶ +2 | ··#·comment·1¶ + | ^^^^^^^^^^^ +3 | -·b¶ +4 | ··#·comment·2¶ +sequenceItem (1:1 ~ 2:14) + + + +`; + +exports[`"- a\\n # comment 1\\n- b\\n # comment 2" 2`] = ` +comment (4:3 ~ 4:14) +2 | ··#·comment·1¶ +3 | -·b¶ +4 | ··#·comment·2¶ + | ^^^^^^^^^^^ +sequenceItem (3:1 ~ 4:14) + + + +`; + +exports[`"- a\\n # comment 1\\n- bb\\n # comment 2" 1`] = ` +comment (2:3 ~ 2:14) +1 | -·a¶ +2 | ··#·comment·1¶ + | ^^^^^^^^^^^ +3 | -·bb¶ +4 | ··#·comment·2¶ +sequenceItem (3:1 ~ 4:14) + + + + +`; + +exports[`"- a\\n # comment 1\\n- bb\\n # comment 2" 2`] = ` +comment (4:3 ~ 4:14) +2 | ··#·comment·1¶ +3 | -·bb¶ +4 | ··#·comment·2¶ + | ^^^^^^^^^^^ +sequenceItem (3:1 ~ 4:14) + + + + +`; + +exports[`"a:\\n # comment 1\\nb:\\n # comment 2" 1`] = ` +comment (2:3 ~ 2:14) +1 | a:¶ +2 | ··#·comment·1¶ + | ^^^^^^^^^^^ +3 | b:¶ +4 | ··#·comment·2¶ +mappingValue (1:2 ~ 2:14) + + + +`; + +exports[`"a:\\n # comment 1\\nb:\\n # comment 2" 2`] = ` +comment (4:3 ~ 4:14) +2 | ··#·comment·1¶ +3 | b:¶ +4 | ··#·comment·2¶ + | ^^^^^^^^^^^ +mappingValue (3:2 ~ 4:14) + + + +`; + +exports[`"a:\\n # comment 1\\nbb:\\n # comment 2" 1`] = ` +comment (2:3 ~ 2:14) +1 | a:¶ +2 | ··#·comment·1¶ + | ^^^^^^^^^^^ +3 | bb:¶ +4 | ··#·comment·2¶ +mappingItem (3:1 ~ 4:14) + + + +`; + +exports[`"a:\\n # comment 1\\nbb:\\n # comment 2" 2`] = ` +comment (4:3 ~ 4:14) +2 | ··#·comment·1¶ +3 | bb:¶ +4 | ··#·comment·2¶ + | ^^^^^^^^^^^ +mappingValue (3:3 ~ 4:14) + + + +`; + exports[`"a:\\n [\\n foo,\\n # end comment\\n ]" 1`] = ` comment (4:5 ~ 4:18) 2 | ··[¶ diff --git a/src/attach.test.ts b/src/attach.test.ts index fad0bdd..9407272 100644 --- a/src/attach.test.ts +++ b/src/attach.test.ts @@ -321,6 +321,30 @@ A: }`, `# --`, `# ---`, + `- a + # comment 1 +- b + # comment 2`, + `- a + # comment 1 +- bb + # comment 2`, + `a: + # comment 1 +b: + # comment 2`, + `a: + # comment 1 +bb: + # comment 2`, + `? + # comment 1 +b: + # comment 2`, + `? + # comment 1 +bb: + # comment 2`, ]; testCases(cases.map((x): TestCaseSingle => [x, selector]), { From e2105f5ed684a3f487dc9f110f0c91aa97830023 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 9 Sep 2020 01:15:28 +0800 Subject: [PATCH 2/2] fix(attach): end comments for the first item in block mapping/sequence --- src/__snapshots__/attach.test.ts.snap | 33 ++++++++++++++++----------- src/attach.ts | 27 ++++++++++++++++++---- src/transform.ts | 2 +- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/__snapshots__/attach.test.ts.snap b/src/__snapshots__/attach.test.ts.snap index fd6805b..ea0feda 100644 --- a/src/__snapshots__/attach.test.ts.snap +++ b/src/__snapshots__/attach.test.ts.snap @@ -45,7 +45,16 @@ mappingValue (2:1 ~ 2:2) `; exports[`"?\\n # comment 1\\nb:\\n # comment 2" 1`] = ` -Error: Unexpected node type undefined +comment (2:3 ~ 2:14) +1 | ?¶ +2 | ··#·comment·1¶ + | ^^^^^^^^^^^ +3 | b:¶ +4 | ··#·comment·2¶ +mappingKey (1:1 ~ 2:14) + + + `; exports[`"?\\n # comment 1\\nb:\\n # comment 2" 2`] = ` @@ -67,10 +76,10 @@ comment (2:3 ~ 2:14) | ^^^^^^^^^^^ 3 | bb:¶ 4 | ··#·comment·2¶ -mappingItem (3:1 ~ 4:14) - - - +mappingKey (1:1 ~ 2:14) + + + `; exports[`"?\\n # comment 1\\nbb:\\n # comment 2" 2`] = ` @@ -1610,10 +1619,9 @@ comment (2:3 ~ 2:14) | ^^^^^^^^^^^ 3 | -·bb¶ 4 | ··#·comment·2¶ -sequenceItem (3:1 ~ 4:14) +sequenceItem (1:1 ~ 2:14) - - + `; @@ -1625,7 +1633,6 @@ comment (4:3 ~ 4:14) | ^^^^^^^^^^^ sequenceItem (3:1 ~ 4:14) - `; @@ -1662,10 +1669,10 @@ comment (2:3 ~ 2:14) | ^^^^^^^^^^^ 3 | bb:¶ 4 | ··#·comment·2¶ -mappingItem (3:1 ~ 4:14) - - - +mappingValue (1:2 ~ 2:14) + + + `; exports[`"a:\\n # comment 1\\nbb:\\n # comment 2" 2`] = ` diff --git a/src/attach.ts b/src/attach.ts index 95d9c29..f2d3601 100644 --- a/src/attach.ts +++ b/src/attach.ts @@ -3,6 +3,7 @@ import { Document, EndCommentAttachable, LeadingCommentAttachable, + MappingKey, Root, TrailingCommentAttachable, YamlUnistNode, @@ -162,6 +163,17 @@ function attachComment( currentNode = trailingNode; } + if (currentNode.type === "sequence" || currentNode.type === "mapping") { + currentNode = currentNode.children[0]; + } + + if (currentNode.type === "mappingItem") { + const [mappingKey, mappingValue] = currentNode.children; + currentNode = isExplicitMappingKey(mappingKey) + ? mappingKey + : mappingValue; + } + while (true) { if (shouldOwnEndComment(currentNode, comment)) { defineParents(comment, currentNode); @@ -226,13 +238,18 @@ function shouldOwnEndComment( (node.children.length === 0 || (node.children.length === 1 && node.children[0]!.type !== "blockFolded" && - node.children[0]!.type !== "blockLiteral" && - (node.type === "mappingValue" || - // explicit key - node.position.start.offset !== - node.children[0]!.position.start.offset))) + node.children[0]!.type !== "blockLiteral")) && + (node.type === "mappingValue" || isExplicitMappingKey(node)) ); default: return false; } } + +function isExplicitMappingKey(node: MappingKey) { + return ( + node.position.start !== node.position.end && + (node.children.length === 0 || + node.position.start.offset !== node.children[0]!.position.start.offset) + ); +} diff --git a/src/transform.ts b/src/transform.ts index ccfb605..837e44d 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -88,7 +88,7 @@ export function transformNode( node: YamlNode, context: Context, ): YamlUnistNode | null { - if (node === null) { + if (node === null || (node.type === undefined && node.value === null)) { return null; }