From a4c70bb029a754e65ef5f32fdc4ab07a397616ed Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 6 Jun 2015 03:32:22 +0100 Subject: [PATCH] this commit makes the following changes to the way paths are handled: - store paths on parents instead of containers - implement one central hub that all traversal paths and scopes get access to in order to abstract out access to common functions --- src/babel/traversal/path/index.js | 26 +++++++++++++------------- src/babel/traversal/scope/index.js | 27 ++++++++------------------- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/babel/traversal/path/index.js b/src/babel/traversal/path/index.js index 25756d071f..63bbc22ecf 100644 --- a/src/babel/traversal/path/index.js +++ b/src/babel/traversal/path/index.js @@ -1,4 +1,5 @@ import type File from "../../transformation/file"; +import type Hub from "../hub"; import * as virtualTypes from "./lib/virtual-types"; import traverse from "../index"; import assign from "lodash/object/assign"; @@ -6,22 +7,21 @@ import Scope from "../scope"; import * as t from "../../types"; export default class NodePath { - constructor(parent, container, containerKey) { - this.containerKey = containerKey; - this.container = container; - this.contexts = []; - this.parent = parent; - this.data = {}; + constructor(hub, parent) { + this.contexts = []; + this.parent = parent; + this.data = {}; + this.hub = hub; } /** * Description */ - static get({ parentPath, parent, container, containerKey, key }) { + static get({ hub, parentPath, parent, container, containerKey, key }) { var targetNode = container[key]; - var paths = container._paths = container._paths || []; - var path; + var paths = parent._paths = parent._paths || []; + var path for (var i = 0; i < paths.length; i++) { var pathCheck = paths[i]; @@ -32,11 +32,11 @@ export default class NodePath { } if (!path) { - path = new NodePath(parent, container, containerKey); + path = new NodePath(hub, parent); paths.push(path); } - path.setup(parentPath, key); + path.setup(parentPath, container, containerKey, key); return path; } @@ -45,12 +45,12 @@ export default class NodePath { * Description */ - static getScope(path: NodePath, scope: Scope, file?: File) { + static getScope(path: NodePath, scope: Scope, hub?: Hub) { var ourScope = scope; // we're entering a new scope so let's construct it! if (path.isScope()) { - ourScope = new Scope(path, scope, file); + ourScope = new Scope(path, scope, hub); } return ourScope; diff --git a/src/babel/traversal/scope/index.js b/src/babel/traversal/scope/index.js index 9a6bf1f034..63a510542a 100644 --- a/src/babel/traversal/scope/index.js +++ b/src/babel/traversal/scope/index.js @@ -1,7 +1,7 @@ import includes from "lodash/collection/includes"; import repeating from "repeating"; import type NodePath from "../path"; -import type File from "../../transformation/file"; +import type Hub from "../hub"; import traverse from "../index"; import defaults from "lodash/object/defaults"; import * as messages from "../../messages"; @@ -140,7 +140,7 @@ export default class Scope { * within. */ - constructor(path: NodePath, parent?: Scope, file?: File) { + constructor(path: NodePath, parent?: Scope, hub?: Hub) { if (parent && parent.block === path.node) { return parent; } @@ -153,7 +153,7 @@ export default class Scope { } this.parent = parent; - this.file = parent ? parent.file : file; + this.hub = parent ? parent.hub : hub; this.parentBlock = path.parent; this.block = path.node; @@ -177,17 +177,6 @@ export default class Scope { traverse(node, opts, this, state, this.path); } - - /** - * Since `Scope` instances are unique to their traversal we need some other - * way to compare if scopes are the same. Here we just compare `this.bindings` - * as it will be the same across all instances. - */ - - is(scope) { - return this.bindings === scope.bindings; - } - /** * Description */ @@ -344,7 +333,7 @@ export default class Scope { if (!duplicate) duplicate = local.kind === "param" && (kind === "let" || kind === "const"); if (duplicate) { - throw this.file.errorWithNode(id, messages.get("scopeDuplicateDeclaration", name), TypeError); + throw this.hub.file.errorWithNode(id, messages.get("scopeDuplicateDeclaration", name), TypeError); } } @@ -374,7 +363,7 @@ export default class Scope { state.binding.name = newName; } - var file = this.file; + var file = this.hub.file; if (file) { this._renameFromMap(file.moduleFormatter.localImports, oldName, newName, state.binding); //this._renameFromMap(file.moduleFormatter.localExports, oldName, newName); @@ -414,7 +403,7 @@ export default class Scope { */ toArray(node: Object, i?: number) { - var file = this.file; + var file = this.hub.file; if (t.isIdentifier(node)) { var binding = this.getBinding(node.name); @@ -436,7 +425,7 @@ export default class Scope { } else if (i) { args.push(t.literal(i)); helperName = "sliced-to-array"; - if (this.file.isLoose("es6.forOf")) helperName += "-loose"; + if (this.hub.file.isLoose("es6.forOf")) helperName += "-loose"; } return t.callExpression(file.addHelper(helperName), args); } @@ -716,7 +705,7 @@ export default class Scope { declar._generated = true; declar._blockHoist = 2; - this.file.attachAuxiliaryComment(declar); + this.hub.file.attachAuxiliaryComment(declar); var [declarPath] = path.unshiftContainer("body", [declar]); this.registerBinding(kind, declarPath);