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 {TypeDisplayOptions} from './base'; | 4 import {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 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 return 'dynamic'; | 348 return 'dynamic'; |
349 } | 349 } |
350 | 350 |
351 switch (node.kind) { | 351 switch (node.kind) { |
352 case ts.SyntaxKind.TypeQuery: | 352 case ts.SyntaxKind.TypeQuery: |
353 name = 'dynamic'; | 353 name = 'dynamic'; |
354 // TODO(jacobr): evaluate supporting this case. | 354 // TODO(jacobr): evaluate supporting this case. |
355 // let query = <ts.TypeQueryNode>node; | 355 // let query = <ts.TypeQueryNode>node; |
356 // name += '/* TypeQuery: typeof ' + base.ident(query.exprName) + ' */'; | 356 // name += '/* TypeQuery: typeof ' + base.ident(query.exprName) + ' */'; |
357 break; | 357 break; |
358 case ts.SyntaxKind.TypePredicate: | 358 case ts.SyntaxKind.TypePredicate: { |
359 return this.generateDartTypeName((node as ts.TypePredicateNode).type, op
tions); | 359 let predicate = node as ts.TypePredicateNode; |
| 360 name = 'bool'; |
| 361 comment = base.ident(predicate.parameterName) + ' is ' + |
| 362 this.generateDartTypeName(predicate.type, addInsideComment(options))
; |
| 363 } break; |
360 case ts.SyntaxKind.TupleType: | 364 case ts.SyntaxKind.TupleType: |
361 let tuple = <ts.TupleTypeNode>node; | 365 let tuple = <ts.TupleTypeNode>node; |
362 name = 'List<'; | 366 name = 'List<'; |
363 let mergedType = new MergedType(this); | 367 let mergedType = new MergedType(this); |
364 tuple.elementTypes.forEach((t) => mergedType.merge(t)); | 368 tuple.elementTypes.forEach((t) => mergedType.merge(t)); |
365 name += this.generateDartTypeName(mergedType.toTypeNode(), addInsideType
Argument(options)); | 369 name += this.generateDartTypeName(mergedType.toTypeNode(), addInsideType
Argument(options)); |
366 name += '>'; | 370 name += '>'; |
367 // This is intentionally not valid Dart code so that it is clear this is
n't a Dart | 371 // This is intentionally not valid Dart code so that it is clear this is
n't a Dart |
368 // code comment that should use the /*= syntax. | 372 // code comment that should use the /*= syntax. |
369 comment = 'Tuple of <' + | 373 comment = 'Tuple of <' + |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
771 | 775 |
772 toTypeNode(type: ts.Type): ts.TypeNode { | 776 toTypeNode(type: ts.Type): ts.TypeNode { |
773 if (!type) return null; | 777 if (!type) return null; |
774 let symbol = type.getSymbol(); | 778 let symbol = type.getSymbol(); |
775 if (!symbol) return null; | 779 if (!symbol) return null; |
776 | 780 |
777 let referenceType = <ts.TypeReferenceNode>ts.createNode(ts.SyntaxKind.TypeRe
ference); | 781 let referenceType = <ts.TypeReferenceNode>ts.createNode(ts.SyntaxKind.TypeRe
ference); |
778 // TODO(jacobr): property need to prefix the name better. | 782 // TODO(jacobr): property need to prefix the name better. |
779 referenceType.typeName = this.createEntityName(symbol); | 783 referenceType.typeName = this.createEntityName(symbol); |
780 referenceType.typeName.parent = referenceType; | 784 referenceType.typeName.parent = referenceType; |
| 785 let decl = this.getSymbolDeclaration(symbol); |
| 786 base.copyLocation(decl, referenceType); |
781 return referenceType; | 787 return referenceType; |
782 } | 788 } |
783 | 789 |
784 createEntityName(symbol: ts.Symbol): ts.EntityName { | 790 createEntityName(symbol: ts.Symbol): ts.EntityName { |
785 let parts = this.tc.getFullyQualifiedName(symbol).split('.'); | 791 let parts = this.tc.getFullyQualifiedName(symbol).split('.'); |
786 let identifier = <ts.Identifier>ts.createNode(ts.SyntaxKind.Identifier); | 792 let identifier = <ts.Identifier>ts.createNode(ts.SyntaxKind.Identifier); |
787 identifier.text = parts[parts.length - 1]; | 793 identifier.text = parts[parts.length - 1]; |
788 // TODO(jacobr): do we need to include all parts in the entity name? | 794 // TODO(jacobr): do we need to include all parts in the entity name? |
789 return identifier; | 795 return identifier; |
790 } | 796 } |
(...skipping 25 matching lines...) Expand all Loading... |
816 | 822 |
817 commonSupertype(nodeA: ts.TypeNode, nodeB: ts.TypeNode): ts.TypeNode { | 823 commonSupertype(nodeA: ts.TypeNode, nodeB: ts.TypeNode): ts.TypeNode { |
818 if (nodeA == null || nodeB == null) return null; | 824 if (nodeA == null || nodeB == null) return null; |
819 if (nodeA.kind === ts.SyntaxKind.TypeReference && nodeB.kind === ts.SyntaxKi
nd.TypeReference) { | 825 if (nodeA.kind === ts.SyntaxKind.TypeReference && nodeB.kind === ts.SyntaxKi
nd.TypeReference) { |
820 // Handle the trivial case where the types are identical except for type a
rguments. | 826 // Handle the trivial case where the types are identical except for type a
rguments. |
821 // We could do a better job and actually attempt to merge type arguments. | 827 // We could do a better job and actually attempt to merge type arguments. |
822 let refA = nodeA as ts.TypeReferenceNode; | 828 let refA = nodeA as ts.TypeReferenceNode; |
823 let refB = nodeB as ts.TypeReferenceNode; | 829 let refB = nodeB as ts.TypeReferenceNode; |
824 if (base.ident(refA.typeName) === base.ident(refB.typeName)) { | 830 if (base.ident(refA.typeName) === base.ident(refB.typeName)) { |
825 let merge = <ts.TypeReferenceNode>ts.createNode(ts.SyntaxKind.TypeRefere
nce); | 831 let merge = <ts.TypeReferenceNode>ts.createNode(ts.SyntaxKind.TypeRefere
nce); |
| 832 base.copyLocation(refA, merge); |
826 merge.typeName = refA.typeName; | 833 merge.typeName = refA.typeName; |
827 return merge; | 834 return merge; |
828 } | 835 } |
829 } | 836 } |
830 return this.toTypeNode(this.getCommonSupertype( | 837 return this.toTypeNode(this.getCommonSupertype( |
831 this.tc.getTypeAtLocation(nodeA), this.tc.getTypeAtLocation(nodeB))); | 838 this.tc.getTypeAtLocation(nodeA), this.tc.getTypeAtLocation(nodeB))); |
832 } | 839 } |
833 | 840 |
834 getCommonSupertype(a: ts.Type, b: ts.Type): ts.Type { | 841 getCommonSupertype(a: ts.Type, b: ts.Type): ts.Type { |
835 if (a === b) return a; | 842 if (a === b) return a; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
872 let qname = this.tc.getFullyQualifiedName(symbol); | 879 let qname = this.tc.getFullyQualifiedName(symbol); |
873 // Some Qualified Names include their file name. Might be a bug in TypeScrip
t, | 880 // Some Qualified Names include their file name. Might be a bug in TypeScrip
t, |
874 // for the time being just special case. | 881 // for the time being just special case. |
875 if (symbol.flags & (ts.SymbolFlags.Class | ts.SymbolFlags.Function | ts.Symb
olFlags.Variable)) { | 882 if (symbol.flags & (ts.SymbolFlags.Class | ts.SymbolFlags.Function | ts.Symb
olFlags.Variable)) { |
876 qname = symbol.getName(); | 883 qname = symbol.getName(); |
877 } | 884 } |
878 if (FACADE_DEBUG) console.error('fn:', fileName, 'cfn:', canonicalFileName,
'qn:', qname); | 885 if (FACADE_DEBUG) console.error('fn:', fileName, 'cfn:', canonicalFileName,
'qn:', qname); |
879 return {fileName: canonicalFileName, qname}; | 886 return {fileName: canonicalFileName, qname}; |
880 } | 887 } |
881 } | 888 } |
OLD | NEW |