walk variable patterns for parsed stuff

fixes #4357
This commit is contained in:
Tobias Koppers 2017-02-23 23:06:05 +01:00
parent e618bdc6c8
commit 21aa837eb2
6 changed files with 70 additions and 3 deletions

View File

@ -675,6 +675,7 @@ Parser.prototype.walkVariableDeclarators = function walkVariableDeclarators(decl
if(idx >= 0) _this.scope.definitions.splice(idx, 1);
}
} else {
_this.walkPattern(declarator.id);
_this.enterPattern(declarator.id, function(name, decl) {
if(!_this.applyPluginsBailResult1("var " + name, decl)) {
_this.scope.renames["$" + name] = undefined;
@ -689,6 +690,37 @@ Parser.prototype.walkVariableDeclarators = function walkVariableDeclarators(decl
});
};
Parser.prototype.walkPattern = function walkPattern(pattern) {
if(pattern.type === "Identifier")
return;
if(this["walk" + pattern.type])
this["walk" + pattern.type](pattern);
};
Parser.prototype.walkObjectPattern = function walkObjectPattern(pattern) {
for(var i = 0, len = pattern.properties.length; i < len; i++) {
var prop = pattern.properties[i];
if(prop) {
if(prop.computed)
this.walkExpression(prop.key);
if(prop.value)
this.walkPattern(prop.value);
}
}
};
Parser.prototype.walkArrayPattern = function walkArrayPattern(pattern) {
for(var i = 0, len = pattern.elements.length; i < len; i++) {
var element = pattern.elements[i];
if(element)
this.walkPattern(element);
}
};
Parser.prototype.walkRestElement = function walkRestElement(pattern) {
this.walkPattern(pattern.argument);
};
Parser.prototype.walkExpressions = function walkExpressions(expressions) {
for(var expressionsIndex = 0, len = expressions.length; expressionsIndex < len; expressionsIndex++) {
var expression = expressions[expressionsIndex];
@ -961,7 +993,7 @@ Parser.prototype.inScope = function inScope(params, fn) {
var param = params[paramIndex];
if(typeof param !== "string") {
param = _this.enterPattern(param, function(param) {
_this.enterPattern(param, function(param) {
_this.scope.renames["$" + param] = undefined;
_this.scope.definitions.push(param);
});
@ -977,7 +1009,7 @@ Parser.prototype.inScope = function inScope(params, fn) {
Parser.prototype.enterPattern = function enterPattern(pattern, onIdent) {
if(pattern && this["enter" + pattern.type])
return this["enter" + pattern.type](pattern, onIdent);
this["enter" + pattern.type](pattern, onIdent);
};
Parser.prototype.enterIdentifier = function enterIdentifier(pattern, onIdent) {

View File

@ -0,0 +1 @@
module.exports = "a";

View File

@ -0,0 +1 @@
export default "b";

View File

@ -0,0 +1,27 @@
import b from "./b";
it("should parse dynamic property names", function() {
var o = {
[require("./a")]: "a",
[b]: "b"
};
o.should.be.eql({
a: "a",
b: "b"
});
});
it("should match dynamic property names", function() {
const {
[require("./a")]: aa,
[b]: bb
} = { a: "a", b: "b" };
const [x,, ...[{
[b]: {
[b]: cc
}
}]] = [0, 1, {b: {b: "c"}}];
aa.should.be.eql("a");
bb.should.be.eql("b");
cc.should.be.eql("c");
});

View File

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

View File

@ -1,4 +1,4 @@
module.exports = function supportsIteratorDestructuring() {
module.exports = function supportsObjectDestructuring() {
try {
var f = eval("(function f({x, y}) { return x + y; })");
return f({ x: 1, y: 2 }) === 3;