From 52f614dcdf4909c1114f5c95f0f0312701939ab9 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 10 Jun 2015 01:36:36 +0100 Subject: [PATCH] add better path execution status algo --- .../path/inference/inferer-reference.js | 6 ++- src/babel/traversal/path/introspection.js | 53 ++++++++++++++----- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/babel/traversal/path/inference/inferer-reference.js b/src/babel/traversal/path/inference/inferer-reference.js index faf5e906a9..880f73cc38 100644 --- a/src/babel/traversal/path/inference/inferer-reference.js +++ b/src/babel/traversal/path/inference/inferer-reference.js @@ -51,8 +51,10 @@ function getTypeAnnotationBindingConstantViolations(path, name) { var visitedScopes = []; constantViolations = []; for (let violation of (rawConstantViolations: Array)) { - if (visitedScopes.indexOf(violation.scope) >= 0) continue; - visitedScopes.push(violation.scope); + var violationScope = violation.scope; + if (visitedScopes.indexOf(violationScope) >= 0) continue; + + visitedScopes.push(violationScope); constantViolations.push(violation); } diff --git a/src/babel/traversal/path/introspection.js b/src/babel/traversal/path/introspection.js index 102e9badea..2ded47c9d9 100644 --- a/src/babel/traversal/path/introspection.js +++ b/src/babel/traversal/path/introspection.js @@ -241,27 +241,56 @@ export function willIMaybeExecutesBefore(target) { */ export function _guessExecutionStatusRelativeTo(target) { - var self = this.getStatementParent(); - target = target.getStatementParent(); - if (target === self) return "before"; - // check if the two paths are in different functions, we can't track execution of these var targetFuncParent = target.scope.getFunctionParent(); - var selfFuncParent = self.scope.getFunctionParent(); + var selfFuncParent = this.scope.getFunctionParent(); if (targetFuncParent !== selfFuncParent) { return "function"; } - // crawl up the targets parents until we hit the container we're in and check keys - do { - // todo: if (target === self) return "after"; + var targetPaths = getAncestry(target); + var selfPaths = getAncestry(this); - if (target.container === self.container) { - return target.key >= self.key ? "before" : "after"; + // get ancestor where the branches intersect + var commonPath; + var targetIndex; + var selfIndex; + for (selfIndex = 0; selfIndex < selfPaths.length; selfIndex++) { + var selfPath = selfPaths[selfIndex]; + targetIndex = targetPaths.indexOf(selfPath); + if (targetIndex >= 0) { + commonPath = selfPath; + break; } - } while(self = self.parentPath); + } + if (!commonPath) { + return "before"; + } - return "before"; + // get the relationship paths that associate these nodes to their common ancestor + var targetRelationship = targetPaths[targetIndex - 1]; + var selfRelationship = selfPaths[selfIndex - 1]; + if (!targetRelationship || !selfRelationship) { + return "before"; + } + + // container list so let's see which one is after the other + if (targetRelationship.containerKey && targetRelationship.container === selfRelationship.container) { + return targetRelationship.key > selfRelationship.key ? "before" : "after"; + } + + // otherwise we're associated by a parent node, check which key comes before the other + var targetKeyPosition = t.VISITOR_KEYS[targetRelationship.type].indexOf(targetRelationship.key); + var selfKeyPosition = t.VISITOR_KEYS[selfRelationship.type].indexOf(selfRelationship.key); + return targetKeyPosition > selfKeyPosition ? "before" : "after"; +} + +function getAncestry(path) { + var paths = []; + do { + paths.push(path); + } while(path = path.parentPath); + return paths; } /**