Skip to content

Commit

Permalink
Fix format on html with frontMatter
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Aug 25, 2020
1 parent 7e5f6b9 commit 8065155
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
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>.

0 comments on commit 8065155

Please sign in to comment.