import ts , { ImportDeclaration , SourceFile , StringLiteral , factory , Program } from 'typescript' ; import { UpdateFileService } from '../utils' ; import { createCssSelectorForTs } from 'cyia-code-util' ; export abstract class MigrationBase { constructor ( protected sourceFile , protected updateFileService , protected program ) { this . selector = createCssSelectorForTs ( this . sourceFile ) ; } private printer = ts . createPrinter ( ) ; protected selector : ReturnType < typeof createCssSelectorForTs > ; abstract run ( ) ; createImportDeclaration ( importNameList , importPackageName , commentOptions ? ) { const node = factory . createImportDeclaration ( undefined , undefined , factory . createImportClause ( false , undefined , factory . createNamedImports ( importNameList . map ( importName => factory . createImportSpecifier ( undefined , factory . createIdentifier ( importName ) ) ) ) ) , factory . createStringLiteral ( importPackageName ) ) ; if ( commentOptions && commentOptions . content !== undefined ) { return ts . addSyntheticLeadingComment ( node , commentOptions . kind || ts . SyntaxKind . SingleLineCommentTrivia , commentOptions . content , commentOptions . newLine ) ; } return node ; } createNamedImports ( importSpecifierList ) { return factory . createNamedImports ( importSpecifierList ) ; } printNodeContent ( node ) { return this . printer . printNode ( ts . EmitHint . Unspecified , node , undefined ) ; } createStringLiteral ( string ) { return factory . createStringLiteral ( string ) ; } getImportDeclarationList ( ) { return this . selector . queryAll ( 'ImportDeclaration' ) . filter ( item => ts . isImportDeclaration ( item ) ) . filter ( ( item ) => this . getImportDeclarationPackageName ( item ) . startsWith ( 'ngx-tethys' ) ) as any [ ] ; } getImportDeclarationPackageName ( importDeclaration ) { return ( importDeclaration . moduleSpecifier as ts . StringLiteral ) . text ; } getImportDeclarationImportSpecifierList ( importDeclaration ) { if ( importDeclaration . importClause && importDeclaration . importClause . namedBindings && ts . isNamedImports ( importDeclaration . importClause . namedBindings ) ) { return importDeclaration . importClause . namedBindings . elements . slice ( ) ; } return [ ] ; } addComment ( node , commentOptions ) { if ( commentOptions . content !== undefined ) { return ts . addSyntheticLeadingComment ( node , commentOptions . kind || ts . SyntaxKind . SingleLineCommentTrivia , commentOptions . content , commentOptions . newLine ) ; } return node ; } /** 更新引入声明,因为ts 4.0设置为readonly的原因,无法直接赋值 */ updateImportDeclaration ( node , moduleSpecifier ? , importClause ? ) { return factory . updateImportDeclaration ( node , node . decorators , node . modifiers , importClause || node . importClause , moduleSpecifier || node . moduleSpecifier ) ; } /** 更新引入声明,因为ts 4.0设置为readonly的原因,无法直接赋值 */ updateImportClause ( node , namedImports ) { return factory . updateImportClause ( node , false , node . name , namedImports || node . namedBindings ) ; } }