@@ -109,7 +109,7 @@ export const ClassTransformVisitor = {
109109 /*!
110110 * Visits function calls.
111111 */
112- CallExpression ( path ) {
112+ "OptionalCallExpression| CallExpression" ( path ) {
113113 const { node } = path ;
114114 const { callee } = node ;
115115 // If the file already has sap.ui.define, get the names of variables it creates to use for the class logic.
@@ -122,9 +122,19 @@ export const ClassTransformVisitor = {
122122 if ( t . isSuper ( callee ) ) {
123123 replaceConstructorSuperCall ( path , node , this . superClassName ) ;
124124 } else if ( t . isSuper ( callee . object ) ) {
125- replaceObjectSuperCall ( path , node , this . superClassName ) ;
125+ replaceObjectSuperCall (
126+ path ,
127+ node ,
128+ this . superClassName ,
129+ path . isOptionalCallExpression ( )
130+ ) ;
126131 } else if ( isSuperApply ( callee ) ) {
127- replaceSuperApplyCall ( path , node , this . superClassName ) ;
132+ replaceSuperApplyCall (
133+ path ,
134+ node ,
135+ this . superClassName ,
136+ path . isOptionalCallExpression ( )
137+ ) ;
128138 }
129139 }
130140 } ,
@@ -175,24 +185,37 @@ function replaceConstructorSuperCall(path, node, superClassName) {
175185/**
176186 * Replace super.method() call
177187 */
178- function replaceObjectSuperCall ( path , node , superClassName ) {
179- replaceSuperNamedCall ( path , node , superClassName , node . callee . property . name ) ;
188+ function replaceObjectSuperCall ( path , node , superClassName , optional = false ) {
189+ replaceSuperNamedCall (
190+ path ,
191+ node ,
192+ superClassName ,
193+ node . callee . property . name ,
194+ optional
195+ ) ;
180196}
181197
182198/**
183199 * Replace super.method.apply() call
184200 */
185- function replaceSuperApplyCall ( path , node , superClassName ) {
201+ function replaceSuperApplyCall ( path , node , superClassName , optional = false ) {
186202 const methodName = node . callee . object . property . name ;
203+ const op = optional ? "?." : "." ;
187204 path . replaceWith (
188205 t . callExpression (
189- t . identifier ( `${ superClassName } .prototype.${ methodName } . apply` ) ,
206+ t . identifier ( `${ superClassName } .prototype.${ methodName } ${ op } apply` ) ,
190207 node . arguments
191208 )
192209 ) ;
193210}
194211
195- function replaceSuperNamedCall ( path , node , superClassName , methodName ) {
212+ function replaceSuperNamedCall (
213+ path ,
214+ node ,
215+ superClassName ,
216+ methodName ,
217+ optional = false
218+ ) {
196219 // .call() is better for simple args (or not args) but doesn't work right for spread args
197220 // if it gets further transpiled by babel spread args transform (will be .call.apply(...).
198221 const thisEx = t . thisExpression ( ) ;
@@ -201,9 +224,10 @@ function replaceSuperNamedCall(path, node, superClassName, methodName) {
201224 const callArgs = hasSpread
202225 ? [ thisEx , t . arrayExpression ( node . arguments ) ]
203226 : [ thisEx , ...node . arguments ] ;
227+ const op = optional ? "?." : "." ;
204228 path . replaceWith (
205229 t . callExpression (
206- t . identifier ( `${ superClassName } .prototype.${ methodName } . ${ caller } ` ) ,
230+ t . identifier ( `${ superClassName } .prototype.${ methodName } ${ op } ${ caller } ` ) ,
207231 callArgs
208232 )
209233 ) ;
0 commit comments