Skip to content

Commit

Permalink
Generated module conversion step - stripNamespaces
Browse files Browse the repository at this point in the history
This step converts each file into an exported module by hoisting the namespace bodies into the global scope and transferring internal markers down onto declarations as needed.

The namespaces are reconstructed as "barrel"-style modules, which are identical to the old namespace objects in structure. These reconstructed namespaces are then imported in the newly module-ified files, making existing expressions like "ts." valid.
  • Loading branch information
jakebailey committed Oct 28, 2022
1 parent 77a2601 commit bdff460
Show file tree
Hide file tree
Showing 587 changed files with 4,394 additions and 1,500 deletions.
3 changes: 3 additions & 0 deletions src/compiler/_namespaces/ts.moduleSpecifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* Generated file to emulate the ts.moduleSpecifiers namespace. */

export * from "../moduleSpecifiers";
3 changes: 3 additions & 0 deletions src/compiler/_namespaces/ts.performance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* Generated file to emulate the ts.performance namespace. */

export * from "../performance";
74 changes: 74 additions & 0 deletions src/compiler/_namespaces/ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Generated file to emulate the ts namespace. */

export * from "../corePublic";
export * from "../core";
export * from "../debug";
export * from "../semver";
export * from "../performanceCore";
export * from "../perfLogger";
export * from "../tracing";
export * from "../types";
export * from "../sys";
export * from "../path";
export * from "../diagnosticInformationMap.generated";
export * from "../scanner";
export * from "../utilitiesPublic";
export * from "../utilities";
export * from "../factory/baseNodeFactory";
export * from "../factory/parenthesizerRules";
export * from "../factory/nodeConverters";
export * from "../factory/nodeFactory";
export * from "../factory/emitNode";
export * from "../factory/emitHelpers";
export * from "../factory/nodeTests";
export * from "../factory/utilities";
export * from "../factory/utilitiesPublic";
export * from "../parser";
export * from "../commandLineParser";
export * from "../moduleNameResolver";
export * from "../binder";
export * from "../symbolWalker";
export * from "../checker";
export * from "../visitorPublic";
export * from "../sourcemap";
export * from "../transformers/utilities";
export * from "../transformers/destructuring";
export * from "../transformers/taggedTemplate";
export * from "../transformers/ts";
export * from "../transformers/classFields";
export * from "../transformers/typeSerializer";
export * from "../transformers/legacyDecorators";
export * from "../transformers/es2017";
export * from "../transformers/es2018";
export * from "../transformers/es2019";
export * from "../transformers/es2020";
export * from "../transformers/es2021";
export * from "../transformers/esnext";
export * from "../transformers/jsx";
export * from "../transformers/es2016";
export * from "../transformers/es2015";
export * from "../transformers/es5";
export * from "../transformers/generators";
export * from "../transformers/module/module";
export * from "../transformers/module/system";
export * from "../transformers/module/esnextAnd2015";
export * from "../transformers/module/node";
export * from "../transformers/declarations/diagnostics";
export * from "../transformers/declarations";
export * from "../transformer";
export * from "../emitter";
export * from "../watchUtilities";
export * from "../program";
export * from "../builderStatePublic";
export * from "../builderState";
export * from "../builder";
export * from "../builderPublic";
export * from "../resolutionCache";
export * from "../watch";
export * from "../watchPublic";
export * from "../tsbuild";
export * from "../tsbuildPublic";
import * as moduleSpecifiers from "./ts.moduleSpecifiers";
export { moduleSpecifiers };
import * as performance from "./ts.performance";
export { performance };
8 changes: 5 additions & 3 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ts from "./_namespaces/ts";

