Skip to content

Commit

Permalink
Removing some unnecessary methods from host and function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Jul 28, 2022
1 parent 2475bf2 commit b71c45a
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 124 deletions.
119 changes: 47 additions & 72 deletions src/compiler/builder.ts

Large diffs are not rendered by default.

12 changes: 3 additions & 9 deletions src/compiler/builderPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ namespace ts {
export type AffectedFileResult<T> = { result: T; affected: SourceFile | Program; } | undefined;

export interface BuilderProgramHost {
/**
* return true if file names are treated with case sensitivity
*/
useCaseSensitiveFileNames(): boolean;
/**
* If provided this would be used this hash instead of actual file shape text for detecting changes
*/
Expand All @@ -25,13 +21,11 @@ namespace ts {
*/
/*@internal*/
storeFilesChangingSignatureDuringEmit?: boolean;
/**
* Gets the current time
*/
/*@internal*/
now?(): Date;
}

/*@internal*/
export type HostForComputeHash = Pick<BuilderProgramHost, "createHash">;

/**
* Builder to manage the program state changes
*/
Expand Down
33 changes: 12 additions & 21 deletions src/compiler/builderState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ namespace ts {
return false;
}

/**
* Compute the hash to store the shape of the file
*/
export type ComputeHash = ((data: string) => string) | undefined;

function getReferencedFilesFromImportedModuleSymbol(symbol: Symbol): Path[] {
return mapDefined(symbol.declarations, declaration => getSourceFileOfNode(declaration)?.resolvedPath);
}
Expand Down Expand Up @@ -262,11 +257,12 @@ namespace ts {
/**
* Creates the state of file references and signature for the new program from oldState if it is safe
*/
export function create(newProgram: Program, getCanonicalFileName: GetCanonicalFileName, oldState?: Readonly<BuilderState>, disableUseFileVersionAsSignature?: boolean): BuilderState {
export function create(newProgram: Program, oldState?: Readonly<BuilderState>, disableUseFileVersionAsSignature?: boolean): BuilderState {
const fileInfos = new Map<Path, FileInfo>();
const referencedMap = newProgram.getCompilerOptions().module !== ModuleKind.None ? createManyToManyPathMap() : undefined;
const exportedModulesMap = referencedMap ? createManyToManyPathMap() : undefined;
const useOldState = canReuseOldState(referencedMap, oldState);
const getCanonicalFileName = createGetCanonicalFileName(newProgram.useCaseSensitiveFileNames());

// Ensure source files have parent pointers set
newProgram.getTypeChecker();
Expand Down Expand Up @@ -326,16 +322,14 @@ namespace ts {
programOfThisState: Program,
path: Path,
cancellationToken: CancellationToken | undefined,
computeHash: ComputeHash,
getCanonicalFileName: GetCanonicalFileName,
host: HostForComputeHash,
): readonly SourceFile[] {
const result = getFilesAffectedByWithOldState(
state,
programOfThisState,
path,
cancellationToken,
computeHash,
getCanonicalFileName,
host,
);
state.oldSignatures?.clear();
state.oldExportedModulesMap?.clear();
Expand All @@ -347,19 +341,18 @@ namespace ts {
programOfThisState: Program,
path: Path,
cancellationToken: CancellationToken | undefined,
computeHash: ComputeHash,
getCanonicalFileName: GetCanonicalFileName,
host: HostForComputeHash,
): readonly SourceFile[] {
const sourceFile = programOfThisState.getSourceFileByPath(path);
if (!sourceFile) {
return emptyArray;
}

if (!updateShapeSignature(state, programOfThisState, sourceFile, cancellationToken, computeHash, getCanonicalFileName)) {
if (!updateShapeSignature(state, programOfThisState, sourceFile, cancellationToken, host)) {
return [sourceFile];
}

return (state.referencedMap ? getFilesAffectedByUpdatedShapeWhenModuleEmit : getFilesAffectedByUpdatedShapeWhenNonModuleEmit)(state, programOfThisState, sourceFile, cancellationToken, computeHash, getCanonicalFileName);
return (state.referencedMap ? getFilesAffectedByUpdatedShapeWhenModuleEmit : getFilesAffectedByUpdatedShapeWhenNonModuleEmit)(state, programOfThisState, sourceFile, cancellationToken, host);
}

export function updateSignatureOfFile(state: BuilderState, signature: string | undefined, path: Path) {
Expand All @@ -375,8 +368,7 @@ namespace ts {
programOfThisState: Program,
sourceFile: SourceFile,
cancellationToken: CancellationToken | undefined,
computeHash: ComputeHash,
getCanonicalFileName: GetCanonicalFileName,
host: HostForComputeHash,
useFileVersionAsSignature = state.useFileVersionAsSignature
) {
// If we have cached the result for this file, that means hence forth we should assume file shape is uptodate
Expand All @@ -393,8 +385,8 @@ namespace ts {
latestSignature = computeSignatureWithDiagnostics(
sourceFile,
text,
computeHash,
getCanonicalFileName,
host,
createGetCanonicalFileName(programOfThisState.useCaseSensitiveFileNames()),
data,
);
if (latestSignature !== prevSignature) {
Expand Down Expand Up @@ -590,8 +582,7 @@ namespace ts {
programOfThisState: Program,
sourceFileWithUpdatedShape: SourceFile,
cancellationToken: CancellationToken | undefined,
computeHash: ComputeHash,
getCanonicalFileName: GetCanonicalFileName,
host: HostForComputeHash,
) {
if (isFileAffectingGlobalScope(sourceFileWithUpdatedShape)) {
return getAllFilesExcludingDefaultLibraryFile(state, programOfThisState, sourceFileWithUpdatedShape);
Expand All @@ -615,7 +606,7 @@ namespace ts {
if (!seenFileNamesMap.has(currentPath)) {
const currentSourceFile = programOfThisState.getSourceFileByPath(currentPath)!;
seenFileNamesMap.set(currentPath, currentSourceFile);
if (currentSourceFile && updateShapeSignature(state, programOfThisState, currentSourceFile, cancellationToken, computeHash, getCanonicalFileName)) {
if (currentSourceFile && updateShapeSignature(state, programOfThisState, currentSourceFile, cancellationToken, host)) {
queue.push(...getReferencedByPaths(state, currentSourceFile.resolvedPath));
}
}
Expand Down
17 changes: 8 additions & 9 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ namespace ts {
if (sourceMapFilePath) {
const sourceMap = sourceMapGenerator.toString();
writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap, /*writeByteOrderMark*/ false, sourceFiles);
if (printer.bundleFileInfo) printer.bundleFileInfo.mapHash = computeSignature(sourceMap, maybeBind(host, host.createHash));
if (printer.bundleFileInfo) printer.bundleFileInfo.mapHash = computeSignature(sourceMap, host);
}
}
else {
Expand All @@ -566,7 +566,7 @@ namespace ts {
writeFile(host, emitterDiagnostics, jsFilePath, text, !!compilerOptions.emitBOM, sourceFiles, { sourceMapUrlPos, diagnostics: transform.diagnostics });
// We store the hash of the text written in the buildinfo to ensure that text of the referenced d.ts file is same as whats in the buildinfo
// This is needed because incremental can be toggled between two runs and we might use stale file text to do text manipulation in prepend mode
if (printer.bundleFileInfo) printer.bundleFileInfo.hash = computeSignature(text, maybeBind(host, host.createHash));
if (printer.bundleFileInfo) printer.bundleFileInfo.hash = computeSignature(text, host);

// Reset state
writer.clear();
Expand Down Expand Up @@ -749,7 +749,6 @@ namespace ts {
getCommandLine: (ref: ProjectReference) => ParsedCommandLine | undefined,
customTransformers?: CustomTransformers
): EmitUsingBuildInfoResult {
const createHash = maybeBind(host, host.createHash);
const { buildInfoPath, jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath } = getOutputPathsForBundle(config.options, /*forceDtsPaths*/ false);
let buildInfo: BuildInfo;
if (host.getBuildInfo) {
Expand All @@ -768,20 +767,20 @@ namespace ts {
const jsFileText = host.readFile(Debug.checkDefined(jsFilePath));
if (!jsFileText) return jsFilePath!;
// If the jsFileText is not same has what it was created with, tsbuildinfo is stale so dont use it
if (computeSignature(jsFileText, createHash) !== buildInfo.bundle.js.hash) return jsFilePath!;
if (computeSignature(jsFileText, host) !== buildInfo.bundle.js.hash) return jsFilePath!;
const sourceMapText = sourceMapFilePath && host.readFile(sourceMapFilePath);
// error if no source map or for now if inline sourcemap
if ((sourceMapFilePath && !sourceMapText) || config.options.inlineSourceMap) return sourceMapFilePath || "inline sourcemap decoding";
if (sourceMapFilePath && computeSignature(sourceMapText!, createHash) !== buildInfo.bundle.js.mapHash) return sourceMapFilePath;
if (sourceMapFilePath && computeSignature(sourceMapText!, host) !== buildInfo.bundle.js.mapHash) return sourceMapFilePath;

// read declaration text
const declarationText = declarationFilePath && host.readFile(declarationFilePath);
if (declarationFilePath && !declarationText) return declarationFilePath;
if (declarationFilePath && computeSignature(declarationText!, createHash) !== buildInfo.bundle.dts!.hash) return declarationFilePath;
if (declarationFilePath && computeSignature(declarationText!, host) !== buildInfo.bundle.dts!.hash) return declarationFilePath;
const declarationMapText = declarationMapPath && host.readFile(declarationMapPath);
// error if no source map or for now if inline sourcemap
if ((declarationMapPath && !declarationMapText) || config.options.inlineSourceMap) return declarationMapPath || "inline sourcemap decoding";
if (declarationMapPath && computeSignature(declarationMapText!, createHash) !== buildInfo.bundle.dts!.mapHash) return declarationMapPath;
if (declarationMapPath && computeSignature(declarationMapText!, host) !== buildInfo.bundle.dts!.mapHash) return declarationMapPath;

const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath!, host.getCurrentDirectory()));
const ownPrependInput = createInputFiles(
Expand Down Expand Up @@ -830,7 +829,7 @@ namespace ts {
newBuildInfo.program = buildInfo.program;
if (newBuildInfo.program && changedDtsText !== undefined && config.options.composite) {
// Update the output signature
(newBuildInfo.program as ProgramBundleEmitBuildInfo).outSignature = computeSignature(changedDtsText, createHash, changedDtsData);
(newBuildInfo.program as ProgramBundleEmitBuildInfo).outSignature = computeSignature(changedDtsText, host, changedDtsData);
}
// Update sourceFileInfo
const { js, dts, sourceFiles } = buildInfo.bundle!;
Expand Down Expand Up @@ -862,7 +861,7 @@ namespace ts {
getSourceFileFromReference: returnUndefined,
redirectTargetsMap: createMultiMap(),
getFileIncludeReasons: notImplemented,
createHash,
createHash: maybeBind(host, host.createHash),
};
emitFiles(
notImplementedResolver,
Expand Down
5 changes: 2 additions & 3 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,15 +703,14 @@ namespace ts.server {
return [];
}
updateProjectIfDirty(this);
this.builderState = BuilderState.create(this.program!, this.projectService.toCanonicalFileName, this.builderState, /*disableUseFileVersionAsSignature*/ true);
this.builderState = BuilderState.create(this.program!, this.builderState, /*disableUseFileVersionAsSignature*/ true);
return mapDefined(
BuilderState.getFilesAffectedBy(
this.builderState,
this.program!,
scriptInfo.path,
this.cancellationToken,
maybeBind(this.projectService.host, this.projectService.host.createHash),
this.getCanonicalFileName,
this.projectService.host,
),
sourceFile => this.shouldEmitFile(this.projectService.getScriptInfoForPath(sourceFile.path)) ? sourceFile.fileName : undefined
);
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace ts {
});

function makeAssertChanges(getProgram: () => Program): (fileNames: readonly string[]) => void {
const host: BuilderProgramHost = { useCaseSensitiveFileNames: returnTrue };
const host: BuilderProgramHost = {};
let builderProgram: EmitAndSemanticDiagnosticsBuilderProgram | undefined;
return fileNames => {
const program = getProgram();
Expand All @@ -86,7 +86,7 @@ namespace ts {
}

function makeAssertChangesWithCancellationToken(getProgram: () => Program): (fileNames: readonly string[], cancelAfterEmitLength?: number) => void {
const host: BuilderProgramHost = { useCaseSensitiveFileNames: returnTrue };
const host: BuilderProgramHost = {};
let builderProgram: EmitAndSemanticDiagnosticsBuilderProgram | undefined;
let cancel = false;
const cancellationToken: CancellationToken = {
Expand Down
4 changes: 0 additions & 4 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5237,10 +5237,6 @@ declare namespace ts {
affected: SourceFile | Program;
} | undefined;
interface BuilderProgramHost {
/**
* return true if file names are treated with case sensitivity
*/
useCaseSensitiveFileNames(): boolean;
/**
* If provided this would be used this hash instead of actual file shape text for detecting changes
*/
Expand Down
4 changes: 0 additions & 4 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5237,10 +5237,6 @@ declare namespace ts {
affected: SourceFile | Program;
} | undefined;
interface BuilderProgramHost {
/**
* return true if file names are treated with case sensitivity
*/
useCaseSensitiveFileNames(): boolean;
/**
* If provided this would be used this hash instead of actual file shape text for detecting changes
*/
Expand Down

0 comments on commit b71c45a

Please sign in to comment.