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 let result = translateSource(tsCode, options); | 15 let result = translateSource(tsCode, options); |
16 // The Dart formatter is aggressive at terminating statements with \n | |
17 // which clutters the expectation output without providing value. | |
18 if (result[result.length - 1] === '\n') { | |
19 result = result.slice(0, -1); | |
20 } | |
21 return chai.expect(result); | 16 return chai.expect(result); |
22 } | 17 } |
23 | 18 |
24 export function expectErroneousCode(tsCode: Input, options: main.TranspilerOptio
ns = {}) { | 19 export function expectErroneousCode(tsCode: Input, options: main.TranspilerOptio
ns = {}) { |
25 options.failFast = false; // Collect *all* errors. | 20 options.failFast = false; // Collect *all* errors. |
26 return chai.expect(() => translateSource(tsCode, options)); | 21 return chai.expect(() => translateSource(tsCode, options)); |
27 } | 22 } |
28 | 23 |
29 let compilerOptions = main.COMPILER_OPTIONS; | 24 let compilerOptions = main.COMPILER_OPTIONS; |
30 let defaultLibName = ts.getDefaultLibFileName(compilerOptions); | 25 let defaultLibName = ts.getDefaultLibFileName(compilerOptions); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 let entryPoints = Object.keys(nameToContent); | 57 let entryPoints = Object.keys(nameToContent); |
63 let program: ts.Program = ts.createProgram(entryPoints, compilerOptions, compi
lerHost); | 58 let program: ts.Program = ts.createProgram(entryPoints, compilerOptions, compi
lerHost); |
64 if (program.getSyntacticDiagnostics().length > 0) { | 59 if (program.getSyntacticDiagnostics().length > 0) { |
65 // Throw first error. | 60 // Throw first error. |
66 let first = program.getSyntacticDiagnostics()[0]; | 61 let first = program.getSyntacticDiagnostics()[0]; |
67 throw new Error(`${first.start}: ${first.messageText} in ${nameToContent[ent
ryPoints[0]]}`); | 62 throw new Error(`${first.start}: ${first.messageText} in ${nameToContent[ent
ryPoints[0]]}`); |
68 } | 63 } |
69 return program; | 64 return program; |
70 } | 65 } |
71 | 66 |
72 export const FAKE_MAIN = 'angular2/some/main.ts'; | 67 export const FAKE_MAIN = 'demo/some/main.ts'; |
73 | 68 |
74 export function translateSources(contents: Input, options: main.TranspilerOption
s = {}): StringMap { | 69 export function translateSources(contents: Input, options: main.TranspilerOption
s = {}): StringMap { |
75 // Default to quick stack traces. | 70 // Default to quick stack traces. |
76 if (!options.hasOwnProperty('failFast')) options.failFast = true; | 71 if (!options.hasOwnProperty('failFast')) options.failFast = true; |
77 let namesToContent: StringMap; | 72 let namesToContent: StringMap; |
78 if (typeof contents === 'string') { | 73 if (typeof contents === 'string') { |
79 namesToContent = {}; | 74 namesToContent = {}; |
80 namesToContent[FAKE_MAIN] = contents; | 75 namesToContent[FAKE_MAIN] = contents; |
81 } else { | 76 } else { |
82 namesToContent = contents; | 77 namesToContent = contents; |
83 } | 78 } |
84 options.enforceUnderscoreConventions = true; | 79 options.enforceUnderscoreConventions = true; |
85 let transpiler = new main.Transpiler(options); | 80 let transpiler = new main.Transpiler(options); |
86 let program = parseFiles(namesToContent); | 81 let program = parseFiles(namesToContent); |
87 return transpiler.translateProgram(program); | 82 return transpiler.translateProgram(program); |
88 } | 83 } |
89 | 84 |
| 85 export function translateSource(contents: Input, options: main.TranspilerOptions
= {}): string { |
| 86 options.useHtml = true; |
90 | 87 |
91 export function translateSource(contents: Input, options: main.TranspilerOptions
= {}): string { | |
92 let results = translateSources(contents, options); | 88 let results = translateSources(contents, options); |
93 // Return the main outcome, from 'main.ts'. | 89 // Return the main outcome, from 'main.ts'. |
94 return results[FAKE_MAIN]; | 90 let result = results[FAKE_MAIN]; |
| 91 // strip out the package:js import as it clutters the output. |
| 92 result = result.replace(/import "package:js\/js.dart";\s+/g, ''); |
| 93 result = result.replace(/^@JS\(\)\s+library [^;]+;\s+/g, ''); |
| 94 return result.trim(); |
95 } | 95 } |
OLD | NEW |