Skip to content

Commit afc0fa2

Browse files
Added kind to transformation context.
1 parent 211b056 commit afc0fa2

File tree

8 files changed

+89
-71
lines changed

8 files changed

+89
-71
lines changed

src/compiler/_namespaces/ts.ts

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ export * from "../transformers/declarations/emitBinder";
6161
export * from "../transformers/declarations/emitResolver";
6262
export * from "../transformers/declarations/transpileDeclaration";
6363
export * from "../transformers/declarations/localInferenceResolver";
64-
export * from "../transformers/declarations/types";
6564
export * from "../transformers/declarations";
6665
export * from "../transformer";
6766
export * from "../emitter";

src/compiler/transformer.ts

+53-29
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import {
44
Bundle,
55
chainBundle,
66
CompilerOptions,
7+
CoreEmitResolver,
78
createEmitHelperFactory,
89
CustomTransformer,
910
CustomTransformerFactory,
1011
CustomTransformers,
1112
Debug,
13+
Diagnostic,
1214
DiagnosticWithLocation,
1315
disposeEmitNodes,
1416
EmitFlags,
@@ -30,6 +32,7 @@ import {
3032
getUseDefineForClassFields,
3133
Identifier,
3234
isBundle,
35+
IsolatedTransformationContext,
3336
isSourceFile,
3437
LexicalEnvironmentFlags,
3538
map,
@@ -40,6 +43,7 @@ import {
4043
NodeFlags,
4144
noop,
4245
notImplemented,
46+
NullTransformationContext,
4347
returnUndefined,
4448
ScriptTarget,
4549
setEmitFlags,
@@ -49,6 +53,7 @@ import {
4953
SyntaxKind,
5054
tracing,
5155
TransformationContext,
56+
TransformationContextKind,
5257
TransformationResult,
5358
transformClassFields,
5459
transformDeclarations,
@@ -267,6 +272,7 @@ export function transformNodes<T extends Node>(resolver: EmitResolver | undefine
267272
// The transformation context is provided to each transformer as part of transformer
268273
// initialization.
269274
const context: TransformationContext = {
275+
kind: TransformationContextKind.FullContext,
270276
factory,
271277
getCompilerOptions: () => options,
272278
getEmitResolver: () => resolver!, // TODO: GH#18217
@@ -662,33 +668,51 @@ export function transformNodes<T extends Node>(resolver: EmitResolver | undefine
662668
}
663669
}
664670
}
665-
666671
/** @internal */
667-
export const nullTransformationContext: TransformationContext = {
668-
factory: factory, // eslint-disable-line object-shorthand
669-
getCompilerOptions: () => ({}),
670-
getEmitResolver: notImplemented,
671-
getEmitHost: notImplemented,
672-
getEmitHelperFactory: notImplemented,
673-
startLexicalEnvironment: noop,
674-
resumeLexicalEnvironment: noop,
675-
suspendLexicalEnvironment: noop,
676-
endLexicalEnvironment: returnUndefined,
677-
setLexicalEnvironmentFlags: noop,
678-
getLexicalEnvironmentFlags: () => 0,
679-
hoistVariableDeclaration: noop,
680-
hoistFunctionDeclaration: noop,
681-
addInitializationStatement: noop,
682-
startBlockScope: noop,
683-
endBlockScope: returnUndefined,
684-
addBlockScopedVariable: noop,
685-
requestEmitHelper: noop,
686-
readEmitHelpers: notImplemented,
687-
enableSubstitution: noop,
688-
enableEmitNotification: noop,
689-
isSubstitutionEnabled: notImplemented,
690-
isEmitNotificationEnabled: notImplemented,
691-
onSubstituteNode: noEmitSubstitution,
692-
onEmitNode: noEmitNotification,
693-
addDiagnostic: noop,
694-
};
672+
export function createTransformationContext(kind: TransformationContextKind.NullContext): NullTransformationContext;
673+
/** @internal */
674+
export function createTransformationContext(
675+
kind: TransformationContextKind.IsolatedContext,
676+
options: CompilerOptions,
677+
diagnostics: Diagnostic[],
678+
resolver: CoreEmitResolver,
679+
): IsolatedTransformationContext;
680+
export function createTransformationContext(
681+
kind: TransformationContextKind.IsolatedContext | TransformationContextKind.NullContext,
682+
options: CompilerOptions = {},
683+
diagnostics?: Diagnostic[],
684+
resolver?: EmitResolver | CoreEmitResolver,
685+
host?: EmitHost,
686+
): NullTransformationContext | IsolatedTransformationContext | TransformationContext {
687+
return {
688+
kind,
689+
factory: factory, // eslint-disable-line object-shorthand
690+
getCompilerOptions: () => options,
691+
getEmitResolver: !resolver ? notImplemented : () => resolver,
692+
getEmitHost: !host ? notImplemented : () => host,
693+
getEmitHelperFactory: notImplemented,
694+
startLexicalEnvironment: noop,
695+
resumeLexicalEnvironment: noop,
696+
suspendLexicalEnvironment: noop,
697+
endLexicalEnvironment: returnUndefined,
698+
setLexicalEnvironmentFlags: noop,
699+
getLexicalEnvironmentFlags: () => 0,
700+
hoistVariableDeclaration: noop,
701+
hoistFunctionDeclaration: noop,
702+
addInitializationStatement: noop,
703+
startBlockScope: noop,
704+
endBlockScope: returnUndefined,
705+
addBlockScopedVariable: noop,
706+
requestEmitHelper: noop,
707+
readEmitHelpers: notImplemented,
708+
enableSubstitution: noop,
709+
enableEmitNotification: noop,
710+
isSubstitutionEnabled: notImplemented,
711+
isEmitNotificationEnabled: notImplemented,
712+
onSubstituteNode: noEmitSubstitution,
713+
onEmitNode: noEmitNotification,
714+
addDiagnostic: !diagnostics ? noop : (diag: Diagnostic) => diagnostics.push(diag),
715+
};
716+
}
717+
/** @internal */
718+
export const nullTransformationContext: NullTransformationContext = createTransformationContext(TransformationContextKind.NullContext);

src/compiler/transformers/declarations.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ import {
220220
SyntaxKind,
221221
toFileNameLowerCase,
222222
TransformationContext,
223+
TransformationContextKind,
223224
transformNodes,
224225
tryCast,
225226
tryGetModuleSpecifierFromDeclaration,
@@ -292,7 +293,7 @@ const declarationEmitNodeBuilderFlags = NodeBuilderFlags.MultilineObjectLiterals
292293
*
293294
* @internal
294295
*/
295-
export function transformDeclarations(context: (TransformationContext & { useLocalInferenceTypePrint?: false; }) | IsolatedTransformationContext) {
296+
export function transformDeclarations(context: TransformationContext | IsolatedTransformationContext) {
296297
const throwDiagnostic = () => Debug.fail("Diagnostic emitted without context");
297298
let getSymbolAccessibilityDiagnostic: GetSymbolAccessibilityDiagnostic = throwDiagnostic;
298299
let needsDeclare = true;
@@ -339,7 +340,7 @@ export function transformDeclarations(context: (TransformationContext & { useLoc
339340
return checkEntityNameVisibility(name, container ?? enclosingDeclaration);
340341
},
341342
});
342-
if (context.useLocalInferenceTypePrint) {
343+
if (context.kind === TransformationContextKind.IsolatedContext) {
343344
const resolver: CoreEmitResolver = context.getEmitResolver();
344345
// Ideally nothing should require the symbol tracker in isolated declarations mode.
345346
// createLiteralConstValue is the one exception
@@ -370,7 +371,7 @@ export function transformDeclarations(context: (TransformationContext & { useLoc
370371
}
371372
}
372373
else {
373-
Debug.assert(!context.useLocalInferenceTypePrint);
374+
Debug.assert(context.kind === TransformationContextKind.FullContext);
374375
const host = context.getEmitHost();
375376
const resolver = context.getEmitResolver();
376377
const symbolTracker: SymbolTracker = createSymbolTracker(resolver, host);

src/compiler/transformers/declarations/emitBinder.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import {
6262
isVarConst,
6363
isVariableDeclaration,
6464
MappedTypeNode,
65-
MemberKey,
6665
MethodDeclaration,
6766
MethodSignature,
6867
ModifierFlags,
@@ -816,6 +815,11 @@ export function bindSourceFileForDeclarationEmit(file: SourceFile) {
816815
}
817816
}
818817

818+
/** @internal */
819+
export type MemberKey = string & {
820+
__memberKey: void;
821+
};
822+
819823
/**
820824
* Gets the symbolic name for a member from its type.
821825
* @internal

src/compiler/transformers/declarations/transpileDeclaration.ts

+4-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
createPrinter,
88
createSourceMapGenerator,
99
createTextWriter,
10+
createTransformationContext,
1011
Debug,
1112
Diagnostic,
1213
EmitHost,
@@ -21,11 +22,11 @@ import {
2122
getSourceFilePathInNewDir,
2223
normalizePath,
2324
normalizeSlashes,
24-
nullTransformationContext,
2525
PrinterOptions,
2626
SourceFile,
2727
SourceMapGenerator,
2828
sys,
29+
TransformationContextKind,
2930
transformDeclarations,
3031
TranspileDeclarationsOptions,
3132
TranspileDeclarationsOutput,
@@ -46,19 +47,8 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
4647
};
4748
const emitResolver = createEmitDeclarationResolver(sourceFile);
4849
const diagnostics: Diagnostic[] = [];
49-
const transformer = transformDeclarations({
50-
useLocalInferenceTypePrint: true,
51-
...nullTransformationContext,
52-
getEmitResolver() {
53-
return emitResolver;
54-
},
55-
getCompilerOptions() {
56-
return compilerOptions;
57-
},
58-
addDiagnostic(diag: any) {
59-
diagnostics.push(diag);
60-
},
61-
});
50+
const transformationContext = createTransformationContext(TransformationContextKind.IsolatedContext, compilerOptions, diagnostics, emitResolver);
51+
const transformer = transformDeclarations(transformationContext);
6252
const result = transformer(sourceFile);
6353

6454
const printer = createPrinter({

src/compiler/transformers/declarations/types.ts

-21
This file was deleted.

src/compiler/types.ts

+21
Original file line numberDiff line numberDiff line change
@@ -9167,7 +9167,14 @@ export const enum LexicalEnvironmentFlags {
91679167
VariablesHoistedInParameters = 1 << 1, // a temp variable was hoisted while visiting a parameter list
91689168
}
91699169

9170+
export const enum TransformationContextKind {
9171+
FullContext = 0,
9172+
IsolatedContext = 1,
9173+
NullContext = 2,
9174+
}
9175+
91709176
export interface CoreTransformationContext {
9177+
/** @internal */ kind: TransformationContextKind;
91719178
readonly factory: NodeFactory;
91729179

91739180
/** Gets the compiler options supplied to the transformer. */
@@ -9209,6 +9216,7 @@ export interface CoreTransformationContext {
92099216
}
92109217

92119218
export interface TransformationContext extends CoreTransformationContext {
9219+
kind: TransformationContextKind.FullContext;
92129220
/** @internal */ getEmitResolver(): EmitResolver;
92139221
/** @internal */ getEmitHost(): EmitHost;
92149222
/** @internal */ getEmitHelperFactory(): EmitHelperFactory;
@@ -9258,6 +9266,19 @@ export interface TransformationContext extends CoreTransformationContext {
92589266
/** @internal */ addDiagnostic(diag: DiagnosticWithLocation): void;
92599267
}
92609268

9269+
/** @internal */
9270+
export interface IsolatedTransformationContext extends CoreTransformationContext {
9271+
kind: TransformationContextKind.IsolatedContext;
9272+
getEmitResolver(): CoreEmitResolver;
9273+
getCompilerOptions(): CompilerOptions;
9274+
factory: NodeFactory;
9275+
addDiagnostic(diag: Diagnostic): void;
9276+
}
9277+
/** @internal */
9278+
export interface NullTransformationContext extends CoreTransformationContext {
9279+
kind: TransformationContextKind.NullContext;
9280+
}
9281+
92619282
export interface TransformationResult<T extends Node> {
92629283
/** Gets the transformed source files. */
92639284
transformed: T[];

src/services/textChanges.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ import {
128128
NodeArray,
129129
NodeFactoryFlags,
130130
nodeIsSynthesized,
131+
NullTransformationContext,
131132
nullTransformationContext,
132133
ObjectLiteralElementLike,
133134
ObjectLiteralExpression,
@@ -162,7 +163,6 @@ import {
162163
textSpanEnd,
163164
Token,
164165
tokenToString,
165-
TransformationContext,
166166
TypeLiteralNode,
167167
TypeNode,
168168
TypeParameterDeclaration,
@@ -1367,7 +1367,7 @@ function isTrivia(s: string) {
13671367

13681368
// A transformation context that won't perform parenthesization, as some parenthesization rules
13691369
// are more aggressive than is strictly necessary.
1370-
const textChangesTransformationContext: TransformationContext = {
1370+
const textChangesTransformationContext: NullTransformationContext = {
13711371
...nullTransformationContext,
13721372
factory: createNodeFactory(
13731373
nullTransformationContext.factory.flags | NodeFactoryFlags.NoParenthesizerRules,

0 commit comments

Comments
 (0)