import * as t from "@babel/types" ; import { trackComments } from "../util" ; export const DeclareVariable = { exit ( path ) { const { id } = path . node ; path . replaceWith ( { type : "VariableDeclaration" , kind : "var" , declarations : [ t . variableDeclarator ( id ) ] , declare : true , } ) ; } , } ; export const DeclareClass = { exit ( path , state ) { const { id , body , typeParameters , leadingComments , trailingComments , loc , } = path . node ; const superClass = path . node . extends . length > 0 ? path . node . extends [ 0 ] : undefined ; const replacementNode = { type : "ClassDeclaration" , id , typeParameters , superClass , superClassTypeParameters : superClass ? superClass . typeParameters : undefined , body , declare : true , leadingComments , trailingComments , loc , } ; trackComments ( replacementNode , state ) ; path . replaceWith ( replacementNode ) ; } , } ; export const DeclareFunction = { exit ( path , state ) { const { id , leadingComments , trailingComments , loc } = path . node ; const { name , typeAnnotation } = id ; const functionType = typeAnnotation . typeAnnotation ; const replacementNode = { type : "TSDeclareFunction" , id : t . identifier ( name ) , typeParameters : functionType . typeParameters , params : functionType . parameters , returnType : functionType . typeAnnotation , declare : ! t . isDeclareExportDeclaration ( path . parent ) , async : false , generator : false , leadingComments , trailingComments , loc , } ; trackComments ( replacementNode , state ) ; path . replaceWith ( replacementNode ) ; } , } ; export const DeclareExportDeclaration = { exit ( path , state ) { const { declaration , default : _default , leadingComments , trailingComments , loc , } = path . node ; const replacementNode = { type : _default ? "ExportDefaultDeclaration" : "ExportNamedDeclaration" , declaration , leadingComments , trailingComments , loc , } ; trackComments ( replacementNode , state ) ; path . replaceWith ( replacementNode ) ; } , } ;