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-08-02 18:53:34 -04:00
|
|
|
import {JavaScriptEmitter} from '@angular/compiler/src/output/js_emitter';
|
2016-04-28 20:50:03 -04:00
|
|
|
import * as o from '@angular/compiler/src/output/output_ast';
|
2019-08-15 16:42:17 -04:00
|
|
|
import {newArray} from '@angular/compiler/src/util';
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2017-03-14 12:16:15 -04:00
|
|
|
import {stripSourceMapAndNewLine} from './abstract_emitter_spec';
|
2017-01-27 17:23:12 -05:00
|
|
|
|
2017-03-15 18:50:30 -04:00
|
|
|
const someGenFilePath = 'somePackage/someGenFile';
|
2016-11-15 13:14:01 -05:00
|
|
|
const anotherModuleUrl = 'somePackage/someOtherPath';
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2017-05-16 19:30:37 -04:00
|
|
|
const sameModuleIdentifier = new o.ExternalReference(null, 'someLocalId', null);
|
|
|
|
|
|
|
|
const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'someExternalId', null);
|
2016-11-23 17:47:05 -05:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-01-06 17:13:44 -05:00
|
|
|
// Note supported features of our OutputAstin JavaScript / ES5:
|
|
|
|
// - types
|
|
|
|
// - declaring fields
|
|
|
|
|
|
|
|
describe('JavaScriptEmitter', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let emitter: JavaScriptEmitter;
|
|
|
|
let someVar: o.ReadVarExpr;
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2017-05-16 19:30:37 -04:00
|
|
|
emitter = new JavaScriptEmitter();
|
2016-01-06 17:13:44 -05:00
|
|
|
someVar = o.variable('someVar');
|
|
|
|
});
|
|
|
|
|
2017-05-16 19:30:37 -04:00
|
|
|
function emitStmt(stmt: o.Statement, preamble?: string): string {
|
2017-10-04 16:37:27 -04:00
|
|
|
const source = emitter.emitStatements(someGenFilePath, [stmt], preamble);
|
2017-03-14 12:16:15 -04:00
|
|
|
return stripSourceMapAndNewLine(source);
|
2016-01-06 17:13:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should declare variables', () => {
|
|
|
|
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt())).toEqual(`var someVar = 1;`);
|
2017-05-16 19:30:37 -04:00
|
|
|
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(null, [o.StmtModifier.Exported])))
|
|
|
|
.toEqual([
|
|
|
|
'var someVar = 1;',
|
|
|
|
`Object.defineProperty(exports, 'someVar', { get: function() { return someVar; }});`
|
|
|
|
].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];`);
|
2017-07-05 17:51:39 -04:00
|
|
|
expect(emitStmt(o.literalMap([
|
|
|
|
{key: 'someKey', value: o.literal(1), quoted: false},
|
|
|
|
{key: 'a', value: o.literal('a'), quoted: false},
|
|
|
|
{key: '*', value: o.literal('star'), quoted: true},
|
|
|
|
]).toStmt())
|
|
|
|
.replace(/\s+/gm, ''))
|
|
|
|
.toEqual(`{someKey:1,a:'a','*':'star'};`);
|
2017-05-11 13:28:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should break expressions into multiple lines if they are too long', () => {
|
2019-08-15 16:42:17 -04:00
|
|
|
const values: o.Expression[] = newArray(100);
|
2017-05-11 13:28:48 -04:00
|
|
|
values.fill(o.literal(1));
|
|
|
|
values.splice(50, 0, o.fn([], [new o.ReturnStatement(o.literal(1))]));
|
|
|
|
expect(emitStmt(o.variable('fn').callFn(values).toStmt())).toEqual([
|
|
|
|
'fn(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,',
|
|
|
|
' 1,1,1,1,1,1,1,1,1,1,function() {', ' return 1;',
|
|
|
|
' },1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,',
|
|
|
|
' 1,1,1,1,1,1,1,1,1,1,1,1);'
|
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
2016-11-04 13:55:21 -04:00
|
|
|
it('should support blank literals', () => {
|
|
|
|
expect(emitStmt(o.literal(null).toStmt())).toEqual('null;');
|
|
|
|
expect(emitStmt(o.literal(undefined).toStmt())).toEqual('undefined;');
|
|
|
|
});
|
|
|
|
|
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([
|
2017-05-11 13:28:48 -04:00
|
|
|
`var i0 = re` +
|
2016-06-08 19:38:52 -04:00
|
|
|
`quire('somePackage/someOtherPath');`,
|
2017-05-11 13:28:48 -04:00
|
|
|
`i0.someExternalId;`
|
2016-06-08 19:38:52 -04:00
|
|
|
].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(o.not(someVar).toStmt())).toEqual('!someVar;');
|
2017-05-11 13:15:54 -04:00
|
|
|
expect(emitStmt(o.assertNotNull(someVar).toStmt())).toEqual('someVar;');
|
2016-01-06 17:13:44 -05:00
|
|
|
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', () => {
|
|
|
|
expect(emitStmt(o.fn([], []).toStmt())).toEqual(['function() {', '};'].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(o.fn([], [new o.ReturnStatement(o.literal(1))]).toStmt())).toEqual([
|
|
|
|
'function() {', ' return 1;\n};'
|
|
|
|
].join('\n'));
|
|
|
|
expect(emitStmt(o.fn([new o.FnParam('param1')], []).toStmt())).toEqual([
|
|
|
|
'function(param1) {', '};'
|
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support function statements', () => {
|
2020-04-08 13:14:18 -04:00
|
|
|
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], []))).toEqual([
|
|
|
|
'function someFn() {', '}'
|
|
|
|
].join('\n'));
|
2017-05-16 19:30:37 -04:00
|
|
|
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [], null, [o.StmtModifier.Exported])))
|
|
|
|
.toEqual([
|
|
|
|
'function someFn() {', '}',
|
|
|
|
`Object.defineProperty(exports, 'someFn', { get: function() { return someFn; }});`
|
|
|
|
].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [
|
|
|
|
new o.ReturnStatement(o.literal(1))
|
|
|
|
]))).toEqual(['function someFn() {', ' return 1;', '}'].join('\n'));
|
2020-04-08 13:14:18 -04:00
|
|
|
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1')], []))).toEqual([
|
|
|
|
'function someFn(param1) {', '}'
|
|
|
|
].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
|
|
|
});
|
|
|
|
|
2019-12-06 03:41:44 -05:00
|
|
|
it('should support ES5 localized strings', () => {
|
|
|
|
expect(emitStmt(new o.ExpressionStatement(o.localizedString(
|
|
|
|
{}, ['ab\\:c', 'd"e\'f'], ['ph1'],
|
|
|
|
[o.literal(7, o.NUMBER_TYPE).plus(o.literal(8, o.NUMBER_TYPE))]))))
|
|
|
|
.toEqual(
|
|
|
|
String.raw
|
|
|
|
`$localize((this&&this.__makeTemplateObject||function(e,t){return Object.defineProperty?Object.defineProperty(e,"raw",{value:t}):e.raw=t,e})(['ab\\:c', ':ph1:d"e\'f'], ['ab\\\\:c', ':ph1:d"e\'f']), (7 + 8));`);
|
|
|
|
});
|
|
|
|
|
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) {', ' var stack = error.stack;',
|
|
|
|
' catchFn(error,stack);', '}'
|
|
|
|
].join('\n'));
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should support support throwing', () => {
|
|
|
|
expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;');
|
|
|
|
});
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
describe('classes', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let callSomeMethod: o.Statement;
|
2016-01-06 17:13:44 -05:00
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt();
|
|
|
|
});
|
2016-01-06 17:13:44 -05:00
|
|
|
|
|
|
|
it('should support declaring classes', () => {
|
2020-04-08 13:14:18 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt('SomeClass', null!, [], [], null!, []))).toEqual([
|
|
|
|
'function SomeClass() {', '}'
|
|
|
|
].join('\n'));
|
2017-05-16 19:30:37 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
2020-04-08 13:14:18 -04:00
|
|
|
'SomeClass', null!, [], [], null!, [], [o.StmtModifier.Exported])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual([
|
2016-06-08 19:38:52 -04:00
|
|
|
'function SomeClass() {', '}',
|
2016-01-06 17:13:44 -05:00
|
|
|
`Object.defineProperty(exports, 'SomeClass', { get: function() { return SomeClass; }});`
|
|
|
|
].join('\n'));
|
2020-04-08 13:14:18 -04:00
|
|
|
expect(
|
|
|
|
emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null!, [])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual([
|
2016-06-08 19:38:52 -04:00
|
|
|
'function SomeClass() {', '}',
|
2016-01-06 17:13:44 -05:00
|
|
|
'SomeClass.prototype = Object.create(SomeSuperClass.prototype);'
|
|
|
|
].join('\n'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support declaring constructors', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const superCall = o.SUPER_EXPR.callFn([o.variable('someParam')]).toStmt();
|
2017-03-24 12:59:58 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
2020-04-08 13:14:18 -04:00
|
|
|
'SomeClass', null!, [], [], new o.ClassMethod(null!, [], []), [])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual(['function SomeClass() {', '}'].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
2020-04-08 13:14:18 -04:00
|
|
|
'SomeClass', null!, [], [],
|
|
|
|
new o.ClassMethod(null!, [new o.FnParam('someParam')], []), [])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual(['function SomeClass(someParam) {', '}'].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', o.variable('SomeSuperClass'), [], [],
|
2020-04-08 13:14:18 -04:00
|
|
|
new o.ClassMethod(null!, [], [superCall]), [])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual([
|
2016-06-08 19:38:52 -04:00
|
|
|
'function SomeClass() {', ' var self = this;',
|
|
|
|
' SomeSuperClass.call(this, someParam);', '}',
|
2016-01-06 17:13:44 -05:00
|
|
|
'SomeClass.prototype = Object.create(SomeSuperClass.prototype);'
|
|
|
|
].join('\n'));
|
2020-04-08 13:14:18 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
|
|
|
'SomeClass', null!, [], [], new o.ClassMethod(null!, [], [callSomeMethod]), [])))
|
2016-06-08 19:38:52 -04:00
|
|
|
.toEqual([
|
|
|
|
'function SomeClass() {', ' var self = this;', ' self.someMethod();', '}'
|
|
|
|
].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(
|
2020-04-08 13:14:18 -04:00
|
|
|
'SomeClass', null!, [], [new o.ClassGetter('someGetter', [])], null!, [])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual([
|
2016-06-08 19:38:52 -04:00
|
|
|
'function SomeClass() {', '}',
|
|
|
|
`Object.defineProperty(SomeClass.prototype, 'someGetter', { get: function() {`, `}});`
|
2016-01-06 17:13:44 -05:00
|
|
|
].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
2020-04-08 13:14:18 -04:00
|
|
|
'SomeClass', null!, [], [new o.ClassGetter('someGetter', [callSomeMethod])],
|
|
|
|
null!, [])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual([
|
2016-06-08 19:38:52 -04:00
|
|
|
'function SomeClass() {', '}',
|
2016-01-06 17:13:44 -05:00
|
|
|
`Object.defineProperty(SomeClass.prototype, 'someGetter', { get: function() {`,
|
2016-06-08 19:38:52 -04:00
|
|
|
` var self = this;`, ` self.someMethod();`, `}});`
|
2016-01-06 17:13:44 -05:00
|
|
|
].join('\n'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support methods', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
2020-04-08 13:14:18 -04:00
|
|
|
'SomeClass', null!, [], [], null!, [new o.ClassMethod('someMethod', [], [])])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual([
|
2016-06-08 19:38:52 -04:00
|
|
|
'function SomeClass() {', '}', 'SomeClass.prototype.someMethod = function() {', '};'
|
2016-01-06 17:13:44 -05:00
|
|
|
].join('\n'));
|
|
|
|
expect(emitStmt(new o.ClassStmt(
|
2020-04-08 13:14:18 -04:00
|
|
|
'SomeClass', null!, [], [], null!,
|
2016-01-06 17:13:44 -05:00
|
|
|
[new o.ClassMethod('someMethod', [new o.FnParam('someParam')], [])])))
|
|
|
|
.toEqual([
|
2016-06-08 19:38:52 -04:00
|
|
|
'function SomeClass() {', '}',
|
|
|
|
'SomeClass.prototype.someMethod = function(someParam) {', '};'
|
2016-01-06 17:13:44 -05:00
|
|
|
].join('\n'));
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(emitStmt(new o.ClassStmt(
|
2020-04-08 13:14:18 -04:00
|
|
|
'SomeClass', null!, [], [], null!,
|
2016-06-08 19:38:52 -04:00
|
|
|
[new o.ClassMethod('someMethod', [], [callSomeMethod])])))
|
2016-01-06 17:13:44 -05:00
|
|
|
.toEqual([
|
2016-06-08 19:38:52 -04:00
|
|
|
'function SomeClass() {', '}', 'SomeClass.prototype.someMethod = function() {',
|
|
|
|
' var self = this;', ' self.someMethod();', '};'
|
2016-01-06 17:13:44 -05:00
|
|
|
].join('\n'));
|
|
|
|
});
|
|
|
|
});
|
2017-03-15 18:50:30 -04:00
|
|
|
|
|
|
|
it('should support a preamble', () => {
|
2017-05-16 19:30:37 -04:00
|
|
|
expect(emitStmt(o.variable('a').toStmt(), '/* SomePreamble */')).toBe([
|
2017-03-15 18:50:30 -04:00
|
|
|
'/* SomePreamble */', 'a;'
|
|
|
|
].join('\n'));
|
|
|
|
});
|
2016-01-06 17:13:44 -05:00
|
|
|
});
|
|
|
|
}
|