Skip to content
Merged
Prev Previous commit
Next Next commit
Compiler flag to specify line ending #1693 unit tests
  • Loading branch information
kmashint committed May 3, 2015
commit 2e0a55c4d352a7010f7dc05eeff1b584f2d70bcc
9 changes: 6 additions & 3 deletions 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,9 +94,9 @@ module ts {
}
}

let newLine =
options.newLine === NewLineKind.CarriageReturnLineFeed ? "\r\n" :
options.newLine === NewLineKind.LineFeed ? "\n" :
let newLine =
options.newLine === NewLineKind.CarriageReturnLineFeed ? NEWLINE_CRLF :
options.newLine === NewLineKind.LineFeed ? NEWLINE_LF :
sys.newLine;

return {
Expand Down
26 changes: 22 additions & 4 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use tabs


// 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 @@ -1042,7 +1051,16 @@ module Harness {

case 'newline':
case 'newlines':
newLine = setting.value;
if (setting.value.toLowerCase() === 'crlf') {
options.newLine = ts.NewLineKind.CarriageReturnLineFeed;
} else if (setting.value.toLowerCase() === 'lf') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else on the next line (I know, the rest of the file is inconsistent)

options.newLine = ts.NewLineKind.LineFeed;
} else if (setting.value === '\\n') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right- none of the other options work this way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what happened; any idea how many other tests use that flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A grep search revealed only the contextualTyping.ts uses this other @newline flag as in the comment, no matches for @newlines plural.
// Handle old usage, e.g. contextualTyping.ts:// @newline: \n

I could use newlineflag for this new option, or change the old seldom-used option to something more internal, e.g. @normalizeNewline since it seems to be used in normalizeLineEndings(...).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't even know why the heck we ever introduced that - I think @danquirk might've added it after we futzed with baselining issues, but I think it would be safe to assume we can use the LF flag here.

// Handle old usage, e.g. contextualTyping.ts:// @newline: \n
newLine = setting.value;
} else {
throw new Error('Unknown option for newLine: ' + setting.value);
}
break;

case 'comments':
Expand Down Expand Up @@ -1103,7 +1121,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
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


4 changes: 4 additions & 0 deletions tests/cases/compiler/newLineFlagWithCR.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @newline: CR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support CR so I guess we shouldn't have this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was suggested to add a negative test for an invalid command-line arg but the test framework doesn't seem to support it so I'll remove the test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about any miscommunication - my understanding is that our harness doesn't support that sort of thing, though it's highly possible that @mhegazy might know something I'm not aware of.

var x=1;
x=2;

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;