UNPKG

1.44 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = undefined;
7
8var _escope = require('escope');
9
10var _functionType = require('./utils/functionType');
11
12var emptyFn = function emptyFn() {}; // eslint-disable-line no-empty-function
13
14/**
15 * A helper for traversing with scope info from escope.
16 *
17 * Usage:
18 *
19 * traverser.traverse(ast, withScope(ast, {
20 * enter(node, parent, scope) {
21 * // do something with node and scope
22 * }
23 * }))
24 *
25 * @param {Object} ast The AST root node also passed to traverser.
26 * @param {Object} cfg Object with enter function as expected by traverser.
27 * @return {Object} Object with enter function to be passed to traverser.
28 */
29function withScope(ast, _ref) {
30 var _ref$enter = _ref.enter,
31 _enter = _ref$enter === undefined ? emptyFn : _ref$enter;
32
33 var scopeManager = (0, _escope.analyze)(ast, { ecmaVersion: 6, sourceType: 'module' });
34 var currentScope = scopeManager.acquire(ast);
35
36 return {
37 enter: function enter(node, parent) {
38 if ((0, _functionType.isFunction)(node)) {
39 currentScope = scopeManager.acquire(node);
40 }
41 return _enter.call(this, node, parent, currentScope);
42 }
43 // NOTE: leave() is currently not implemented.
44 // See escope docs for supporting it if need arises: https://github.com/estools/escope
45
46 };
47}
48exports.default = withScope;
49module.exports = exports['default'];
\No newline at end of file