|
| 1 | +/// <reference path='../node_modules/typescript/bin/typescript.d.ts' /> |
| 2 | +import base = require('./base'); |
| 3 | +import ts = require('typescript'); |
| 4 | +import ts2dart = require('./main'); |
| 5 | + |
| 6 | +type FacadeHandler = (c: ts.CallExpression, context: ts.Expression) => void; |
| 7 | + |
| 8 | +export class FacadeConverter extends base.TranspilerBase { |
| 9 | + private tc: ts.TypeChecker; |
| 10 | + private forbiddenNames: {[fileName: string]: boolean}; |
| 11 | + |
| 12 | + constructor(transpiler: ts2dart.Transpiler) { |
| 13 | + super(transpiler); |
| 14 | + this.forbiddenNames = {}; |
| 15 | + for (var fileName in this.subs) { |
| 16 | + Object.keys(this.subs[fileName]) |
| 17 | + .map((fnName) => fnName.substring(fnName.lastIndexOf('.') + 1)) |
| 18 | + .forEach((fnName) => this.forbiddenNames[fnName] = true); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + setTypeChecker(tc: ts.TypeChecker) { this.tc = tc; } |
| 23 | + |
| 24 | + maybeHandleCall(c: ts.CallExpression): boolean { |
| 25 | + if (!this.tc) return false; |
| 26 | + |
| 27 | + var symbol: ts.Symbol; |
| 28 | + var context: ts.Expression; |
| 29 | + |
| 30 | + if (c.expression.kind === ts.SyntaxKind.Identifier) { |
| 31 | + // Function call. |
| 32 | + symbol = this.tc.getSymbolAtLocation(c.expression); |
| 33 | + context = null; |
| 34 | + } else if (c.expression.kind === ts.SyntaxKind.PropertyAccessExpression) { |
| 35 | + // Method call. |
| 36 | + var pa = <ts.PropertyAccessExpression>c.expression; |
| 37 | + symbol = this.tc.getSymbolAtLocation(pa.name); |
| 38 | + context = pa.expression; |
| 39 | + } else { |
| 40 | + // Not a call we recognize. |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + if (!symbol) { |
| 45 | + var ident = base.ident(c.expression); |
| 46 | + if (ident && this.forbiddenNames[ident]) this.reportMissingType(c, ident); |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + if (symbol.flags & ts.SymbolFlags.Alias) symbol = this.tc.getAliasedSymbol(symbol); |
| 51 | + if (!symbol.valueDeclaration) return false; |
| 52 | + |
| 53 | + var fileName = symbol.valueDeclaration.getSourceFile().fileName; |
| 54 | + fileName = fileName.replace(/(\.d)?\.ts$/, ''); |
| 55 | + |
| 56 | + // console.log('fn:', fileName); |
| 57 | + var fileSubs = this.subs[fileName]; |
| 58 | + var qn = this.tc.getFullyQualifiedName(symbol); |
| 59 | + // Function Qualified Names include their file name. Might be a bug in TypeScript, for the |
| 60 | + // time being just special case. |
| 61 | + if (symbol.flags & ts.SymbolFlags.Function) qn = symbol.getName(); |
| 62 | + |
| 63 | + // console.log('qn', qn); |
| 64 | + |
| 65 | + var qnSub = fileSubs[qn]; |
| 66 | + if (!qnSub) return false; |
| 67 | + |
| 68 | + qnSub(c, context); |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + private subs: ts.Map<ts.Map<FacadeHandler>> = { |
| 73 | + 'lib': { |
| 74 | + 'Array.push': (c: ts.CallExpression, context: ts.Expression) => { |
| 75 | + this.visit(context); |
| 76 | + this.emitCall('add', c.arguments); |
| 77 | + }, |
| 78 | + 'Array.map': (c: ts.CallExpression, context: ts.Expression) => { |
| 79 | + this.visit(context); |
| 80 | + this.emitCall('map', c.arguments); |
| 81 | + this.emitCall('toList'); |
| 82 | + }, |
| 83 | + 'Array.forEach': (c: ts.CallExpression, context: ts.Expression) => { |
| 84 | + this.visit(context); |
| 85 | + this.emitCall('forEach', c.arguments); |
| 86 | + this.emitCall('toList'); |
| 87 | + }, |
| 88 | + 'Array.slice': (c: ts.CallExpression, context: ts.Expression) => { |
| 89 | + this.emitCall('ListWrapper.slice', [context, ...c.arguments]); |
| 90 | + }, |
| 91 | + 'Array.splice': (c: ts.CallExpression, context: ts.Expression) => { |
| 92 | + this.emitCall('ListWrapper.splice', [context, ...c.arguments]); |
| 93 | + }, |
| 94 | + 'Array.concat': (c: ts.CallExpression, context: ts.Expression) => { |
| 95 | + this.emit('new List . from ('); |
| 96 | + this.visit(context); |
| 97 | + this.emit(') .. addAll ('); |
| 98 | + this.visit(c.arguments[0]); |
| 99 | + this.emit(')'); |
| 100 | + }, |
| 101 | + 'Array.isArray': (c: ts.CallExpression, context: ts.Expression) => { |
| 102 | + this.visit(context); |
| 103 | + this.emit('is List'); |
| 104 | + }, |
| 105 | + }, |
| 106 | + 'angular2/traceur-runtime': { |
| 107 | + 'Map.set': (c: ts.CallExpression, context: ts.Expression) => { |
| 108 | + this.visit(context); |
| 109 | + this.emit('['); |
| 110 | + this.visit(c.arguments[0]); |
| 111 | + this.emit(']'); |
| 112 | + this.emit('='); |
| 113 | + this.visit(c.arguments[1]); |
| 114 | + }, |
| 115 | + 'Map.get': (c: ts.CallExpression, context: ts.Expression) => { |
| 116 | + this.visit(context); |
| 117 | + this.emit('['); |
| 118 | + this.visit(c.arguments[0]); |
| 119 | + this.emit(']'); |
| 120 | + }, |
| 121 | + }, |
| 122 | + 'angular2/src/facade/lang': { |
| 123 | + 'CONST_EXPR': (c: ts.CallExpression, context: ts.Expression) => { |
| 124 | + // `const` keyword is emitted in the array literal handling, as it needs to be transitive. |
| 125 | + this.visitList(c.arguments); |
| 126 | + }, |
| 127 | + 'FORWARD_REF': (c: ts.CallExpression, context: ts.Expression) => { |
| 128 | + // The special function FORWARD_REF translates to an unwrapped value in Dart. |
| 129 | + const callback = <ts.FunctionExpression>c.arguments[0]; |
| 130 | + if (callback.kind !== ts.SyntaxKind.ArrowFunction) { |
| 131 | + this.reportError(c, 'FORWARD_REF takes only arrow functions'); |
| 132 | + return; |
| 133 | + } |
| 134 | + this.visit(callback.body); |
| 135 | + } |
| 136 | + }, |
| 137 | + }; |
| 138 | + |
| 139 | + private emitCall(name: string, args?: ts.Expression[]) { |
| 140 | + this.emit('.'); |
| 141 | + this.emit(name); |
| 142 | + this.emit('('); |
| 143 | + if (args) this.visitList(args); |
| 144 | + this.emit(')'); |
| 145 | + } |
| 146 | + |
| 147 | + checkPropertyAccess(pa: ts.PropertyAccessExpression) { |
| 148 | + if (!this.tc) return; |
| 149 | + var ident = pa.name.text; |
| 150 | + if (this.forbiddenNames[ident] && !this.tc.getSymbolAtLocation(pa.name)) { |
| 151 | + this.reportMissingType(pa, ident); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + reportMissingType(n: ts.Node, ident: string) { |
| 156 | + this.reportError(n, `Untyped property access to "${ident}" which could be special.\n` + |
| 157 | + ` Please add type declarations to disambiguate.`); |
| 158 | + } |
| 159 | + |
| 160 | + isInsideConstExpr(node: ts.Node): boolean { |
| 161 | + return this.isConstCall( |
| 162 | + <ts.CallExpression>this.getAncestor(node, ts.SyntaxKind.CallExpression)); |
| 163 | + } |
| 164 | + |
| 165 | + private isConstCall(node: ts.CallExpression): boolean { |
| 166 | + return node && base.ident(node.expression) === 'CONST_EXPR'; |
| 167 | + } |
| 168 | +} |
0 commit comments