From 77a3dfbcfc37585da8e634e73032ee3f44fef1b3 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Tue, 27 Dec 2016 11:59:56 -0800 Subject: [PATCH] tsserver should use newline provided by the host (#13185) --- src/harness/unittests/compileOnSave.ts | 32 ++++++++++++++++++- .../unittests/tsserverProjectSystem.ts | 7 ++-- src/server/lsHost.ts | 4 +++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/harness/unittests/compileOnSave.ts b/src/harness/unittests/compileOnSave.ts index d7731a625fe..c8deb9e7554 100644 --- a/src/harness/unittests/compileOnSave.ts +++ b/src/harness/unittests/compileOnSave.ts @@ -467,6 +467,36 @@ namespace ts.projectSystem { }); describe("EmitFile test", () => { + it("should respect line endings", () => { + test("\n"); + test("\r\n"); + + function test(newLine: string) { + const lines = ["var x = 1;", "var y = 2;"]; + const path = "/a/app"; + const f = { + path: path + ".ts", + content: lines.join(newLine) + }; + const host = createServerHost([f], { newLine }); + const session = createSession(host); + session.executeCommand({ + seq: 1, + type: "request", + command: "open", + arguments: { file: f.path } + }); + session.executeCommand({ + seq: 2, + type: "request", + command: "compileOnSaveEmitFile", + arguments: { file: f.path } + }); + const emitOutput = host.readFile(path + ".js"); + assert.equal(emitOutput, f.content + newLine, "content of emit output should be identical with the input + newline"); + } + }) + it("should emit specified file", () => { const file1 = { path: "/a/b/f1.ts", @@ -480,7 +510,7 @@ namespace ts.projectSystem { path: "/a/b/tsconfig.json", content: `{}` }; - const host = createServerHost([file1, file2, configFile, libFile]); + const host = createServerHost([file1, file2, configFile, libFile], { newLine: "\r\n" }); const typingsInstaller = createTestTypingsInstaller(host); const session = new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ false); diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index d379b5bbf9c..5b9f4332359 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -141,6 +141,7 @@ namespace ts.projectSystem { useCaseSensitiveFileNames?: boolean; executingFilePath?: string; currentDirectory?: string; + newLine?: string; } export function createServerHost(fileOrFolderList: FileOrFolder[], params?: TestServerHostCreationParameters): TestServerHost { @@ -151,7 +152,8 @@ namespace ts.projectSystem { params.useCaseSensitiveFileNames !== undefined ? params.useCaseSensitiveFileNames : false, params.executingFilePath || getExecutingFilePathFromLibFile(), params.currentDirectory || "/", - fileOrFolderList); + fileOrFolderList, + params.newLine); return host; } @@ -329,7 +331,6 @@ namespace ts.projectSystem { export class TestServerHost implements server.ServerHost { args: string[] = []; - newLine: "\n"; private fs: ts.FileMap; private getCanonicalFileName: (s: string) => string; @@ -342,7 +343,7 @@ namespace ts.projectSystem { private filesOrFolders: FileOrFolder[]; - constructor(public useCaseSensitiveFileNames: boolean, private executingFilePath: string, private currentDirectory: string, fileOrFolderList: FileOrFolder[]) { + constructor(public useCaseSensitiveFileNames: boolean, private executingFilePath: string, private currentDirectory: string, fileOrFolderList: FileOrFolder[], public readonly newLine = "\n") { this.getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames); this.toPath = s => toPath(s, currentDirectory, this.getCanonicalFileName); diff --git a/src/server/lsHost.ts b/src/server/lsHost.ts index d73a31933f4..98b3dd9a8c0 100644 --- a/src/server/lsHost.ts +++ b/src/server/lsHost.ts @@ -135,6 +135,10 @@ namespace ts.server { } } + getNewLine() { + return this.host.newLine; + } + getProjectVersion() { return this.project.getProjectVersion(); }