From f8ed32e9c6da24b5c0adea36c037501bae664420 Mon Sep 17 00:00:00 2001 From: waynzh Date: Mon, 23 Jun 2025 16:05:44 +0800 Subject: [PATCH 1/4] Fix comments order --- src/ast/nodes.ts | 2 +- src/script-setup/index.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ast/nodes.ts b/src/ast/nodes.ts index 5ebf7039..35f868df 100644 --- a/src/ast/nodes.ts +++ b/src/ast/nodes.ts @@ -7,7 +7,7 @@ import type { ScopeManager } from "eslint-scope" import type { ParseError } from "./errors" import type { HasLocation } from "./locations" import type { Token } from "./tokens" -// eslint-disable-next-line node/no-extraneous-import -- ignore + import type { TSESTree } from "@typescript-eslint/utils" //------------------------------------------------------------------------------ diff --git a/src/script-setup/index.ts b/src/script-setup/index.ts index 671a2bc5..81bb75bb 100644 --- a/src/script-setup/index.ts +++ b/src/script-setup/index.ts @@ -308,6 +308,10 @@ export function parseScriptSetupElements( } result.ast.tokens.sort((a, b) => a.range[0] - b.range[0]) } + + if (result.ast.comments != null) { + result.ast.comments.sort((a, b) => a.range[0] - b.range[0]) + } result.ast.body.sort((a, b) => a.range[0] - b.range[0]) const programStartOffset = result.ast.body.reduce( From 54e6e83287de73b1a7dd5ee571d2f6ec9f3ec004 Mon Sep 17 00:00:00 2001 From: waynzh Date: Mon, 23 Jun 2025 16:47:06 +0800 Subject: [PATCH 2/4] chore: update --- src/ast/nodes.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ast/nodes.ts b/src/ast/nodes.ts index 35f868df..d652e4e0 100644 --- a/src/ast/nodes.ts +++ b/src/ast/nodes.ts @@ -7,7 +7,6 @@ import type { ScopeManager } from "eslint-scope" import type { ParseError } from "./errors" import type { HasLocation } from "./locations" import type { Token } from "./tokens" - import type { TSESTree } from "@typescript-eslint/utils" //------------------------------------------------------------------------------ From 2893957fe99d80d3b84855e9c43bf41b02a6f2bc Mon Sep 17 00:00:00 2001 From: waynzh Date: Mon, 23 Jun 2025 17:03:29 +0800 Subject: [PATCH 3/4] Add test --- test/index.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/index.js b/test/index.js index e4d898b3..b68d827b 100644 --- a/test/index.js +++ b/test/index.js @@ -896,6 +896,44 @@ describe("Basic tests", async () => { assert.strictEqual(messages.length, 1) assert.strictEqual(messages[0].message, "'c' is not defined.") }) + + it("should sort comments by their original source position", () => { + const code = ` + + + +` + + const result = parseForESLint(code, { sourceType: "module" }) + const comments = result.ast.comments + + // Should have 2 comments + assert.strictEqual(comments.length, 2) + + // Comments should be sorted by their original position in source code + assert.strictEqual(comments[0].type, "Line") + assert.strictEqual(comments[0].value, " first") + assert.strictEqual(comments[0].loc.start.line, 3) + + assert.strictEqual(comments[1].type, "Block") + assert.strictEqual(comments[1].value, " second") + assert.strictEqual(comments[1].loc.start.line, 9) + + // Verify comments are sorted by range + assert.ok(comments[0].range[0] < comments[1].range[0]) + }) }) }) From 531ecbc8a8bca84f18309ee24d80fe50a1549154 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Tue, 24 Jun 2025 10:24:05 +0900 Subject: [PATCH 4/4] Update index.js --- test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index b68d827b..bf60f368 100644 --- a/test/index.js +++ b/test/index.js @@ -928,7 +928,7 @@ export default {} assert.strictEqual(comments[0].loc.start.line, 3) assert.strictEqual(comments[1].type, "Block") - assert.strictEqual(comments[1].value, " second") + assert.strictEqual(comments[1].value, "*\n * second\n ") assert.strictEqual(comments[1].loc.start.line, 9) // Verify comments are sorted by range