OLD | NEW |
1 import * as ts from 'typescript'; | 1 import * as ts from 'typescript'; |
2 | 2 |
3 import * as base from './base'; | 3 import * as base from './base'; |
4 import {Set, TypeDisplayOptions} from './base'; | 4 import {Set, TypeDisplayOptions} from './base'; |
5 import {DART_LIBRARIES_FOR_BROWSER_TYPES, TS_TO_DART_TYPENAMES} from './dart_lib
raries_for_browser_types'; | 5 import {DART_LIBRARIES_FOR_BROWSER_TYPES, TS_TO_DART_TYPENAMES} from './dart_lib
raries_for_browser_types'; |
6 import {Transpiler} from './main'; | 6 import {Transpiler} from './main'; |
7 import {MergedType} from './merge'; | 7 import {MergedType} from './merge'; |
8 | 8 |
9 const FACADE_DEBUG = false; | 9 const FACADE_DEBUG = false; |
10 const FACADE_NODE_MODULES_PREFIX = /^(\.\.\/)*node_modules\//; | 10 const FACADE_NODE_MODULES_PREFIX = /^(\.\.\/)*node_modules\//; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 } | 75 } |
76 | 76 |
77 class DartNameRecord { | 77 class DartNameRecord { |
78 name: string; | 78 name: string; |
79 constructor(private node: ts.Node, name: string, private library: DartLibrary)
{ | 79 constructor(private node: ts.Node, name: string, private library: DartLibrary)
{ |
80 this.name = name; | 80 this.name = name; |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 export class DartLibrary { | 84 export class DartLibrary { |
85 constructor(private fileName: string) { this.usedNames = {}; } | 85 constructor(private fileName: string) { |
| 86 this.usedNames = {}; |
| 87 } |
86 | 88 |
87 /** | 89 /** |
88 * @returns {boolean} whether the name was added. | 90 * @returns {boolean} whether the name was added. |
89 */ | 91 */ |
90 addName(name: string): boolean { | 92 addName(name: string): boolean { |
91 if (Object.prototype.hasOwnProperty.call(this.usedNames, name)) { | 93 if (Object.prototype.hasOwnProperty.call(this.usedNames, name)) { |
92 return false; | 94 return false; |
93 } | 95 } |
94 this.usedNames[name] = true; | 96 this.usedNames[name] = true; |
95 return true; | 97 return true; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 const file = m[fileName]; | 260 const file = m[fileName]; |
259 if (file === undefined) { | 261 if (file === undefined) { |
260 return; | 262 return; |
261 } | 263 } |
262 Object.keys(file) | 264 Object.keys(file) |
263 .map((propName) => propName.substring(propName.lastIndexOf('.') + 1)) | 265 .map((propName) => propName.substring(propName.lastIndexOf('.') + 1)) |
264 .forEach((propName) => candidates[propName] = true); | 266 .forEach((propName) => candidates[propName] = true); |
265 } | 267 } |
266 } | 268 } |
267 | 269 |
268 setTypeChecker(tc: ts.TypeChecker) { this.tc = tc; } | 270 setTypeChecker(tc: ts.TypeChecker) { |
| 271 this.tc = tc; |
| 272 } |
269 | 273 |
270 pushTypeParameterNames(n: ts.FunctionLikeDeclaration) { | 274 pushTypeParameterNames(n: ts.FunctionLikeDeclaration) { |
271 if (!n.typeParameters) return; | 275 if (!n.typeParameters) return; |
272 this.genericMethodDeclDepth++; | 276 this.genericMethodDeclDepth++; |
273 } | 277 } |
274 | 278 |
275 popTypeParameterNames(n: ts.FunctionLikeDeclaration) { | 279 popTypeParameterNames(n: ts.FunctionLikeDeclaration) { |
276 if (!n.typeParameters) return; | 280 if (!n.typeParameters) return; |
277 this.genericMethodDeclDepth--; | 281 this.genericMethodDeclDepth--; |
278 } | 282 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 let symbol = this.tc.getSymbolAtLocation(name); | 319 let symbol = this.tc.getSymbolAtLocation(name); |
316 if (symbol !== t.symbol) return false; | 320 if (symbol !== t.symbol) return false; |
317 | 321 |
318 // Check that the Type Parameter has been declared by a function declaration
. | 322 // Check that the Type Parameter has been declared by a function declaration
. |
319 return symbol.declarations.some(d => d.parent.kind === ts.SyntaxKind.Functio
nDeclaration); | 323 return symbol.declarations.some(d => d.parent.kind === ts.SyntaxKind.Functio
nDeclaration); |
320 } | 324 } |
321 | 325 |
322 generateTypeList(types: ts.TypeNode[], options: TypeDisplayOptions, seperator
= ','): string { | 326 generateTypeList(types: ts.TypeNode[], options: TypeDisplayOptions, seperator
= ','): string { |
323 let that = this; | 327 let that = this; |
324 return types | 328 return types |
325 .map((type) => { return that.generateDartTypeName(type, addInsideTypeArg
ument(options)); }) | 329 .map((type) => { |
| 330 return that.generateDartTypeName(type, addInsideTypeArgument(options))
; |
| 331 }) |
326 .join(seperator); | 332 .join(seperator); |
327 } | 333 } |
328 | 334 |
329 generateDartTypeName(node: ts.TypeNode, options?: TypeDisplayOptions): string
{ | 335 generateDartTypeName(node: ts.TypeNode, options?: TypeDisplayOptions): string
{ |
330 if (!options) { | 336 if (!options) { |
331 options = { | 337 options = { |
332 insideComment: this.insideCodeComment, | 338 insideComment: this.insideCodeComment, |
333 insideTypeArgument: this.insideTypeArgument | 339 insideTypeArgument: this.insideTypeArgument |
334 }; | 340 }; |
335 } | 341 } |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 /** | 713 /** |
708 * This method works around the lack of Dart support for union types | 714 * This method works around the lack of Dart support for union types |
709 * generating a valid Dart type that satisfies all the types passed in. | 715 * generating a valid Dart type that satisfies all the types passed in. |
710 */ | 716 */ |
711 toSimpleDartType(types: Array<ts.TypeNode>): ts.TypeNode { | 717 toSimpleDartType(types: Array<ts.TypeNode>): ts.TypeNode { |
712 // We use MergeType to ensure that we have already deduped types that are | 718 // We use MergeType to ensure that we have already deduped types that are |
713 // equivalent even if they aren't obviously identical. | 719 // equivalent even if they aren't obviously identical. |
714 // MergedType will also follow typed aliases, etc which allows us to avoid | 720 // MergedType will also follow typed aliases, etc which allows us to avoid |
715 // including that logic here as well. | 721 // including that logic here as well. |
716 let mergedType = new MergedType(this); | 722 let mergedType = new MergedType(this); |
717 types.forEach((type) => { mergedType.merge(type); }); | 723 types.forEach((type) => { |
| 724 mergedType.merge(type); |
| 725 }); |
718 return mergedType.toSimpleTypeNode(); | 726 return mergedType.toSimpleTypeNode(); |
719 } | 727 } |
720 | 728 |
721 findCommonType(type: ts.TypeNode, common: ts.TypeNode): ts.TypeNode { | 729 findCommonType(type: ts.TypeNode, common: ts.TypeNode): ts.TypeNode { |
722 if (common === type) return common; | 730 if (common === type) return common; |
723 | 731 |
724 // If both types generate the exact same Dart type name without comments the
n | 732 // If both types generate the exact same Dart type name without comments the
n |
725 // there is no need to do anything. The types | 733 // there is no need to do anything. The types |
726 if (this.generateDartTypeName(common, {hideComment: true}) === | 734 if (this.generateDartTypeName(common, {hideComment: true}) === |
727 this.generateDartTypeName(type, {hideComment: true})) { | 735 this.generateDartTypeName(type, {hideComment: true})) { |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
863 let qname = this.tc.getFullyQualifiedName(symbol); | 871 let qname = this.tc.getFullyQualifiedName(symbol); |
864 // Some Qualified Names include their file name. Might be a bug in TypeScrip
t, | 872 // Some Qualified Names include their file name. Might be a bug in TypeScrip
t, |
865 // for the time being just special case. | 873 // for the time being just special case. |
866 if (symbol.flags & (ts.SymbolFlags.Class | ts.SymbolFlags.Function | ts.Symb
olFlags.Variable)) { | 874 if (symbol.flags & (ts.SymbolFlags.Class | ts.SymbolFlags.Function | ts.Symb
olFlags.Variable)) { |
867 qname = symbol.getName(); | 875 qname = symbol.getName(); |
868 } | 876 } |
869 if (FACADE_DEBUG) console.error('fn:', fileName, 'cfn:', canonicalFileName,
'qn:', qname); | 877 if (FACADE_DEBUG) console.error('fn:', fileName, 'cfn:', canonicalFileName,
'qn:', qname); |
870 return {fileName: canonicalFileName, qname}; | 878 return {fileName: canonicalFileName, qname}; |
871 } | 879 } |
872 } | 880 } |
OLD | NEW |