flesh out eslint support
This commit is contained in:
parent
0aee3c06ec
commit
974b71bcc3
@ -1,5 +1,5 @@
|
||||
{
|
||||
"parser": "/Users/sebastian/Projects/6to5/6to5/eslint.js",
|
||||
"parser": "/Users/sebastian/Projects/babel/core/eslint.js",
|
||||
"ecmaFeatures": {
|
||||
"blockBindings": true
|
||||
},
|
||||
@ -12,7 +12,12 @@
|
||||
"key-spacing": 0,
|
||||
"no-return-assign": 0,
|
||||
"consistent-return": 0,
|
||||
"no-shadow": 0
|
||||
"no-shadow": 0,
|
||||
"no-comma-dangle": 0,
|
||||
"no-use-before-define": 0,
|
||||
"no-empty": 0,
|
||||
"new-parens": 0,
|
||||
"no-cond-assign": 0
|
||||
},
|
||||
"env": {
|
||||
"node": true
|
||||
|
||||
41
eslint.js
41
eslint.js
@ -1,6 +1,9 @@
|
||||
var traverse = require("./lib/babel/traversal");
|
||||
var Module = require("module");
|
||||
var acorn = require("acorn-babel");
|
||||
var acornToEsprima = require("./lib/babel/helpers/acorn-to-esprima");
|
||||
var traverse = require("./lib/babel/traversal");
|
||||
var extend = require("lodash/object/extend");
|
||||
var Module = require("module");
|
||||
var acorn = require("acorn-babel");
|
||||
var t = require("./lib/babel/types");
|
||||
|
||||
var hasPatched = false;
|
||||
|
||||
@ -8,27 +11,18 @@ function monkeypatch() {
|
||||
if (hasPatched) return;
|
||||
hasPatched = true;
|
||||
|
||||
var mod = new Module(require.resolve("eslint"));
|
||||
|
||||
// monkeypatch estraverse
|
||||
//var estraverse = mod.require("estraverse");
|
||||
var estraverse = require("estraverse");
|
||||
extend(estraverse.VisitorKeys, t.VISITOR_KEYS);
|
||||
|
||||
// monkeypatch escope
|
||||
var escope = mod.require("escope");
|
||||
console.log(escope);
|
||||
}
|
||||
|
||||
var tokTypes = acorn.tokTypes;
|
||||
|
||||
function toEsprimaToken(token) {
|
||||
var type = token.type;
|
||||
|
||||
if (type === tokTypes.name) {
|
||||
token.type = "Identifier";
|
||||
} else if (type === tokTypes.semi || type === tokTypes.comma || type === tokTypes.parenL || type === tokTypes.parenR || type === tokTypes.braceL || type === tokTypes.braceR) {
|
||||
token.type = "Punctuator";
|
||||
token.value = type.type;
|
||||
}
|
||||
var escope = require("eslint/node_modules/escope");
|
||||
var analyze = escope.analyze;
|
||||
escope.analyze = function (ast, opts) {
|
||||
opts.sourceType = 'module';
|
||||
opts.ecmaVersion = 6;
|
||||
return analyze.call(this, ast, opts)
|
||||
};
|
||||
}
|
||||
|
||||
exports.parse = function (code) {
|
||||
@ -46,14 +40,13 @@ exports.parse = function (code) {
|
||||
var ast = acorn.parse(code, opts);
|
||||
|
||||
// convert tokens
|
||||
ast.tokens = tokens.map(function (token) {
|
||||
return toEsprimaToken(token) || token;
|
||||
});
|
||||
ast.tokens = tokens.map(acornToEsprima.toEsprimaToken);
|
||||
|
||||
// add comments
|
||||
ast.comments = comments;
|
||||
|
||||
// transform esprima and acorn divergent nodes
|
||||
acornToEsprima.toEsprimaAST(ast);
|
||||
|
||||
return ast;
|
||||
};
|
||||
|
||||
@ -70,6 +70,7 @@
|
||||
"babel": "4.5.1",
|
||||
"browserify": "^9.0.3",
|
||||
"chai": "^2.0.0",
|
||||
"eslint": "^0.15.1",
|
||||
"esvalid": "^1.1.0",
|
||||
"istanbul": "^0.3.5",
|
||||
"matcha": "^0.6.0",
|
||||
|
||||
@ -4,14 +4,12 @@ transform.version = require("../../../package").version;
|
||||
|
||||
transform.transform = transform;
|
||||
|
||||
transform.run = function (code, opts) {
|
||||
opts ||= {};
|
||||
transform.run = function (code, opts = {}) {
|
||||
opts.sourceMap = "inline";
|
||||
return new Function(transform(code, opts).code)();
|
||||
};
|
||||
|
||||
transform.load = function (url, callback, opts, hold) {
|
||||
opts ||= {};
|
||||
transform.load = function (url, callback, opts = {}, hold) {
|
||||
opts.filename ||= url;
|
||||
|
||||
var xhr = global.ActiveXObject ? new global.ActiveXObject("Microsoft.XMLHTTP") : new global.XMLHttpRequest();
|
||||
|
||||
@ -43,8 +43,7 @@ export function transformFile(filename, opts, callback) {
|
||||
});
|
||||
}
|
||||
|
||||
export function transformFileSync(filename, opts) {
|
||||
opts ||= {};
|
||||
export function transformFileSync(filename, opts = {}) {
|
||||
opts.filename = filename;
|
||||
return transform(fs.readFileSync(filename), opts);
|
||||
}
|
||||
|
||||
@ -138,10 +138,7 @@ var hookExtensions = function (_exts) {
|
||||
|
||||
hookExtensions(util.canCompile.EXTENSIONS);
|
||||
|
||||
export default function (opts) {
|
||||
// normalize options
|
||||
opts ||= {};
|
||||
|
||||
export default function (opts = {}) {
|
||||
if (opts.only != null) onlyRegex = util.regexify(opts.only);
|
||||
if (opts.ignore != null) ignoreRegex = util.regexify(opts.ignore);
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ import t from "./types";
|
||||
|
||||
export default function (body, namespace, whitelist = []) {
|
||||
each(File.helpers, function (name) {
|
||||
if (whitelist.length && whitelist.indexOf(name) == -1) return;
|
||||
if (whitelist.length && whitelist.indexOf(name) === -1) return;
|
||||
|
||||
var key = t.identifier(t.toIdentifier(name));
|
||||
body.push(t.expressionStatement(
|
||||
|
||||
@ -11,9 +11,7 @@ import n from "./node";
|
||||
import t from "../types";
|
||||
|
||||
class CodeGenerator {
|
||||
constructor(ast, opts, code) {
|
||||
opts ||= {};
|
||||
|
||||
constructor(ast, opts = {}, code) {
|
||||
this.comments = ast.comments || [];
|
||||
this.tokens = ast.tokens || [];
|
||||
this.format = CodeGenerator.normalizeOptions(code, opts);
|
||||
@ -91,8 +89,7 @@ class CodeGenerator {
|
||||
return this.print(node, parent, opts);
|
||||
};
|
||||
|
||||
print.sequence = (nodes, opts) => {
|
||||
opts ||= {};
|
||||
print.sequence = (nodes, opts = {}) => {
|
||||
opts.statement = true;
|
||||
return this.printJoin(print, nodes, opts);
|
||||
};
|
||||
@ -101,8 +98,7 @@ class CodeGenerator {
|
||||
return this.printJoin(print, nodes, opts);
|
||||
};
|
||||
|
||||
print.list = function (items, opts) {
|
||||
opts ||= {};
|
||||
print.list = function (items, opts = {}) {
|
||||
opts.separator ||= ", ";
|
||||
print.join(items, opts);
|
||||
};
|
||||
@ -118,7 +114,7 @@ class CodeGenerator {
|
||||
return print;
|
||||
}
|
||||
|
||||
print(node, parent, opts) {
|
||||
print(node, parent, opts = {}) {
|
||||
if (!node) return "";
|
||||
|
||||
if (parent && parent._compact) {
|
||||
@ -130,8 +126,6 @@ class CodeGenerator {
|
||||
this.format.concise = true;
|
||||
}
|
||||
|
||||
opts ||= {};
|
||||
|
||||
var newline = (leading) => {
|
||||
if (!opts.statement && !n.isUserWhitespacable(node, parent)) {
|
||||
return;
|
||||
@ -197,11 +191,9 @@ class CodeGenerator {
|
||||
this.format.concise = oldConcise;
|
||||
}
|
||||
|
||||
printJoin(print, nodes, opts) {
|
||||
printJoin(print, nodes, opts = {}) {
|
||||
if (!nodes || !nodes.length) return;
|
||||
|
||||
opts ||= {};
|
||||
|
||||
var len = nodes.length;
|
||||
|
||||
if (opts.indent) this.indent();
|
||||
|
||||
@ -3,9 +3,7 @@ import each from "lodash/collection/each";
|
||||
import map from "lodash/collection/map";
|
||||
import t from "../../types";
|
||||
|
||||
function crawl(node, state) {
|
||||
state ||= {};
|
||||
|
||||
function crawl(node, state = {}) {
|
||||
if (t.isMemberExpression(node)) {
|
||||
crawl(node.object, state);
|
||||
if (node.computed) crawl(node.property, state);
|
||||
|
||||
49
src/babel/helpers/acorn-to-esprima.js
Normal file
49
src/babel/helpers/acorn-to-esprima.js
Normal file
@ -0,0 +1,49 @@
|
||||
import traverse from "../traversal";
|
||||
import { tokTypes } from "acorn-babel";
|
||||
import t from "../types";
|
||||
|
||||
export function toEsprimaToken(token) {
|
||||
var type = token.type;
|
||||
|
||||
if (type === tokTypes.name) {
|
||||
token.type = "Identifier";
|
||||
} else if (type === tokTypes.semi || type === tokTypes.comma || type === tokTypes.parenL || type === tokTypes.parenR || type === tokTypes.braceL || type === tokTypes.braceR) {
|
||||
token.type = "Punctuator";
|
||||
token.value = type.type;
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
export function toEsprimaAST(ast) {
|
||||
traverse(ast, astTransformVisitor);
|
||||
}
|
||||
|
||||
var astTransformVisitor = {
|
||||
noScope: true,
|
||||
enter(node) {
|
||||
if (t.isImportBatchSpecifier(node)) {
|
||||
node.type = "ImportNamespaceSpecifier";
|
||||
node.id = node.name;
|
||||
delete node.name;
|
||||
} else if (t.isFunction(node)) {
|
||||
node.defaults = [];
|
||||
|
||||
node.params = node.params.map(function (param) {
|
||||
if (t.isAssignmentPattern(param)) {
|
||||
node.defaults.push(param.right);
|
||||
return param.left;
|
||||
} else {
|
||||
node.defaults.push(null);
|
||||
return param;
|
||||
}
|
||||
});
|
||||
|
||||
if (t.isRestElement(node.params[node.params.length - 1])) {
|
||||
node.rest = node.params.pop();
|
||||
}
|
||||
} else if (t.isClassProperty(node)) {
|
||||
this.remove();
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -369,9 +369,7 @@ export default class File {
|
||||
// todo, (node, msg)
|
||||
}
|
||||
|
||||
errorWithNode(node, msg, Error) {
|
||||
Error ||= SyntaxError;
|
||||
|
||||
errorWithNode(node, msg, Error = SyntaxError) {
|
||||
var loc = node.loc.start;
|
||||
var err = new Error("Line " + loc.line + ": " + msg);
|
||||
err.loc = loc;
|
||||
|
||||
@ -49,7 +49,7 @@ var visit = function (node, name, scope) {
|
||||
selfReference: false,
|
||||
outerDeclar: scope.getBindingIdentifier(name),
|
||||
references: [],
|
||||
name: name,
|
||||
name: name
|
||||
};
|
||||
|
||||
// check to see if we have a local binding of the id we're setting inside of
|
||||
|
||||
@ -4,16 +4,16 @@ import * as messages from "../../messages";
|
||||
import t from "../../types";
|
||||
|
||||
|
||||
var isIllegalBareSuper = function (node, parent) {
|
||||
function isIllegalBareSuper(node, parent) {
|
||||
if (!isSuper(node, parent)) return false;
|
||||
if (t.isMemberExpression(parent, { computed: false })) return false;
|
||||
if (t.isCallExpression(parent, { callee: node })) return false;
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
var isSuper = function (node, parent) {
|
||||
function isSuper(node, parent) {
|
||||
return t.isIdentifier(node, { name: "super" }) && t.isReferenced(node, parent);
|
||||
};
|
||||
}
|
||||
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
|
||||
@ -141,7 +141,7 @@ var spec = function (node, parent, scope, file) {
|
||||
|
||||
var iteratorKey = scope.generateUidIdentifier("iterator");
|
||||
|
||||
var node = util.template("for-of", {
|
||||
var template = util.template("for-of", {
|
||||
ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
|
||||
ITERATOR_COMPLETION: scope.generateUidIdentifier("iteratorNormalCompletion"),
|
||||
ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
|
||||
@ -151,16 +151,16 @@ var spec = function (node, parent, scope, file) {
|
||||
BODY: null
|
||||
});
|
||||
|
||||
var loop = node[3].block.body[0];
|
||||
var loop = template[3].block.body[0];
|
||||
|
||||
//
|
||||
|
||||
scope.traverse(node, breakVisitor, {
|
||||
iteratorKey: iteratorKey,
|
||||
label: t.isLabeledStatement(parent) && parent.label.name,
|
||||
wrapReturn: function (node) {
|
||||
return t.ifStatement(t.memberExpression(iteratorKey, t.identifier("return")), node);
|
||||
},
|
||||
label: t.isLabeledStatement(parent) && parent.label.name
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
@ -168,6 +168,6 @@ var spec = function (node, parent, scope, file) {
|
||||
return {
|
||||
declar: declar,
|
||||
loop: loop,
|
||||
node: node
|
||||
node: template
|
||||
};
|
||||
};
|
||||
|
||||
@ -11,7 +11,7 @@ export function ComprehensionExpression(node, parent, scope, file) {
|
||||
return callback(node, parent, scope, file);
|
||||
}
|
||||
|
||||
var generator = function (node) {
|
||||
function generator(node) {
|
||||
var body = [];
|
||||
var container = t.functionExpression(null, [], t.blockStatement(body), true);
|
||||
container._aliasFunction = true;
|
||||
@ -21,9 +21,9 @@ var generator = function (node) {
|
||||
}));
|
||||
|
||||
return t.callExpression(container, []);
|
||||
};
|
||||
}
|
||||
|
||||
var array = function (node, parent, scope, file) {
|
||||
function array(node, parent, scope, file) {
|
||||
var uid = scope.generateUidBasedOnNode(parent, file);
|
||||
|
||||
var container = util.template("array-comprehension-container", {
|
||||
@ -50,4 +50,4 @@ var array = function (node, parent, scope, file) {
|
||||
body.push(returnStatement);
|
||||
|
||||
return container;
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user