How to use escope - 10 common examples

To help you get started, we’ve selected a few escope examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github karthikv / tradeship / patches / escope.js View on Github external
};

referencer.prototype.visitProperty = function(node) {
  visitDecorators.call(this, node);
  visitProperty.call(this, node);
};

function visitDecorators(node) {
  if (!node.decorators) {
    return;
  }
  node.decorators.forEach(d => this.visit(d));
}

// register class properties by visiting them as regular properties
referencer.prototype.ClassProperty = function(node) {
  this.visitProperty(node);
};

module.exports = require("escope");
github karthikv / tradeship / patches / escope.js View on Github external
const referencer = require("escope/lib/referencer").default;

const { visitClass, visitProperty } = referencer.prototype;

// visit decorators on classes/properties to resolve their identifiers
referencer.prototype.visitClass = function(node) {
  visitDecorators.call(this, node);
  visitClass.call(this, node);
};

referencer.prototype.visitProperty = function(node) {
  visitDecorators.call(this, node);
  visitProperty.call(this, node);
};

function visitDecorators(node) {
  if (!node.decorators) {
    return;
  }
  node.decorators.forEach(d => this.visit(d));
}

// register class properties by visiting them as regular properties
referencer.prototype.ClassProperty = function(node) {
  this.visitProperty(node);
};
github karthikv / tradeship / patches / escope.js View on Github external
const referencer = require("escope/lib/referencer").default;

const { visitClass, visitProperty } = referencer.prototype;

// visit decorators on classes/properties to resolve their identifiers
referencer.prototype.visitClass = function(node) {
  visitDecorators.call(this, node);
  visitClass.call(this, node);
};

referencer.prototype.visitProperty = function(node) {
  visitDecorators.call(this, node);
  visitProperty.call(this, node);
};

