babel/eslint/babel-eslint-parser/acorn-to-esprima.js
2015-04-13 17:12:08 -07:00

99 lines
2.5 KiB
JavaScript

var traverse = require("babel-core").traverse;
var tt = require("babel-core").acorn.tokTypes;
var t = require("babel-core").types;
exports.toToken = function (token) {
var type = token.type;
if (type === tt.name) {
token.type = "Identifier";
} else if (type === tt.semi || type === tt.comma ||
type === tt.parenL || type === tt.parenR ||
type === tt.braceL || type === tt.braceR ||
type === tt.slash || type === tt.dot ||
type === tt.bracketL || type === tt.bracketR ||
type === tt.ellipsis || type === tt.arrow ||
type === tt.star ||
type.isAssign) {
token.type = "Punctuator";
if (!token.value) token.value = type.label;
} else if (type === tt.jsxTagStart) {
token.type = "Punctuator";
token.value = "<";
} else if (type === tt.jsxTagEnd) {
token.type = "Punctuator";
token.value = ">";
} else if (type === tt.jsxName) {
token.type = "JSXIdentifier";
} else if (type.keyword) {
token.type = "Keyword";
} else if (type === tt.num) {
token.type = "Numeric";
token.value = String(token.value);
} else if (type === tt.string) {
token.type = "String";
token.value = JSON.stringify(token.value);
}
return token;
};
exports.toAST = function (ast) {
ast.sourceType = "module";
traverse(ast, astTransformVisitor);
};
function isCompatTag(tagName) {
return tagName && /^[a-z]|\-/.test(tagName);
}
var astTransformVisitor = {
noScope: true,
exit: function (node, parent) {
if (t.isSpreadProperty(node)) {
node.type = "Property";
node.kind = "init";
node.computed = true;
node.key = node.value = node.argument;
delete node.argument;
}
if (t.isRestElement(node)) {
return node.argument;
}
// modules
if (t.isImportDeclaration(node)) {
delete node.isType;
}
if (t.isExportDeclaration(node)) {
if (t.isClassExpression(node.declaration)) {
node.declaration.type = "ClassDeclaration";
} else if (t.isFunctionExpression(node.declaration)) {
node.declaration.type = "FunctionDeclaration";
}
}
// classes
if (t.isReferencedIdentifier(node, parent, { name: "super" })) {
return t.inherits(t.thisExpression(), node);
}
// functions
if (t.isFunction(node)) {
if (node.async) node.generator = true;
delete node.async;
}
if (t.isAwaitExpression(node)) {
node.type = "YieldExpression";
node.delegate = node.all;
delete node.all;
}
}
};