Skip to content

Commit

Permalink
feat(yaml): preserve the first document head end marker (#5502)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 25, 2018
1 parent a50a8e2 commit ab7f46b
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/language-yaml/printer-yaml.js
Expand Up @@ -169,7 +169,12 @@ function _print(node, parentNode, path, options, print) {
return join(
hardline,
[
shouldPrintDocumentHeadEndMarker(node, nextDocument) === "head"
shouldPrintDocumentHeadEndMarker(
node,
nextDocument,
parentNode,
options
) === "head"
? join(
hardline,
[
Expand Down Expand Up @@ -582,8 +587,24 @@ function shouldPrintDocumentEndMarker(document, nextDocument) {
);
}

function shouldPrintDocumentHeadEndMarker(document, nextDocument) {
function shouldPrintDocumentHeadEndMarker(
document,
nextDocument,
root,
options
) {
if (
/**
* ---
* preserve the first document head end marker
*/
(root.children[0] === document &&
/---(\s|$)/.test(
options.originalText.slice(
options.locStart(document),
options.locStart(document) + 4
)
)) ||
/**
* %DIRECTIVE
* ---
Expand Down
25 changes: 25 additions & 0 deletions tests/yaml_document/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -54,3 +54,28 @@ f
g
`;

exports[`with-document-head.yml - yaml-verify 1`] = `
---
123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
123
`;

exports[`with-document-head-like.yml - yaml-verify 1`] = `
---666
123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---666
123
`;

exports[`without-document-head.yml - yaml-verify 1`] = `
123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123
`;
2 changes: 2 additions & 0 deletions tests/yaml_document/with-document-head-like.yml
@@ -0,0 +1,2 @@
---666
123
2 changes: 2 additions & 0 deletions tests/yaml_document/with-document-head.yml
@@ -0,0 +1,2 @@
---
123
1 change: 1 addition & 0 deletions tests/yaml_document/without-document-head.yml
@@ -0,0 +1 @@
123
2 changes: 2 additions & 0 deletions tests/yaml_mapping/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -302,6 +302,7 @@ exports[`props.yml - yaml-verify 1`] = `
--- !!map &anchor
a: 123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
!!map &anchor
a: 123
Expand All @@ -311,6 +312,7 @@ exports[`props.yml - yaml-verify 2`] = `
--- !!map &anchor
a: 123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
!!map &anchor
a: 123
Expand Down
3 changes: 3 additions & 0 deletions tests/yaml_root/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -7,6 +7,7 @@ exports[`documents.yml - yaml-verify 1`] = `
---
456
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
123
---
456
Expand All @@ -21,6 +22,7 @@ exports[`documents-with-directive.yml - yaml-verify 1`] = `
---
456
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
123
...
%YAML 1.2
Expand Down Expand Up @@ -241,6 +243,7 @@ foobar: !sexy
- bunny
- chocolate
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
# Collection Types #############################################################
################################################################################
Expand Down
2 changes: 2 additions & 0 deletions tests/yaml_sequence/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -113,6 +113,7 @@ exports[`props.yml - yaml-verify 1`] = `
- 123
- 456
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
!!set &anchor
- 123
- 456
Expand All @@ -124,6 +125,7 @@ exports[`props.yml - yaml-verify 2`] = `
- 123
- 456
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
!!set &anchor
- 123
- 456
Expand Down

0 comments on commit ab7f46b

Please sign in to comment.