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

HTML: Fix format on files with frontMatter #9043

Merged
merged 1 commit into from Aug 25, 2020
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
23 changes: 23 additions & 0 deletions changelog_unreleased/html/pr-9043.md
@@ -0,0 +1,23 @@
#### Fix format on html with frontMatter ([#9043](https://github.com/prettier/prettier/pull/9043) by [@fisker](https://github.com/fisker))

<!-- prettier-ignore -->
```html
<!-- Input -->
---
layout: foo
---

Test <a
href="https://prettier.io">abc</a>.

<!-- Prettier stable -->
TypeError: Cannot read property 'end' of undefined
...

<!-- Prettier master -->
---
layout: foo
---

Test <a href="https://prettier.io">abc</a>.
```
4 changes: 4 additions & 0 deletions src/language-html/parser-html.js
Expand Up @@ -280,6 +280,10 @@ function _parse(text, options, parserOptions, shouldParseFrontMatter = true) {
};

if (frontMatter) {
frontMatter.sourceSpan = {
start: { offset: 0 },
end: { offset: frontMatter.raw.length },
};
rawAst.children.unshift(frontMatter);
}

Expand Down
45 changes: 45 additions & 0 deletions tests/html/front-matter/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -28,3 +28,48 @@ slug: home

================================================================================
`;

exports[`issue-9042.html format 1`] = `
====================================options=====================================
parsers: ["html"]
printWidth: 80
| printWidth
=====================================input======================================
---
layout: foo
---

Test <a
href="https://prettier.io">abc</a>.

=====================================output=====================================
---
layout: foo
---

Test <a href="https://prettier.io">abc</a>.

================================================================================
`;

exports[`issue-9042-no-empty-line.html format 1`] = `
====================================options=====================================
parsers: ["html"]
printWidth: 80
| printWidth
=====================================input======================================
---
layout: foo
---
Test <a
href="https://prettier.io">abc</a>.

=====================================output=====================================
---
layout: foo
---

Test <a href="https://prettier.io">abc</a>.

================================================================================
`;
5 changes: 5 additions & 0 deletions tests/html/front-matter/issue-9042-no-empty-line.html
@@ -0,0 +1,5 @@
---
layout: foo
---
Test <a
href="https://prettier.io">abc</a>.
6 changes: 6 additions & 0 deletions tests/html/front-matter/issue-9042.html
@@ -0,0 +1,6 @@
---
layout: foo
---

Test <a
href="https://prettier.io">abc</a>.