function visitDecorators(node) {
  if (!node.decorators) {
    return;
  }
  node.decorators.forEach(d => this.visit(d));
}
github dumberjs / dumber / lib / parser.js View on Github external
function globalIndentifiers (code) {
  let ast = ensureParsed(code);
  let scopeManager = analyze(ast, {ecmaVersion: 6});
  let globalScope = scopeManager.acquire(ast);

  // This is very interesting.
  // If you do `let globals = {};`, globals actually has some properties inherited
  // like __defineSetter__, which makes globals['__defineSetter__'] not empty!
  // Check last test in spec/parser.uses-common-js.spec.js
  let globals = Object.create(null);

  globalScope.through.forEach(function (ref) {
    let name = ref.identifier.name;
    // user defined the variable in global scope
    let variable = globalScope.set.get(name);
    // amdefine will be ignored in browser,
    // don't remove 'define' from the list.
    if (variable && !(name === 'define' && extract(amdefinePattern, variable.defs[0].node))) {
      return;
github elastic / timelion / node_modules / gulp-eslint / node_modules / eslint / lib / eslint.js View on Github external
throw ex;
                }
            });

            // save config so rules can access as necessary
            currentConfig = config;
            controller = new estraverse.Controller();

            ecmaFeatures = currentConfig.ecmaFeatures;
            ecmaVersion = (ecmaFeatures.blockBindings || ecmaFeatures.classes ||
                    ecmaFeatures.modules || ecmaFeatures.defaultParams ||
                    ecmaFeatures.destructuring) ? 6 : 5;


            // gather data that may be needed by the rules
            scopeManager = escope.analyze(ast, {
                ignoreEval: true,
                nodejsScope: ecmaFeatures.globalReturn,
                ecmaVersion: ecmaVersion,
                sourceType: ecmaFeatures.modules ? "module" : "script"
            });
            currentScopes = scopeManager.scopes;

            /*
             * Index the scopes by the start range of their block for efficient
             * lookup in getScope.
             */
            scopeMap = [];
            currentScopes.forEach(function(scope, index) {
                var range = scope.block.range[0];

                // Sometimes two scopes are returned for a given node. This is
github mradionov / tleaf / src / parse.js View on Github external
function parse(source) {
  var ast;
  try {
    ast = esprima.parse(source);
  } catch (err) {
    throw new UserError('Source file is not valid.', err);
  }

  var scopeManager = escope.analyze(ast);

  // global scope
  var currentScope = scopeManager.acquire(ast);

  var calls = [];

  estraverse.traverse(ast, {
    enter: function (node) {
      // find all call expressions, because all angular unit types are
      // defined as function calls: .controller(), .service(), etc
      if (node.type === 'CallExpression') {

        var calleeProp = _.get(node, 'callee.property', {});

        if (_.contains(config.units.process, calleeProp.name)) {
          // save matching node with an appropriate scope
github ThomasR / minislides / build / varNameReplacer.js View on Github external
module.exports = input => {
    // create syntax tree and scope analysis
    let ast = esprima.parse(input);
    var scopes = escope.analyze(ast).scopes;

    // Determine global identifiers.
    let globalNames = scopes.filter(scope => scope.implicit && scope.implicit.left).reduce((result, scope) => {
        let found = scope.implicit.left.map(global => global.identifier.name);
        return result.concat(found);
    }, []);
    globalNames.forEach(global => {
        if (global.length === 1 && target.indexOf(global) > -1) {
            // This is too dangerous
            throw new Error(`Cannot replace global variable "${global}"`);
        }
    });

    // get local variables (AST nodes)
    let locals = scopes.reduce((result, scope) => {
        scope.variables.filter(v => v.name !== 'arguments').forEach(variable => {
github BladeRunnerJS / brjs / brjs-sdk / build-resources / includes / sdk / jsdoc-toolkit-resources / jsdoc-toolkit / node_modules / gulp-eslint / node_modules / eslint / lib / eslint.js View on Github external
ex.message = "Error while loading rule '" + key + "': " + ex.message;
                        throw ex;
                    }

                } else {
                    throw new Error("Definition for rule '" + key + "' was not found.");
                }
            });

            // save config so rules can access as necessary
            currentConfig = config;
            currentText = text;
            controller = new estraverse.Controller();

            // gather data that may be needed by the rules
            currentScopes = escope.analyze(ast, { ignoreEval: true }).scopes;

            /*
             * Index the scopes by the start range of their block for efficient
             * lookup in getScope.
             */
            scopeMap = [];
            currentScopes.forEach(function (scope, index) {
                var range = scope.block.range[0];

                // Sometimes two scopes are returned for a given node. This is
                // handled later in a known way, so just don't overwrite here.
                if (!scopeMap[range]) {
                    scopeMap[range] = index;
                }
            });
github karthikv / tradeship / patches / escope.js View on Github external
const referencer = require("escope/lib/referencer").default;

const { visitClass, visitProperty } = referencer.prototype;

// visit decorators on classes/properties to resolve their identifiers
referencer.prototype.visitClass = function(node) {
  visitDecorators.call(this, node);
  visitClass.call(this, node);
};

referencer.prototype.visitProperty = function(node) {
  visitDecorators.call(this, node);
  visitProperty.call(this, node);
};

function visitDecorators(node) {
  if (!node.decorators) {
    return;
  }
github BladeRunnerJS / brjs / brjs-sdk / build-resources / includes / sdk / jsdoc-toolkit-resources / jsdoc-toolkit / node_modules / gulp-eslint / node_modules / eslint / lib / eslint.js View on Github external
// Don't do this for Program nodes - they have no parents
        if (parents.length) {

            // if current node is function declaration, add it to the list
            var current = controller.current();
            if (current.type === "FunctionDeclaration" || current.type === "FunctionExpression") {
                parents.push(current);
            }

            // Ascend the current node's parents
            for (var i = parents.length - 1; i >= 0; --i) {

                // The first node that requires a scope is the node that will be
                // our current node's innermost scope.
                if (escope.Scope.isScopeRequired(parents[i])) {
                    innerBlock = parents[i];
                    break;
                }
            }

            // Find and return the innermost scope
            selectedScopeIndex = scopeMap[innerBlock.range[0]];

            // Named function expressions create two nested scope objects. The
            // outer scope contains only the function expression name. We return
            // the inner scope.
            if (innerBlock.type === "FunctionExpression" && innerBlock.id && innerBlock.id.name) {
                ++selectedScopeIndex;
            }

            return currentScopes[selectedScopeIndex];

escope

ECMAScript scope analyzer

BSD-2-Clause
Latest version published 2 years ago

Package Health Score

71 / 100
Full package analysis