@@ -23,6 +23,7 @@ export interface DenoForkContext {
2323 getGlobalsForName : ( id : ts . __String ) => ts . SymbolTable ;
2424 mergeGlobalSymbolTable : ( node : ts . Node , source : ts . SymbolTable , unidirectional ?: boolean ) => void ;
2525 combinedGlobals : ts . SymbolTable ;
26+ preProcessSourceFiles : ( files : readonly ts . SourceFile [ ] ) => void ;
2627}
2728
2829export function createDenoForkContext ( {
@@ -39,6 +40,7 @@ export function createDenoForkContext({
3940 getGlobalsForName,
4041 mergeGlobalSymbolTable,
4142 combinedGlobals : createNodeGlobalsSymbolTable ( ) ,
43+ preProcessSourceFiles,
4244 } ;
4345
4446 function hasNodeSourceFile ( node : ts . Node | undefined ) {
@@ -139,6 +141,109 @@ export function createDenoForkContext({
139141 }
140142 }
141143 }
144+
145+ function preProcessSourceFiles ( files : readonly ts . SourceFile [ ] ) {
146+ for ( const sourceFile of files ) {
147+ if ( sourceFile . path . endsWith ( ".d.ts" ) && hasNodeSourceFile ( sourceFile ) && sourceFile . path . includes ( "/@types/node/" ) ) {
148+ processTypesNodeFile ( sourceFile ) ;
149+ }
150+ }
151+ }
152+
153+ function processTypesNodeFile ( sourceFile : ts . SourceFile ) {
154+ function makeNodeWhitespace ( node : ts . Node ) {
155+ sourceFile . text = sourceFile . text . slice ( 0 , node . pos )
156+ + " " . repeat ( node . pos + node . end )
157+ + sourceFile . text . slice ( node . end ) ;
158+ }
159+
160+ const globalNames = new Set ( [
161+ "TextDecoder" as ts . __String ,
162+ "TextEncoder" ,
163+ ] ) ;
164+ ( globalThis as any ) . Deno . core . print ( "Looking at: " + sourceFile . path + "\n" ) ;
165+ for ( const stmt of sourceFile . statements ) {
166+ searchGlobalAugmentationInStmt ( stmt ) ;
167+ }
168+
169+ function searchGlobalAugmentationInStmt ( stmt : ts . Statement ) {
170+ if ( ! ts . isModuleDeclaration ( stmt ) || stmt . body === undefined || ! ts . isModuleBlock ( stmt . body ) ) {
171+ return ;
172+ }
173+
174+ if ( ts . isGlobalScopeAugmentation ( stmt ) ) {
175+ for ( let i = stmt . body . statements . length - 1 ; i >= 0 ; i -- ) {
176+ const childStmt = stmt . body . statements [ i ] ;
177+ if ( shouldRemoveStmt ( childStmt ) ) {
178+ // makeNodeWhitespace(childStmt);
179+ // (stmt.body.statements as any as unknown[]).splice(i, 1);
180+ // (globalThis as any).Deno.core.print("DATA: " + JSON.stringify(sourceFile, undefined, 2) + "\n");
181+ }
182+ }
183+ }
184+ else {
185+ for ( const child of stmt . body . statements ) {
186+ searchGlobalAugmentationInStmt ( child ) ;
187+ }
188+ }
189+ }
190+
191+ function shouldRemoveStmt ( stmt : ts . Statement ) {
192+ if ( ! ts . isVariableStatement ( stmt ) ) {
193+ return false ;
194+ }
195+ for ( let i = stmt . declarationList . declarations . length - 1 ; i >= 0 ; i -- ) {
196+ const varDecl = stmt . declarationList . declarations [ i ] ;
197+ if ( shouldRemoveVarDecl ( varDecl ) ) {
198+ if ( ts . isIdentifier ( varDecl . name ) ) {
199+ if ( stmt . declarationList . declarations . length === 1 ) {
200+ return true ;
201+ }
202+ else {
203+ ( stmt . declarationList . declarations as any as unknown [ ] ) . splice ( i , 1 ) ;
204+ }
205+ }
206+ }
207+ }
208+ return false ;
209+ }
210+
211+ function shouldRemoveVarDecl ( decl : ts . VariableDeclaration ) {
212+ if ( ts . isIdentifier ( decl . name ) && decl . type ) {
213+ if (
214+ decl . name . escapedText === "PerformanceObserver"
215+ || decl . name . escapedText === "PerformanceObserverEntryList"
216+ || decl . name . escapedText === "PerformanceResourceTiming"
217+ ) {
218+ return ;
219+ }
220+ if ( ts . isConditionalTypeNode ( decl . type ) && ts . isTypeQueryNode ( decl . type . checkType ) ) {
221+ if ( ts . isTypeLiteralNode ( decl . type . extendsType ) ) {
222+ const typeLit = decl . type . extendsType ;
223+ for ( let i = typeLit . members . length - 1 ; i >= 0 ; i -- ) {
224+ const member = typeLit . members [ i ] ;
225+ if ( member . name && ts . isIdentifier ( member . name ) ) {
226+ if ( member . name . escapedText === "onmessage" || member . name . escapedText === "onabort" ) {
227+ ( member . name as any ) . escapedText = "Deno" ;
228+ ( globalThis as any ) . Deno . core . print ( "REMOVED: " + decl . name . escapedText + "\n" ) ;
229+ }
230+ else if ( member . name . escapedText === "ReportingObserver" ) {
231+ ( typeLit . members as any as any [ ] ) . splice ( i , 1 ) ;
232+ }
233+ }
234+ }
235+ }
236+ // return ts.isIdentifier(checkType.exprName) && checkType.exprName.escapedText === "globalThis";
237+ // (globalThis as any).Deno.core.print("Looking at: " + decl.name.escapedText + "\n");
238+ // if (globalNames.has(decl.name.escapedText)) {
239+ // (globalThis as any).Deno.core.print("REMOVED: " + decl.name.escapedText + "\n");
240+ // return true;
241+ // }
242+ }
243+ }
244+ return false ;
245+ }
246+ }
142247}
143248
144249export interface NpmPackageReference {
0 commit comments