File tree Expand file tree Collapse file tree 3 files changed +42
-1
lines changed Expand file tree Collapse file tree 3 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,6 @@ import type { ScopeManager } from "eslint-scope"
7
7
import type { ParseError } from "./errors"
8
8
import type { HasLocation } from "./locations"
9
9
import type { Token } from "./tokens"
10
- // eslint-disable-next-line node/no-extraneous-import -- ignore
11
10
import type { TSESTree } from "@typescript-eslint/utils"
12
11
13
12
//------------------------------------------------------------------------------
Original file line number Diff line number Diff line change @@ -308,6 +308,10 @@ export function parseScriptSetupElements(
308
308
}
309
309
result . ast . tokens . sort ( ( a , b ) => a . range [ 0 ] - b . range [ 0 ] )
310
310
}
311
+
312
+ if ( result . ast . comments != null ) {
313
+ result . ast . comments . sort ( ( a , b ) => a . range [ 0 ] - b . range [ 0 ] )
314
+ }
311
315
result . ast . body . sort ( ( a , b ) => a . range [ 0 ] - b . range [ 0 ] )
312
316
313
317
const programStartOffset = result . ast . body . reduce (
Original file line number Diff line number Diff line change @@ -896,6 +896,44 @@ describe("Basic tests", async () => {
896
896
assert . strictEqual ( messages . length , 1 )
897
897
assert . strictEqual ( messages [ 0 ] . message , "'c' is not defined." )
898
898
} )
899
+
900
+ it ( "should sort comments by their original source position" , ( ) => {
901
+ const code = `<script lang="ts" setup>
902
+ const test = () => {
903
+ // first
904
+ return false
905
+ }
906
+ </script>
907
+
908
+ <script lang="ts">
909
+ /**
910
+ * second
911
+ */
912
+ export default {}
913
+ </script>
914
+
915
+ <template>
916
+ <div @click="test" />
917
+ </template>`
918
+
919
+ const result = parseForESLint ( code , { sourceType : "module" } )
920
+ const comments = result . ast . comments
921
+
922
+ // Should have 2 comments
923
+ assert . strictEqual ( comments . length , 2 )
924
+
925
+ // Comments should be sorted by their original position in source code
926
+ assert . strictEqual ( comments [ 0 ] . type , "Line" )
927
+ assert . strictEqual ( comments [ 0 ] . value , " first" )
928
+ assert . strictEqual ( comments [ 0 ] . loc . start . line , 3 )
929
+
930
+ assert . strictEqual ( comments [ 1 ] . type , "Block" )
931
+ assert . strictEqual ( comments [ 1 ] . value , "*\n * second\n " )
932
+ assert . strictEqual ( comments [ 1 ] . loc . start . line , 9 )
933
+
934
+ // Verify comments are sorted by range
935
+ assert . ok ( comments [ 0 ] . range [ 0 ] < comments [ 1 ] . range [ 0 ] )
936
+ } )
899
937
} )
900
938
} )
901
939
You can’t perform that action at this time.
0 commit comments