OLD | NEW |
1 import * as dartStyle from 'dart-style'; | 1 import * as dartStyle from 'dart-style'; |
2 import * as path from 'path'; | 2 import * as path from 'path'; |
3 import * as ts from 'typescript'; | 3 import * as ts from 'typescript'; |
4 | 4 |
5 import {OutputContext, Transpiler} from './main'; | 5 import {OutputContext, Transpiler} from './main'; |
6 | 6 |
7 /** | 7 /** |
8 * Map from identifier name to resolved type. | 8 * Map from identifier name to resolved type. |
9 * Example: 'E' should map to a TypeNode for number when resolving a usage of My
Array<number> | 9 * Example: 'E' should map to a TypeNode for number when resolving a usage of My
Array<number> |
10 * where MyArray is the alias type: | 10 * where MyArray is the alias type: |
11 * type MyArray<E> = Array<T> | 11 * type MyArray<E> = Array<T> |
12 */ | 12 */ |
13 export type ResolvedTypeMap = { | 13 export type ResolvedTypeMap = Map<string, ts.TypeNode>; |
14 [name: string]: ts.TypeNode | |
15 }; | |
16 | 14 |
17 /*** | 15 /*** |
18 * Options for how TypeScript types are represented as Dart types. | 16 * Options for how TypeScript types are represented as Dart types. |
19 */ | 17 */ |
20 export interface TypeDisplayOptions { | 18 export interface TypeDisplayOptions { |
21 /// We are displaying the type inside a comment so we don't have to restrict t
o valid Dart syntax. | 19 /// We are displaying the type inside a comment so we don't have to restrict t
o valid Dart syntax. |
22 /// For example, we can display string literal type using the regular TypeScri
pt syntax. | 20 /// For example, we can display string literal type using the regular TypeScri
pt syntax. |
23 /// | 21 /// |
24 /// Example: | 22 /// Example: |
25 /// TypeScript type: number|string | 23 /// TypeScript type: number|string |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 * arguments that are not representable in Dart. | 58 * arguments that are not representable in Dart. |
61 */ | 59 */ |
62 resolvedTypeArguments?: ResolvedTypeMap; | 60 resolvedTypeArguments?: ResolvedTypeMap; |
63 } | 61 } |
64 | 62 |
65 /** | 63 /** |
66 * Summary information on what is imported via a particular import. | 64 * Summary information on what is imported via a particular import. |
67 */ | 65 */ |
68 export class ImportSummary { | 66 export class ImportSummary { |
69 showAll: boolean = false; | 67 showAll: boolean = false; |
70 shown: Set = {}; | 68 shown: Set<String> = new Set(); |
71 asPrefix: string; | 69 asPrefix: string; |
72 } | 70 } |
73 | 71 |
74 export type ClassLike = ts.ClassDeclaration | ts.InterfaceDeclaration; | 72 export type ClassLike = ts.ClassDeclaration | ts.InterfaceDeclaration; |
75 export type NamedDeclaration = ClassLike | ts.PropertyDeclaration | ts.VariableD
eclaration | | 73 export type NamedDeclaration = ClassLike | ts.PropertyDeclaration | ts.VariableD
eclaration | |
76 ts.MethodDeclaration | ts.ModuleDeclaration | ts.FunctionDeclaration; | 74 ts.MethodDeclaration | ts.ModuleDeclaration | ts.FunctionDeclaration; |
77 | 75 |
78 export type Set = { | |
79 [s: string]: boolean | |
80 }; | |
81 | |
82 /** | 76 /** |
83 * Interface extending the true InterfaceDeclaration interface to add optional s
tate we store on | 77 * Interface extending the true InterfaceDeclaration interface to add optional s
tate we store on |
84 * interfaces to simplify conversion to Dart classes. | 78 * interfaces to simplify conversion to Dart classes. |
85 */ | 79 */ |
86 export interface ExtendedInterfaceDeclaration extends ts.InterfaceDeclaration { | 80 export interface ExtendedInterfaceDeclaration extends ts.InterfaceDeclaration { |
87 /** | 81 /** |
88 * VariableDeclaration associated with this interface that we want to treat as
the concrete | 82 * VariableDeclaration associated with this interface that we want to treat as
the concrete |
89 * location of this interface to enable interfaces that act like constructors. | 83 * location of this interface to enable interfaces that act like constructors. |
90 * Because Dart does not permit calling objects like constructors we have to a
dd this workaround. | 84 * Because Dart does not permit calling objects like constructors we have to a
dd this workaround. |
91 */ | 85 */ |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 } | 360 } |
367 get insideTypeArgument() { | 361 get insideTypeArgument() { |
368 return this.transpiler.insideTypeArgument; | 362 return this.transpiler.insideTypeArgument; |
369 } | 363 } |
370 | 364 |
371 get insideCodeComment() { | 365 get insideCodeComment() { |
372 return this.transpiler.insideCodeComment; | 366 return this.transpiler.insideCodeComment; |
373 } | 367 } |
374 | 368 |
375 getImportSummary(libraryUri: string): ImportSummary { | 369 getImportSummary(libraryUri: string): ImportSummary { |
376 if (!Object.hasOwnProperty.call(this.transpiler.imports, libraryUri)) { | 370 if (!this.transpiler.imports.has(libraryUri)) { |
377 let summary = new ImportSummary(); | 371 let summary = new ImportSummary(); |
378 this.transpiler.imports[libraryUri] = summary; | 372 this.transpiler.imports.set(libraryUri, summary); |
379 return summary; | 373 return summary; |
380 } | 374 } |
381 return this.transpiler.imports[libraryUri]; | 375 return this.transpiler.imports.get(libraryUri); |
382 } | 376 } |
383 | 377 |
384 /** | 378 /** |
385 * Add an import. If an identifier is specified, only show that name. | 379 * Add an import. If an identifier is specified, only show that name. |
386 */ | 380 */ |
387 addImport(libraryUri: string, identifier?: string): ImportSummary { | 381 addImport(libraryUri: string, identifier?: string): ImportSummary { |
388 let summary = this.getImportSummary(libraryUri); | 382 let summary = this.getImportSummary(libraryUri); |
389 if (identifier) { | 383 if (identifier) { |
390 summary.shown[identifier] = true; | 384 summary.shown.add(identifier); |
391 } else { | 385 } else { |
392 summary.showAll = true; | 386 summary.showAll = true; |
393 } | 387 } |
394 return summary; | 388 return summary; |
395 } | 389 } |
396 | 390 |
397 /** | 391 /** |
398 * Return resolved named possibly including a prefix for the identifier. | 392 * Return resolved named possibly including a prefix for the identifier. |
399 */ | 393 */ |
400 resolveImportForSourceFile(sourceFile: ts.SourceFile, context: ts.SourceFile,
identifier: string): | 394 resolveImportForSourceFile(sourceFile: ts.SourceFile, context: ts.SourceFile,
identifier: string): |
(...skipping 26 matching lines...) Expand all Loading... |
427 } | 421 } |
428 | 422 |
429 visitEach(nodes: ts.Node[]) { | 423 visitEach(nodes: ts.Node[]) { |
430 nodes.forEach((n) => this.visit(n)); | 424 nodes.forEach((n) => this.visit(n)); |
431 } | 425 } |
432 | 426 |
433 visitEachIfPresent(nodes?: ts.Node[]) { | 427 visitEachIfPresent(nodes?: ts.Node[]) { |
434 if (nodes) this.visitEach(nodes); | 428 if (nodes) this.visitEach(nodes); |
435 } | 429 } |
436 | 430 |
437 visitList(nodes: ts.Node[], separator = ',') { | 431 visitList(nodes: ts.Node[], separator?: string) { |
| 432 separator = separator || ','; |
438 for (let i = 0; i < nodes.length; i++) { | 433 for (let i = 0; i < nodes.length; i++) { |
439 this.visit(nodes[i]); | 434 this.visit(nodes[i]); |
440 if (i < nodes.length - 1) this.emitNoSpace(separator); | 435 if (i < nodes.length - 1) this.emitNoSpace(separator); |
441 } | 436 } |
442 } | 437 } |
443 | 438 |
444 /** | 439 /** |
445 * Returns whether any parameters were actually emitted. | 440 * Returns whether any parameters were actually emitted. |
446 */ | 441 */ |
447 visitParameterList(nodes: ts.ParameterDeclaration[]): boolean { | 442 visitParameterList(nodes: ts.ParameterDeclaration[]): boolean { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 if (hasValidParameters) this.emitNoSpace(','); | 541 if (hasValidParameters) this.emitNoSpace(','); |
547 let positionalOptional = parameters.slice(firstInitParamIdx, parameters.le
ngth); | 542 let positionalOptional = parameters.slice(firstInitParamIdx, parameters.le
ngth); |
548 this.emit('['); | 543 this.emit('['); |
549 this.visitParameterList(positionalOptional); | 544 this.visitParameterList(positionalOptional); |
550 this.emitNoSpace(']'); | 545 this.emitNoSpace(']'); |
551 } | 546 } |
552 | 547 |
553 this.emitNoSpace(')'); | 548 this.emitNoSpace(')'); |
554 } | 549 } |
555 } | 550 } |
OLD | NEW |