@@ -13,6 +13,7 @@ import {
1313} from '../compileMatcher/Placeholder'
1414import createReplacementConverter , { bulkConvert } from '../convertReplacement'
1515import cloneNode from '../util/cloneNode'
16+ import transferComments from '../util/transferComments'
1617export { unescapeIdentifier }
1718
1819export function compileArrayPlaceholderReplacement (
@@ -25,18 +26,25 @@ export function compileArrayPlaceholderReplacement(
2526 getArrayPlaceholder ( identifier ) || getRestPlaceholder ( identifier )
2627 if ( arrayPlaceholder && isCapturePlaceholder ( arrayPlaceholder ) ) {
2728 const convertReplacement = createReplacementConverter ( pattern )
29+
30+ const baseGenerate = ( match : ReplaceableMatch ) : Node | Node [ ] => {
31+ const captures = match . arrayCaptures ?. [ arrayPlaceholder ]
32+ if ( captures ) {
33+ return [
34+ ...bulkConvert (
35+ captures . map ( ( c ) => cloneNode ( c ) ) ,
36+ convertReplacement
37+ ) ,
38+ ]
39+ }
40+ return [ ...bulkConvert ( cloneNode ( pattern . value ) , convertReplacement ) ]
41+ }
42+
2843 return {
2944 generate : ( match : ReplaceableMatch ) : Node | Node [ ] => {
30- const captures = match . arrayCaptures ?. [ arrayPlaceholder ]
31- if ( captures ) {
32- return [
33- ...bulkConvert (
34- captures . map ( ( c ) => cloneNode ( c ) ) ,
35- convertReplacement
36- ) ,
37- ]
38- }
39- return [ ...bulkConvert ( cloneNode ( pattern . value ) , convertReplacement ) ]
45+ const result = baseGenerate ( match )
46+ transferComments ( pattern . node , result )
47+ return result
4048 } ,
4149 }
4250 }
@@ -50,16 +58,22 @@ export default function compilePlaceholderReplacement(
5058 const placeholder = getPlaceholder ( identifier )
5159 if ( placeholder && isCapturePlaceholder ( placeholder ) ) {
5260 const convertReplacement = createReplacementConverter ( pattern )
61+ const baseGenerate = ( match : ReplaceableMatch ) : Node | Node [ ] => {
62+ const capture = match . captures ?. [ placeholder ]
63+ if ( capture ) {
64+ const clone = cloneNode ( capture )
65+ const astx = getAstxMatchInfo ( capture )
66+ if ( astx ?. subcapture ) return convertReplacement ( astx . subcapture )
67+ return convertReplacement ( clone )
68+ }
69+ return convertReplacement ( cloneNode ( pattern . value ) )
70+ }
71+
5372 return {
5473 generate : ( match : ReplaceableMatch ) : Node | Node [ ] => {
55- const capture = match . captures ?. [ placeholder ]
56- if ( capture ) {
57- const clone = cloneNode ( capture )
58- const astx = getAstxMatchInfo ( capture )
59- if ( astx ?. subcapture ) return convertReplacement ( astx . subcapture )
60- return convertReplacement ( clone )
61- }
62- return convertReplacement ( cloneNode ( pattern . value ) )
74+ const result = baseGenerate ( match )
75+ transferComments ( pattern . node , result )
76+ return result
6377 } ,
6478 }
6579 }
0 commit comments