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): simplify noCache condition #362

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ A useful resource as you dive deeper are the [unit tests](__tests__/). They're g
- _NOTE_: These are fairly short and unfortunately leave a lot to be desired... especially when you consider that this plugin is actually one of the simpler integrations out there.
1. At this point, you may be ready to read the more complicated bits of [`index`](src/index.ts) in detail and see how it interacts with the other modules.
- The integration tests [TBD] could be useful to review at this point as well.
1. Once you're pretty familiar with `index`, you can dive into some of the cache code in [`tscache`](src/tscache.ts), [`nocache`](src/nocache.ts), and [`rollingcache`](src/rollingcache.ts).
1. Once you're pretty familiar with `index`, you can dive into some of the cache code in [`tscache`](src/tscache.ts) and [`rollingcache`](src/rollingcache.ts).
1. And finally, you can see some of the Rollup logging nuances in [`context`](src/context.ts) and [`rollupcontext`](src/rollupcontext.ts), and then the TS logging nuances in [`print-diagnostics`](src/print-diagnostics.ts), and [`diagnostics-format-host`](src/diagnostics-format-host.ts)
- While these are necessary to the implementation, they are fairly ancillary to understanding and working with the codebase.
15 changes: 0 additions & 15 deletions __tests__/nocache.spec.ts

This file was deleted.

39 changes: 0 additions & 39 deletions src/nocache.ts

This file was deleted.

71 changes: 27 additions & 44 deletions src/tscache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { RollingCache } from "./rollingcache";
import { ICache } from "./icache";
import { tsModule } from "./tsproxy";
import { formatHost } from "./diagnostics-format-host";
import { NoCache } from "./nocache";

export interface ICode
{
Expand Down Expand Up @@ -103,9 +102,9 @@ export class TsCache
private cacheVersion = "9";
private cachePrefix = "rpt2_";
private dependencyTree: Graph;
private ambientTypes: ITypeSnapshot[];
private ambientTypes!: ITypeSnapshot[];
private ambientTypesDirty = false;
private cacheDir: string | undefined;
private cacheDir!: string;
private codeCache!: ICache<ICode | undefined>;
private typesCache!: ICache<string>;
private semanticDiagnosticsCache!: ICache<IDiagnostics[]>;
Expand All @@ -114,25 +113,28 @@ export class TsCache

constructor(private noCache: boolean, hashIgnoreUnknown: boolean, private host: tsTypes.LanguageServiceHost, private cacheRoot: string, private options: tsTypes.CompilerOptions, private rollupConfig: any, rootFilenames: string[], private context: IContext)
{
this.hashOptions.ignoreUnknown = hashIgnoreUnknown;
if (!noCache)
this.dependencyTree = new Graph({ directed: true });
this.dependencyTree.setDefaultNodeLabel((_node: string) => ({ dirty: false }));

if (noCache)
{
this.cacheDir = `${this.cacheRoot}/${this.cachePrefix}${objHash(
{
version: this.cacheVersion,
rootFilenames,
options: this.options,
rollupConfig: this.rollupConfig,
tsVersion: tsModule.version,
},
this.hashOptions,
)}`;
} else {
this.clean();
return;
}

this.dependencyTree = new Graph({ directed: true });
this.dependencyTree.setDefaultNodeLabel((_node: string) => ({ dirty: false }));
this.hashOptions.ignoreUnknown = hashIgnoreUnknown;
this.cacheDir = `${this.cacheRoot}/${this.cachePrefix}${objHash(
{
version: this.cacheVersion,
rootFilenames,
options: this.options,
rollupConfig: this.rollupConfig,
tsVersion: tsModule.version,
},
this.hashOptions,
)}`;

this.init();

const automaticTypes = tsModule.getAutomaticTypeDirectiveNames(options, tsModule.sys)
.map((entry) => tsModule.resolveTypeReferenceDirective(entry, undefined, options, tsModule.sys))
Expand All @@ -143,8 +145,6 @@ export class TsCache
.concat(automaticTypes)
.map((id) => ({ id, snapshot: this.host.getScriptSnapshot(id) }));

this.init();

this.checkAmbientTypes();
}

Expand Down Expand Up @@ -200,6 +200,9 @@ export class TsCache

public done()
{
if (this.noCache)
return;

this.context.info(blue("rolling caches"));
this.codeCache.roll();
this.semanticDiagnosticsCache.roll();
Expand Down Expand Up @@ -254,12 +257,6 @@ export class TsCache

private checkAmbientTypes(): void
{
if (this.noCache)
{
this.ambientTypesDirty = true;
return;
}

this.context.debug(blue("Ambient types:"));
const typeHashes = this.ambientTypes.filter((snapshot) => snapshot.snapshot !== undefined)
.map((snapshot) =>
Expand Down Expand Up @@ -312,24 +309,10 @@ export class TsCache

private init()
{
if (this.noCache)
{
this.codeCache = new NoCache<ICode>();
this.typesCache = new NoCache<string>();
this.syntacticDiagnosticsCache = new NoCache<IDiagnostics[]>();
this.semanticDiagnosticsCache = new NoCache<IDiagnostics[]>();
}
else
{
// this is an invariant, it should never happen: cacheDir should only not exist when noCache
if (this.cacheDir === undefined)
throw new Error(`this.cacheDir undefined`);

this.codeCache = new RollingCache<ICode>(`${this.cacheDir}/code`, true);
this.typesCache = new RollingCache<string>(`${this.cacheDir}/types`, true);
this.syntacticDiagnosticsCache = new RollingCache<IDiagnostics[]>(`${this.cacheDir}/syntacticDiagnostics`, true);
this.semanticDiagnosticsCache = new RollingCache<IDiagnostics[]>(`${this.cacheDir}/semanticDiagnostics`, true);
}
this.codeCache = new RollingCache<ICode>(`${this.cacheDir}/code`, true);
this.typesCache = new RollingCache<string>(`${this.cacheDir}/types`, true);
this.syntacticDiagnosticsCache = new RollingCache<IDiagnostics[]>(`${this.cacheDir}/syntacticDiagnostics`, true);
this.semanticDiagnosticsCache = new RollingCache<IDiagnostics[]>(`${this.cacheDir}/semanticDiagnostics`, true);
}

private markAsDirty(id: string): void
Expand Down