Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
Patch SpreadProperty to escope's PatternVisitor
Browse files Browse the repository at this point in the history
Once new escope is released, we can patch SpreadProperty to PatternVisitor.
This will solve estools/escope#88 and eslint/eslint#4481.
  • Loading branch information
Constellation committed Nov 20, 2015
1 parent 52d1c84 commit ba7e1b6
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ function monkeypatch() {
var referencerMod = createModule(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
var definitionLoc;
try {
Expand Down Expand Up @@ -266,6 +279,12 @@ function monkeypatch() {
}
};

if (patternVisitor) {
patternVisitor.prototype.SpreadProperty = function (node) {
this.visit(node.argument);
};
}

// visit flow type in VariableDeclaration
var variableDeclaration = referencer.prototype.VariableDeclaration;
referencer.prototype.VariableDeclaration = function(node) {
Expand All @@ -276,15 +295,18 @@ function monkeypatch() {
if (typeAnnotation) {
checkIdentifierOrVisit.call(this, typeAnnotation);
}
if (id.type === "ObjectPattern") {
// check if object destructuring has a spread
var hasSpread = id.properties.filter(function(p) {
return p._babelType === "SpreadProperty";
});
// visit properties if so
if (hasSpread.length > 0) {
for (var j = 0; j < id.properties.length; j++) {
this.visit(id.properties[j]);
if (!patternVisitor) {
// Old method. Once escope in eslint is updated, this code is not necessary.
if (id.type === "ObjectPattern") {
// check if object destructuring has a spread
var hasSpread = id.properties.filter(function(p) {
return p._babelType === "SpreadProperty";
});
// visit properties if so
if (hasSpread.length > 0) {
for (var j = 0; j < id.properties.length; j++) {
this.visit(id.properties[j]);
}
}
}
}
Expand Down

0 comments on commit ba7e1b6

Please sign in to comment.