| OLD | NEW |
| 1 /// <reference path="../typings/chai/chai.d.ts"/> | 1 /// <reference path="../typings/chai/chai.d.ts"/> |
| 2 /// <reference path="../typings/mocha/mocha.d.ts"/> | 2 /// <reference path="../typings/mocha/mocha.d.ts"/> |
| 3 /// <reference path="../typings/node/node.d.ts"/> | 3 /// <reference path="../typings/node/node.d.ts"/> |
| 4 import chai = require('chai'); | 4 import chai = require('chai'); |
| 5 import fs = require('fs'); | 5 import fs = require('fs'); |
| 6 import main = require('../lib/main'); | 6 import main = require('../lib/main'); |
| 7 import ts = require('typescript'); | 7 import ts = require('typescript'); |
| 8 | 8 |
| 9 export type StringMap = { | 9 export type StringMap = { |
| 10 [k: string]: string | 10 [k: string]: string |
| 11 }; | 11 }; |
| 12 export type Input = string | StringMap; | 12 export type Input = string | StringMap; |
| 13 | 13 |
| 14 export function expectTranslate(tsCode: Input, options: main.TranspilerOptions =
{}) { | 14 export function expectTranslate(tsCode: Input, options?: main.TranspilerOptions)
{ |
| 15 options = options || {}; |
| 15 let result = translateSource(tsCode, options); | 16 let result = translateSource(tsCode, options); |
| 16 return chai.expect(result); | 17 return chai.expect(result); |
| 17 } | 18 } |
| 18 | 19 |
| 19 export function expectErroneousCode(tsCode: Input, options: main.TranspilerOptio
ns = {}) { | 20 export function expectErroneousCode(tsCode: Input, options?: main.TranspilerOpti
ons) { |
| 21 options = options || {}; |
| 20 options.failFast = false; // Collect *all* errors. | 22 options.failFast = false; // Collect *all* errors. |
| 21 return chai.expect(() => translateSource(tsCode, options)); | 23 return chai.expect(() => translateSource(tsCode, options)); |
| 22 } | 24 } |
| 23 | 25 |
| 24 let compilerOptions = main.COMPILER_OPTIONS; | 26 let compilerOptions = main.COMPILER_OPTIONS; |
| 25 let defaultLibName = ts.getDefaultLibFileName(compilerOptions); | 27 let defaultLibName = ts.getDefaultLibFileName(compilerOptions); |
| 26 let libSource = fs.readFileSync(ts.getDefaultLibFilePath(compilerOptions), 'utf-
8'); | 28 let libSource = fs.readFileSync(ts.getDefaultLibFilePath(compilerOptions), 'utf-
8'); |
| 27 let libSourceFile: ts.SourceFile; | 29 let libSourceFile: ts.SourceFile; |
| 28 | 30 |
| 29 export function parseFiles(nameToContent: StringMap): ts.Program { | 31 export function parseFiles(nameToContent: StringMap): ts.Program { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 if (program.getSyntacticDiagnostics().length > 0) { | 67 if (program.getSyntacticDiagnostics().length > 0) { |
| 66 // Throw first error. | 68 // Throw first error. |
| 67 let first = program.getSyntacticDiagnostics()[0]; | 69 let first = program.getSyntacticDiagnostics()[0]; |
| 68 throw new Error(`${first.start}: ${first.messageText} in ${nameToContent[ent
ryPoints[0]]}`); | 70 throw new Error(`${first.start}: ${first.messageText} in ${nameToContent[ent
ryPoints[0]]}`); |
| 69 } | 71 } |
| 70 return program; | 72 return program; |
| 71 } | 73 } |
| 72 | 74 |
| 73 export const FAKE_MAIN = 'demo/some/main.ts'; | 75 export const FAKE_MAIN = 'demo/some/main.ts'; |
| 74 | 76 |
| 75 export function translateSources(contents: Input, options: main.TranspilerOption
s = {}): StringMap { | 77 export function translateSources(contents: Input, options?: main.TranspilerOptio
ns): StringMap { |
| 78 options = options || {}; |
| 76 // Default to quick stack traces. | 79 // Default to quick stack traces. |
| 77 if (!options.hasOwnProperty('failFast')) options.failFast = true; | 80 if (!options.hasOwnProperty('failFast')) options.failFast = true; |
| 78 let namesToContent: StringMap; | 81 let namesToContent: StringMap; |
| 79 if (typeof contents === 'string') { | 82 if (typeof contents === 'string') { |
| 80 namesToContent = {}; | 83 namesToContent = {}; |
| 81 namesToContent[FAKE_MAIN] = contents; | 84 namesToContent[FAKE_MAIN] = contents; |
| 82 } else { | 85 } else { |
| 83 namesToContent = contents; | 86 namesToContent = contents; |
| 84 } | 87 } |
| 85 options.enforceUnderscoreConventions = true; | 88 options.enforceUnderscoreConventions = true; |
| 86 let transpiler = new main.Transpiler(options); | 89 let transpiler = new main.Transpiler(options); |
| 87 let program = parseFiles(namesToContent); | 90 let program = parseFiles(namesToContent); |
| 88 return transpiler.translateProgram(program); | 91 return transpiler.translateProgram(program); |
| 89 } | 92 } |
| 90 | 93 |
| 91 export function translateSource(contents: Input, options: main.TranspilerOptions
= {}): string { | 94 export function translateSource(contents: Input, options?: main.TranspilerOption
s): string { |
| 95 options = options || {}; |
| 92 let results = translateSources(contents, options); | 96 let results = translateSources(contents, options); |
| 93 // Return the main outcome, from 'main.ts'. | 97 // Return the main outcome, from 'main.ts'. |
| 94 let result = results[FAKE_MAIN]; | 98 let result = results[FAKE_MAIN]; |
| 95 // strip out the package:js import as it clutters the output. | 99 // strip out the package:js import as it clutters the output. |
| 96 result = result.replace(/import "package:js\/js.dart";\s+/g, ''); | 100 result = result.replace(/import "package:js\/js.dart";\s+/g, ''); |
| 97 result = result.replace(/^@JS\("?[^)]*"?\)\s+library [^;]+;\s+/g, ''); | 101 result = result.replace(/^@JS\("?[^)]*"?\)\s+library [^;]+;\s+/g, ''); |
| 98 return result.trim(); | 102 return result.trim(); |
| 99 } | 103 } |
| OLD | NEW |