/* @internal */
namespace ts {
/** @internal */
export const enum ModuleInstanceState {
NonInstantiated = 0,
Instantiated = 1,
Expand All @@ -15,6 +15,7 @@ interface ActiveLabel {
referenced: boolean;
}

/** @internal */
export function getModuleInstanceState(node: ts.ModuleDeclaration, visited?: ts.ESMap<number, ModuleInstanceState | undefined>): ModuleInstanceState {
if (node.body && !node.body.parent) {
// getModuleInstanceStateForAliasTarget needs to walk up the parent chain, so parent pointers must be set on this tree already
Expand Down Expand Up @@ -173,6 +174,7 @@ function initFlowNode<T extends ts.FlowNode>(node: T) {

const binder = createBinder();

/** @internal */
export function bindSourceFile(file: ts.SourceFile, options: ts.CompilerOptions) {
ts.performance.mark("beforeBind");
ts.perfLogger.logStartBindFile("" + file.fileName);
Expand Down Expand Up @@ -3498,6 +3500,7 @@ function isPurelyTypeDeclaration(s: ts.Statement): boolean {
}
}

/** @internal */
export function isExportsOrModuleExportsOrAlias(sourceFile: ts.SourceFile, node: ts.Expression): boolean {
let i = 0;
const q = ts.createQueue<ts.Expression>();
Expand Down Expand Up @@ -3533,4 +3536,3 @@ function lookupSymbolForName(container: ts.Node, name: ts.__String): ts.Symbol |
}
return container.symbol && container.symbol.exports && container.symbol.exports.get(name);
}
}
36 changes: 33 additions & 3 deletions src/compiler/builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*@internal*/
namespace ts {
import * as ts from "./_namespaces/ts";

/** @internal */
export interface ReusableDiagnostic extends ReusableDiagnosticRelatedInformation {
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
reportsUnnecessary?: {};
Expand All @@ -9,6 +10,7 @@ export interface ReusableDiagnostic extends ReusableDiagnosticRelatedInformation
skippedOn?: keyof ts.CompilerOptions;
}

/** @internal */
export interface ReusableDiagnosticRelatedInformation {
category: ts.DiagnosticCategory;
code: number;
Expand All @@ -18,8 +20,10 @@ export interface ReusableDiagnosticRelatedInformation {
messageText: string | ReusableDiagnosticMessageChain;
}

/** @internal */
export type ReusableDiagnosticMessageChain = ts.DiagnosticMessageChain;

/** @internal */
export interface ReusableBuilderProgramState extends ts.BuilderState {
/**
* Cache of bind and check diagnostics for files with their Path being the key
Expand Down Expand Up @@ -67,11 +71,13 @@ export interface ReusableBuilderProgramState extends ts.BuilderState {
latestChangedDtsFile: string | undefined;
}

/** @internal */
export const enum BuilderFileEmit {
DtsOnly,
Full
}

/** @internal */
/**
* State to store the changed files, affected files and cache semantic diagnostics
*/
Expand Down Expand Up @@ -133,6 +139,7 @@ export interface BuilderProgramState extends ts.BuilderState, ReusableBuilderPro
filesChangingSignature?: ts.Set<ts.Path>;
}

/** @internal */
export type SavedBuildProgramEmitState = Pick<BuilderProgramState,
"affectedFilesPendingEmit" |
"affectedFilesPendingEmitIndex" |
Expand Down Expand Up @@ -767,11 +774,17 @@ function getBinderAndCheckerDiagnosticsOfFile(state: BuilderProgramState, source
return ts.filterSemanticDiagnostics(diagnostics, state.compilerOptions);
}

/** @internal */
export type ProgramBuildInfoFileId = number & { __programBuildInfoFileIdBrand: any };
/** @internal */
export type ProgramBuildInfoFileIdListId = number & { __programBuildInfoFileIdListIdBrand: any };
/** @internal */
export type ProgramBuildInfoDiagnostic = ProgramBuildInfoFileId | [fileId: ProgramBuildInfoFileId, diagnostics: readonly ReusableDiagnostic[]];
/** @internal */
export type ProgramBuilderInfoFilePendingEmit = [fileId: ProgramBuildInfoFileId, emitKind: BuilderFileEmit];
/** @internal */
export type ProgramBuildInfoReferencedMap = [fileId: ProgramBuildInfoFileId, fileIdListId: ProgramBuildInfoFileIdListId][];
/** @internal */
export type ProgramBuildInfoBuilderStateFileInfo = Omit<ts.BuilderState.FileInfo, "signature"> & {
/**
* Signature is
Expand All @@ -781,15 +794,18 @@ export type ProgramBuildInfoBuilderStateFileInfo = Omit<ts.BuilderState.FileInfo
*/
signature: string | false | undefined;
};
/** @internal */
/**
* [fileId, signature] if different from file's signature
* fileId if file wasnt emitted
*/
export type ProgramBuildInfoEmitSignature = ProgramBuildInfoFileId | [fileId: ProgramBuildInfoFileId, signature: string];
/** @internal */
/**
* ProgramBuildInfoFileInfo is string if FileInfo.version === FileInfo.signature && !FileInfo.affectsGlobalScope otherwise encoded FileInfo
*/
export type ProgramBuildInfoFileInfo = string | ProgramBuildInfoBuilderStateFileInfo;
/** @internal */
export interface ProgramMultiFileEmitBuildInfo {
fileNames: readonly string[];
fileInfos: readonly ProgramBuildInfoFileInfo[];
Expand All @@ -805,6 +821,7 @@ export interface ProgramMultiFileEmitBuildInfo {
latestChangedDtsFile?: string;
}

/** @internal */
export interface ProgramBundleEmitBuildInfo {
fileNames: readonly string[];
fileInfos: readonly string[];
Expand All @@ -813,8 +830,10 @@ export interface ProgramBundleEmitBuildInfo {
latestChangedDtsFile?: string;
}

/** @internal */
export type ProgramBuildInfo = ProgramMultiFileEmitBuildInfo | ProgramBundleEmitBuildInfo;

/** @internal */
export function isProgramBundleEmitBuildInfo(info: ProgramBuildInfo): info is ProgramBundleEmitBuildInfo {
return !!ts.outFile(info.options || {});
}
Expand Down Expand Up @@ -1039,18 +1058,21 @@ function convertToReusableDiagnosticRelatedInformation(diagnostic: ts.Diagnostic
};
}

/** @internal */
export enum BuilderProgramKind {
SemanticDiagnosticsBuilderProgram,
EmitAndSemanticDiagnosticsBuilderProgram
}

/** @internal */
export interface BuilderCreationParameters {
newProgram: ts.Program;
host: ts.BuilderProgramHost;
oldProgram: ts.BuilderProgram | undefined;
configFileParsingDiagnostics: readonly ts.Diagnostic[];
}

/** @internal */
export function getBuilderCreationParameters(newProgramOrRootNames: ts.Program | readonly string[] | undefined, hostOrOptions: ts.BuilderProgramHost | ts.CompilerOptions | undefined, oldProgramOrHost?: ts.BuilderProgram | ts.CompilerHost, configFileParsingDiagnosticsOrOldProgram?: readonly ts.Diagnostic[] | ts.BuilderProgram, configFileParsingDiagnostics?: readonly ts.Diagnostic[], projectReferences?: readonly ts.ProjectReference[]): BuilderCreationParameters {
let host: ts.BuilderProgramHost;
let newProgram: ts.Program;
Expand Down Expand Up @@ -1087,6 +1109,7 @@ function getTextHandlingSourceMapForSignature(text: string, data: ts.WriteFileCa
return data?.sourceMapUrlPos !== undefined ? text.substring(0, data.sourceMapUrlPos) : text;
}

/** @internal */
export function computeSignatureWithDiagnostics(
sourceFile: ts.SourceFile,
text: string,
Expand Down Expand Up @@ -1120,12 +1143,16 @@ export function computeSignatureWithDiagnostics(
}
}

/** @internal */
export function computeSignature(text: string, computeHash: ts.BuilderState.ComputeHash | undefined, data?: ts.WriteFileCallbackData) {
return (computeHash ?? ts.generateDjb2Hash)(getTextHandlingSourceMapForSignature(text, data));
}

/** @internal */
export function createBuilderProgram(kind: BuilderProgramKind.SemanticDiagnosticsBuilderProgram, builderCreationParameters: BuilderCreationParameters): ts.SemanticDiagnosticsBuilderProgram;
/** @internal */
export function createBuilderProgram(kind: BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram, builderCreationParameters: BuilderCreationParameters): ts.EmitAndSemanticDiagnosticsBuilderProgram;
/** @internal */
export function createBuilderProgram(kind: BuilderProgramKind, { newProgram, host, oldProgram, configFileParsingDiagnostics }: BuilderCreationParameters) {
// Return same program if underlying program doesnt change
let oldState = oldProgram && oldProgram.getState();
Expand Down Expand Up @@ -1467,6 +1494,7 @@ function addToAffectedFilesPendingEmit(state: BuilderProgramState, affectedFileP
}
}

/** @internal */
export function toBuilderStateFileInfo(fileInfo: ProgramBuildInfoFileInfo): ts.BuilderState.FileInfo {
return ts.isString(fileInfo) ?
{ version: fileInfo, signature: fileInfo, affectsGlobalScope: undefined, impliedFormat: undefined } :
Expand All @@ -1475,6 +1503,7 @@ export function toBuilderStateFileInfo(fileInfo: ProgramBuildInfoFileInfo): ts.B
{ version: fileInfo.version, signature: fileInfo.signature === false ? undefined : fileInfo.version, affectsGlobalScope: fileInfo.affectsGlobalScope, impliedFormat: fileInfo.impliedFormat };
}

/** @internal */
export function createBuilderProgramUsingProgramBuildInfo(program: ProgramBuildInfo, buildInfoPath: string, host: ts.ReadBuildProgramHost): ts.EmitAndSemanticDiagnosticsBuilderProgram {
const buildInfoDirectory = ts.getDirectoryPath(ts.getNormalizedAbsolutePath(buildInfoPath, host.getCurrentDirectory()));
const getCanonicalFileName = ts.createGetCanonicalFileName(host.useCaseSensitiveFileNames());
Expand Down Expand Up @@ -1577,6 +1606,7 @@ export function createBuilderProgramUsingProgramBuildInfo(program: ProgramBuildI
}
}

/** @internal */
export function getBuildInfoFileVersionMap(
program: ProgramBuildInfo,
buildInfoPath: string,
Expand All @@ -1593,6 +1623,7 @@ export function getBuildInfoFileVersionMap(
return fileInfos;
}

/** @internal */
export function createRedirectedBuilderProgram(getState: () => { program?: ts.Program | undefined; compilerOptions: ts.CompilerOptions; }, configFileParsingDiagnostics: readonly ts.Diagnostic[]): ts.BuilderProgram {
return {
getState: ts.notImplemented,
Expand Down Expand Up @@ -1621,4 +1652,3 @@ export function createRedirectedBuilderProgram(getState: () => { program?: ts.Pr
return ts.Debug.checkDefined(getState().program);
}
}
}
4 changes: 2 additions & 2 deletions src/compiler/builderPublic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace ts {
import * as ts from "./_namespaces/ts";

export type AffectedFileResult<T> = { result: T; affected: ts.SourceFile | ts.Program; } | undefined;

export interface BuilderProgramHost {
Expand Down Expand Up @@ -178,4 +179,3 @@ export function createAbstractBuilder(newProgramOrRootNames: ts.Program | readon
const { newProgram, configFileParsingDiagnostics: newConfigFileParsingDiagnostics } = ts.getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences);
return ts.createRedirectedBuilderProgram(() => ({ program: newProgram, compilerOptions: newProgram.getCompilerOptions() }), newConfigFileParsingDiagnostics);
}
}
8 changes: 5 additions & 3 deletions src/compiler/builderState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*@internal*/
namespace ts {
import * as ts from "./_namespaces/ts";

/** @internal */
export function getFileEmitOutput(program: ts.Program, sourceFile: ts.SourceFile, emitOnlyDtsFiles: boolean,
cancellationToken?: ts.CancellationToken, customTransformers?: ts.CustomTransformers, forceDtsEmit?: boolean): ts.EmitOutput {
const outputFiles: ts.OutputFile[] = [];
Expand All @@ -10,6 +11,7 @@ export function getFileEmitOutput(program: ts.Program, sourceFile: ts.SourceFile
outputFiles.push({ name: fileName, writeByteOrderMark, text });
}
}
/** @internal */
export interface BuilderState {
/**
* Information of the file eg. its version, signature etc
Expand Down Expand Up @@ -57,6 +59,7 @@ export interface BuilderState {
*/
allFileNames?: readonly string[];
}
/** @internal */
export namespace BuilderState {
/**
* Information about the source file: Its version and optional signature from last emit
Expand Down Expand Up @@ -625,4 +628,3 @@ export namespace BuilderState {
return ts.arrayFrom(ts.mapDefinedIterator(seenFileNamesMap.values(), value => value));
}
}
}
4 changes: 2 additions & 2 deletions src/compiler/builderStatePublic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace ts {
import * as ts from "./_namespaces/ts";

export interface EmitOutput {
outputFiles: OutputFile[];
emitSkipped: boolean;
Expand All @@ -11,4 +12,3 @@ export interface OutputFile {
text: string;
/* @internal */ buildInfo?: ts.BuildInfo
}
}
Loading

0 comments on commit bdff460

Please sign in to comment.