diff --git a/tools/transpiler/spec/functions_spec.js b/tools/transpiler/spec/functions_spec.js index ec409cb2d7..097142c4de 100644 --- a/tools/transpiler/spec/functions_spec.js +++ b/tools/transpiler/spec/functions_spec.js @@ -4,6 +4,11 @@ function sum(a, b) { return a + b; } +class ConstructorWithNamedParams { + constructor(a, {b=1, c=2}) { + this.sum = a + b + c; + } +} export function main() { describe('functions', function() { @@ -35,6 +40,11 @@ export function main() { expect(f({a: 10})).toBe(12); expect(f()).toBe(3); }); + + it("should support new expressions", function () { + var obj = new ConstructorWithNamedParams(100, {b:10}); + expect(obj.sum).toEqual(112); + }); }); describe("optional params", function () { diff --git a/tools/transpiler/src/codegeneration/NamedParamsTransformer.js b/tools/transpiler/src/codegeneration/NamedParamsTransformer.js index 22bd7b4a3f..68e056c491 100644 --- a/tools/transpiler/src/codegeneration/NamedParamsTransformer.js +++ b/tools/transpiler/src/codegeneration/NamedParamsTransformer.js @@ -32,11 +32,27 @@ export class NamedParamsTransformer extends ParseTreeTransformer { */ transformCallExpression(tree) { tree = super.transformCallExpression(tree); + this._handleNamedParams(tree); + return tree; + } + + /** + * Transform new expressions. + * + * @param {NewExpression} tree + * @return {ParseTree} + */ + transformNewExpression(tree) { + tree = super.transformNewExpression(tree); + this._handleNamedParams(tree); + return tree; + } + + _handleNamedParams(tree) { if (this._isLastArgAnNonEmptyObjectLiteral(tree) && ! this._isLastArgObjectLiteralWithQuotedKeys(tree)) { this._replaceLastArgWithNamedParams(tree); } - return tree; } _isLastArgAnNonEmptyObjectLiteral(tree) {