@@ -4,9 +4,9 @@ import * as FileUtils from "./FileUtils"
44import * as JsonUtils from "./JsonUtils"
55import * as VueCodeCleaner from "./VueCodeCleaner"
66import * as TscUtils from "./TscUtils"
7+ import { TypeMap } from "./TscUtils"
78
89import * as babelParser from "@babel/parser"
9- import tsc , { SourceFile } from "typescript"
1010import * as path from "node:path"
1111import * as fs from "node:fs"
1212
@@ -41,12 +41,12 @@ function toVueAst(file: string): babelParser.ParseResult {
4141 return codeToJsAst ( cleanedCode ) ;
4242}
4343
44- function createTsc ( file : string ) : TscUtils . TscResult | undefined {
44+ function typeMapForFile ( file : string ) : TypeMap | undefined {
4545 try {
46- return TscUtils . tscForFile ( file )
46+ return TscUtils . typeMapForFile ( file )
4747 } catch ( err ) {
4848 if ( err instanceof Error ) {
49- console . warn ( "Retrieving types" , err . message ) ;
49+ console . warn ( "Retrieving types" , file , ":" , err . message ) ;
5050 }
5151 return undefined ;
5252 }
@@ -60,15 +60,12 @@ async function createJSAst(options: Options) {
6060 const srcFiles : string [ ] = await FileUtils . filesWithExtensions ( options , Defaults . JS_EXTENSIONS ) ;
6161 for ( const file of srcFiles ) {
6262 try {
63- const ast = fileToJsAst ( file ) ;
63+ const ast : babelParser . ParseResult = fileToJsAst ( file ) ;
6464 writeAstFile ( file , ast , options ) ;
6565 try {
66- let ts = options . tsTypes ? createTsc ( file ) : undefined ;
67- if ( ts ) {
68- const tsAst : SourceFile = ts . program . getSourceFile ( file ) ! ;
69- tsc . forEachChild ( tsAst , ts . addType ) ;
70- writeTypesFile ( file , ts . seenTypes , options ) ;
71- ts . seenTypes . clear ( ) ;
66+ let ts : TypeMap | undefined = options . tsTypes ? typeMapForFile ( file ) : undefined ;
67+ if ( ts && ts . size !== 0 ) {
68+ writeTypesFile ( file , ts , options ) ;
7269 }
7370 } catch ( err ) {
7471 if ( err instanceof Error ) {
@@ -93,7 +90,7 @@ async function createVueAst(options: Options) {
9390 const srcFiles : string [ ] = await FileUtils . filesWithExtensions ( options , [ ".vue" ] ) ;
9491 for ( const file of srcFiles ) {
9592 try {
96- const ast = toVueAst ( file ) ;
93+ const ast : babelParser . ParseResult = toVueAst ( file ) ;
9794 if ( ast ) {
9895 writeAstFile ( file , ast , options ) ;
9996 }
@@ -109,8 +106,8 @@ async function createVueAst(options: Options) {
109106 * Write AST data to a json file
110107 */
111108function writeAstFile ( file : string , ast : babelParser . ParseResult , options : Options ) {
112- const relativePath = path . relative ( options . src , file )
113- const outAstFile = path . join ( options . output , relativePath + ".json" ) ;
109+ const relativePath : string = path . relative ( options . src , file )
110+ const outAstFile : string = path . join ( options . output , relativePath + ".json" ) ;
114111 const data = {
115112 fullName : file ,
116113 relativeName : relativePath ,
@@ -124,16 +121,16 @@ function writeAstFile(file: string, ast: babelParser.ParseResult, options: Optio
124121/**
125122 * Write tsc type data to a json file
126123 */
127- function writeTypesFile ( file : string , seenTypes : Map < number , string > , options : Options ) {
128- const relativePath = path . relative ( options . src , file )
129- const outTypeFile = path . join ( options . output , relativePath + ".typemap" ) ;
124+ function writeTypesFile ( file : string , seenTypes : TypeMap , options : Options ) {
125+ const relativePath : string = path . relative ( options . src , file )
126+ const outTypeFile : string = path . join ( options . output , relativePath + ".typemap" ) ;
130127 fs . mkdirSync ( path . dirname ( outTypeFile ) , { recursive : true } ) ;
131128 fs . writeFileSync ( outTypeFile , JsonUtils . stringify ( Object . fromEntries ( seenTypes ) ) ) ;
132129 console . log ( "Converted types for" , relativePath , "to" , outTypeFile ) ;
133130}
134131
135132async function createXAst ( options : Options ) {
136- const srcDir = options . src ;
133+ const srcDir : string = options . src ;
137134 try {
138135 fs . accessSync ( srcDir , fs . constants . R_OK ) ;
139136 } catch ( err ) {
0 commit comments