added support for default arguments

fixes #2622
This commit is contained in:
Tobias Koppers 2016-06-16 01:04:59 +02:00
parent 7fdfdbd43b
commit f6efe19d06
6 changed files with 76 additions and 8 deletions

View File

@ -873,6 +873,7 @@ Parser.prototype.walkIdentifier = function walkIdentifier(expression) {
Parser.prototype.inScope = function inScope(params, fn) {
var oldScope = this.scope;
var _this = this;
this.scope = {
inTry: false,
inShorthand: false,
@ -881,15 +882,51 @@ Parser.prototype.inScope = function inScope(params, fn) {
};
params.forEach(function(param) {
if(typeof param !== "string") {
if(param.type !== "Identifier")
return;
param = param.name;
param = _this.enterPattern(param, function(param) {
_this.scope.renames["$" + param] = undefined;
_this.scope.definitions.push(param);
});
} else {
_this.scope.renames["$" + param] = undefined;
_this.scope.definitions.push(param);
}
this.scope.renames["$" + param] = undefined;
this.scope.definitions.push(param);
}, this);
});
fn();
this.scope = oldScope;
_this.scope = oldScope;
};
Parser.prototype.enterPattern = function enterPattern(pattern, onIdent) {
if(this["enter" + pattern.type])
return this["enter" + pattern.type](pattern, onIdent);
};
Parser.prototype.enterIdentifier = function enterIdentifier(pattern, onIdent) {
onIdent(pattern.name);
};
Parser.prototype.enterObjectPattern = function enterObjectPattern(pattern, onIdent) {
pattern.properties.forEach(function(property) {
switch(property.type) {
case "AssignmentProperty":
this.enterPattern(property.value, onIdent);
break;
}
}, this);
};
Parser.prototype.enterArrayPattern = function enterArrayPattern(pattern, onIdent) {
pattern.elements.forEach(function(pattern) {
this.enterPattern(pattern, onIdent);
}, this);
};
Parser.prototype.enterRestElement = function enterRestElement(pattern, onIdent) {
this.enterPattern(pattern.argument, onIdent);
};
Parser.prototype.enterAssignmentPattern = function enterAssignmentPattern(pattern, onIdent) {
this.enterPattern(pattern.left, onIdent);
this.walkExpression(pattern.right);
};
Parser.prototype.evaluateExpression = function evaluateExpression(expression) {

View File

@ -4,7 +4,7 @@
"author": "Tobias Koppers @sokra",
"description": "Packs CommonJs/AMD modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.",
"dependencies": {
"acorn": "^3.0.0",
"acorn": "^3.2.0",
"async": "^1.3.0",
"clone": "^1.0.2",
"enhanced-resolve": "^2.2.0",

View File

@ -0,0 +1,16 @@
import { a, b } from "./module";
function func(x = a, y = b) {
return [x, y];
}
var func2 = function(x = a, y = b) {
return [x, y];
}
it("should import into default parameters", function() {
func().should.be.eql(["a", "b"]);
func2().should.be.eql(["a", "b"]);
func(1).should.be.eql([1, "b"]);
func2(2).should.be.eql([2, "b"]);
});

View File

@ -0,0 +1,2 @@
export var a = "a";
export var b = "b";

View File

@ -0,0 +1,5 @@
var supportsDefaultArgs = require("../../../helpers/supportsDefaultArgs");
module.exports = function(config) {
return !config.minimize && supportsDefaultArgs();
};

View File

@ -0,0 +1,8 @@
module.exports = function supportsDefaultArgs() {
try {
var f = eval("(function f(a = 123) { return a; })");
return f() === 123;
} catch(e) {
return false;
}
};