Skip to content

Commit

Permalink
tsserver should use newline provided by the host (#13185) (#13186)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladima committed Dec 27, 2016
1 parent dde8411 commit 150463e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
32 changes: 31 additions & 1 deletion src/harness/unittests/compileOnSave.ts
Expand Up @@ -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(<server.protocol.OpenRequest>{
seq: 1,
type: "request",
command: "open",
arguments: { file: f.path }
});
session.executeCommand(<server.protocol.CompileOnSaveEmitFileRequest>{
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",
Expand All @@ -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);

Expand Down
7 changes: 4 additions & 3 deletions src/harness/unittests/tsserverProjectSystem.ts
Expand Up @@ -141,6 +141,7 @@ namespace ts.projectSystem {
useCaseSensitiveFileNames?: boolean;
executingFilePath?: string;
currentDirectory?: string;
newLine?: string;
}

export function createServerHost(fileOrFolderList: FileOrFolder[], params?: TestServerHostCreationParameters): TestServerHost {
Expand All @@ -151,7 +152,8 @@ namespace ts.projectSystem {
params.useCaseSensitiveFileNames !== undefined ? params.useCaseSensitiveFileNames : false,
params.executingFilePath || getExecutingFilePathFromLibFile(),
params.currentDirectory || "/",
fileOrFolderList);
fileOrFolderList,
params.newLine);
return host;
}

Expand Down Expand Up @@ -329,7 +331,6 @@ namespace ts.projectSystem {

export class TestServerHost implements server.ServerHost {
args: string[] = [];
newLine: "\n";

private fs: ts.FileMap<FSEntry>;
private getCanonicalFileName: (s: string) => string;
Expand All @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions src/server/lsHost.ts
Expand Up @@ -135,6 +135,10 @@ namespace ts.server {
}
}

getNewLine() {
return this.host.newLine;
}

getProjectVersion() {
return this.project.getProjectVersion();
}
Expand Down

0 comments on commit 150463e

Please sign in to comment.