Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(yaml): preserve the first document head end marker #5502

Merged
merged 2 commits into from Nov 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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