Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ var harnessSources = [
"services/preProcessFile.ts",
"services/patternMatcher.ts",
"versionCache.ts",
"convertToBase64.ts"
"convertToBase64.ts",
"transpile.ts"
].map(function (f) {
return path.join(unittestsDirectory, f);
})).concat([
Expand Down
15 changes: 8 additions & 7 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ module ts {
/** The version of the TypeScript compiler release */
export const version = "1.5.3";

const carriageReturnLineFeed = "\r\n";
const lineFeed = "\n";

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

let newLine =
options.newLine === NewLineKind.CarriageReturnLineFeed ? carriageReturnLineFeed :
options.newLine === NewLineKind.LineFeed ? lineFeed :
sys.newLine;
const newLine = getNewLineCharacter(options);

return {
getSourceFile,
Expand Down Expand Up @@ -175,6 +169,7 @@ module ts {
getGlobalDiagnostics,
getSemanticDiagnostics,
getDeclarationDiagnostics,
getCompilerOptionsDiagnostics,
getTypeChecker,
getDiagnosticsProducingTypeChecker,
getCommonSourceDirectory: () => commonSourceDirectory,
Expand Down Expand Up @@ -291,6 +286,12 @@ module ts {
}
}

function getCompilerOptionsDiagnostics(): Diagnostic[] {
let allDiagnostics: Diagnostic[] = [];
addRange(allDiagnostics, diagnostics.getGlobalDiagnostics());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there a reason we can't just call getGlobalDiagnostics? Does it cache?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It also gets the checker global diagnostics like cannot find Array. I did not want to pass a flag to it, do adding the new function seemed more appealing.

return sortAndDeduplicateDiagnostics(allDiagnostics);
}

function getGlobalDiagnostics(): Diagnostic[] {
let typeChecker = getDiagnosticsProducingTypeChecker();

Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ module ts {
getGlobalDiagnostics(): Diagnostic[];
getSemanticDiagnostics(sourceFile?: SourceFile): Diagnostic[];
getDeclarationDiagnostics(sourceFile?: SourceFile): Diagnostic[];
/* @internal */ getCompilerOptionsDiagnostics(): Diagnostic[];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is bad. We now have multiple ways to get the same diagnostics. Previously, diagnostics were entirely split, and any given diagnostic would only be in one of the four "getXXXDiagnostics" methods. Now both getGlobalDiagnostics and getCompilerOptions diagnostics will return the same errors. This makes it much harder for clients to know which one they should call. If you do want to expose getCompilerOptionsDiagnostics, then i would remove the compiler options diagnostics from getGlobalDiagnostics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is why i made it internal.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not just concerned about external clients. I'm also concerned about our own code :)

This is now more confusing for us in terms of knowing which of these functions we should be calling. Previously it was simple. To get all errors, with no duplicates, you just called each of these functions. Now you have to know "oh... if i'm transpiling, then i should call "getCompilerOptionsDiagnostics" but not "getGlobalDiagnostics". This is not discoverable and will cause confusion.

Also, do we need to fix up anything with 'emit' in the LS case? If the user has hte option set to just transpile, should we be changing how we get errors?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

transpile is not an option. it is an entry point. the LS never uses transpile, nor does the commandline compiler.


/**
* Gets a type checker that can be used to semantically analyze source fils in the program.
Expand Down
15 changes: 15 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,21 @@ module ts {

return result;
}

const carriageReturnLineFeed = "\r\n";
const lineFeed = "\n";
export function getNewLineCharacter(options: CompilerOptions): string {
if (options.newLine === NewLineKind.CarriageReturnLineFeed) {
return carriageReturnLineFeed;
}
else if (options.newLine === NewLineKind.LineFeed) {
return lineFeed;
}
else if (sys) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you mean typeof sys !== undefined.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe just have a defaultNewLine parameter. I don't mind either way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think sys is always defined now, it is just may have no value if you are in services.

return sys.newLine
}
return carriageReturnLineFeed;
}
}

module ts {
Expand Down
20 changes: 16 additions & 4 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,8 @@ module ts {
* Extra compiler options that will unconditionally be used bu this function are:
* - isolatedModules = true
* - allowNonTsExtensions = true
* - noLib = true
* - noResolve = true
*/
export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[]): string {
let options = compilerOptions ? clone(compilerOptions) : getDefaultCompilerOptions();
Expand All @@ -1772,15 +1774,25 @@ module ts {
// Filename can be non-ts file.
options.allowNonTsExtensions = true;

// We are not returning a sourceFile for lib file when asked by the program,
// so pass --noLib to avoid reporting a file not found error.
options.noLib = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not sure what this comment means.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

not sure how to explain this more.. i updated the comment message, the reference here is to getSourceFile method in the host implementation.. see:

 getSourceFile: (fileName, target) => fileName === inputFileName ? sourceFile : undefined,


// We are not doing a full typecheck, we are not resolving the whole context,
// so pass --noResolve to avoid reporting missing file errors.
options.noResolve = true;

// Parse
var inputFileName = fileName || "module.ts";
var sourceFile = createSourceFile(inputFileName, input, options.target);
let inputFileName = fileName || "module.ts";
let sourceFile = createSourceFile(inputFileName, input, options.target);

// Store syntactic diagnostics
if (diagnostics && sourceFile.parseDiagnostics) {
diagnostics.push(...sourceFile.parseDiagnostics);
}

let newLine = getNewLineCharacter(options);

// Output
let outputText: string;

Expand All @@ -1795,13 +1807,13 @@ module ts {
useCaseSensitiveFileNames: () => false,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "",
getNewLine: () => (sys && sys.newLine) || "\r\n"
getNewLine: () => newLine
};

var program = createProgram([inputFileName], options, compilerHost);

if (diagnostics) {
diagnostics.push(...program.getGlobalDiagnostics());
diagnostics.push(...program.getCompilerOptionsDiagnostics());
}

// Emit
Expand Down
59 changes: 59 additions & 0 deletions tests/cases/unittests/transpile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/// <reference path="..\..\..\src\harness\harness.ts" />

module ts {
describe("Transpile", () => {

function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void {
let diagnostics: Diagnostic[] = [];
let result = transpile(input, compilerOptions, "file.ts", diagnostics);

for (let i = 0; i < expectedDiagnosticCodes.length; i++) {
assert.equal(expectedDiagnosticCodes[i], diagnostics[i] && diagnostics[i].code, `Could not find expeced diagnostic.`);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When this fails what do we see? Also just use a string literal here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it shows a message and the two codes.. not sure i see what else to put in htere..

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd rather you checked the sizes instead of diagnostics[i] &&, and that you always print out the entire results if something is wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

size is checked in the next line.

}
assert.equal(diagnostics.length, expectedDiagnosticCodes.length, "Resuting diagnostics count does not match expected");

if (expectedOutput !== undefined) {
assert.equal(result, expectedOutput);
}
}

it("Generates correct compilerOptions diagnostics", () => {
// Expecting 5047: "Option 'isolatedModules' can only be used when either option'--module' is provided or option 'target' is 'ES6' or higher."
runTest(`var x = 0;`, {}, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [5047]);
});

it("Generates no diagnostics with valid inputs", () => {
// No errors
runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
});

it("Generates no diagnostics for missing file references", () => {
runTest(`/// <reference path="file2.ts" />
var x = 0;`,
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
});

it("Generates no diagnostics for missing module imports", () => {
runTest(`import {a} from "module2";`,
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
});

it("Generates expected syntactic diagnostics", () => {
runTest(`a b`,
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [1005]); /// 1005: ';' Expected
});

it("Does not generate semantic diagnostics", () => {
runTest(`var x: string = 0;`,
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
});

it("Generates module output", () => {
runTest(`var x = 0;`, { module: ModuleKind.AMD }, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`);
});

it("Uses correct newLine character", () => {
runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, `var x = 0;\n`, /*expectedDiagnosticCodes*/ []);
});
});
}