Skip to content

Commit 77a3dfb

Browse files
authored
tsserver should use newline provided by the host (#13185)
1 parent bec32d4 commit 77a3dfb

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/harness/unittests/compileOnSave.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,36 @@ namespace ts.projectSystem {
467467
});
468468

469469
describe("EmitFile test", () => {
470+
it("should respect line endings", () => {
471+
test("\n");
472+
test("\r\n");
473+
474+
function test(newLine: string) {
475+
const lines = ["var x = 1;", "var y = 2;"];
476+
const path = "/a/app";
477+
const f = {
478+
path: path + ".ts",
479+
content: lines.join(newLine)
480+
};
481+
const host = createServerHost([f], { newLine });
482+
const session = createSession(host);
483+
session.executeCommand(<server.protocol.OpenRequest>{
484+
seq: 1,
485+
type: "request",
486+
command: "open",
487+
arguments: { file: f.path }
488+
});
489+
session.executeCommand(<server.protocol.CompileOnSaveEmitFileRequest>{
490+
seq: 2,
491+
type: "request",
492+
command: "compileOnSaveEmitFile",
493+
arguments: { file: f.path }
494+
});
495+
const emitOutput = host.readFile(path + ".js");
496+
assert.equal(emitOutput, f.content + newLine, "content of emit output should be identical with the input + newline");
497+
}
498+
})
499+
470500
it("should emit specified file", () => {
471501
const file1 = {
472502
path: "/a/b/f1.ts",
@@ -480,7 +510,7 @@ namespace ts.projectSystem {
480510
path: "/a/b/tsconfig.json",
481511
content: `{}`
482512
};
483-
const host = createServerHost([file1, file2, configFile, libFile]);
513+
const host = createServerHost([file1, file2, configFile, libFile], { newLine: "\r\n" });
484514
const typingsInstaller = createTestTypingsInstaller(host);
485515
const session = new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ false);
486516

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ namespace ts.projectSystem {
141141
useCaseSensitiveFileNames?: boolean;
142142
executingFilePath?: string;
143143
currentDirectory?: string;
144+
newLine?: string;
144145
}
145146

146147
export function createServerHost(fileOrFolderList: FileOrFolder[], params?: TestServerHostCreationParameters): TestServerHost {
@@ -151,7 +152,8 @@ namespace ts.projectSystem {
151152
params.useCaseSensitiveFileNames !== undefined ? params.useCaseSensitiveFileNames : false,
152153
params.executingFilePath || getExecutingFilePathFromLibFile(),
153154
params.currentDirectory || "/",
154-
fileOrFolderList);
155+
fileOrFolderList,
156+
params.newLine);
155157
return host;
156158
}
157159

@@ -329,7 +331,6 @@ namespace ts.projectSystem {
329331

330332
export class TestServerHost implements server.ServerHost {
331333
args: string[] = [];
332-
newLine: "\n";
333334

334335
private fs: ts.FileMap<FSEntry>;
335336
private getCanonicalFileName: (s: string) => string;
@@ -342,7 +343,7 @@ namespace ts.projectSystem {
342343

343344
private filesOrFolders: FileOrFolder[];
344345

345-
constructor(public useCaseSensitiveFileNames: boolean, private executingFilePath: string, private currentDirectory: string, fileOrFolderList: FileOrFolder[]) {
346+
constructor(public useCaseSensitiveFileNames: boolean, private executingFilePath: string, private currentDirectory: string, fileOrFolderList: FileOrFolder[], public readonly newLine = "\n") {
346347
this.getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames);
347348
this.toPath = s => toPath(s, currentDirectory, this.getCanonicalFileName);
348349

src/server/lsHost.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ namespace ts.server {
135135
}
136136
}
137137

138+
getNewLine() {
139+
return this.host.newLine;
140+
}
141+
138142
getProjectVersion() {
139143
return this.project.getProjectVersion();
140144
}

0 commit comments

Comments
 (0)