Skip to content

Commit

Permalink
(fix) retrieval of comment above script/style (sveltejs#206)
Browse files Browse the repository at this point in the history
Search of comment belonging to those blocks was wrong
Fixes sveltejs#205
  • Loading branch information
dummdidumm authored Feb 15, 2021
1 parent 0aa2dc7 commit 0fb3ccd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# prettier-plugin-svelte changelog

## 2.1.5 (Unreleased)

* Fix retrieval of comment belonging to script/style block ([#205](https://github.com/sveltejs/prettier-plugin-svelte/issues/205))

## 2.1.4

* Don't print an empty line at the end of code embedded inside Markdown (further fixes) ([#202](https://github.com/sveltejs/prettier-plugin-svelte/issues/202))
Expand Down
14 changes: 9 additions & 5 deletions src/print/node-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,16 @@ export function getLeadingComment(path: FastPath): CommentNode | undefined {

let node: Node = path.getNode();
let prev: Node | undefined = siblings.find((child) => child.end === node.start);
while (prev && (prev.type !== 'Comment' || isEmptyTextNode(prev))) {
node = prev;
prev = siblings.find((child) => child.end === node.start);
while (prev) {
if (prev.type === 'Comment') {
return prev;
} else if (isEmptyTextNode(prev)) {
node = prev;
prev = siblings.find((child) => child.end === node.start);
} else {
return undefined;
}
}

return prev && prev.type === 'Comment' ? prev : undefined;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions test/printer/samples/comments-above-html-block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script></script>

<!-- asd -->
<div />

<style></style>

0 comments on commit 0fb3ccd

Please sign in to comment.