Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cache): makeName -> createHash for clarity #355

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions src/tscache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as tsTypes from "typescript";
import { emptyDirSync, pathExistsSync, readdirSync, removeSync, statSync } from "fs-extra";
import * as _ from "lodash";
import { Graph, alg } from "graphlib";
import hash from "object-hash";
import objHash from "object-hash";
import { blue, yellow, green } from "colors/safe";

import { IContext } from "./context";
Expand Down Expand Up @@ -117,7 +117,7 @@ export class TsCache
this.hashOptions.ignoreUnknown = hashIgnoreUnknown;
if (!noCache)
{
this.cacheDir = `${this.cacheRoot}/${this.cachePrefix}${hash(
this.cacheDir = `${this.cacheRoot}/${this.cachePrefix}${objHash(
{
version: this.cacheVersion,
rootFilenames,
Expand Down Expand Up @@ -208,18 +208,18 @@ export class TsCache
return transform();
}

const name = this.makeName(id, snapshot);
const hash = this.createHash(id, snapshot);

this.context.info(`${blue("transpiling")} '${id}'`);
this.context.debug(` cache: '${this.codeCache.path(name)}'`);
this.context.debug(` cache: '${this.codeCache.path(hash)}'`);

if (this.codeCache.exists(name) && !this.isDirty(id, false))
if (this.codeCache.exists(hash) && !this.isDirty(id, false))
{
this.context.debug(green(" cache hit"));
const data = this.codeCache.read(name);
const data = this.codeCache.read(hash);
if (data)
{
this.codeCache.write(name, data);
this.codeCache.write(hash, data);
return data;
}
else
Expand All @@ -229,7 +229,7 @@ export class TsCache
this.context.debug(yellow(" cache miss"));

const transformedData = transform();
this.codeCache.write(name, transformedData);
this.codeCache.write(hash, transformedData);
this.markAsDirty(id);
return transformedData;
}
Expand All @@ -253,19 +253,19 @@ export class TsCache
}

this.context.debug(blue("Ambient types:"));
const typeNames = this.ambientTypes.filter((snapshot) => snapshot.snapshot !== undefined)
const typeHashes = this.ambientTypes.filter((snapshot) => snapshot.snapshot !== undefined)
.map((snapshot) =>
{
this.context.debug(` ${snapshot.id}`);
return this.makeName(snapshot.id, snapshot.snapshot!);
return this.createHash(snapshot.id, snapshot.snapshot!);
});
// types dirty if any d.ts changed, added or removed
this.ambientTypesDirty = !this.typesCache.match(typeNames);
this.ambientTypesDirty = !this.typesCache.match(typeHashes);

if (this.ambientTypesDirty)
this.context.info(yellow("ambient types changed, redoing all semantic diagnostics"));

typeNames.forEach(this.typesCache.touch, this.typesCache);
typeHashes.forEach(this.typesCache.touch, this.typesCache);
}

private getDiagnostics(type: string, cache: ICache<IDiagnostics[]>, id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[]
Expand All @@ -276,18 +276,18 @@ export class TsCache
return convertDiagnostic(type, check());
}

const name = this.makeName(id, snapshot);
const hash = this.createHash(id, snapshot);

this.context.debug(` cache: '${cache.path(name)}'`);
this.context.debug(` cache: '${cache.path(hash)}'`);

if (cache.exists(name) && !this.isDirty(id, true))
if (cache.exists(hash) && !this.isDirty(id, true))
{
this.context.debug(green(" cache hit"));

const data = cache.read(name);
const data = cache.read(hash);
if (data)
{
cache.write(name, data);
cache.write(hash, data);
return data;
}
else
Expand All @@ -297,7 +297,7 @@ export class TsCache
this.context.debug(yellow(" cache miss"));

const convertedData = convertDiagnostic(type, check());
cache.write(name, convertedData);
cache.write(hash, convertedData);
this.markAsDirty(id);
return convertedData;
}
Expand Down Expand Up @@ -359,9 +359,9 @@ export class TsCache
});
}

private makeName(id: string, snapshot: tsTypes.IScriptSnapshot)
private createHash(id: string, snapshot: tsTypes.IScriptSnapshot)
{
const data = snapshot.getText(0, snapshot.getLength());
return hash({ data, id }, this.hashOptions);
return objHash({ data, id }, this.hashOptions);
}
}