@@ -5,14 +5,27 @@ import { tsPlugin } from '@sveltejs/acorn-typescript';
55
66const ParserWithTS = acorn . Parser . extend ( tsPlugin ( ) ) ;
77
8+ /**
9+ * @typedef {Comment & {
10+ * start: number;
11+ * end: number;
12+ * }} CommentWithLocation
13+ */
14+
815/**
916 * @param {string } source
17+ * @param {Comment[] } comments
1018 * @param {boolean } typescript
1119 * @param {boolean } [is_script]
1220 */
13- export function parse ( source , typescript , is_script ) {
21+ export function parse ( source , comments , typescript , is_script ) {
1422 const parser = typescript ? ParserWithTS : acorn . Parser ;
15- const { onComment, add_comments } = get_comment_handlers ( source ) ;
23+
24+ const { onComment, add_comments } = get_comment_handlers (
25+ source ,
26+ /** @type {CommentWithLocation[] } */ ( comments )
27+ ) ;
28+
1629 // @ts -ignore
1730 const parse_statement = parser . prototype . parseStatement ;
1831
@@ -53,13 +66,18 @@ export function parse(source, typescript, is_script) {
5366
5467/**
5568 * @param {string } source
69+ * @param {Comment[] } comments
5670 * @param {boolean } typescript
5771 * @param {number } index
5872 * @returns {acorn.Expression & { leadingComments?: CommentWithLocation[]; trailingComments?: CommentWithLocation[]; } }
5973 */
60- export function parse_expression_at ( source , typescript , index ) {
74+ export function parse_expression_at ( source , comments , typescript , index ) {
6175 const parser = typescript ? ParserWithTS : acorn . Parser ;
62- const { onComment, add_comments } = get_comment_handlers ( source ) ;
76+
77+ const { onComment, add_comments } = get_comment_handlers (
78+ source ,
79+ /** @type {CommentWithLocation[] } */ ( comments )
80+ ) ;
6381
6482 const ast = parser . parseExpressionAt ( source , index , {
6583 onComment,
@@ -78,26 +96,17 @@ export function parse_expression_at(source, typescript, index) {
7896 * to add them after the fact. They are needed in order to support `svelte-ignore` comments
7997 * in JS code and so that `prettier-plugin-svelte` doesn't remove all comments when formatting.
8098 * @param {string } source
99+ * @param {CommentWithLocation[] } comments
81100 */
82- function get_comment_handlers ( source ) {
83- /**
84- * @typedef {Comment & {
85- * start: number;
86- * end: number;
87- * }} CommentWithLocation
88- */
89-
90- /** @type {CommentWithLocation[] } */
91- const comments = [ ] ;
92-
101+ function get_comment_handlers ( source , comments ) {
93102 return {
94103 /**
95104 * @param {boolean } block
96105 * @param {string } value
97106 * @param {number } start
98107 * @param {number } end
99108 */
100- onComment : ( block , value , start , end ) => {
109+ onComment : ( block , value , start , end , start_loc , end_loc ) => {
101110 if ( block && / \n / . test ( value ) ) {
102111 let a = start ;
103112 while ( a > 0 && source [ a - 1 ] !== '\n' ) a -= 1 ;
@@ -109,13 +118,21 @@ function get_comment_handlers(source) {
109118 value = value . replace ( new RegExp ( `^${ indentation } ` , 'gm' ) , '' ) ;
110119 }
111120
112- comments . push ( { type : block ? 'Block' : 'Line' , value, start, end } ) ;
121+ comments . push ( {
122+ type : block ? 'Block' : 'Line' ,
123+ value,
124+ start,
125+ end,
126+ loc : { start : start_loc , end : end_loc }
127+ } ) ;
113128 } ,
114129
115130 /** @param {acorn.Node & { leadingComments?: CommentWithLocation[]; trailingComments?: CommentWithLocation[]; } } ast */
116131 add_comments ( ast ) {
117132 if ( comments . length === 0 ) return ;
118133
134+ comments = comments . slice ( ) ;
135+
119136 walk ( ast , null , {
120137 _ ( node , { next, path } ) {
121138 let comment ;
0 commit comments