Skip to content

Commit 94b1983

Browse files
committed
Add TypeScriptInternals.getGlobalVariableAnalyzer()
1 parent 64ebf51 commit 94b1983

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

apps/api-extractor/src/analyzer/TypeScriptInternals.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
/* eslint-disable @typescript-eslint/no-explicit-any */
55

66
import * as ts from 'typescript';
7+
import { InternalError } from '@microsoft/node-core-library';
8+
9+
/**
10+
* Exposes the TypeScript compiler internals for detecting global variable names.
11+
*/
12+
export interface IGlobalVariableAnalyzer {
13+
hasGlobalName(name: string): boolean;
14+
}
715

816
export class TypeScriptInternals {
917

@@ -92,4 +100,19 @@ export class TypeScriptInternals {
92100
return (declaration as any).localSymbol;
93101
}
94102

103+
public static getGlobalVariableAnalyzer(program: ts.Program): IGlobalVariableAnalyzer {
104+
const anyProgram: any = program;
105+
if (!anyProgram.getDiagnosticsProducingTypeChecker) {
106+
throw new InternalError('Missing Program.getDiagnosticsProducingTypeChecker');
107+
}
108+
const typeChecker: any = anyProgram.getDiagnosticsProducingTypeChecker();
109+
if (!typeChecker.getEmitResolver) {
110+
throw new InternalError('Missing TypeChecker.getEmitResolver');
111+
}
112+
const resolver: any = typeChecker.getEmitResolver();
113+
if (!resolver.hasGlobalName) {
114+
throw new InternalError('Missing EmitResolver.hasGlobalName');
115+
}
116+
return resolver;
117+
}
95118
}

0 commit comments

Comments
 (0)