How to use the esutils.ast function in esutils

To help you get started, we’ve selected a few esutils 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 estools / esvalid / index.js View on Github external
[].push.apply(result, fn(xs[i]));
  }
  return result;
}

// filter :: forall a. (a -> Boolean) -> [a] -> [a]
function filter(xs, predicate) {
  var filtered = [];
  for (var i = 0, l = xs.length; i < l; ++i) {
    if (predicate(xs[i])) filtered.push(xs[i]);
  }
  return filtered;
}

// isExpression :: Node -> Boolean
var isExpression = esutils.ast.isExpression;
// isStatement :: Node -> Boolean
var isStatement = esutils.ast.isStatement;
// isSourceElement :: Node -> Boolean
var isSourceElement = esutils.ast.isSourceElement;
// directives :: [Maybe Node] -> [Node]
function directives(stmts) {
  if (stmts && stmts.length > 0) {
    var s = stmts[0];
    if (s && s.type === "ExpressionStatement" && s.expression && s.expression.type === "Literal" && typeof s.expression.value === "string")
      return [s.expression.value].concat(directives([].slice.call(stmts, 1)));
  }
  return [];
}

var OBJECT_PROPERTY_KINDS = ["init", "get", "set"];
var VARIABLE_DECLARATION_KINDS = ["var", "let", "const"];
github unassert-js / unassert / index.js View on Github external
function isBodyOfIterationStatement (parentNode, key) {
    return esutils.ast.isIterationStatement(parentNode) && key === 'body';
}
github angelozerr / tern.java / core / ternjs / node_modules / tern-eslint / node_modules / eslint / lib / ast-utils.js View on Github external
)
        );
    },

    /**
     * Gets the trailing statement of a given node.
     *
     *     if (code)
     *         consequent;
     *
     * When taking this `IfStatement`, returns `consequent;` statement.
     *
     * @param {ASTNode} A node to get.
     * @returns {ASTNode|null} The trailing statement's node.
     */
    getTrailingStatement: esutils.ast.trailingStatement,

    /**
     * Finds the variable by a given name in a given scope and its upper scopes.
     *
     * @param {escope.Scope} initScope - A scope to start find.
     * @param {string} name - A variable name to find.
     * @returns {escope.Variable|null} A found variable or `null`.
     */
    getVariableByName: function(initScope, name) {
        var scope = initScope;
        while (scope) {
            var variable = scope.set.get(name);
            if (variable) {
                return variable;
            }
github graalvm / graaljs / tools / node_modules / eslint / lib / rules / utils / ast-utils.js View on Github external
)
        );
    },

    /**
     * Gets the trailing statement of a given node.
     *
     *     if (code)
     *         consequent;
     *
     * When taking this `IfStatement`, returns `consequent;` statement.
     *
     * @param {ASTNode} A node to get.
     * @returns {ASTNode|null} The trailing statement's node.
     */
    getTrailingStatement: esutils.ast.trailingStatement,

    /**
     * Finds the variable by a given name in a given scope and its upper scopes.
     *
     * @param {eslint-scope.Scope} initScope - A scope to start find.
     * @param {string} name - A variable name to find.
     * @returns {eslint-scope.Variable|null} A found variable or `null`.
     */
    getVariableByName(initScope, name) {
        let scope = initScope;

        while (scope) {
            const variable = scope.set.get(name);

            if (variable) {
                return variable;
github eslint / eslint / lib / rules / utils / ast-utils.js View on Github external
comment.indexOf("eslint-") === 0
            )
        );
    },

    /**
     * Gets the trailing statement of a given node.
     *
     *     if (code)
     *         consequent;
     *
     * When taking this `IfStatement`, returns `consequent;` statement.
     * @param {ASTNode} A node to get.
     * @returns {ASTNode|null} The trailing statement's node.
     */
    getTrailingStatement: esutils.ast.trailingStatement,

    /**
     * Finds the variable by a given name in a given scope and its upper scopes.
     * @param {eslint-scope.Scope} initScope A scope to start find.
     * @param {string} name A variable name to find.
     * @returns {eslint-scope.Variable|null} A found variable or `null`.
     */
    getVariableByName(initScope, name) {
        let scope = initScope;

        while (scope) {
            const variable = scope.set.get(name);

            if (variable) {
                return variable;
            }
github estools / esvalid / index.js View on Github external
if (node.name == null)
          errors.push(new E(node, "Identifier `name` member must be non-null"));
        else if (!esutils.keyword.isIdentifierName(node.name))
          errors.push(new E(node, "Identifier `name` member must be a valid IdentifierName"));
        else if (esutils.keyword.isReservedWordES5(node.name, state.strict))
          errors.push(new E(node, "Identifier `name` member must not be a ReservedWord"));
        break;

      case "IfStatement":
        if (!isExpression(node.test))
          errors.push(new E(node, "IfStatement `test` member must be an expression node"));
        if (!isStatement(node.consequent))
          errors.push(new E(node, "IfStatement `consequent` member must be a statement node"));
        if (node.alternate != null && !isStatement(node.alternate))
          errors.push(new E(node, "IfStatement `alternate` member must be a statement node or null"));
        if (node.alternate != null && node.consequent != null && esutils.ast.isProblematicIfStatement(node))
          errors.push(new E(node, "IfStatement with null `alternate` must not be the `consequent` of an IfStatement with a non-null `alternate`"));
        if (node.test != null)
          [].push.apply(errors, recurse(node.test));
        if (node.consequent != null)
          [].push.apply(errors, recurse(node.consequent));
        if (node.alternate != null)
          [].push.apply(errors, recurse(node.alternate));
        break;

      case "LabeledStatement":
        if (node.label == null) {
          errors.push(new E(node, "LabeledStatement `label` member must be an Identifier node"));
        } else {
          if (node.label.type !== "Identifier")
            errors.push(new E(node, "LabeledStatement `label` member must be an Identifier node"));
          else if (state.labels.indexOf(node.label.name) >= 0)