acorn-to-esprima

Convert acorn tokens to esprima

MIT 15 个版本
安装
npm install acorn-to-esprima
yarn add acorn-to-esprima
pnpm add acorn-to-esprima
bun add acorn-to-esprima
README

acorn-to-esprima

Some functions to help transform an acorn/babel ast to esprima format.

Primarily for use in babel-eslint, babel-jscs, and ast explorer

There are no dependencies (the methods were changed to pass in dependencies instead)

The current functions exposed are:

  • function attachComments(ast, comments, tokens)
    • This modifies the comments passed in.
  • function toTokens(tokens, tt)
    • tt is require("babel-core").acorn.tokTypes
    • Converts template string tokens (convertTemplateType)
    • filters out comment tokens
    • runs toToken over each token
  • function toToken(token, tt)
    • Sets token.type, token.range, and token.value
  • function toAST(ast, traverse)
    • traverse is require("babel-core").traverse;
    • traverses over the ast and makes any necessary changes (usually es6+)
  • function convertComments(comments)
    • Modifies comment.type

How to use:

Check out the parse method of https://github.com/babel/babel-eslint/blob/master/index.js

// example
exports.parse = function (code) {
  var comments = opts.onComment = [];
  var tokens = opts.onToken = [];

  var ast;
  try {
    ast = parse(code, {
        locations: true,
        ranges: true
    });
  } catch (err) { throw err; }

  tokens.pop();
  ast.tokens = acornToEsprima.toTokens(tokens, tt);

  acornToEsprima.convertComments(comments);
  ast.comments = comments;
  acornToEsprima.attachComments(ast, comments, ast.tokens);

  acornToEsprima.toAST(ast, traverse);

  return ast;
}
版本列表
2.0.8 2016-01-30
2.0.7 2016-01-30
2.0.6 2015-12-29
2.0.4 2015-12-09
2.0.3 2015-12-09
2.0.2 2015-12-04
2.0.1 2015-11-28
2.0.0 2015-11-26
1.0.7 2015-11-17
1.0.6 2015-11-14
1.0.5 2015-11-01
1.0.4 2015-09-14
1.0.3 2015-09-14
1.0.2 2015-08-31
1.0.1 2015-08-25