Skip to content

Commit 4248e50

Browse files
committed
committing this before revert
1 parent 3127de6 commit 4248e50

File tree

2 files changed

+111
-29
lines changed

2 files changed

+111
-29
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,43 +1545,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
15451545
denoGlobalThisSymbol.declarations = [];
15461546
denoGlobals.set(denoGlobalThisSymbol.escapedName, denoGlobalThisSymbol);
15471547

1548-
const denoContext = deno.createDenoForkContext({
1548+
var denoContext = deno.createDenoForkContext({
15491549
globals: denoGlobals,
15501550
nodeGlobals,
15511551
mergeSymbol,
15521552
});
15531553

1554-
const nodeGlobalThisSymbol = createSymbol(SymbolFlags.Module, "globalThis" as __String, CheckFlags.Readonly);
1554+
denoContext.preProcessSourceFiles(host.getSourceFiles());
1555+
1556+
var nodeGlobalThisSymbol = createSymbol(SymbolFlags.Module, "globalThis" as __String, CheckFlags.Readonly);
15551557
nodeGlobalThisSymbol.exports = denoContext.combinedGlobals;
15561558
nodeGlobalThisSymbol.declarations = [];
15571559
nodeGlobals.set(nodeGlobalThisSymbol.escapedName, nodeGlobalThisSymbol);
15581560

1559-
// deno: huge hacks to get @types/node to work
1560-
nodeGlobals.set(
1561-
"onmessage" as __String,
1562-
createSymbol(SymbolFlags.Module, "onmessage" as __String, CheckFlags.Readonly),
1563-
);
1564-
nodeGlobals.set(
1565-
"onabort" as __String,
1566-
createSymbol(SymbolFlags.Module, "onabort" as __String, CheckFlags.Readonly),
1567-
);
1568-
nodeGlobals.set(
1569-
"ReportingObserver" as __String,
1570-
createSymbol(SymbolFlags.Module, "ReportingObserver" as __String, CheckFlags.Readonly),
1571-
);
1572-
nodeGlobals.set(
1573-
"PerformanceObserver" as __String,
1574-
createSymbol(SymbolFlags.Module, "PerformanceObserver" as __String, CheckFlags.Readonly),
1575-
);
1576-
nodeGlobals.set(
1577-
"PerformanceObserverEntryList" as __String,
1578-
createSymbol(SymbolFlags.Module, "PerformanceObserverEntryList" as __String, CheckFlags.Readonly),
1579-
);
1580-
nodeGlobals.set(
1581-
"PerformanceResourceTiming" as __String,
1582-
createSymbol(SymbolFlags.Module, "PerformanceResourceTiming" as __String, CheckFlags.Readonly),
1583-
);
1584-
15851561
var argumentsSymbol = createSymbol(SymbolFlags.Property, "arguments" as __String);
15861562
var requireSymbol = createSymbol(SymbolFlags.Property, "require" as __String);
15871563
var isolatedModulesLikeFlagName = compilerOptions.verbatimModuleSyntax ? "verbatimModuleSyntax" : "isolatedModules";
@@ -34542,7 +34518,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3454234518
// However, resolveNameHelper will continue and call this callback again, so we'll eventually get a correct suggestion.
3454334519
if (symbol) return symbol;
3454434520
let candidates: Symbol[];
34545-
if (symbols === denoGlobals || symbols == nodeGlobals) {
34521+
if (symbols === denoGlobals || symbols === nodeGlobals) {
3454634522
const primitives = mapDefined(
3454734523
["string", "number", "boolean", "object", "bigint", "symbol"],
3454834524
s => symbols.has((s.charAt(0).toUpperCase() + s.slice(1)) as __String)
@@ -44376,6 +44352,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4437644352
? Diagnostics.Subsequent_property_declarations_must_have_the_same_type_Property_0_must_be_of_type_1_but_here_has_type_2
4437744353
: Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2;
4437844354
const declName = declarationNameToString(nextDeclarationName);
44355+
(globalThis as any).Deno.core.print("Error for: " + declName + "\n");
4437944356
const err = error(
4438044357
nextDeclarationName,
4438144358
message,

src/compiler/deno.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2829
export 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

144249
export interface NpmPackageReference {

0 commit comments

Comments
 (0)