Skip to content

Commit 78197ed

Browse files
Merge pull request #2518 from Microsoft/preserveNewLines3
Always preserve new lines for array and object literals and additional constructs like blocks.
2 parents 485b5a1 + 62460ae commit 78197ed

File tree

1,982 files changed

+11645
-29470
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,982 files changed

+11645
-29470
lines changed

Jakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOu
254254
options += " --stripInternal"
255255
}
256256

257-
options += " --cacheDownlevelForOfLength --preserveNewLines";
257+
options += " --cacheDownlevelForOfLength";
258258

259259
var cmd = host + " " + dir + compilerFilename + " " + options + " ";
260260
cmd = cmd + sources.join(" ");

src/compiler/commandLineParser.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ module ts {
140140
description: Diagnostics.Do_not_emit_declarations_for_code_that_has_an_internal_annotation,
141141
experimental: true
142142
},
143-
{
144-
name: "preserveNewLines",
145-
type: "boolean",
146-
description: Diagnostics.Preserve_new_lines_when_emitting_code,
147-
experimental: true
148-
},
149143
{
150144
name: "cacheDownlevelForOfLength",
151145
type: "boolean",

src/compiler/emitter.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ module ts {
8888
let writeLine = writer.writeLine;
8989
let increaseIndent = writer.increaseIndent;
9090
let decreaseIndent = writer.decreaseIndent;
91-
let preserveNewLines = compilerOptions.preserveNewLines || false;
9291

9392
let currentSourceFile: SourceFile;
9493

@@ -730,7 +729,7 @@ module ts {
730729

731730
increaseIndent();
732731

733-
if (preserveNewLines && nodeStartPositionsAreOnSameLine(parent, nodes[0])) {
732+
if (nodeStartPositionsAreOnSameLine(parent, nodes[0])) {
734733
if (spacesBetweenBraces) {
735734
write(" ");
736735
}
@@ -741,7 +740,7 @@ module ts {
741740

742741
for (let i = 0, n = nodes.length; i < n; i++) {
743742
if (i) {
744-
if (preserveNewLines && nodeEndIsOnSameLineAsNodeStart(nodes[i - 1], nodes[i])) {
743+
if (nodeEndIsOnSameLineAsNodeStart(nodes[i - 1], nodes[i])) {
745744
write(", ");
746745
}
747746
else {
@@ -759,7 +758,7 @@ module ts {
759758

760759
decreaseIndent();
761760

762-
if (preserveNewLines && nodeEndPositionsAreOnSameLine(parent, lastOrUndefined(nodes))) {
761+
if (nodeEndPositionsAreOnSameLine(parent, lastOrUndefined(nodes))) {
763762
if (spacesBetweenBraces) {
764763
write(" ");
765764
}
@@ -1658,7 +1657,7 @@ module ts {
16581657
// If the code is not indented, an optional valueToWriteWhenNotIndenting will be
16591658
// emitted instead.
16601659
function indentIfOnDifferentLines(parent: Node, node1: Node, node2: Node, valueToWriteWhenNotIndenting?: string): boolean {
1661-
let realNodesAreOnDifferentLines = preserveNewLines && !nodeIsSynthesized(parent) && !nodeEndIsOnSameLineAsNodeStart(node1, node2);
1660+
let realNodesAreOnDifferentLines = !nodeIsSynthesized(parent) && !nodeEndIsOnSameLineAsNodeStart(node1, node2);
16621661

16631662
// Always use a newline for synthesized code if the synthesizer desires it.
16641663
let synthesizedNodeIsOnDifferentLine = synthesizedNodeStartsOnNewLine(node2);
@@ -1966,7 +1965,7 @@ module ts {
19661965
}
19671966

19681967
function emitBlock(node: Block) {
1969-
if (preserveNewLines && isSingleLineEmptyBlock(node)) {
1968+
if (isSingleLineEmptyBlock(node)) {
19701969
emitToken(SyntaxKind.OpenBraceToken, node.pos);
19711970
write(" ");
19721971
emitToken(SyntaxKind.CloseBraceToken, node.statements.end);
@@ -2350,7 +2349,7 @@ module ts {
23502349
write("default:");
23512350
}
23522351

2353-
if (preserveNewLines && node.statements.length === 1 && nodeStartPositionsAreOnSameLine(node, node.statements[0])) {
2352+
if (node.statements.length === 1 && nodeStartPositionsAreOnSameLine(node, node.statements[0])) {
23542353
write(" ");
23552354
emit(node.statements[0]);
23562355
}
@@ -3092,7 +3091,7 @@ module ts {
30923091

30933092
// If we didn't have to emit any preamble code, then attempt to keep the arrow
30943093
// function on one line.
3095-
if (preserveNewLines && !preambleEmitted && nodeStartPositionsAreOnSameLine(node, body)) {
3094+
if (!preambleEmitted && nodeStartPositionsAreOnSameLine(node, body)) {
30963095
write(" ");
30973096
emitStart(body);
30983097
write("return ");
@@ -3140,7 +3139,7 @@ module ts {
31403139

31413140
let preambleEmitted = writer.getTextPos() !== initialTextPos;
31423141

3143-
if (preserveNewLines && !preambleEmitted && nodeEndIsOnSameLineAsNodeStart(body, body)) {
3142+
if (!preambleEmitted && nodeEndIsOnSameLineAsNodeStart(body, body)) {
31443143
for (let statement of body.statements) {
31453144
write(" ");
31463145
emit(statement);

src/compiler/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,6 @@ module ts {
15881588
version?: boolean;
15891589
watch?: boolean;
15901590
/* @internal */ stripInternal?: boolean;
1591-
/* @internal */ preserveNewLines?: boolean;
15921591
/* @internal */ cacheDownlevelForOfLength?: boolean;
15931592
[option: string]: string | number | boolean;
15941593
}

src/harness/harness.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,6 @@ module Harness {
10081008
options.outDir = setting.value;
10091009
break;
10101010

1011-
case 'preservenewlines':
1012-
options.preserveNewLines = !!setting.value;
1013-
break;
1014-
10151011
case 'sourceroot':
10161012
options.sourceRoot = setting.value;
10171013
break;
@@ -1465,7 +1461,7 @@ module Harness {
14651461
var optionRegex = /^[\/]{2}\s*@(\w+)\s*:\s*(\S*)/gm; // multiple matches on multiple lines
14661462

14671463
// List of allowed metadata names
1468-
var fileMetadataNames = ["filename", "comments", "declaration", "module", "nolib", "sourcemap", "target", "out", "outdir", "noemitonerror", "noimplicitany", "noresolve", "newline", "newlines", "emitbom", "errortruncation", "usecasesensitivefilenames", "preserveconstenums", "preservenewlines", "includebuiltfile", "suppressimplicitanyindexerrors", "stripinternal"];
1464+
var fileMetadataNames = ["filename", "comments", "declaration", "module", "nolib", "sourcemap", "target", "out", "outdir", "noemitonerror", "noimplicitany", "noresolve", "newline", "newlines", "emitbom", "errortruncation", "usecasesensitivefilenames", "preserveconstenums", "includebuiltfile", "suppressimplicitanyindexerrors", "stripinternal"];
14691465

14701466
function extractCompilerSettings(content: string): CompilerSetting[] {
14711467

tests/baselines/reference/2dArrays.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ var Board = (function () {
3030
function Board() {
3131
}
3232
Board.prototype.allShipsSunk = function () {
33-
return this.ships.every(function (val) {
34-
return val.isSunk;
35-
});
33+
return this.ships.every(function (val) { return val.isSunk; });
3634
};
3735
return Board;
3836
})();

tests/baselines/reference/APISample_compile.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,8 +2021,6 @@ function compile(fileNames, options) {
20212021
}
20222022
exports.compile = compile;
20232023
compile(process.argv.slice(2), {
2024-
noEmitOnError: true,
2025-
noImplicitAny: true,
2026-
target: 1 /* ES5 */,
2027-
module: 1 /* CommonJS */
2024+
noEmitOnError: true, noImplicitAny: true,
2025+
target: 1 /* ES5 */, module: 1 /* CommonJS */
20282026
});

tests/baselines/reference/APISample_linter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,8 @@ function delint(sourceFile) {
20552055
if (ifStatement.thenStatement.kind !== 176 /* Block */) {
20562056
report(ifStatement.thenStatement, "An if statement's contents should be wrapped in a block body.");
20572057
}
2058-
if (ifStatement.elseStatement && ifStatement.elseStatement.kind !== 176 /* Block */ && ifStatement.elseStatement.kind !== 180 /* IfStatement */) {
2058+
if (ifStatement.elseStatement &&
2059+
ifStatement.elseStatement.kind !== 176 /* Block */ && ifStatement.elseStatement.kind !== 180 /* IfStatement */) {
20592060
report(ifStatement.elseStatement, "An else statement's contents should be wrapped in a block body.");
20602061
}
20612062
break;

tests/baselines/reference/APISample_transform.js

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,43 +2051,29 @@ function transform(contents, compilerOptions) {
20512051
// Create a compilerHost object to allow the compiler to read and write files
20522052
var compilerHost = {
20532053
getSourceFile: function (fileName, target) {
2054-
return files[fileName] !== undefined ? ts.createSourceFile(fileName, files[fileName], target) : undefined;
2054+
return files[fileName] !== undefined ?
2055+
ts.createSourceFile(fileName, files[fileName], target) : undefined;
20552056
},
20562057
writeFile: function (name, text, writeByteOrderMark) {
2057-
outputs.push({
2058-
name: name,
2059-
text: text,
2060-
writeByteOrderMark: writeByteOrderMark
2061-
});
2058+
outputs.push({ name: name, text: text, writeByteOrderMark: writeByteOrderMark });
20622059
},
2063-
getDefaultLibFileName: function () {
2064-
return "lib.d.ts";
2065-
},
2066-
useCaseSensitiveFileNames: function () {
2067-
return false;
2068-
},
2069-
getCanonicalFileName: function (fileName) {
2070-
return fileName;
2071-
},
2072-
getCurrentDirectory: function () {
2073-
return "";
2074-
},
2075-
getNewLine: function () {
2076-
return "\n";
2077-
}
2060+
getDefaultLibFileName: function () { return "lib.d.ts"; },
2061+
useCaseSensitiveFileNames: function () { return false; },
2062+
getCanonicalFileName: function (fileName) { return fileName; },
2063+
getCurrentDirectory: function () { return ""; },
2064+
getNewLine: function () { return "\n"; }
20782065
};
20792066
// Create a program from inputs
2080-
var program = ts.createProgram([
2081-
"file.ts"
2082-
], compilerOptions, compilerHost);
2067+
var program = ts.createProgram(["file.ts"], compilerOptions, compilerHost);
20832068
// Query for early errors
20842069
var errors = ts.getPreEmitDiagnostics(program);
20852070
var emitResult = program.emit();
20862071
errors = errors.concat(emitResult.diagnostics);
20872072
return {
20882073
outputs: outputs,
20892074
errors: errors.map(function (e) {
2090-
return e.file.fileName + "(" + (e.file.getLineAndCharacterOfPosition(e.start).line + 1) + "): " + ts.flattenDiagnosticMessageText(e.messageText, os.EOL);
2075+
return e.file.fileName + "(" + (e.file.getLineAndCharacterOfPosition(e.start).line + 1) + "): "
2076+
+ ts.flattenDiagnosticMessageText(e.messageText, os.EOL);
20912077
})
20922078
};
20932079
}

tests/baselines/reference/APISample_watcher.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,33 +2080,21 @@ function watch(rootFileNames, options) {
20802080
var files = {};
20812081
// initialize the list of files
20822082
rootFileNames.forEach(function (fileName) {
2083-
files[fileName] = {
2084-
version: 0
2085-
};
2083+
files[fileName] = { version: 0 };
20862084
});
20872085
// Create the language service host to allow the LS to communicate with the host
20882086
var servicesHost = {
2089-
getScriptFileNames: function () {
2090-
return rootFileNames;
2091-
},
2092-
getScriptVersion: function (fileName) {
2093-
return files[fileName] && files[fileName].version.toString();
2094-
},
2087+
getScriptFileNames: function () { return rootFileNames; },
2088+
getScriptVersion: function (fileName) { return files[fileName] && files[fileName].version.toString(); },
20952089
getScriptSnapshot: function (fileName) {
20962090
if (!fs.existsSync(fileName)) {
20972091
return undefined;
20982092
}
20992093
return ts.ScriptSnapshot.fromString(fs.readFileSync(fileName).toString());
21002094
},
2101-
getCurrentDirectory: function () {
2102-
return process.cwd();
2103-
},
2104-
getCompilationSettings: function () {
2105-
return options;
2106-
},
2107-
getDefaultLibFileName: function (options) {
2108-
return ts.getDefaultLibFilePath(options);
2109-
}
2095+
getCurrentDirectory: function () { return process.cwd(); },
2096+
getCompilationSettings: function () { return options; },
2097+
getDefaultLibFileName: function (options) { return ts.getDefaultLibFilePath(options); }
21102098
};
21112099
// Create the language service files
21122100
var services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry());
@@ -2115,10 +2103,7 @@ function watch(rootFileNames, options) {
21152103
// First time around, emit all files
21162104
emitFile(fileName);
21172105
// Add a watch on the file to handle next change
2118-
fs.watchFile(fileName, {
2119-
persistent: true,
2120-
interval: 250
2121-
}, function (curr, prev) {
2106+
fs.watchFile(fileName, { persistent: true, interval: 250 }, function (curr, prev) {
21222107
// Check timestamp
21232108
if (+curr.mtime <= +prev.mtime) {
21242109
return;
@@ -2143,7 +2128,9 @@ function watch(rootFileNames, options) {
21432128
});
21442129
}
21452130
function logErrors(fileName) {
2146-
var allDiagnostics = services.getCompilerOptionsDiagnostics().concat(services.getSyntacticDiagnostics(fileName)).concat(services.getSemanticDiagnostics(fileName));
2131+
var allDiagnostics = services.getCompilerOptionsDiagnostics()
2132+
.concat(services.getSyntacticDiagnostics(fileName))
2133+
.concat(services.getSemanticDiagnostics(fileName));
21472134
allDiagnostics.forEach(function (diagnostic) {
21482135
if (diagnostic.file) {
21492136
var lineChar = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
@@ -2156,10 +2143,7 @@ function watch(rootFileNames, options) {
21562143
}
21572144
}
21582145
// Initialize files constituting the program as all .ts files in the current directory
2159-
var currentDirectoryFiles = fs.readdirSync(process.cwd()).filter(function (fileName) {
2160-
return fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts";
2161-
});
2146+
var currentDirectoryFiles = fs.readdirSync(process.cwd()).
2147+
filter(function (fileName) { return fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts"; });
21622148
// Start the watcher
2163-
watch(currentDirectoryFiles, {
2164-
module: 1 /* CommonJS */
2165-
});
2149+
watch(currentDirectoryFiles, { module: 1 /* CommonJS */ });

0 commit comments

Comments
 (0)