Skip to content

Commit

Permalink
Addressed code review.
Browse files Browse the repository at this point in the history
Signed-off-by: Titian Cernicova-Dragomir <tcernicovad1@bloomberg.net>
  • Loading branch information
dragomirtitian committed Jan 2, 2024
1 parent 97161e0 commit a2fab36
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 53 deletions.
69 changes: 18 additions & 51 deletions src/compiler/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import {
Bundle,
chainBundle,
CompilerOptions,
CoreEmitResolver,
createEmitHelperFactory,
CustomTransformer,
CustomTransformerFactory,
CustomTransformers,
Debug,
Diagnostic,
DiagnosticWithLocation,
disposeEmitNodes,
EmitFlags,
Expand All @@ -32,7 +30,6 @@ import {
getUseDefineForClassFields,
Identifier,
isBundle,
IsolatedTransformationContext,
isSourceFile,
LexicalEnvironmentFlags,
map,
Expand All @@ -42,7 +39,6 @@ import {
NodeFactory,
NodeFlags,
noop,
notImplemented,
NullTransformationContext,
returnUndefined,
ScriptTarget,
Expand Down Expand Up @@ -668,51 +664,22 @@ export function transformNodes<T extends Node>(resolver: EmitResolver | undefine
}
}
}

/** @internal */
export function createTransformationContext(kind: TransformationContextKind.NullContext): NullTransformationContext;
/** @internal */
export function createTransformationContext(
kind: TransformationContextKind.IsolatedContext,
options: CompilerOptions,
diagnostics: Diagnostic[],
resolver: CoreEmitResolver,
): IsolatedTransformationContext;
export function createTransformationContext(
kind: TransformationContextKind.IsolatedContext | TransformationContextKind.NullContext,
options: CompilerOptions = {},
diagnostics?: Diagnostic[],
resolver?: EmitResolver | CoreEmitResolver,
host?: EmitHost,
): NullTransformationContext | IsolatedTransformationContext | TransformationContext {
return {
kind,
factory: factory, // eslint-disable-line object-shorthand
getCompilerOptions: () => options,
getEmitResolver: !resolver ? notImplemented : () => resolver,
getEmitHost: !host ? notImplemented : () => host,
getEmitHelperFactory: notImplemented,
startLexicalEnvironment: noop,
resumeLexicalEnvironment: noop,
suspendLexicalEnvironment: noop,
endLexicalEnvironment: returnUndefined,
setLexicalEnvironmentFlags: noop,
getLexicalEnvironmentFlags: () => 0,
hoistVariableDeclaration: noop,
hoistFunctionDeclaration: noop,
addInitializationStatement: noop,
startBlockScope: noop,
endBlockScope: returnUndefined,
addBlockScopedVariable: noop,
requestEmitHelper: noop,
readEmitHelpers: notImplemented,
enableSubstitution: noop,
enableEmitNotification: noop,
isSubstitutionEnabled: notImplemented,
isEmitNotificationEnabled: notImplemented,
onSubstituteNode: noEmitSubstitution,
onEmitNode: noEmitNotification,
addDiagnostic: !diagnostics ? noop : (diag: Diagnostic) => diagnostics.push(diag),
};
}
/** @internal */
export const nullTransformationContext: NullTransformationContext = createTransformationContext(TransformationContextKind.NullContext);
export const nullTransformationContext: NullTransformationContext = {
kind: TransformationContextKind.NullContext,
factory: factory, // eslint-disable-line object-shorthand
getCompilerOptions: () => ({}),
startLexicalEnvironment: noop,
resumeLexicalEnvironment: noop,
suspendLexicalEnvironment: noop,
endLexicalEnvironment: returnUndefined,
setLexicalEnvironmentFlags: noop,
getLexicalEnvironmentFlags: () => 0,
hoistVariableDeclaration: noop,
hoistFunctionDeclaration: noop,
addInitializationStatement: noop,
startBlockScope: noop,
endBlockScope: returnUndefined,
addBlockScopedVariable: noop,
};
11 changes: 9 additions & 2 deletions src/compiler/transformers/declarations/transpileDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
createPrinter,
createSourceMapGenerator,
createTextWriter,
createTransformationContext,
Debug,
Diagnostic,
EmitHost,
Expand All @@ -20,8 +19,10 @@ import {
getRelativePathToDirectoryOrUrl,
getRootLength,
getSourceFilePathInNewDir,
IsolatedTransformationContext,
normalizePath,
normalizeSlashes,
nullTransformationContext,
PrinterOptions,
SourceFile,
SourceMapGenerator,
Expand All @@ -47,7 +48,13 @@ export function transpileDeclaration(sourceFile: SourceFile, transpileOptions: T
};
const emitResolver = createEmitDeclarationResolver(sourceFile);
const diagnostics: Diagnostic[] = [];
const transformationContext = createTransformationContext(TransformationContextKind.IsolatedContext, compilerOptions, diagnostics, emitResolver);
const transformationContext: IsolatedTransformationContext = {
...nullTransformationContext,
kind: TransformationContextKind.IsolatedContext,
getCompilerOptions: () => compilerOptions,
addDiagnostic: diag => diagnostics.push(diag),
getEmitResolver: () => emitResolver,
};
const transformer = transformDeclarations(transformationContext);
const result = transformer(sourceFile);

Expand Down

0 comments on commit a2fab36

Please sign in to comment.