Skip to content

Commit f9df4e6

Browse files
authored
Respect newLine compiler option in language service output (#19279)
1 parent 0c1730a commit f9df4e6

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

Diff for: Jakefile.js

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ var harnessSources = harnessCoreSources.concat([
154154
"symbolWalker.ts",
155155
"languageService.ts",
156156
"publicApi.ts",
157+
"hostNewLineSupport.ts",
157158
].map(function (f) {
158159
return path.join(unittestsDirectory, f);
159160
})).concat([

Diff for: src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3199,7 +3199,7 @@ namespace ts {
31993199

32003200
const carriageReturnLineFeed = "\r\n";
32013201
const lineFeed = "\n";
3202-
export function getNewLineCharacter(options: CompilerOptions | PrinterOptions, system?: System): string {
3202+
export function getNewLineCharacter(options: CompilerOptions | PrinterOptions, system?: { newLine: string }): string {
32033203
switch (options.newLine) {
32043204
case NewLineKind.CarriageReturnLineFeed:
32053205
return carriageReturnLineFeed;

Diff for: src/harness/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
"./unittests/telemetry.ts",
140140
"./unittests/languageService.ts",
141141
"./unittests/programMissingFiles.ts",
142-
"./unittests/publicApi.ts"
142+
"./unittests/publicApi.ts",
143+
"./unittests/hostNewLineSupport.ts"
143144
]
144145
}

Diff for: src/harness/unittests/hostNewLineSupport.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/// <reference path="..\harness.ts" />
2+
namespace ts {
3+
describe("hostNewLineSupport", () => {
4+
function testLSWithFiles(settings: CompilerOptions, files: Harness.Compiler.TestFile[]) {
5+
function snapFor(path: string): IScriptSnapshot {
6+
if (path === "lib.d.ts") {
7+
return {
8+
dispose() {},
9+
getChangeRange() { return undefined; },
10+
getLength() { return 0; },
11+
getText(_start, _end) {
12+
return "";
13+
}
14+
};
15+
}
16+
const result = forEach(files, f => f.unitName === path ? f : undefined);
17+
if (result) {
18+
return {
19+
dispose() {},
20+
getChangeRange() { return undefined; },
21+
getLength() { return result.content.length; },
22+
getText(start, end) {
23+
return result.content.substring(start, end);
24+
}
25+
};
26+
}
27+
return undefined;
28+
}
29+
const lshost: LanguageServiceHost = {
30+
getCompilationSettings: () => settings,
31+
getScriptFileNames: () => map(files, f => f.unitName),
32+
getScriptVersion: () => "1",
33+
getScriptSnapshot: name => snapFor(name),
34+
getDefaultLibFileName: () => "lib.d.ts",
35+
getCurrentDirectory: () => "",
36+
};
37+
return ts.createLanguageService(lshost);
38+
}
39+
40+
function verifyNewLines(content: string, options: CompilerOptions) {
41+
const ls = testLSWithFiles(options, [{
42+
content,
43+
fileOptions: {},
44+
unitName: "input.ts"
45+
}]);
46+
const result = ls.getEmitOutput("input.ts");
47+
assert(!result.emitSkipped, "emit was skipped");
48+
assert(result.outputFiles.length === 1, "a number of files other than 1 was output");
49+
assert(result.outputFiles[0].name === "input.js", `Expected output file name input.js, but got ${result.outputFiles[0].name}`);
50+
assert(result.outputFiles[0].text.match(options.newLine === NewLineKind.CarriageReturnLineFeed ? /\r\n/ : /[^\r]\n/), "expected to find appropriate newlines");
51+
assert(!result.outputFiles[0].text.match(options.newLine === NewLineKind.CarriageReturnLineFeed ? /[^\r]\n/ : /\r\n/), "expected not to find inappropriate newlines");
52+
}
53+
54+
function verifyBothNewLines(content: string) {
55+
verifyNewLines(content, { newLine: NewLineKind.CarriageReturnLineFeed });
56+
verifyNewLines(content, { newLine: NewLineKind.LineFeed });
57+
}
58+
59+
it("should exist and respect provided compiler options", () => {
60+
verifyBothNewLines(`
61+
function foo() {
62+
return 2 + 2;
63+
}
64+
`);
65+
});
66+
});
67+
}

Diff for: src/services/services.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ namespace ts {
11461146
getCancellationToken: () => cancellationToken,
11471147
getCanonicalFileName,
11481148
useCaseSensitiveFileNames: () => useCaseSensitivefileNames,
1149-
getNewLine: () => getNewLineOrDefaultFromHost(host),
1149+
getNewLine: () => getNewLineCharacter(newSettings, { newLine: getNewLineOrDefaultFromHost(host) }),
11501150
getDefaultLibFileName: (options) => host.getDefaultLibFileName(options),
11511151
writeFile: noop,
11521152
getCurrentDirectory: () => currentDirectory,

0 commit comments

Comments
 (0)