import type * as d from '../../../declarations' ; import { addCreateEvents } from '../create-event' ; import { addLegacyProps } from '../legacy-props' ; import { RUNTIME_APIS , addCoreRuntimeApi } from '../core-runtime-apis' ; import ts from 'typescript' ; export const updateNativeConstructor = ( classMembers , moduleFile , cmp , ensureSuper ) => { if ( cmp . isPlain ) { return ; } const cstrMethodIndex = classMembers . findIndex ( ( m ) => m . kind === ts . SyntaxKind . Constructor ) ; if ( cstrMethodIndex >= 0 ) { const cstrMethod = classMembers [ cstrMethodIndex ] as ts . ConstructorDeclaration ; let statements = [ ... nativeInit ( moduleFile , cmp ) , ... addCreateEvents ( moduleFile , cmp ) , ... cstrMethod . body . statements , ... addLegacyProps ( moduleFile , cmp ) , ] ; if ( ensureSuper ) { const hasSuper = cstrMethod . body . statements . some ( ( s ) => s . kind === ts . SyntaxKind . SuperKeyword ) ; if ( ! hasSuper ) { statements = [ createNativeConstructorSuper ( ) , ... statements ] ; } } classMembers [ cstrMethodIndex ] = ts . updateConstructor ( cstrMethod , cstrMethod . decorators , cstrMethod . modifiers , cstrMethod . parameters , ts . updateBlock ( cstrMethod . body , statements ) ) ; } else { let statements = [ ... nativeInit ( moduleFile , cmp ) , ... addCreateEvents ( moduleFile , cmp ) , ... addLegacyProps ( moduleFile , cmp ) , ] ; if ( ensureSuper ) { statements = [ createNativeConstructorSuper ( ) , ... statements ] ; } const cstrMethod = ts . createConstructor ( undefined , undefined , undefined , ts . createBlock ( statements , true ) ) ; classMembers . unshift ( cstrMethod ) ; } } ; param moduleFile param cmp returns const nativeInit = ( moduleFile , cmp ) : ReadonlyArray < ts . ExpressionStatement > => { const initStatements = [ nativeRegisterHostStatement ( ) ] ; if ( cmp . encapsulation === 'shadow' ) { initStatements . push ( nativeAttachShadowStatement ( moduleFile ) ) ; } return initStatements ; } ; const nativeRegisterHostStatement = ( ) => { return ts . createStatement ( ts . createCall ( ts . createPropertyAccess ( ts . createThis ( ) , ts . createIdentifier ( '__registerHost' ) ) , undefined , undefined ) ) ; } ; param moduleFile returns const nativeAttachShadowStatement = ( moduleFile ) : ts . ExpressionStatement => { addCoreRuntimeApi ( moduleFile , RUNTIME_APIS . attachShadow ) ; return ts . factory . createExpressionStatement ( ts . factory . createCallExpression ( ts . factory . createPropertyAccessExpression ( ts . factory . createThis ( ) , ts . factory . createIdentifier ( '__attachShadow' ) ) , undefined , undefined ) ) ; } ; const createNativeConstructorSuper = ( ) => { return ts . createExpressionStatement ( ts . createCall ( ts . createIdentifier ( 'super' ) , undefined , undefined ) ) ; } ;