Skip to content

Commit 5c920f3

Browse files
committed
Dont unnecessarily make textStorage internal
1 parent afdf1e9 commit 5c920f3

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

src/server/editorServices.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,7 +2201,7 @@ namespace ts.server {
22012201
if (!declarationInfo) return undefined;
22022202

22032203
declarationInfo.getSnapshot(); // Ensure synchronized
2204-
const existingMapper = declarationInfo.textStorage.mapper;
2204+
const existingMapper = declarationInfo.mapper;
22052205
if (existingMapper !== undefined) {
22062206
return existingMapper ? existingMapper : undefined;
22072207
}
@@ -2220,11 +2220,11 @@ namespace ts.server {
22202220
const mapper = getDocumentPositionMapper(
22212221
{ getCanonicalFileName: this.toCanonicalFileName, log: s => this.logger.info(s), getSourceFileLike: f => this.getSourceFileLike(f, projectName) },
22222222
declarationInfo.fileName,
2223-
declarationInfo.textStorage.getLineInfo(),
2223+
declarationInfo.getLineInfo(),
22242224
readMapFile
22252225
);
22262226
readMapFile = undefined; // Remove ref to project
2227-
declarationInfo.textStorage.mapper = mapper || false;
2227+
declarationInfo.mapper = mapper || false;
22282228
return mapper;
22292229
}
22302230

@@ -2243,20 +2243,22 @@ namespace ts.server {
22432243

22442244
// Key doesnt matter since its only for text and lines
22452245
if (info.cacheSourceFile) return info.cacheSourceFile.sourceFile;
2246-
if (info.textStorage.sourceFileLike) return info.textStorage.sourceFileLike;
22472246

2248-
info.textStorage.sourceFileLike = {
2249-
get text() {
2250-
Debug.fail("shouldnt need text");
2251-
return "";
2252-
},
2253-
getLineAndCharacterOfPosition: pos => {
2254-
const lineOffset = info.positionToLineOffset(pos);
2255-
return { line: lineOffset.line - 1, character: lineOffset.offset - 1 };
2256-
},
2257-
getPositionOfLineAndCharacter: (line, character) => info.lineOffsetToPosition(line + 1, character + 1)
2258-
};
2259-
return info.textStorage.sourceFileLike;
2247+
// Create sourceFileLike
2248+
if (!info.sourceFileLike) {
2249+
info.sourceFileLike = {
2250+
get text() {
2251+
Debug.fail("shouldnt need text");
2252+
return "";
2253+
},
2254+
getLineAndCharacterOfPosition: pos => {
2255+
const lineOffset = info.positionToLineOffset(pos);
2256+
return { line: lineOffset.line - 1, character: lineOffset.offset - 1 };
2257+
},
2258+
getPositionOfLineAndCharacter: (line, character) => info.lineOffsetToPosition(line + 1, character + 1)
2259+
};
2260+
}
2261+
return info.sourceFileLike;
22602262
}
22612263

22622264
setHostConfiguration(args: protocol.ConfigureRequestArguments) {

src/server/scriptInfo.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ namespace ts.server {
4646
*/
4747
private pendingReloadFromDisk = false;
4848

49-
mapper: DocumentPositionMapper | false | undefined = false;
50-
sourceFileLike: SourceFileLike | undefined;
51-
5249
constructor(private readonly host: ServerHost, private readonly fileName: NormalizedPath, initialVersion: ScriptInfoVersion | undefined, private readonly info: ScriptInfo) {
5350
this.version = initialVersion || { svc: 0, text: 0 };
5451
}
@@ -73,8 +70,8 @@ namespace ts.server {
7370
this.text = newText;
7471
this.lineMap = undefined;
7572
this.fileSize = undefined;
76-
this.mapper = undefined;
77-
this.sourceFileLike = undefined;
73+
this.info.mapper = undefined;
74+
this.info.sourceFileLike = undefined;
7875
this.version.text++;
7976
}
8077

@@ -84,8 +81,8 @@ namespace ts.server {
8481
this.text = undefined;
8582
this.lineMap = undefined;
8683
this.fileSize = undefined;
87-
this.mapper = undefined;
88-
this.sourceFileLike = undefined;
84+
this.info.mapper = undefined;
85+
this.info.sourceFileLike = undefined;
8986
}
9087

9188
/**
@@ -287,7 +284,7 @@ namespace ts.server {
287284

288285
/* @internal */
289286
fileWatcher: FileWatcher | undefined;
290-
/* @internal */ textStorage: TextStorage;
287+
private textStorage: TextStorage;
291288

292289
/*@internal*/
293290
readonly isDynamic: boolean;
@@ -304,6 +301,10 @@ namespace ts.server {
304301

305302
/*@internal*/
306303
mapInfo?: ScriptInfo;
304+
/*@internal*/
305+
mapper: DocumentPositionMapper | false | undefined = false;
306+
/*@internal*/
307+
sourceFileLike: SourceFileLike | undefined;
307308

308309
constructor(
309310
private readonly host: ServerHost,
@@ -583,5 +584,10 @@ namespace ts.server {
583584
public isJavaScript() {
584585
return this.scriptKind === ScriptKind.JS || this.scriptKind === ScriptKind.JSX;
585586
}
587+
588+
/*@internal*/
589+
getLineInfo(): LineInfo {
590+
return this.textStorage.getLineInfo();
591+
}
586592
}
587593
}

src/testRunner/unittests/textStorage.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace ts.textStorage {
1414

1515
const host = projectSystem.createServerHost([f]);
1616
// Since script info is not used in these tests, just cheat by passing undefined
17-
const ts1 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, /*info*/undefined!);
18-
const ts2 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, /*info*/undefined!);
17+
const ts1 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, {} as server.ScriptInfo);
18+
const ts2 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, {} as server.ScriptInfo);
1919

2020
ts1.useScriptVersionCache_TestOnly();
2121
ts2.useText();
@@ -49,7 +49,7 @@ namespace ts.textStorage {
4949
it("should switch to script version cache if necessary", () => {
5050
const host = projectSystem.createServerHost([f]);
5151
// Since script info is not used in these tests, just cheat by passing undefined
52-
const ts1 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, /*info*/undefined!);
52+
const ts1 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, {} as server.ScriptInfo);
5353

5454
ts1.getSnapshot();
5555
assert.isFalse(ts1.hasScriptVersionCache_TestOnly(), "should not have script version cache - 1");
@@ -67,15 +67,15 @@ namespace ts.textStorage {
6767
it("should be able to return the file size immediately after construction", () => {
6868
const host = projectSystem.createServerHost([f]);
6969
// Since script info is not used in these tests, just cheat by passing undefined
70-
const ts1 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, /*info*/undefined!);
70+
const ts1 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, {} as server.ScriptInfo);
7171

7272
assert.strictEqual(f.content.length, ts1.getTelemetryFileSize());
7373
});
7474

7575
it("should be able to return the file size when backed by text", () => {
7676
const host = projectSystem.createServerHost([f]);
7777
// Since script info is not used in these tests, just cheat by passing undefined
78-
const ts1 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, /*info*/undefined!);
78+
const ts1 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, {} as server.ScriptInfo);
7979

8080
ts1.useText(f.content);
8181
assert.isFalse(ts1.hasScriptVersionCache_TestOnly());
@@ -86,7 +86,7 @@ namespace ts.textStorage {
8686
it("should be able to return the file size when backed by a script version cache", () => {
8787
const host = projectSystem.createServerHost([f]);
8888
// Since script info is not used in these tests, just cheat by passing undefined
89-
const ts1 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, /*info*/undefined!);
89+
const ts1 = new server.TextStorage(host, server.asNormalizedPath(f.path), /*initialVersion*/ undefined, {} as server.ScriptInfo);
9090

9191
ts1.useScriptVersionCache_TestOnly();
9292
assert.isTrue(ts1.hasScriptVersionCache_TestOnly());
@@ -126,7 +126,7 @@ namespace ts.textStorage {
126126

127127
const host = projectSystem.createServerHost([changingFile]);
128128
// Since script info is not used in these tests, just cheat by passing undefined
129-
const ts1 = new server.TextStorage(host, server.asNormalizedPath(changingFile.path), /*initialVersion*/ undefined, /*info*/undefined!);
129+
const ts1 = new server.TextStorage(host, server.asNormalizedPath(changingFile.path), /*initialVersion*/ undefined, {} as server.ScriptInfo);
130130

131131
assert.isTrue(ts1.reloadFromDisk());
132132

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8046,6 +8046,7 @@ declare namespace ts.server {
80468046
readonly containingProjects: Project[];
80478047
private formatSettings;
80488048
private preferences;
8049+
private textStorage;
80498050
constructor(host: ServerHost, fileName: NormalizedPath, scriptKind: ScriptKind, hasMixedContent: boolean, path: Path, initialVersion?: ScriptInfoVersion);
80508051
isScriptOpen(): boolean;
80518052
open(newText: string): void;

0 commit comments

Comments
 (0)