Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler flag to specify line ending #2921

Merged
merged 9 commits into from
May 4, 2015
7 changes: 7 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ module ts {
paramType: Diagnostics.KIND,
error: Diagnostics.Argument_for_module_option_must_be_commonjs_amd_system_or_umd
},
{
name: "newLine",
type: { "crlf": NewLineKind.CarriageReturnLineFeed, "lf": NewLineKind.LineFeed },
description: Diagnostics.Emit_newline_Colon_CRLF_dos_or_LF_unix,
paramType: Diagnostics.NEWLINE,
error: Diagnostics.Argument_for_newLine_option_must_be_CRLF_or_LF
},
{
name: "noEmit",
type: "boolean",
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ module ts {
Preserve_new_lines_when_emitting_code: { code: 6057, category: DiagnosticCategory.Message, key: "Preserve new-lines when emitting code." },
Specifies_the_root_directory_of_input_files_Use_to_control_the_output_directory_structure_with_outDir: { code: 6058, category: DiagnosticCategory.Message, key: "Specifies the root directory of input files. Use to control the output directory structure with --outDir." },
File_0_is_not_under_rootDir_1_rootDir_is_expected_to_contain_all_source_files: { code: 6059, category: DiagnosticCategory.Error, key: "File '{0}' is not under 'rootDir' '{1}'. 'rootDir' is expected to contain all source files." },
Emit_newline_Colon_CRLF_dos_or_LF_unix: { code: 6060, category: DiagnosticCategory.Message, key: "Emit newline: 'CRLF' (dos) or 'LF' (unix)." },
NEWLINE: { code: 6061, category: DiagnosticCategory.Message, key: "NEWLINE" },
Argument_for_newLine_option_must_be_CRLF_or_LF: { code: 6062, category: DiagnosticCategory.Error, key: "Argument for 'newLine' option must be 'CRLF' or 'LF'." },
Variable_0_implicitly_has_an_1_type: { code: 7005, category: DiagnosticCategory.Error, key: "Variable '{0}' implicitly has an '{1}' type." },
Parameter_0_implicitly_has_an_1_type: { code: 7006, category: DiagnosticCategory.Error, key: "Parameter '{0}' implicitly has an '{1}' type." },
Member_0_implicitly_has_an_1_type: { code: 7008, category: DiagnosticCategory.Error, key: "Member '{0}' implicitly has an '{1}' type." },
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,18 @@
"category": "Error",
"code": 6059
},
"Emit newline: 'CRLF' (dos) or 'LF' (unix).": {
"category": "Message",
"code": 6060
},
"NEWLINE": {
"category": "Message",
"code": 6061
},
"Argument for 'newLine' option must be 'CRLF' or 'LF'.": {
"category": "Error",
"code": 6062
},


"Variable '{0}' implicitly has an '{1}' type.": {
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module ts {

/** The version of the TypeScript compiler release */
export const version = "1.5.0";

const NEWLINE_CRLF = "\r\n";
const NEWLINE_LF = "\n";

export function findConfigFile(searchPath: string): string {
var fileName = "tsconfig.json";
Expand Down Expand Up @@ -91,14 +94,19 @@ module ts {
}
}

let newLine =
options.newLine === NewLineKind.CarriageReturnLineFeed ? NEWLINE_CRLF :
options.newLine === NewLineKind.LineFeed ? NEWLINE_LF :
sys.newLine;

return {
getSourceFile,
getDefaultLibFileName: options => combinePaths(getDirectoryPath(normalizePath(sys.getExecutingFilePath())), getDefaultLibFileName(options)),
writeFile,
getCurrentDirectory: () => currentDirectory || (currentDirectory = sys.getCurrentDirectory()),
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
getCanonicalFileName,
getNewLine: () => sys.newLine
getNewLine: () => newLine
};
}

Expand Down
6 changes: 6 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,7 @@ module ts {
locale?: string;
mapRoot?: string;
module?: ModuleKind;
newLine?: NewLineKind;
noEmit?: boolean;
noEmitHelpers?: boolean;
noEmitOnError?: boolean;
Expand Down Expand Up @@ -1689,6 +1690,11 @@ module ts {
System = 4,
}

export const enum NewLineKind {
CarriageReturnLineFeed = 0,
LineFeed = 1,
}

export interface LineAndCharacter {
line: number;
/*
Expand Down
32 changes: 26 additions & 6 deletions src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,9 @@ module Harness {
return result;
}

const NEWLINE_CRLF = "\r\n";
const NEWLINE_LF = "\n";

export var defaultLibFileName = 'lib.d.ts';
export var defaultLibSourceFile = createSourceFileAndAssertInvariants(defaultLibFileName, IO.readFile(libFolder + 'lib.core.d.ts'), /*languageVersion*/ ts.ScriptTarget.Latest);
export var defaultES6LibSourceFile = createSourceFileAndAssertInvariants(defaultLibFileName, IO.readFile(libFolder + 'lib.core.es6.d.ts'), /*languageVersion*/ ts.ScriptTarget.Latest);
Expand All @@ -822,7 +825,8 @@ module Harness {
scriptTarget: ts.ScriptTarget,
useCaseSensitiveFileNames: boolean,
// the currentDirectory is needed for rwcRunner to passed in specified current directory to compiler host
currentDirectory?: string): ts.CompilerHost {
currentDirectory?: string,
newLineKind?: ts.NewLineKind): ts.CompilerHost {

// Local get canonical file name function, that depends on passed in parameter for useCaseSensitiveFileNames
function getCanonicalFileName(fileName: string): string {
Expand All @@ -841,6 +845,11 @@ module Harness {
};
inputFiles.forEach(register);

let newLine =
newLineKind === ts.NewLineKind.CarriageReturnLineFeed ? NEWLINE_CRLF :
newLineKind === ts.NewLineKind.LineFeed ? NEWLINE_LF :
ts.sys.newLine;

return {
getCurrentDirectory,
getSourceFile: (fn, languageVersion) => {
Expand Down Expand Up @@ -869,7 +878,7 @@ module Harness {
writeFile,
getCanonicalFileName,
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
getNewLine: () => ts.sys.newLine
getNewLine: () => newLine
};
}

Expand Down Expand Up @@ -1041,7 +1050,18 @@ module Harness {
break;

case 'newline':
case 'newlines':
if (setting.value.toLowerCase() === 'crlf') {
options.newLine = ts.NewLineKind.CarriageReturnLineFeed;
}
else if (setting.value.toLowerCase() === 'lf') {
options.newLine = ts.NewLineKind.LineFeed;
}
else {
throw new Error('Unknown option for newLine: ' + setting.value);
}
break;

case 'normalizenewline':
newLine = setting.value;
break;

Expand Down Expand Up @@ -1103,7 +1123,7 @@ module Harness {
var programFiles = inputFiles.concat(includeBuiltFiles).map(file => file.unitName);
var program = ts.createProgram(programFiles, options, createCompilerHost(inputFiles.concat(includeBuiltFiles).concat(otherFiles),
(fn, contents, writeByteOrderMark) => fileOutputs.push({ fileName: fn, code: contents, writeByteOrderMark: writeByteOrderMark }),
options.target, useCaseSensitiveFileNames, currentDirectory));
options.target, useCaseSensitiveFileNames, currentDirectory, options.newLine));

var emitResult = program.emit();

Expand Down Expand Up @@ -1486,7 +1506,7 @@ module Harness {
// List of allowed metadata names
var fileMetadataNames = ["filename", "comments", "declaration", "module",
"nolib", "sourcemap", "target", "out", "outdir", "noemithelpers", "noemitonerror",
"noimplicitany", "noresolve", "newline", "newlines", "emitbom",
"noimplicitany", "noresolve", "newline", "normalizenewline", "emitbom",
"errortruncation", "usecasesensitivefilenames", "preserveconstenums",
"includebuiltfile", "suppressimplicitanyindexerrors", "stripinternal",
"separatecompilation", "inlinesourcemap", "maproot", "sourceroot",
Expand Down Expand Up @@ -1744,4 +1764,4 @@ module Harness {
}

// TODO: not sure why Utils.evalFile isn't working with this, eventually will concat it like old compiler instead of eval
eval(Harness.tcServicesFile);
eval(Harness.tcServicesFile);
9 changes: 9 additions & 0 deletions tests/baselines/reference/newLineFlagWithCRLF.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [newLineFlagWithCRLF.ts]
var x=1;
x=2;



//// [newLineFlagWithCRLF.js]
var x = 1;
x = 2;
8 changes: 8 additions & 0 deletions tests/baselines/reference/newLineFlagWithCRLF.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/newLineFlagWithCRLF.ts ===
var x=1;
>x : Symbol(x, Decl(newLineFlagWithCRLF.ts, 0, 3))

x=2;
>x : Symbol(x, Decl(newLineFlagWithCRLF.ts, 0, 3))


11 changes: 11 additions & 0 deletions tests/baselines/reference/newLineFlagWithCRLF.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/newLineFlagWithCRLF.ts ===
var x=1;
>x : number
>1 : number

x=2;
>x=2 : number
>x : number
>2 : number


9 changes: 9 additions & 0 deletions tests/baselines/reference/newLineFlagWithLF.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [newLineFlagWithLF.ts]
var x=1;
x=2;



//// [newLineFlagWithLF.js]
var x = 1;
x = 2;
8 changes: 8 additions & 0 deletions tests/baselines/reference/newLineFlagWithLF.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/newLineFlagWithLF.ts ===
var x=1;
>x : Symbol(x, Decl(newLineFlagWithLF.ts, 0, 3))

x=2;
>x : Symbol(x, Decl(newLineFlagWithLF.ts, 0, 3))


11 changes: 11 additions & 0 deletions tests/baselines/reference/newLineFlagWithLF.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/newLineFlagWithLF.ts ===
var x=1;
>x : number
>1 : number

x=2;
>x=2 : number
>x : number
>2 : number


2 changes: 1 addition & 1 deletion tests/cases/compiler/contextualTyping.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @newline: \n
// @normalizenewline: \n
// @sourcemap: true
// DEFAULT INTERFACES
interface IFoo {
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/compiler/newLineFlagWithCRLF.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @newline: CRLF
var x=1;
x=2;

4 changes: 4 additions & 0 deletions tests/cases/compiler/newLineFlagWithLF.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @newline: LF
var x=1;
x=2;