OLD | NEW |
1 import * as dartStyle from 'dart-style'; | 1 import * as dartStyle from 'dart-style'; |
2 import * as fs from 'fs'; | 2 import * as fs from 'fs'; |
3 import * as path from 'path'; | 3 import * as path from 'path'; |
4 import * as ts from 'typescript'; | 4 import * as ts from 'typescript'; |
5 | 5 |
6 import * as base from './base'; | 6 import * as base from './base'; |
7 import {ImportSummary, TranspilerBase} from './base'; | 7 import {ImportSummary, TranspilerBase} from './base'; |
8 import DeclarationTranspiler from './declaration'; | 8 import DeclarationTranspiler from './declaration'; |
9 import {FacadeConverter} from './facade_converter'; | 9 import {FacadeConverter} from './facade_converter'; |
10 import * as merge from './merge'; | 10 import * as merge from './merge'; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 this.declarationTranspiler.setTypeChecker(program.getTypeChecker()); | 113 this.declarationTranspiler.setTypeChecker(program.getTypeChecker()); |
114 | 114 |
115 // Only write files that were explicitly passed in. | 115 // Only write files that were explicitly passed in. |
116 let fileSet: {[s: string]: boolean} = {}; | 116 let fileSet: {[s: string]: boolean} = {}; |
117 fileNames.forEach((f) => fileSet[f] = true); | 117 fileNames.forEach((f) => fileSet[f] = true); |
118 let sourceFiles = program.getSourceFiles().filter((sourceFile) => fileSet[so
urceFile.fileName]); | 118 let sourceFiles = program.getSourceFiles().filter((sourceFile) => fileSet[so
urceFile.fileName]); |
119 | 119 |
120 this.errors = []; | 120 this.errors = []; |
121 | 121 |
122 let sourceFileMap: {[s: string]: ts.SourceFile} = {}; | 122 let sourceFileMap: {[s: string]: ts.SourceFile} = {}; |
123 sourceFiles.forEach((f: ts.SourceFile) => { sourceFileMap[f.fileName] = f; }
); | 123 sourceFiles.forEach((f: ts.SourceFile) => { |
| 124 sourceFileMap[f.fileName] = f; |
| 125 }); |
124 | 126 |
125 // Check for global module export declarations and propogate them to all mod
ules they export. | 127 // Check for global module export declarations and propogate them to all mod
ules they export. |
126 sourceFiles.forEach((f: ts.SourceFile) => { | 128 sourceFiles.forEach((f: ts.SourceFile) => { |
127 f.statements.forEach((n: ts.Node) => { | 129 f.statements.forEach((n: ts.Node) => { |
128 if (n.kind !== ts.SyntaxKind.GlobalModuleExportDeclaration) return; | 130 if (n.kind !== ts.SyntaxKind.GlobalModuleExportDeclaration) return; |
129 // This is the name we are interested in for Dart purposes until Dart su
pports AMD module | 131 // This is the name we are interested in for Dart purposes until Dart su
pports AMD module |
130 // loaders. This module name should all be reflected by all modules expo
rted by this | 132 // loaders. This module name should all be reflected by all modules expo
rted by this |
131 // library as we need to specify a global module location for every Dart
library. | 133 // library as we need to specify a global module location for every Dart
library. |
132 let globalModuleName = base.ident((n as ts.GlobalModuleExportDeclaration
).name); | 134 let globalModuleName = base.ident((n as ts.GlobalModuleExportDeclaration
).name); |
133 f.moduleName = globalModuleName; | 135 f.moduleName = globalModuleName; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 let compilerHost: ts.CompilerHost = { | 194 let compilerHost: ts.CompilerHost = { |
193 getSourceFile: (sourceName, languageVersion) => { | 195 getSourceFile: (sourceName, languageVersion) => { |
194 let sourcePath = sourceName; | 196 let sourcePath = sourceName; |
195 if (sourceName === defaultLibFileName) { | 197 if (sourceName === defaultLibFileName) { |
196 sourcePath = ts.getDefaultLibFilePath(COMPILER_OPTIONS); | 198 sourcePath = ts.getDefaultLibFilePath(COMPILER_OPTIONS); |
197 } | 199 } |
198 if (!fs.existsSync(sourcePath)) return undefined; | 200 if (!fs.existsSync(sourcePath)) return undefined; |
199 let contents = fs.readFileSync(sourcePath, 'UTF-8'); | 201 let contents = fs.readFileSync(sourcePath, 'UTF-8'); |
200 return ts.createSourceFile(sourceName, contents, COMPILER_OPTIONS.target
, true); | 202 return ts.createSourceFile(sourceName, contents, COMPILER_OPTIONS.target
, true); |
201 }, | 203 }, |
202 writeFile(name, text, writeByteOrderMark) { fs.writeFile(name, text); }, | 204 writeFile(name, text, writeByteOrderMark) { |
| 205 fs.writeFile(name, text); |
| 206 }, |
203 fileExists: (filename) => fs.existsSync(filename), | 207 fileExists: (filename) => fs.existsSync(filename), |
204 readFile: (filename) => fs.readFileSync(filename, 'utf-8'), | 208 readFile: (filename) => fs.readFileSync(filename, 'utf-8'), |
205 getDefaultLibFileName: () => defaultLibFileName, | 209 getDefaultLibFileName: () => defaultLibFileName, |
206 useCaseSensitiveFileNames: () => true, | 210 useCaseSensitiveFileNames: () => true, |
207 getCanonicalFileName: (filename) => filename, | 211 getCanonicalFileName: (filename) => filename, |
208 getCurrentDirectory: () => '', | 212 getCurrentDirectory: () => '', |
209 getNewLine: () => '\n', | 213 getNewLine: () => '\n', |
210 }; | 214 }; |
211 compilerHost.resolveModuleNames = getModuleResolver(compilerHost); | 215 compilerHost.resolveModuleNames = getModuleResolver(compilerHost); |
212 return compilerHost; | 216 return compilerHost; |
213 } | 217 } |
214 | 218 |
215 // Visible for testing. | 219 // Visible for testing. |
216 getOutputPath(filePath: string, destinationRoot: string): string { | 220 getOutputPath(filePath: string, destinationRoot: string): string { |
217 let relative = this.getDartFileName(filePath); | 221 let relative = this.getDartFileName(filePath); |
218 return this.normalizeSlashes(path.join(destinationRoot, relative)); | 222 return this.normalizeSlashes(path.join(destinationRoot, relative)); |
219 } | 223 } |
220 | 224 |
221 public pushContext(context: OutputContext) { this.outputStack.push(this.output
s[context]); } | 225 public pushContext(context: OutputContext) { |
| 226 this.outputStack.push(this.outputs[context]); |
| 227 } |
222 | 228 |
223 public popContext() { | 229 public popContext() { |
224 if (this.outputStack.length === 0) { | 230 if (this.outputStack.length === 0) { |
225 this.reportError(null, 'Attempting to pop output stack when already empty'
); | 231 this.reportError(null, 'Attempting to pop output stack when already empty'
); |
226 } | 232 } |
227 this.outputStack.pop(); | 233 this.outputStack.pop(); |
228 } | 234 } |
229 | 235 |
230 private translate(sourceFile: ts.SourceFile): string { | 236 private translate(sourceFile: ts.SourceFile): string { |
231 this.currentFile = sourceFile; | 237 this.currentFile = sourceFile; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 filePath = filePath.replace(/([^/]+)\/index.dart$/, '$1.dart'); | 345 filePath = filePath.replace(/([^/]+)\/index.dart$/, '$1.dart'); |
340 return this.getRelativeFileName(filePath); | 346 return this.getRelativeFileName(filePath); |
341 } | 347 } |
342 | 348 |
343 isJsModuleFile(): boolean { | 349 isJsModuleFile(): boolean { |
344 // Treat files as being part of js modules if they match the node module fil
e naming convention | 350 // Treat files as being part of js modules if they match the node module fil
e naming convention |
345 // of module_name/index.js. | 351 // of module_name/index.js. |
346 return !('/' + this.currentFile.fileName).match(/\/index\.(js|es6|d\.ts|ts)$
/); | 352 return !('/' + this.currentFile.fileName).match(/\/index\.(js|es6|d\.ts|ts)$
/); |
347 } | 353 } |
348 | 354 |
349 private get currentOutput(): Output { return this.outputStack[this.outputStack
.length - 1]; } | 355 private get currentOutput(): Output { |
| 356 return this.outputStack[this.outputStack.length - 1]; |
| 357 } |
350 | 358 |
351 emit(s: string) { this.currentOutput.emit(s); } | 359 emit(s: string) { |
352 emitNoSpace(s: string) { this.currentOutput.emitNoSpace(s); } | 360 this.currentOutput.emit(s); |
353 maybeLineBreak() { return this.currentOutput.maybeLineBreak(); } | 361 } |
354 enterCodeComment() { return this.currentOutput.enterCodeComment(); } | 362 emitNoSpace(s: string) { |
355 exitCodeComment() { return this.currentOutput.exitCodeComment(); } | 363 this.currentOutput.emitNoSpace(s); |
| 364 } |
| 365 maybeLineBreak() { |
| 366 return this.currentOutput.maybeLineBreak(); |
| 367 } |
| 368 enterCodeComment() { |
| 369 return this.currentOutput.enterCodeComment(); |
| 370 } |
| 371 exitCodeComment() { |
| 372 return this.currentOutput.exitCodeComment(); |
| 373 } |
356 | 374 |
357 enterTypeArgument() { this.typeArgumentDepth++; } | 375 enterTypeArgument() { |
358 exitTypeArgument() { this.typeArgumentDepth--; } | 376 this.typeArgumentDepth++; |
359 get insideTypeArgument(): boolean { return this.typeArgumentDepth > 0; } | 377 } |
| 378 exitTypeArgument() { |
| 379 this.typeArgumentDepth--; |
| 380 } |
| 381 get insideTypeArgument(): boolean { |
| 382 return this.typeArgumentDepth > 0; |
| 383 } |
360 | 384 |
361 emitType(s: string, comment: string) { return this.currentOutput.emitType(s, c
omment); } | 385 emitType(s: string, comment: string) { |
362 get insideCodeComment() { return this.currentOutput.insideCodeComment; } | 386 return this.currentOutput.emitType(s, comment); |
| 387 } |
| 388 get insideCodeComment() { |
| 389 return this.currentOutput.insideCodeComment; |
| 390 } |
363 | 391 |
364 reportError(n: ts.Node, message: string) { | 392 reportError(n: ts.Node, message: string) { |
365 let file = n.getSourceFile() || this.currentFile; | 393 let file = n.getSourceFile() || this.currentFile; |
366 let fileName = this.getRelativeFileName(file.fileName); | 394 let fileName = this.getRelativeFileName(file.fileName); |
367 let start = n.getStart(file); | 395 let start = n.getStart(file); |
368 let pos = file.getLineAndCharacterOfPosition(start); | 396 let pos = file.getLineAndCharacterOfPosition(start); |
369 // Line and character are 0-based. | 397 // Line and character are 0-based. |
370 let fullMessage = `${fileName}:${pos.line + 1}:${pos.character + 1}: ${messa
ge}`; | 398 let fullMessage = `${fileName}:${pos.line + 1}:${pos.character + 1}: ${messa
ge}`; |
371 if (this.options.failFast) throw new Error(fullMessage); | 399 if (this.options.failFast) throw new Error(fullMessage); |
372 this.errors.push(fullMessage); | 400 this.errors.push(fullMessage); |
(...skipping 25 matching lines...) Expand all Loading... |
398 | 426 |
399 for (let i = 0; i < this.transpilers.length; i++) { | 427 for (let i = 0; i < this.transpilers.length; i++) { |
400 if (this.transpilers[i].visitNode(node)) return; | 428 if (this.transpilers[i].visitNode(node)) return; |
401 } | 429 } |
402 | 430 |
403 this.reportError( | 431 this.reportError( |
404 node, | 432 node, |
405 'Unsupported node type ' + (<any>ts).SyntaxKind[node.kind] + ': ' + node
.getFullText()); | 433 'Unsupported node type ' + (<any>ts).SyntaxKind[node.kind] + ': ' + node
.getFullText()); |
406 } | 434 } |
407 | 435 |
408 private normalizeSlashes(path: string) { return path.replace(/\\/g, '/'); } | 436 private normalizeSlashes(path: string) { |
| 437 return path.replace(/\\/g, '/'); |
| 438 } |
409 | 439 |
410 private translateComment(comment: string): string { | 440 private translateComment(comment: string): string { |
411 let rawComment = comment; | 441 let rawComment = comment; |
412 comment = comment.replace(/\{@link ([^\}]+)\}/g, '[$1]'); | 442 comment = comment.replace(/\{@link ([^\}]+)\}/g, '[$1]'); |
413 | 443 |
414 // Remove the following tags and following comments till end of line. | 444 // Remove the following tags and following comments till end of line. |
415 comment = comment.replace(/@param.*$/gm, ''); | 445 comment = comment.replace(/@param.*$/gm, ''); |
416 comment = comment.replace(/@throws.*$/gm, ''); | 446 comment = comment.replace(/@throws.*$/gm, ''); |
417 comment = comment.replace(/@return.*$/gm, ''); | 447 comment = comment.replace(/@return.*$/gm, ''); |
418 | 448 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 try { | 595 try { |
566 let transpiler = new Transpiler(args); | 596 let transpiler = new Transpiler(args); |
567 if (args.destination) console.error('Transpiling', args._, 'to', args.destin
ation); | 597 if (args.destination) console.error('Transpiling', args._, 'to', args.destin
ation); |
568 transpiler.transpile(args._, args.destination); | 598 transpiler.transpile(args._, args.destination); |
569 } catch (e) { | 599 } catch (e) { |
570 if (e.name !== 'DartFacadeError') throw e; | 600 if (e.name !== 'DartFacadeError') throw e; |
571 console.error(e.message); | 601 console.error(e.message); |
572 process.exit(1); | 602 process.exit(1); |
573 } | 603 } |
574 } | 604 } |
OLD | NEW |