@@ -82,13 +82,15 @@ const renameElementTagInTemplate = ({
8282 offset,
8383 recorder,
8484 fromName,
85- toName
85+ toName,
86+ defaultAttributes
8687} : {
8788 recorder : UpdateRecorder ;
8889 template : string ;
8990 offset : number ;
9091 fromName : string ;
9192 toName : string ;
93+ defaultAttributes ?: { name : string ; value : string } [ ] ;
9294} ) : void => {
9395 findElement ( template , element => element . name === fromName ) . forEach ( el => {
9496 recorder . remove ( el . startSourceSpan . start . offset + 1 + offset , fromName . length ) ;
@@ -97,6 +99,17 @@ const renameElementTagInTemplate = ({
9799 recorder . remove ( el . endSourceSpan ?. start . offset + 2 + offset , fromName . length ) ;
98100 recorder . insertLeft ( el . endSourceSpan ?. start . offset + 2 + offset , toName ) ;
99101 }
102+
103+ if ( defaultAttributes && defaultAttributes . length > 0 ) {
104+ const existingAttrNames = new Set ( el . attrs . map ( attr => attr . name ) ) ;
105+ const attrsToAdd = defaultAttributes . filter ( attr => ! existingAttrNames . has ( attr . name ) ) ;
106+
107+ if ( attrsToAdd . length > 0 ) {
108+ const insertPosition = el . startSourceSpan . start . offset + 1 + offset + toName . length ;
109+ const attrsString = attrsToAdd . map ( attr => ` ${ attr . name } ="${ attr . value } "` ) . join ( '' ) ;
110+ recorder . insertLeft ( insertPosition , attrsString ) ;
111+ }
112+ }
100113 } ) ;
101114} ;
102115
@@ -130,15 +143,55 @@ const renameApiInTemplate = ({
130143 template : string ;
131144 offset : number ;
132145 elementName : string ;
133- apis : { replace : string ; replaceWith : string } [ ] ;
146+ apis : { replace : string ; replaceWith : string | string [ ] } [ ] ;
134147} ) : void => {
135148 findElement ( template , element => element . name === elementName ) . forEach ( el => {
136149 for ( const api of apis ) {
137150 el . attrs
138- . filter ( attr => attr . name === api . replace )
151+ . filter (
152+ attr =>
153+ attr . name === api . replace ||
154+ attr . name === `[${ api . replace } ]` ||
155+ attr . name === `(${ api . replace } )`
156+ )
139157 . forEach ( attr => {
140- recorder . remove ( attr . sourceSpan . start . offset + offset , api . replace . length ) ;
141- recorder . insertLeft ( attr . sourceSpan . start . offset + offset , api . replaceWith ) ;
158+ const hasBrackets = attr . name . startsWith ( '[' ) || attr . name . startsWith ( '(' ) ;
159+ const isArray = Array . isArray ( api . replaceWith ) ;
160+
161+ if ( isArray ) {
162+ const valueStart = attr . valueSpan ?. start . offset ;
163+ const valueEnd = attr . valueSpan ?. end . offset ;
164+
165+ if ( ! valueStart || ! valueEnd ) {
166+ return ;
167+ }
168+
169+ const value = template . substring ( valueStart , valueEnd ) ;
170+ const attrLength = attr . sourceSpan . toString ( ) . length ;
171+
172+ // Remove the original attribute
173+ recorder . remove ( attr . sourceSpan . start . offset + offset , attrLength ) ;
174+
175+ // Insert new attributes
176+ const newAttrs = ( api . replaceWith as string [ ] )
177+ . map ( ( newName : string ) => {
178+ const attrName = hasBrackets ? `[${ newName } ]` : newName ;
179+ return `${ attrName } ="${ value } "` ;
180+ } )
181+ . join ( ' ' ) ;
182+
183+ recorder . insertLeft ( attr . sourceSpan . start . offset + offset , newAttrs ) ;
184+ } else {
185+ const newName = api . replaceWith as string ;
186+ if ( hasBrackets ) {
187+ const startOffset = attr . sourceSpan . start . offset + offset + 1 ;
188+ recorder . remove ( startOffset , api . replace . length ) ;
189+ recorder . insertLeft ( startOffset , newName ) ;
190+ } else {
191+ recorder . remove ( attr . sourceSpan . start . offset + offset , api . replace . length ) ;
192+ recorder . insertLeft ( attr . sourceSpan . start . offset + offset , newName ) ;
193+ }
194+ }
142195 } ) ;
143196 }
144197 } ) ;
@@ -187,6 +240,7 @@ interface RenameElementTagParams {
187240 recorder : UpdateRecorder ;
188241 fromName : string ;
189242 toName : string ;
243+ defaultAttributes ?: { name : string ; value : string } [ ] ;
190244}
191245
192246export const renameElementTag = ( {
@@ -195,15 +249,17 @@ export const renameElementTag = ({
195249 sourceFile,
196250 recorder,
197251 fromName,
198- toName
252+ toName,
253+ defaultAttributes
199254} : RenameElementTagParams ) : void => {
200255 getInlineTemplates ( sourceFile ) . forEach ( template =>
201256 renameElementTagInTemplate ( {
202257 template : sourceFile . text . substring ( template . getStart ( ) + 1 , template . getEnd ( ) - 1 ) ,
203258 offset : template . getStart ( ) + 1 ,
204259 toName,
205260 fromName,
206- recorder
261+ recorder,
262+ defaultAttributes
207263 } )
208264 ) ;
209265 getTemplateUrl ( sourceFile ) . forEach ( templateUrl => {
@@ -215,7 +271,8 @@ export const renameElementTag = ({
215271 offset : 0 ,
216272 toName,
217273 fromName,
218- recorder : templateRecorder
274+ recorder : templateRecorder ,
275+ defaultAttributes
219276 } ) ;
220277 tree . commitUpdate ( templateRecorder ) ;
221278 } ) ;
@@ -266,7 +323,7 @@ export const renameApi = ({
266323 sourceFile : ts . SourceFile ;
267324 recorder : UpdateRecorder ;
268325 elementName : string ;
269- apis : { replace : string ; replaceWith : string } [ ] ;
326+ apis : { replace : string ; replaceWith : string | string [ ] } [ ] ;
270327} ) : void => {
271328 getInlineTemplates ( sourceFile ) . forEach ( template =>
272329 renameApiInTemplate ( {
0 commit comments