2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {CompileIdentifierMetadata} from '@angular/compiler/src/compile_metadata';
|
2016-10-19 12:17:36 -04:00
|
|
|
import {assetUrl} from '@angular/compiler/src/identifiers';
|
2016-06-08 19:38:52 -04:00
|
|
|
import * as o from '@angular/compiler/src/output/output_ast';
|
2016-11-15 14:13:20 -05:00
|
|
|
import {ImportResolver} from '@angular/compiler/src/output/path_util';
|
2016-08-25 03:50:16 -04:00
|
|
|
import {EventEmitter} from '@angular/core';
|
|
|
|
import {BaseError} from '@angular/core/src/facade/errors';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {ViewType} from '@angular/core/src/linker/view_type';
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
export class ExternalClass {
|
|
|
|
changeable: any;
|
|
|
|
constructor(public data: any) { this.changeable = data; }
|
2016-06-08 18:45:15 -04:00
|
|
|
someMethod(a: any /** TODO #9100 */) { return {'param': a, 'data': this.data}; }
|
2016-01-06 17:13:44 -05:00
|
|
|
}
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const testDataIdentifier = new CompileIdentifierMetadata({
|
2016-01-06 17:13:44 -05:00
|
|
|
name: 'ExternalClass',
|
2016-11-15 13:14:01 -05:00
|
|
|
moduleUrl: `@angular/compiler/test/output/output_emitter_util`,
|
2016-08-29 11:52:25 -04:00
|
|
|
reference: ExternalClass
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const eventEmitterIdentifier = new CompileIdentifierMetadata(
|
2016-08-29 11:52:25 -04:00
|
|
|
{name: 'EventEmitter', moduleUrl: assetUrl('core'), reference: EventEmitter});
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const enumIdentifier = new CompileIdentifierMetadata({
|
2016-01-06 17:13:44 -05:00
|
|
|
name: 'ViewType.HOST',
|
2016-04-28 20:50:03 -04:00
|
|
|
moduleUrl: assetUrl('core', 'linker/view_type'),
|
2016-08-29 11:52:25 -04:00
|
|
|
reference: ViewType.HOST
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const baseErrorIdentifier = new CompileIdentifierMetadata(
|
2016-08-29 11:52:25 -04:00
|
|
|
{name: 'BaseError', moduleUrl: assetUrl('core', 'facade/errors'), reference: BaseError});
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
export var codegenExportsVars = [
|
|
|
|
'getExpressions',
|
|
|
|
];
|
|
|
|
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const _getExpressionsStmts: o.Statement[] = [
|
2016-06-08 19:38:52 -04:00
|
|
|
o.variable('readVar').set(o.literal('someValue')).toDeclStmt(),
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
o.variable('changedVar').set(o.literal('initialValue')).toDeclStmt(),
|
|
|
|
o.variable('changedVar').set(o.literal('changedValue')).toStmt(),
|
|
|
|
|
|
|
|
o.variable('map')
|
|
|
|
.set(o.literalMap([
|
|
|
|
['someKey', o.literal('someValue')],
|
|
|
|
['changeable', o.literal('initialValue')],
|
|
|
|
]))
|
|
|
|
.toDeclStmt(),
|
|
|
|
o.variable('map').key(o.literal('changeable')).set(o.literal('changedValue')).toStmt(),
|
|
|
|
|
|
|
|
o.variable('externalInstance')
|
|
|
|
.set(o.importExpr(testDataIdentifier).instantiate([o.literal('someValue')]))
|
|
|
|
.toDeclStmt(),
|
|
|
|
o.variable('externalInstance').prop('changeable').set(o.literal('changedValue')).toStmt(),
|
|
|
|
|
|
|
|
o.variable('fn')
|
2016-06-08 19:38:52 -04:00
|
|
|
.set(o.fn(
|
|
|
|
[new o.FnParam('param')],
|
|
|
|
[new o.ReturnStatement(o.literalMap([['param', o.variable('param')]]))], o.DYNAMIC_TYPE))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toDeclStmt(),
|
|
|
|
|
|
|
|
o.variable('throwError')
|
2016-08-25 03:50:16 -04:00
|
|
|
.set(o.fn([], [new o.ThrowStmt(
|
|
|
|
o.importExpr(baseErrorIdentifier).instantiate([o.literal('someError')]))]))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toDeclStmt(),
|
|
|
|
|
|
|
|
o.variable('catchError')
|
2016-06-08 19:38:52 -04:00
|
|
|
.set(o.fn(
|
|
|
|
[new o.FnParam('runCb')],
|
|
|
|
[new o.TryCatchStmt(
|
|
|
|
[o.variable('runCb').callFn([]).toStmt()],
|
|
|
|
[new o.ReturnStatement(o.literalArr([o.CATCH_ERROR_VAR, o.CATCH_STACK_VAR]))])],
|
|
|
|
o.DYNAMIC_TYPE))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toDeclStmt(),
|
|
|
|
|
|
|
|
o.variable('dynamicInstance')
|
2016-06-08 19:38:52 -04:00
|
|
|
.set(o.variable('DynamicClass').instantiate([
|
|
|
|
o.literal('someValue'), o.literal('dynamicValue')
|
|
|
|
]))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toDeclStmt(),
|
|
|
|
o.variable('dynamicInstance').prop('dynamicChangeable').set(o.literal('changedValue')).toStmt(),
|
|
|
|
|
|
|
|
new o.ReturnStatement(o.literalMap([
|
|
|
|
['stringLiteral', o.literal('Hello World!')],
|
|
|
|
['intLiteral', o.literal(42)],
|
|
|
|
['boolLiteral', o.literal(true)],
|
|
|
|
['arrayLiteral', o.literalArr([o.literal(0)])],
|
|
|
|
['mapLiteral', o.literalMap([['key0', o.literal(0)]])],
|
|
|
|
|
|
|
|
['readVar', o.variable('readVar')],
|
|
|
|
['changedVar', o.variable('changedVar')],
|
|
|
|
['readKey', o.variable('map').key(o.literal('someKey'))],
|
|
|
|
['changedKey', o.variable('map').key(o.literal('changeable'))],
|
|
|
|
['readPropExternalInstance', o.variable('externalInstance').prop('data')],
|
|
|
|
['readPropDynamicInstance', o.variable('dynamicInstance').prop('dynamicProp')],
|
|
|
|
['readGetterDynamicInstance', o.variable('dynamicInstance').prop('dynamicGetter')],
|
|
|
|
['changedPropExternalInstance', o.variable('externalInstance').prop('changeable')],
|
|
|
|
['changedPropDynamicInstance', o.variable('dynamicInstance').prop('dynamicChangeable')],
|
|
|
|
|
|
|
|
[
|
|
|
|
'invokeMethodExternalInstance',
|
|
|
|
o.variable('externalInstance').callMethod('someMethod', [o.literal('someParam')])
|
|
|
|
],
|
2016-04-22 18:33:32 -04:00
|
|
|
[
|
|
|
|
'invokeMethodExternalInstanceViaBind',
|
|
|
|
o.variable('externalInstance')
|
|
|
|
.prop('someMethod')
|
2016-08-14 13:04:37 -04:00
|
|
|
.callMethod(o.BuiltinMethod.Bind, [o.variable('externalInstance')])
|
2016-04-22 18:33:32 -04:00
|
|
|
.callFn([o.literal('someParam')])
|
|
|
|
],
|
2016-01-06 17:13:44 -05:00
|
|
|
[
|
|
|
|
'invokeMethodDynamicInstance',
|
|
|
|
o.variable('dynamicInstance').callMethod('dynamicMethod', [o.literal('someParam')])
|
|
|
|
],
|
2016-04-22 18:33:32 -04:00
|
|
|
[
|
|
|
|
'invokeMethodDynamicInstanceViaBind',
|
|
|
|
o.variable('dynamicInstance')
|
|
|
|
.prop('dynamicMethod')
|
2016-08-14 13:04:37 -04:00
|
|
|
.callMethod(o.BuiltinMethod.Bind, [o.variable('dynamicInstance')])
|
2016-04-22 18:33:32 -04:00
|
|
|
.callFn([o.literal('someParam')])
|
|
|
|
],
|
2016-01-06 17:13:44 -05:00
|
|
|
[
|
2016-06-08 19:38:52 -04:00
|
|
|
'concatedArray', o.literalArr([o.literal(0)])
|
|
|
|
.callMethod(o.BuiltinMethod.ConcatArray, [o.literalArr([o.literal(1)])])
|
2016-01-06 17:13:44 -05:00
|
|
|
],
|
|
|
|
|
|
|
|
['fn', o.variable('fn')],
|
|
|
|
['closureInDynamicInstance', o.variable('dynamicInstance').prop('closure')],
|
|
|
|
['invokeFn', o.variable('fn').callFn([o.literal('someParam')])],
|
|
|
|
|
|
|
|
[
|
2016-06-08 19:38:52 -04:00
|
|
|
'conditionalTrue', o.literal('')
|
|
|
|
.prop('length')
|
|
|
|
.equals(o.literal(0))
|
|
|
|
.conditional(o.literal('true'), o.literal('false'))
|
2016-01-06 17:13:44 -05:00
|
|
|
],
|
|
|
|
[
|
2016-06-08 19:38:52 -04:00
|
|
|
'conditionalFalse', o.literal('')
|
|
|
|
.prop('length')
|
|
|
|
.notEquals(o.literal(0))
|
|
|
|
.conditional(o.literal('true'), o.literal('false'))
|
2016-01-06 17:13:44 -05:00
|
|
|
],
|
|
|
|
|
|
|
|
['not', o.not(o.literal(false))],
|
|
|
|
|
|
|
|
['externalTestIdentifier', o.importExpr(testDataIdentifier)],
|
|
|
|
['externalSrcIdentifier', o.importExpr(eventEmitterIdentifier)],
|
|
|
|
['externalEnumIdentifier', o.importExpr(enumIdentifier)],
|
|
|
|
|
|
|
|
['externalInstance', o.variable('externalInstance')],
|
|
|
|
['dynamicInstance', o.variable('dynamicInstance')],
|
|
|
|
|
|
|
|
['throwError', o.variable('throwError')],
|
|
|
|
['catchError', o.variable('catchError')],
|
|
|
|
|
|
|
|
[
|
2016-06-08 19:38:52 -04:00
|
|
|
'operators', o.literalMap([
|
2016-01-06 17:13:44 -05:00
|
|
|
['==', createOperatorFn(o.BinaryOperator.Equals)],
|
|
|
|
['!=', createOperatorFn(o.BinaryOperator.NotEquals)],
|
|
|
|
['===', createOperatorFn(o.BinaryOperator.Identical)],
|
|
|
|
['!==', createOperatorFn(o.BinaryOperator.NotIdentical)],
|
|
|
|
['-', createOperatorFn(o.BinaryOperator.Minus)],
|
|
|
|
['+', createOperatorFn(o.BinaryOperator.Plus)],
|
|
|
|
['/', createOperatorFn(o.BinaryOperator.Divide)],
|
|
|
|
['*', createOperatorFn(o.BinaryOperator.Multiply)],
|
|
|
|
['%', createOperatorFn(o.BinaryOperator.Modulo)],
|
|
|
|
['&&', createOperatorFn(o.BinaryOperator.And)],
|
|
|
|
['||', createOperatorFn(o.BinaryOperator.Or)],
|
|
|
|
['<', createOperatorFn(o.BinaryOperator.Lower)],
|
|
|
|
['<=', createOperatorFn(o.BinaryOperator.LowerEquals)],
|
|
|
|
['>', createOperatorFn(o.BinaryOperator.Bigger)],
|
|
|
|
['>=', createOperatorFn(o.BinaryOperator.BiggerEquals)]
|
|
|
|
])
|
|
|
|
],
|
|
|
|
]))
|
|
|
|
];
|
|
|
|
|
|
|
|
export var codegenStmts: o.Statement[] = [
|
|
|
|
new o.CommentStmt('This is a comment'),
|
|
|
|
|
|
|
|
new o.ClassStmt(
|
|
|
|
'DynamicClass', o.importExpr(testDataIdentifier),
|
|
|
|
[
|
|
|
|
new o.ClassField('dynamicProp', o.DYNAMIC_TYPE),
|
|
|
|
new o.ClassField('dynamicChangeable', o.DYNAMIC_TYPE),
|
|
|
|
new o.ClassField('closure', o.FUNCTION_TYPE)
|
|
|
|
],
|
|
|
|
[
|
|
|
|
new o.ClassGetter('dynamicGetter',
|
|
|
|
[
|
|
|
|
new o.ReturnStatement(o.literalMap([
|
|
|
|
['data', o.THIS_EXPR.prop('data')],
|
|
|
|
['dynamicProp', o.THIS_EXPR.prop('dynamicProp')]
|
|
|
|
]))
|
|
|
|
],
|
|
|
|
new o.MapType(o.DYNAMIC_TYPE))
|
|
|
|
],
|
|
|
|
new o.ClassMethod(null,
|
|
|
|
[
|
|
|
|
new o.FnParam('dataParam', o.DYNAMIC_TYPE),
|
|
|
|
new o.FnParam('dynamicPropParam', o.DYNAMIC_TYPE)
|
|
|
|
],
|
|
|
|
[
|
|
|
|
o.SUPER_EXPR.callFn([o.variable('dataParam')])
|
|
|
|
.toStmt(),
|
|
|
|
o.THIS_EXPR.prop('dynamicProp')
|
|
|
|
.set(o.variable('dynamicPropParam'))
|
|
|
|
.toStmt(),
|
|
|
|
o.THIS_EXPR.prop('dynamicChangeable')
|
|
|
|
.set(o.variable('dynamicPropParam'))
|
|
|
|
.toStmt(),
|
|
|
|
o.THIS_EXPR.prop('closure')
|
|
|
|
.set(o.fn([new o.FnParam('param', o.DYNAMIC_TYPE)],
|
|
|
|
[
|
|
|
|
new o.ReturnStatement(o.literalMap([
|
|
|
|
['param', o.variable('param')],
|
|
|
|
['data', o.THIS_EXPR.prop('data')],
|
|
|
|
['dynamicProp', o.THIS_EXPR.prop('dynamicProp')]
|
|
|
|
]))
|
|
|
|
],
|
|
|
|
o.DYNAMIC_TYPE))
|
|
|
|
.toStmt(),
|
|
|
|
]),
|
|
|
|
[
|
|
|
|
new o.ClassMethod('dynamicMethod', [new o.FnParam('param', o.DYNAMIC_TYPE)],
|
|
|
|
[
|
|
|
|
new o.ReturnStatement(o.literalMap([
|
|
|
|
['param', o.variable('param')],
|
|
|
|
['data', o.THIS_EXPR.prop('data')],
|
|
|
|
['dynamicProp', o.THIS_EXPR.prop('dynamicProp')]
|
|
|
|
]))
|
|
|
|
],
|
|
|
|
o.DYNAMIC_TYPE)
|
|
|
|
]),
|
|
|
|
|
|
|
|
o.fn([], _getExpressionsStmts, o.DYNAMIC_TYPE).toDeclStmt('getExpressions')
|
|
|
|
];
|
|
|
|
|
|
|
|
function createOperatorFn(op: o.BinaryOperator) {
|
|
|
|
return o.fn(
|
|
|
|
[new o.FnParam('a'), new o.FnParam('b')],
|
|
|
|
[new o.ReturnStatement(new o.BinaryOperatorExpr(op, o.variable('a'), o.variable('b')))],
|
|
|
|
o.DYNAMIC_TYPE);
|
|
|
|
}
|
|
|
|
|
2016-11-15 14:13:20 -05:00
|
|
|
export class SimpleJsImportGenerator implements ImportResolver {
|
|
|
|
resolveFileToImport(importedUrlStr: string, moduleUrlStr: string): string {
|
|
|
|
return importedUrlStr;
|
|
|
|
}
|
2016-01-06 17:13:44 -05:00
|
|
|
}
|