How to use the esprima.parse function in esprima

To help you get started, we’ve selected a few esprima 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 thomasboyt / defeatureify / index.js View on Github external
var featureify = function(source, config) {
  config = config || {};
  enabled = config.enabled || {};

  var namespace = config.namespace || "Ember";

  var tree = esprima.parse(source, {
    range: true
  });

  var sourceModifier = new SourceModifier(source);
  var walk = function(node) {
    if (node.type === "IfStatement") {
      if (node.test.type === "CallExpression" && node.test.callee) {
        // test object.property.property
        if (node.test.callee.object &&
            node.test.callee.object.object &&
            node.test.callee.object.property) {
          // test namespace.FEATURES.isEnabled()
          if (node.test.callee.object.object.name === namespace &&
              node.test.callee.object.property.name === "FEATURES" &&
              node.test.callee.property.name === "isEnabled") {
            var featureName = node.test.arguments[0].value;
github jaredly / treed / plugins / ijs / eval.js View on Github external
module.exports = function (text, output, scope) {

  var tree = esprima.parse(text);
  var tracker = newScope()

  crawlScope(tree, function (node, scope, parent, param, path) {
    var fn = crawls[node.type]
    if (fn) fn(node, scope, parent, param, path)
  }, tracker, newScope);

  var globals = Object.getOwnPropertyNames(window)
    .concat(['arguments', 'this', 'setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']);

  // fixScope(tracker, tree, globals)

  catchOutputs(tree, '$out')

  var code = escodegen.generate(tree)
  code += ';' + tracker.declared.map(function (name) {
github jscs-dev / node-jscs / lib / modules / string-checker.js View on Github external
checkString: function(str, filename) {
        filename = filename || 'input';
        var tree;
        str = str.replace(/^#![^\n]+\n/, '\n');
        try {
            tree = esprima.parse(str, {loc: true, range: true, comment: true, tokens: true});
        } catch (e) {
            throw new Error('Syntax error at ' + filename + ': ' + e.message);
        }
        var file = new JsFile(filename, str, tree);
        var errors = new Errors(file);
        this._activeRules.forEach(function(rule) {

            // Do not process the rule if it's equals to null (#203)
            if (this._config[rule.getOptionName()] !== null ) {
                rule.check(file, errors);
            }

        }, this);

        // sort errors list to show errors as they appear in source
        errors.getErrorList().sort(function(a, b) {
github probmods / webppl / src / transforms / adify.js View on Github external
function addAdRequire(ast, adRequirePath) {
  var body = ast.body;
  var useStrictNode = body[0];
  assert.ok(isUseStrictExpr(useStrictNode));
  var requireNode = parse("var ad = require('" + adRequirePath + "');").body[0];
  var rest = body.slice(1);
  // Remove any existing calls to require ad, to avoid duplication.
  var restFiltered = rest.filter(function(node) {
    return !_.isEqual(node, requireNode);
  });
  return build.program([useStrictNode, requireNode].concat(restFiltered));
}
github amilajack / eslint-plugin-compat / test / caniuse / caniuse.spec.js View on Github external
describe('CanIUseProvider', () => {
  for (const test of tests) {
    const MemberExpression = new SourceCode(test.code, esprima.parse(test.code, {
      tokens: true,
      loc: true,
      comment: true,
      range: true,
      tolerant: true,
      attachComment: true
    }));

    const isValid = DetermineCompat(MemberExpression.ast.body[0].expression);

    it(test.name || test.id, () => {
      expect(isValid).to.equal(test.pass);
    });
  }
});
github benjamn / ast-types / test / ecmascript.ts View on Github external
function check(src1: any, src2: any) {
      astNodesAreEquivalent.assert(parse(src1), parse(src2));
    }
github ForbesLindesay / interpret / lib / evaluators / calls.js View on Github external
return options.go(evaluate(node.arguments[0], scope, options), function (arg) {
      var node
      try {
        node = esprima.parse(arg)
      } catch (ex) {
        var SyntaxError = scope.get('SyntaxError')
        throw new SyntaxError(ex.message)
      }
      return evaluate(node, scope, options)
    })
  } else if (node.callee.type === 'MemberExpression') {
github estools / esmangle / bin / esmangle.js View on Github external
function compile(content, filename) {
        var tree, licenses, comments, formatOption, preserveLicenseComment, propagateLicenseComment;

        preserveLicenseComment = argv.preserveLicenseComment;
        propagateLicenseComment = argv.propagateLicenseCommentToHeader;

        tree = esprima.parse(content, {
            loc: true,
            range: true,
            raw: true,
            tokens: true,
            comment: preserveLicenseComment || propagateLicenseComment
        });

        comments = concatSingleLineComments(content, tree.comments || []);

        if (preserveLicenseComment || propagateLicenseComment) {
            licenses = comments.reduce(function (results, commentBlock) {
                if (!commentBlock.isLicense()) {
                    return results;
                }
                return results.concat(commentBlock.comments);
            }, []);
github getgauge / gauge-js / src / static-loader.js View on Github external
function createAst(content) {
  try {
    return esprima.parse(content, { loc: true });
  } catch (e) {
    logger.error(e.message);
    return "";
  }
}
github yields / instrument / index.js View on Github external
function parse(src){
  return esprima.parse(src, {
    range: true
  });
};