Skip to content

Commit 18da267

Browse files
committed
Use hash of source file text as version for the source file
1 parent b68662e commit 18da267

File tree

3 files changed

+17
-46
lines changed

3 files changed

+17
-46
lines changed

src/server/editorServices.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,6 @@ namespace ts.server {
655655
/*@internal*/
656656
readonly filenameToScriptInfo = new Map<string, ScriptInfo>();
657657
private readonly scriptInfoInNodeModulesWatchers = new Map<string, ScriptInfoInNodeModulesWatcher>();
658-
/**
659-
* Contains all the deleted script info's version information so that
660-
* it does not reset when creating script info again
661-
* (and could have potentially collided with version where contents mismatch)
662-
*/
663-
private readonly filenameToScriptInfoVersion = new Map<string, ScriptInfoVersion>();
664658
// Set of all '.js' files ever opened.
665659
private readonly allJsFilesForOpenFileTelemetry = new Map<string, true>();
666660

@@ -1598,7 +1592,6 @@ namespace ts.server {
15981592

15991593
private deleteScriptInfo(info: ScriptInfo) {
16001594
this.filenameToScriptInfo.delete(info.path);
1601-
this.filenameToScriptInfoVersion.set(info.path, info.getVersion());
16021595
const realpath = info.getRealpathIfDifferent();
16031596
if (realpath) {
16041597
this.realpathToScriptInfos!.remove(realpath, info); // TODO: GH#18217
@@ -2624,9 +2617,8 @@ namespace ts.server {
26242617
if (!openedByClient && !isDynamic && !(hostToQueryFileExistsOn || this.host).fileExists(fileName)) {
26252618
return;
26262619
}
2627-
info = new ScriptInfo(this.host, fileName, scriptKind!, !!hasMixedContent, path, this.filenameToScriptInfoVersion.get(path)); // TODO: GH#18217
2620+
info = new ScriptInfo(this.host, fileName, scriptKind!, !!hasMixedContent, path); // TODO: GH#18217
26282621
this.filenameToScriptInfo.set(info.path, info);
2629-
this.filenameToScriptInfoVersion.delete(info.path);
26302622
if (!openedByClient) {
26312623
this.watchClosedScriptInfo(info);
26322624
}

src/server/scriptInfo.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
namespace ts.server {
2-
export interface ScriptInfoVersion {
3-
svc: number;
4-
text: number;
5-
}
6-
72
/* @internal */
83
export class TextStorage {
9-
version: ScriptInfoVersion;
4+
version: string | undefined;
105

116
/**
127
* Generated only on demand (based on edits, or information requested)
@@ -46,14 +41,7 @@ namespace ts.server {
4641
*/
4742
private pendingReloadFromDisk = false;
4843

49-
constructor(private readonly host: ServerHost, private readonly info: ScriptInfo, initialVersion?: ScriptInfoVersion) {
50-
this.version = initialVersion || { svc: 0, text: 0 };
51-
}
52-
53-
public getVersion() {
54-
return this.svc
55-
? `SVC-${this.version.svc}-${this.svc.getSnapshotVersion()}`
56-
: `Text-${this.version.text}`;
44+
constructor(private readonly host: ServerHost, private readonly info: ScriptInfo) {
5745
}
5846

5947
public hasScriptVersionCache_TestOnly() {
@@ -77,16 +65,17 @@ namespace ts.server {
7765
public useText(newText?: string) {
7866
this.svc = undefined;
7967
this.text = newText;
68+
this.version = undefined;
8069
this.lineMap = undefined;
8170
this.fileSize = undefined;
8271
this.resetSourceMapInfo();
83-
this.version.text++;
8472
}
8573

8674
public edit(start: number, end: number, newText: string) {
8775
this.switchToScriptVersionCache().edit(start, end - start, newText);
8876
this.ownFileText = false;
8977
this.text = undefined;
78+
this.version = undefined;
9079
this.lineMap = undefined;
9180
this.fileSize = undefined;
9281
this.resetSourceMapInfo();
@@ -142,6 +131,7 @@ namespace ts.server {
142131

143132
public delayReloadFromFileIntoText() {
144133
this.pendingReloadFromDisk = true;
134+
this.version = undefined;
145135
}
146136

147137
/**
@@ -225,7 +215,6 @@ namespace ts.server {
225215
private switchToScriptVersionCache(): ScriptVersionCache {
226216
if (!this.svc || this.pendingReloadFromDisk) {
227217
this.svc = ScriptVersionCache.fromString(this.getOrLoadText());
228-
this.version.svc++;
229218
}
230219
return this.svc;
231220
}
@@ -334,10 +323,10 @@ namespace ts.server {
334323
readonly scriptKind: ScriptKind,
335324
public readonly hasMixedContent: boolean,
336325
readonly path: Path,
337-
initialVersion?: ScriptInfoVersion) {
326+
) {
338327
this.isDynamic = isDynamicFileName(fileName);
339328

340-
this.textStorage = new TextStorage(host, this, initialVersion);
329+
this.textStorage = new TextStorage(host, this);
341330
if (hasMixedContent || this.isDynamic) {
342331
this.textStorage.reload("");
343332
this.realpath = this.path;
@@ -347,11 +336,6 @@ namespace ts.server {
347336
: getScriptKindFromFileName(fileName);
348337
}
349338

350-
/*@internal*/
351-
getVersion() {
352-
return this.textStorage.version;
353-
}
354-
355339
/*@internal*/
356340
getTelemetryFileSize() {
357341
return this.textStorage.getTelemetryFileSize();
@@ -560,10 +544,15 @@ namespace ts.server {
560544
}
561545
}
562546

563-
getLatestVersion(): string {
547+
getLatestVersion() {
564548
// Ensure we have updated snapshot to give back latest version
565-
this.textStorage.getSnapshot();
566-
return this.textStorage.getVersion();
549+
const snapShot = this.textStorage.getSnapshot();
550+
if (this.textStorage.version === undefined) {
551+
// Ensure we have updated snapshot to give back latest version
552+
const text = getSnapshotText(snapShot);
553+
this.textStorage.version = this.host.createHash ? this.host.createHash(text) : generateDjb2Hash(text);
554+
}
555+
return this.textStorage.version;
567556
}
568557

569558
saveTo(fileName: string) {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9309,10 +9309,6 @@ declare namespace ts.server.protocol {
93099309
}
93109310
}
93119311
declare namespace ts.server {
9312-
interface ScriptInfoVersion {
9313-
svc: number;
9314-
text: number;
9315-
}
93169312
function isDynamicFileName(fileName: NormalizedPath): boolean;
93179313
class ScriptInfo {
93189314
private readonly host;
@@ -9327,7 +9323,7 @@ declare namespace ts.server {
93279323
private formatSettings;
93289324
private preferences;
93299325
private textStorage;
9330-
constructor(host: ServerHost, fileName: NormalizedPath, scriptKind: ScriptKind, hasMixedContent: boolean, path: Path, initialVersion?: ScriptInfoVersion);
9326+
constructor(host: ServerHost, fileName: NormalizedPath, scriptKind: ScriptKind, hasMixedContent: boolean, path: Path);
93319327
isScriptOpen(): boolean;
93329328
open(newText: string): void;
93339329
close(fileExists?: boolean): void;
@@ -9792,12 +9788,6 @@ declare namespace ts.server {
97929788
}
97939789
export class ProjectService {
97949790
private readonly scriptInfoInNodeModulesWatchers;
9795-
/**
9796-
* Contains all the deleted script info's version information so that
9797-
* it does not reset when creating script info again
9798-
* (and could have potentially collided with version where contents mismatch)
9799-
*/
9800-
private readonly filenameToScriptInfoVersion;
98019791
private readonly allJsFilesForOpenFileTelemetry;
98029792
/**
98039793
* maps external project file name to list of config files that were the part of this project

0 commit comments

Comments
 (0)