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-12-27 12:36:47 -05:00
|
|
|
import {StaticSymbol} from '@angular/compiler/src/aot/static_symbol';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {CompileIdentifierMetadata} from '@angular/compiler/src/compile_metadata';
|
|
|
|
import * as o from '@angular/compiler/src/output/output_ast';
|
2016-11-23 17:47:05 -05:00
|
|
|
import {ImportResolver} from '@angular/compiler/src/output/path_util';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter';
|
|
|
|
|
2016-11-15 13:14:01 -05:00
|
|
|
const someModuleUrl = 'somePackage/somePath';
|
|
|
|
const anotherModuleUrl = 'somePackage/someOtherPath';
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2016-11-30 13:52:51 -05:00
|
|
|
const sameModuleIdentifier: CompileIdentifierMetadata = {
|
2016-12-27 12:36:47 -05:00
|
|
|
reference: new StaticSymbol(someModuleUrl, 'someLocalId', [])
|
2016-11-30 13:52:51 -05:00
|
|
|
};
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2016-11-30 13:52:51 -05:00
|
|
|
const externalModuleIdentifier: CompileIdentifierMetadata = {
|
2016-12-27 12:36:47 -05:00
|
|
|
reference: new StaticSymbol(anotherModuleUrl, 'someExternalId', [])
|
2016-11-30 13:52:51 -05:00
|
|
|
};
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2016-11-23 17:47:05 -05:00
|
|
|
class SimpleJsImportGenerator implements ImportResolver {
|
|
|
|
fileNameToModuleName(importedUrlStr: string, moduleUrlStr: string): string {
|
|
|
|
return importedUrlStr;
|
|
|
|
}
|
2016-12-27 12:36:47 -05:00
|
|
|
getImportAs(symbol: StaticSymbol): StaticSymbol { return null; }
|
2016-11-23 17:47:05 -05:00
|
|
|
}
|
|
|
|
|
2016-01-06 17:13:44 -05:00
|
|
|
export function main() {
|
2016-12-27 12:36:47 -05:00
|
|
|
// Not supported features of our OutputAst in TS:
|
2016-01-06 17:13:44 -05:00
|
|
|
// - real `const` like in Dart
|
|
|
|
// - final fields
|
|
|
|
|
|
|
|
describe('TypeScriptEmitter', () => {
|
2016-12-27 12:36:47 -05:00
|
|
|
let importResolver: ImportResolver;
|
2016-11-12 08:08:58 -05:00
|
|
|
let emitter: TypeScriptEmitter;
|
|
|
|
let someVar: o.ReadVarExpr;
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-12-27 12:36:47 -05:00
|
|
|
importResolver = new SimpleJsImportGenerator();
|
|
|
|
emitter = new TypeScriptEmitter(importResolver);
|
2016-01-06 17:13:44 -05:00
|
|
|
someVar = o.variable('someVar');
|
|
|
|
});
|
|
|
|
|
2016-12-27 12:36:47 -05:00
|
|
|
function emitStmt(stmt: o.Statement | o.Statement[], exportedVars: string[] = null): string {
|
2016-09-30 12:26:53 -04:00
|
|
|
if (!exportedVars) {
|
2016-01-06 17:13:44 -05:00
|
|
|
exportedVars = [];
|
|
|
|
}
|
2016-12-27 12:36:47 -05:00
|
|
|
const stmts = Array.isArray(stmt) ? stmt : [stmt];
|
|
|
|
return emitter.emitStatements(someModuleUrl, stmts, exportedVars);
|
2016-01-06 17:13:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should declare variables', () => {
|
2016-05-07 11:20:56 -04:00
|
|
|
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt())).toEqual(`var someVar:any = 1;`);
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(null, [o.StmtModifier.Final])))
|
2016-05-07 11:20:56 -04:00
|
|
|
.toEqual(`const someVar:any = 1;`);
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(), ['someVar']))
|
2016-05-07 11:20:56 -04:00
|
|
|
.toEqual(`export var someVar:any = 1;`);
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(o.INT_TYPE)))
|
|
|
|
.toEqual(`var someVar:number = 1;`);
|
|
|
|
});
|
|
|
|
|
2016-12-27 12:36:47 -05:00
|
|
|
describe('declare variables with ExternExpressions as values', () => {
|
|
|
|
it('should create no reexport if the identifier is in the same module', () => {
|
|
|
|
// identifier is in the same module -> no reexport
|
|
|
|
expect(emitStmt(someVar.set(o.importExpr(sameModuleIdentifier)).toDeclStmt(), ['someVar']))
|
|
|
|
.toEqual('export var someVar:any = someLocalId;');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create no reexport if the variable is not exported', () => {
|
|
|
|
expect(emitStmt(someVar.set(o.importExpr(externalModuleIdentifier)).toDeclStmt())).toEqual([
|
|
|
|
`import * as import0 from 'somePackage/someOtherPath';`,
|
|
|
|
`var someVar:any = import0.someExternalId;`
|
|
|
|
].join('\n'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create no reexport if the variable is typed', () => {
|
|
|
|
expect(emitStmt(
|
|
|
|
someVar.set(o.importExpr(externalModuleIdentifier)).toDeclStmt(o.DYNAMIC_TYPE),
|
|
|
|
['someVar']))
|
|
|
|
.toEqual([
|
|
|
|
`import * as import0 from 'somePackage/someOtherPath';`,
|
|
|
|
`export var someVar:any = import0.someExternalId;`
|
|
|
|
].join('\n'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create no reexport if the identifier has members', () => {
|
|
|
|
const externalModuleIdentifierWithMembers: CompileIdentifierMetadata = {
|
|
|
|
reference: new StaticSymbol(anotherModuleUrl, 'someExternalId', ['a'])
|
|
|
|
};
|
|
|
|
expect(emitStmt(
|
|
|
|
someVar.set(o.importExpr(externalModuleIdentifierWithMembers)).toDeclStmt(),
|
|
|
|
['someVar']))
|
|
|
|
.toEqual([
|
|
|
|
`import * as import0 from 'somePackage/someOtherPath';`,
|
|
|
|
`export var someVar:any = import0.someExternalId.a;`
|
|
|
|
].join('\n'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a reexport', () => {
|
|
|
|
expect(
|
|
|
|
emitStmt(someVar.set(o.importExpr(externalModuleIdentifier)).toDeclStmt(), ['someVar']))
|
|
|
|
.toEqual([
|
|
|
|
`export {someExternalId as someVar} from 'somePackage/someOtherPath';`, ``
|
|
|
|
].join('\n'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create multiple reexports from the same file', () => {
|
|
|
|
const someVar2 = o.variable('someVar2');
|
|
|
|
const externalModuleIdentifier2: CompileIdentifierMetadata = {
|
|
|
|
reference: new StaticSymbol(anotherModuleUrl, 'someExternalId2', [])
|
|
|
|
};
|
|
|
|
expect(emitStmt(
|
|
|
|
[
|
|
|
|
someVar.set(o.importExpr(externalModuleIdentifier)).toDeclStmt(),
|
|
|
|
someVar2.set(o.importExpr(externalModuleIdentifier2)).toDeclStmt()
|
|
|
|
],
|
|
|
|
['someVar', 'someVar2']))
|
|
|
|
.toEqual([
|
|
|
|
`export {someExternalId as someVar,someExternalId2 as someVar2} from 'somePackage/someOtherPath';`,
|
|
|
|
``
|
|
|
|
].join('\n'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use `importAs` for reexports', () => {
|
|
|
|
spyOn(importResolver, 'getImportAs')
|
|
|
|
.and.returnValue(new StaticSymbol('somePackage/importAsModule', 'importAsName', []));
|
|
|
|
expect(
|
|
|
|
emitStmt(someVar.set(o.importExpr(externalModuleIdentifier)).toDeclStmt(), ['someVar']))
|
|
|
|
.toEqual([
|
|
|
|
`export {importAsName as someVar} from 'somePackage/importAsModule';`, ``
|
|
|
|
].join('\n'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-06 17:13:44 -05:00
|
|
|
it('should read and write variables', () => {
|
|
|
|
expect(emitStmt(someVar.toStmt())).toEqual(`someVar;`);
|
|
|
|
expect(emitStmt(someVar.set(o.literal(1)).toStmt())).toEqual(`someVar = 1;`);
|
|
|
|
expect(emitStmt(someVar.set(o.variable('someOtherVar').set(o.literal(1))).toStmt()))
|
|
|
|
.toEqual(`someVar = (someOtherVar = 1);`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read and write keys', () => {
|
|
|
|
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).toStmt()))
|
|
|
|
.toEqual(`someMap[someKey];`);
|
|
|
|
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).set(o.literal(1)).toStmt()))
|
|
|
|
.toEqual(`someMap[someKey] = 1;`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read and write properties', () => {
|
|
|
|
expect(emitStmt(o.variable('someObj').prop('someProp').toStmt()))
|
|
|
|
.toEqual(`someObj.someProp;`);
|
|
|
|
expect(emitStmt(o.variable('someObj').prop('someProp').set(o.literal(1)).toStmt()))
|
|
|
|
.toEqual(`someObj.someProp = 1;`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should invoke functions and methods and constructors', () => {
|
|
|
|
expect(emitStmt(o.variable('someFn').callFn([o.literal(1)]).toStmt())).toEqual('someFn(1);');
|
|
|
|
expect(emitStmt(o.variable('someObj').callMethod('someMethod', [o.literal(1)]).toStmt()))
|
|
|
|
.toEqual('someObj.someMethod(1);');
|
|
|
|
expect(emitStmt(o.variable('SomeClass').instantiate([o.literal(1)]).toStmt()))
|
|
|
|
.toEqual('new SomeClass(1);');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support builtin methods', () => {
|
|
|
|
expect(emitStmt(o.variable('arr1')
|
|
|
|
.callMethod(o.BuiltinMethod.ConcatArray, [o.variable('arr2')])
|
|
|
|
.toStmt()))
|
|
|
|
.toEqual('arr1.concat(arr2);');
|
|
|
|
|
|
|
|
expect(emitStmt(o.variable('observable')
|
|
|
|
.callMethod(o.BuiltinMethod.SubscribeObservable, [o.variable('listener')])
|
|
|
|
.toStmt()))
|
|
|
|
.toEqual('observable.subscribe(listener);');
|
2016-04-22 18:33:32 -04:00
|
|
|
|
|
|
|
expect(
|
|
|
|
emitStmt(
|
2016-08-14 13:04:37 -04:00
|
|
|
o.variable('fn').callMethod(o.BuiltinMethod.Bind, [o.variable('someObj')]).toStmt()))
|
2016-04-22 18:33:32 -04:00
|
|
|
.toEqual('fn.bind(someObj);');
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support literals', () => {
|
|
|
|
expect(emitStmt(o.literal(0).toStmt())).toEqual('0;');
|
|
|
|
expect(emitStmt(o.literal(true).toStmt())).toEqual('true;');
|
|
|
|
expect(emitStmt(o.literal('someStr').toStmt())).toEqual(`'someStr';`);
|
|
|
|
expect(emitStmt(o.literalArr([o.literal(1)]).toStmt())).toEqual(`[1];`);
|
2016-08-26 18:54:34 -04:00
|
|
|
expect(emitStmt(o.literalMap([['someKey', o.literal(1)]]).toStmt())).toEqual(`{someKey: 1};`);
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
2016-12-20 21:17:58 -05:00
|
|
|
it('should apply quotes to each entry within a map produced with literalMap when true', () => {
|
|
|
|
expect(
|
|
|
|
emitStmt(
|
|
|
|
o.literalMap([['a', o.literal('a')], ['*', o.literal('star')]], null, true).toStmt())
|
|
|
|
.replace(/\s+/gm, ''))
|
|
|
|
.toEqual(`{'a':'a','*':'star'};`);
|
|
|
|
});
|
|
|
|
|
2016-11-04 13:55:21 -04:00
|
|
|
it('should support blank literals', () => {
|
|
|
|
expect(emitStmt(o.literal(null).toStmt())).toEqual('(null as any);');
|
|
|
|
expect(emitStmt(o.literal(undefined).toStmt())).toEqual('(undefined as any);');
|
2016-11-11 20:12:17 -05:00
|
|
|
expect(emitStmt(o.variable('a', null).isBlank().toStmt())).toEqual('(a == null);');
|
2016-11-04 13:55:21 -04:00
|
|
|
});
|
|
|
|
|
2016-01-06 17:13:44 -05:00
|
|
|
it('should support external identifiers', () => {
|
|
|
|
expect(emitStmt(o.importExpr(sameModuleIdentifier).toStmt())).toEqual('someLocalId;');
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt())).toEqual([
|
|
|
|
`import * as import0 from 'somePackage/someOtherPath';`, `import0.someExternalId;`
|
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
2016-12-27 12:36:47 -05:00
|
|
|
it('should support `importAs` for external identifiers', () => {
|
|
|
|
spyOn(importResolver, 'getImportAs')
|
|
|
|
.and.returnValue(new StaticSymbol('somePackage/importAsModule', 'importAsName', []));
|
|
|
|
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt())).toEqual([
|
|
|
|
`import * as import0 from 'somePackage/importAsModule';`, `import0.importAsName;`
|
|
|
|
].join('\n'));
|
|
|
|
});
|
|
|
|
|
2016-01-06 17:13:44 -05:00
|
|
|
it('should support operators', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const lhs = o.variable('lhs');
|
|
|
|
const rhs = o.variable('rhs');
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(someVar.cast(o.INT_TYPE).toStmt())).toEqual('(<number>someVar);');
|
|
|
|
expect(emitStmt(o.not(someVar).toStmt())).toEqual('!someVar;');
|
|
|
|
expect(
|
|
|
|
emitStmt(someVar.conditional(o.variable('trueCase'), o.variable('falseCase')).toStmt()))
|
2016-04-27 09:30:07 -04:00
|
|
|
.toEqual('(someVar? trueCase: falseCase);');
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
expect(emitStmt(lhs.equals(rhs).toStmt())).toEqual('(lhs == rhs);');
|
|
|
|
expect(emitStmt(lhs.notEquals(rhs).toStmt())).toEqual('(lhs != rhs);');
|
|
|
|
expect(emitStmt(lhs.identical(rhs).toStmt())).toEqual('(lhs === rhs);');
|
|
|
|
expect(emitStmt(lhs.notIdentical(rhs).toStmt())).toEqual('(lhs !== rhs);');
|
|
|
|
expect(emitStmt(lhs.minus(rhs).toStmt())).toEqual('(lhs - rhs);');
|
|
|
|
expect(emitStmt(lhs.plus(rhs).toStmt())).toEqual('(lhs + rhs);');
|
|
|
|
expect(emitStmt(lhs.divide(rhs).toStmt())).toEqual('(lhs / rhs);');
|
|
|
|
expect(emitStmt(lhs.multiply(rhs).toStmt())).toEqual('(lhs * rhs);');
|
|
|
|
expect(emitStmt(lhs.modulo(rhs).toStmt())).toEqual('(lhs % rhs);');
|
|
|
|
expect(emitStmt(lhs.and(rhs).toStmt())).toEqual('(lhs && rhs);');
|
|
|
|
expect(emitStmt(lhs.or(rhs).toStmt())).toEqual('(lhs || rhs);');
|
|
|
|
expect(emitStmt(lhs.lower(rhs).toStmt())).toEqual('(lhs < rhs);');
|
|
|
|
expect(emitStmt(lhs.lowerEquals(rhs).toStmt())).toEqual('(lhs <= rhs);');
|
|
|
|
expect(emitStmt(lhs.bigger(rhs).toStmt())).toEqual('(lhs > rhs);');
|
|
|
|
expect(emitStmt(lhs.biggerEquals(rhs).toStmt())).toEqual('(lhs >= rhs);');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support function expressions', () => {
|
2016-07-13 13:40:16 -04:00
|
|
|
expect(emitStmt(o.fn([], []).toStmt())).toEqual(['():void => {', '};'].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(o.fn([], [new o.ReturnStatement(o.literal(1))], o.INT_TYPE).toStmt()))
|
|
|
|
.toEqual(['():number => {', ' return 1;\n};'].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(o.fn([new o.FnParam('param1', o.INT_TYPE)], []).toStmt())).toEqual([
|
2016-07-13 13:40:16 -04:00
|
|
|
'(param1:number):void => {', '};'
|
2016-06-08 19:38:52 -04:00
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support function statements', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [
|
|
|
|
]))).toEqual(['function someFn():void {', '}'].join('\n'));
|
|
|
|
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], []), ['someFn'])).toEqual([
|
|
|
|
'export function someFn():void {', '}'
|
|
|
|
].join('\n'));
|
|
|
|
expect(emitStmt(new o.DeclareFunctionStmt(
|
|
|
|
'someFn', [], [new o.ReturnStatement(o.literal(1))], o.INT_TYPE)))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual(['function someFn():number {', ' return 1;', '}'].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1', o.INT_TYPE)], [
|
|
|
|
]))).toEqual(['function someFn(param1:number):void {', '}'].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should support comments', () => {
|
|
|
|
expect(emitStmt(new o.CommentStmt('a\nb'))).toEqual(['// a', '// b'].join('\n'));
|
|
|
|
});
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
it('should support if stmt', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const trueCase = o.variable('trueCase').callFn([]).toStmt();
|
|
|
|
const falseCase = o.variable('falseCase').callFn([]).toStmt();
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase]))).toEqual([
|
|
|
|
'if (cond) { trueCase(); }'
|
|
|
|
].join('\n'));
|
|
|
|
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase], [falseCase]))).toEqual([
|
|
|
|
'if (cond) {', ' trueCase();', '} else {', ' falseCase();', '}'
|
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support try/catch', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const bodyStmt = o.variable('body').callFn([]).toStmt();
|
|
|
|
const catchStmt =
|
|
|
|
o.variable('catchFn').callFn([o.CATCH_ERROR_VAR, o.CATCH_STACK_VAR]).toStmt();
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.TryCatchStmt([bodyStmt], [catchStmt]))).toEqual([
|
|
|
|
'try {', ' body();', '} catch (error) {', ' const stack:any = error.stack;',
|
|
|
|
' catchFn(error,stack);', '}'
|
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support support throwing',
|
|
|
|
() => { expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;'); });
|
|
|
|
|
|
|
|
describe('classes', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let callSomeMethod: o.Statement;
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
beforeEach(() => { callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt(); });
|
|
|
|
|
|
|
|
|
|
|
|
it('should support declaring classes', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, [
|
|
|
|
]))).toEqual(['class SomeClass {', '}'].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, []), ['SomeClass']))
|
|
|
|
.toEqual(['export class SomeClass {', '}'].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null, [
|
|
|
|
]))).toEqual(['class SomeClass extends SomeSuperClass {', '}'].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support declaring constructors', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const superCall = o.SUPER_EXPR.callFn([o.variable('someParam')]).toStmt();
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(
|
|
|
|
new o.ClassStmt('SomeClass', null, [], [], new o.ClassMethod(null, [], []), [])))
|
|
|
|
.toEqual(['class SomeClass {', ' constructor() {', ' }', '}'].join('\n'));
|
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [], [],
|
|
|
|
new o.ClassMethod(null, [new o.FnParam('someParam', o.INT_TYPE)], []), [])))
|
|
|
|
.toEqual(
|
|
|
|
['class SomeClass {', ' constructor(someParam:number) {', ' }', '}'].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [], [], new o.ClassMethod(null, [], [superCall]), [])))
|
|
|
|
.toEqual([
|
|
|
|
'class SomeClass {', ' constructor() {', ' super(someParam);', ' }', '}'
|
|
|
|
].join('\n'));
|
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [], [], new o.ClassMethod(null, [], [callSomeMethod]), [])))
|
|
|
|
.toEqual([
|
|
|
|
'class SomeClass {', ' constructor() {', ' this.someMethod();', ' }', '}'
|
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support declaring fields', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [new o.ClassField('someField')], [], null, [])))
|
2016-05-07 11:20:56 -04:00
|
|
|
.toEqual(['class SomeClass {', ' someField:any;', '}'].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [new o.ClassField('someField', o.INT_TYPE)], [], null, [])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual(['class SomeClass {', ' someField:number;', '}'].join('\n'));
|
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null,
|
|
|
|
[new o.ClassField('someField', o.INT_TYPE, [o.StmtModifier.Private])], [], null,
|
|
|
|
[])))
|
2016-08-22 17:28:09 -04:00
|
|
|
.toEqual(['class SomeClass {', ' /*private*/ someField:number;', '}'].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support declaring getters', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [], [new o.ClassGetter('someGetter', [])], null, [])))
|
2016-05-07 11:20:56 -04:00
|
|
|
.toEqual(['class SomeClass {', ' get someGetter():any {', ' }', '}'].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [], [new o.ClassGetter('someGetter', [], o.INT_TYPE)], null,
|
|
|
|
[])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual(['class SomeClass {', ' get someGetter():number {', ' }', '}'].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [], [new o.ClassGetter('someGetter', [callSomeMethod])], null,
|
|
|
|
[])))
|
|
|
|
.toEqual([
|
|
|
|
'class SomeClass {', ' get someGetter():any {', ' this.someMethod();', ' }', '}'
|
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(
|
|
|
|
emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [],
|
|
|
|
[new o.ClassGetter('someGetter', [], null, [o.StmtModifier.Private])], null, [])))
|
2016-06-08 19:38:52 -04:00
|
|
|
.toEqual(
|
|
|
|
['class SomeClass {', ' private get someGetter():any {', ' }', '}'].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support methods', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, [
|
|
|
|
new o.ClassMethod('someMethod', [], [])
|
|
|
|
]))).toEqual(['class SomeClass {', ' someMethod():void {', ' }', '}'].join('\n'));
|
|
|
|
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, [
|
|
|
|
new o.ClassMethod('someMethod', [], [], o.INT_TYPE)
|
|
|
|
]))).toEqual(['class SomeClass {', ' someMethod():number {', ' }', '}'].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(
|
|
|
|
emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [], [], null,
|
|
|
|
[new o.ClassMethod('someMethod', [new o.FnParam('someParam', o.INT_TYPE)], [])])))
|
2016-06-08 19:38:52 -04:00
|
|
|
.toEqual([
|
|
|
|
'class SomeClass {', ' someMethod(someParam:number):void {', ' }', '}'
|
|
|
|
].join('\n'));
|
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null, [], [], null,
|
|
|
|
[new o.ClassMethod('someMethod', [], [callSomeMethod])])))
|
|
|
|
.toEqual([
|
|
|
|
'class SomeClass {', ' someMethod():void {', ' this.someMethod();', ' }', '}'
|
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support builtin types', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
2016-08-22 18:30:18 -04:00
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.DYNAMIC_TYPE)))
|
|
|
|
.toEqual('var a:any = (null as any);');
|
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.BOOL_TYPE)))
|
|
|
|
.toEqual('var a:boolean = (null as any);');
|
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.INT_TYPE)))
|
|
|
|
.toEqual('var a:number = (null as any);');
|
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.NUMBER_TYPE)))
|
|
|
|
.toEqual('var a:number = (null as any);');
|
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.STRING_TYPE)))
|
|
|
|
.toEqual('var a:string = (null as any);');
|
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.FUNCTION_TYPE)))
|
|
|
|
.toEqual('var a:Function = (null as any);');
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support external types', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(sameModuleIdentifier))))
|
2016-08-22 18:30:18 -04:00
|
|
|
.toEqual('var a:someLocalId = (null as any);');
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(externalModuleIdentifier)))).toEqual([
|
|
|
|
`import * as import0 from 'somePackage/someOtherPath';`,
|
2016-08-22 18:30:18 -04:00
|
|
|
`var a:import0.someExternalId = (null as any);`
|
2016-06-08 19:38:52 -04:00
|
|
|
].join('\n'));
|
2016-12-27 12:36:47 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support `importAs` for external types', () => {
|
|
|
|
spyOn(importResolver, 'getImportAs')
|
|
|
|
.and.returnValue(new StaticSymbol('somePackage/importAsModule', 'importAsName', []));
|
|
|
|
const writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(externalModuleIdentifier)))).toEqual([
|
|
|
|
`import * as import0 from 'somePackage/importAsModule';`,
|
|
|
|
`var a:import0.importAsName = (null as any);`
|
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
2016-11-23 12:42:19 -05:00
|
|
|
it('should support expression types', () => {
|
|
|
|
expect(emitStmt(o.variable('a')
|
|
|
|
.set(o.NULL_EXPR)
|
|
|
|
.toDeclStmt(o.expressionType(
|
|
|
|
o.variable('b'), [o.expressionType(o.variable('c'))]))))
|
|
|
|
.toEqual('var a:b<c> = (null as any);');
|
|
|
|
});
|
|
|
|
|
2016-01-06 17:13:44 -05:00
|
|
|
it('should support combined types', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(null))))
|
2016-08-22 18:30:18 -04:00
|
|
|
.toEqual('var a:any[] = (null as any);');
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(o.INT_TYPE))))
|
2016-08-22 18:30:18 -04:00
|
|
|
.toEqual('var a:number[] = (null as any);');
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(null))))
|
2016-08-22 18:30:18 -04:00
|
|
|
.toEqual('var a:{[key: string]:any} = (null as any);');
|
2016-01-06 17:13:44 -05:00
|
|
|
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(o.INT_TYPE))))
|
2016-08-22 18:30:18 -04:00
|
|
|
.toEqual('var a:{[key: string]:number} = (null as any);');
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|