feat(transpiler): add support for named params to new expressions

This commit is contained in:
vsavkin 2014-10-10 11:50:50 -04:00
parent b71cd9f380
commit ee1e54cf0a
2 changed files with 27 additions and 1 deletions

View File

@ -4,6 +4,11 @@ function sum(a, b) {
return a + b; return a + b;
} }
class ConstructorWithNamedParams {
constructor(a, {b=1, c=2}) {
this.sum = a + b + c;
}
}
export function main() { export function main() {
describe('functions', function() { describe('functions', function() {
@ -35,6 +40,11 @@ export function main() {
expect(f({a: 10})).toBe(12); expect(f({a: 10})).toBe(12);
expect(f()).toBe(3); 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 () { describe("optional params", function () {

View File

@ -32,11 +32,27 @@ export class NamedParamsTransformer extends ParseTreeTransformer {
*/ */
transformCallExpression(tree) { transformCallExpression(tree) {
tree = super.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) && if (this._isLastArgAnNonEmptyObjectLiteral(tree) &&
! this._isLastArgObjectLiteralWithQuotedKeys(tree)) { ! this._isLastArgObjectLiteralWithQuotedKeys(tree)) {
this._replaceLastArgWithNamedParams(tree); this._replaceLastArgWithNamedParams(tree);
} }
return tree;
} }
_isLastArgAnNonEmptyObjectLiteral(tree) { _isLastArgAnNonEmptyObjectLiteral(tree) {