Repository: sveltejs/prettier-plugin-svelte (ts)

Issue:
HTML comment is duplicated when using Format on Save
When enabling "Format on Save" in VS Code settings, recently I've noticed that some comments are being duplicated on save. Here are two images:

Before:
![Screen Shot 2021-02-14 at 9 42 46 AM](https://user-images.githubusercontent.com/51680874/107887318-5cc6e880-6ea9-11eb-8318-4d40408bc00a.png)

After:
![Screen Shot 2021-02-14 at 9 43 00 AM](https://user-images.githubusercontent.com/51680874/107887320-618b9c80-6ea9-11eb-83c5-f198c221c051.png)

I didn't have this issue before and I can only prevent it by disabling "Format on Save" or unchecking "Format: Enable" in Settings under Extensions > Svelte.

<!-- Before you submit a bug, please make sure that:
- you have searched and found no existing open issue with the problem at hand
- you don't have   "files.associations": {"*.svelte": "html"  }   inside your VSCode settings (if you can't remember ever doing that, you don't have that)
- you are using Svelte for Vscode (NOT the old "Svelte" by James Birtles) and have disabled all other Svelte-related extensions to reproduce the bug
- if it's a preprocessor related bug like "can't use typescript", did you setup `svelte-preprocess` and/or `svelte.config.js`? See the docs for more info.
-->

Pull request:
(fix) retrieval of comment above script/style
Search of comment belonging to those blocks was wrong
Fixes #205

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 779a384..801d24a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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))
diff --git a/src/print/node-helpers.ts b/src/print/node-helpers.ts
index 8f0776b..f040456 100644
--- a/src/print/node-helpers.ts
+++ b/src/print/node-helpers.ts
@@ -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;
 }
 
 /**


Test patch:
diff --git a/test/printer/samples/comments-above-html-block.html b/test/printer/samples/comments-above-html-block.html
new file mode 100644
index 0000000..9c6843e
--- /dev/null
+++ b/test/printer/samples/comments-above-html-block.html
@@ -0,0 +1,6 @@
+<script></script>
+
+<!-- asd -->
+<div />
+
+<style></style>