Merge pull request babel/babel-eslint#209 from Constellation/pattern-visitor

Patch SpreadProperty to escope's PatternVisitor
This commit is contained in:
Henry Zhu 2015-11-23 13:34:48 -05:00
parent 5352ad41c5
commit 0859607b4e

View File

@ -81,6 +81,19 @@ function monkeypatch() {
var referencerMod = createModule(referencerLoc); var referencerMod = createModule(referencerLoc);
var referencer = require(referencerLoc); var referencer = require(referencerLoc);
// monkeypatch escope/pattern-visitor
var patternVisitorLoc;
var patternVisitorMod;
var patternVisitor;
try {
patternVisitorLoc = Module._resolveFilename("./pattern-visitor", escopeMod);
patternVisitorMod = createModule(patternVisitorLoc);
patternVisitor = require(patternVisitorLoc);
} catch (err) {
// When eslint uses old escope, we cannot find pattern visitor.
// Fallback to the old way.
}
// reference Definition // reference Definition
var definitionLoc; var definitionLoc;
try { try {
@ -266,6 +279,12 @@ function monkeypatch() {
} }
}; };
if (patternVisitor) {
patternVisitor.prototype.SpreadProperty = function (node) {
this.visit(node.argument);
};
}
// visit flow type in VariableDeclaration // visit flow type in VariableDeclaration
var variableDeclaration = referencer.prototype.VariableDeclaration; var variableDeclaration = referencer.prototype.VariableDeclaration;
referencer.prototype.VariableDeclaration = function(node) { referencer.prototype.VariableDeclaration = function(node) {
@ -276,15 +295,18 @@ function monkeypatch() {
if (typeAnnotation) { if (typeAnnotation) {
checkIdentifierOrVisit.call(this, typeAnnotation); checkIdentifierOrVisit.call(this, typeAnnotation);
} }
if (id.type === "ObjectPattern") { if (!patternVisitor) {
// check if object destructuring has a spread // Old method. Once escope in eslint is updated, this code is not necessary.
var hasSpread = id.properties.filter(function(p) { if (id.type === "ObjectPattern") {
return p._babelType === "SpreadProperty"; // check if object destructuring has a spread
}); var hasSpread = id.properties.filter(function(p) {
// visit properties if so return p._babelType === "SpreadProperty";
if (hasSpread.length > 0) { });
for (var j = 0; j < id.properties.length; j++) { // visit properties if so
this.visit(id.properties[j]); if (hasSpread.length > 0) {
for (var j = 0; j < id.properties.length; j++) {
this.visit(id.properties[j]);
}
} }
} }
} }