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 {FacadeConverter} from './facade_converter'; | 4 import {FacadeConverter} from './facade_converter'; |
5 import {Transpiler} from './main'; | 5 import {Transpiler} from './main'; |
6 import {MergedParameter, MergedType, MergedTypeParameters} from './merge'; | 6 import {MergedParameter, MergedType, MergedTypeParameters} from './merge'; |
7 | 7 |
8 export function isFunctionLikeProperty( | 8 export function isFunctionLikeProperty( |
9 decl: ts.PropertyDeclaration|ts.ParameterDeclaration, tc: ts.TypeChecker): b
oolean { | 9 decl: ts.PropertyDeclaration|ts.ParameterDeclaration, tc: ts.TypeChecker): b
oolean { |
10 if (!decl.type) return false; | 10 if (!decl.type) return false; |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 static NUM_FAKE_REST_PARAMETERS = 5; | 23 static NUM_FAKE_REST_PARAMETERS = 5; |
24 | 24 |
25 setTypeChecker(tc: ts.TypeChecker) { | 25 setTypeChecker(tc: ts.TypeChecker) { |
26 this.tc = tc; | 26 this.tc = tc; |
27 } | 27 } |
28 setFacadeConverter(fc: FacadeConverter) { | 28 setFacadeConverter(fc: FacadeConverter) { |
29 this.fc = fc; | 29 this.fc = fc; |
30 } | 30 } |
31 | 31 |
32 getJsPath(node: ts.Node, suppressUnneededPaths = true): string { | 32 getJsPath(node: ts.Node, suppressUnneededPaths: boolean): string { |
33 let path: Array<String> = []; | 33 let path: Array<String> = []; |
34 let moduleDecl = | 34 let moduleDecl = |
35 base.getAncestor(node, ts.SyntaxKind.ModuleDeclaration) as ts.ModuleDecl
aration; | 35 base.getAncestor(node, ts.SyntaxKind.ModuleDeclaration) as ts.ModuleDecl
aration; |
36 while (moduleDecl != null) { | 36 while (moduleDecl != null) { |
37 path.unshift(moduleDecl.name.text); | 37 path.unshift(moduleDecl.name.text); |
38 moduleDecl = | 38 moduleDecl = |
39 base.getAncestor( | 39 base.getAncestor( |
40 moduleDecl.parent, ts.SyntaxKind.ModuleDeclaration) as ts.Modu
leDeclaration; | 40 moduleDecl.parent, ts.SyntaxKind.ModuleDeclaration) as ts.Modu
leDeclaration; |
41 } | 41 } |
42 | 42 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 maybeEmitJsAnnotation(node: ts.Node) { | 101 maybeEmitJsAnnotation(node: ts.Node) { |
102 // No need to emit the annotations as an entity outside the code comment | 102 // No need to emit the annotations as an entity outside the code comment |
103 // will already have the same annotation. | 103 // will already have the same annotation. |
104 if (this.insideCodeComment) return; | 104 if (this.insideCodeComment) return; |
105 | 105 |
106 if (this.isAnonymousInterface(node)) { | 106 if (this.isAnonymousInterface(node)) { |
107 this.emit('@anonymous'); | 107 this.emit('@anonymous'); |
108 this.emit('@JS()'); | 108 this.emit('@JS()'); |
109 return; | 109 return; |
110 } | 110 } |
111 let name: String = this.getJsPath(node); | 111 let name: String = this.getJsPath(node, true); |
112 this.emit('@JS('); | 112 this.emit('@JS('); |
113 if (name.length > 0) { | 113 if (name.length > 0) { |
114 this.emit('"' + name + '"'); | 114 this.emit('"' + name + '"'); |
115 } | 115 } |
116 this.emit(')'); | 116 this.emit(')'); |
117 } | 117 } |
118 | 118 |
119 /** | 119 /** |
120 * Emit fake constructors to placate the Dart Analyzer for JS Interop classes. | 120 * Emit fake constructors to placate the Dart Analyzer for JS Interop classes. |
121 */ | 121 */ |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 let p = properties[i]; | 242 let p = properties[i]; |
243 this.visit(p.type); | 243 this.visit(p.type); |
244 this.visit(p.name); | 244 this.visit(p.name); |
245 } | 245 } |
246 this.emitNoSpace('});'); | 246 this.emitNoSpace('});'); |
247 } | 247 } |
248 } | 248 } |
249 | 249 |
250 visitMergingOverloads(members: Array<ts.Node>) { | 250 visitMergingOverloads(members: Array<ts.Node>) { |
251 // TODO(jacobr): merge method overloads. | 251 // TODO(jacobr): merge method overloads. |
252 let groups: {[name: string]: Array<ts.Node>} = {}; | 252 let groups: Map<string, Array<ts.Node>> = new Map(); |
253 let orderedGroups: Array<Array<ts.Node>> = []; | 253 let orderedGroups: Array<Array<ts.Node>> = []; |
254 members.forEach((node) => { | 254 members.forEach((node) => { |
255 let name = ''; | 255 let name = ''; |
256 switch (node.kind) { | 256 switch (node.kind) { |
257 case ts.SyntaxKind.Block: | 257 case ts.SyntaxKind.Block: |
258 // For JS interop we always skip the contents of a block. | 258 // For JS interop we always skip the contents of a block. |
259 break; | 259 break; |
260 case ts.SyntaxKind.PropertyDeclaration: | 260 case ts.SyntaxKind.PropertyDeclaration: |
261 case ts.SyntaxKind.PropertySignature: | 261 case ts.SyntaxKind.PropertySignature: |
262 case ts.SyntaxKind.VariableDeclaration: { | 262 case ts.SyntaxKind.VariableDeclaration: { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 break; | 302 break; |
303 case ts.SyntaxKind.IndexSignature: | 303 case ts.SyntaxKind.IndexSignature: |
304 name = '[]'; | 304 name = '[]'; |
305 break; | 305 break; |
306 default: | 306 default: |
307 // Create a group with a single entry as merging is not required for t
his node kind. | 307 // Create a group with a single entry as merging is not required for t
his node kind. |
308 orderedGroups.push([node]); | 308 orderedGroups.push([node]); |
309 return; | 309 return; |
310 } | 310 } |
311 let group: Array<ts.Node>; | 311 let group: Array<ts.Node>; |
312 if (Object.prototype.hasOwnProperty.call(groups, name)) { | 312 if (groups.has(name)) { |
313 group = groups[name]; | 313 group = groups.get(name); |
314 } else { | 314 } else { |
315 group = []; | 315 group = []; |
316 groups[name] = group; | 316 groups.set(name, group); |
317 orderedGroups.push(group); | 317 orderedGroups.push(group); |
318 } | 318 } |
319 group.push(node); | 319 group.push(node); |
320 }); | 320 }); |
321 | 321 |
322 orderedGroups.forEach((group: Array<ts.Node>) => { | 322 orderedGroups.forEach((group: Array<ts.Node>) => { |
323 if (group.length === 1) { | 323 if (group.length === 1) { |
324 this.visit(group[0]); | 324 this.visit(group[0]); |
325 return; | 325 return; |
326 } | 326 } |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 | 770 |
771 /** | 771 /** |
772 * Visit a property declaration. | 772 * Visit a property declaration. |
773 * In the special case of property parameters in a constructor, we also allow | 773 * In the special case of property parameters in a constructor, we also allow |
774 * a parameter to be emitted as a property. | 774 * a parameter to be emitted as a property. |
775 * We have to emit properties as getter setter pairs as Dart does not support | 775 * We have to emit properties as getter setter pairs as Dart does not support |
776 * external fields. | 776 * external fields. |
777 * In the special case of property parameters in a constructor, we also allow
a parameter to be | 777 * In the special case of property parameters in a constructor, we also allow
a parameter to be |
778 * emitted as a property. | 778 * emitted as a property. |
779 */ | 779 */ |
780 private visitProperty(decl: ts.PropertyDeclaration|ts.ParameterDeclaration, is
Parameter = false) { | 780 private visitProperty( |
| 781 decl: ts.PropertyDeclaration|ts.ParameterDeclaration, isParameter?: boolea
n) { |
781 let isStatic = base.isStatic(decl); | 782 let isStatic = base.isStatic(decl); |
782 this.emit('external'); | 783 this.emit('external'); |
783 if (isStatic) this.emit('static'); | 784 if (isStatic) this.emit('static'); |
784 this.visit(decl.type); | 785 this.visit(decl.type); |
785 this.emit('get'); | 786 this.emit('get'); |
786 this.visitName(decl.name); | 787 this.visitName(decl.name); |
787 this.emitNoSpace(';'); | 788 this.emitNoSpace(';'); |
788 | 789 |
789 this.emit('external'); | 790 this.emit('external'); |
790 if (isStatic) this.emit('static'); | 791 if (isStatic) this.emit('static'); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 this.emitNoSpace('<'); | 889 this.emitNoSpace('<'); |
889 this.enterTypeArguments(); | 890 this.enterTypeArguments(); |
890 this.visitList(typeParameters); | 891 this.visitList(typeParameters); |
891 this.exitTypeArguments(); | 892 this.exitTypeArguments(); |
892 this.emitNoSpace('>'); | 893 this.emitNoSpace('>'); |
893 } | 894 } |
894 this.visitParameters(signature.parameters); | 895 this.visitParameters(signature.parameters); |
895 this.emitNoSpace(';'); | 896 this.emitNoSpace(';'); |
896 } | 897 } |
897 } | 898 } |
OLD | NEW |