Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/ast/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

//------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions src/script-setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
38 changes: 38 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<script lang="ts" setup>
const test = () => {
// first
return false
}
</script>

<script lang="ts">
/**
* second
*/
export default {}
</script>

<template>
<div @click="test" />
</template>`

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])
})
})
})

Expand Down