diff --git a/src/ast/nodes.ts b/src/ast/nodes.ts index 5ebf703..d652e4e 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" -// 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 671a2bc..81bb75b 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( diff --git a/test/index.js b/test/index.js index e4d898b..bf60f36 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, "*\n * second\n ") + assert.strictEqual(comments[1].loc.start.line, 9) + + // Verify comments are sorted by range + assert.ok(comments[0].range[0] < comments[1].range[0]) + }) }) })
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: