add whitespace option and move util.errorWithNode to File
This commit is contained in:
parent
bf61c1e85d
commit
85f0f6fb14
@ -4,6 +4,7 @@ var SHEBANG_REGEX = /^\#\!.*/;
|
||||
|
||||
var transform = require("./transform");
|
||||
var generate = require("./generator");
|
||||
var acorn = require("acorn-6to5");
|
||||
var util = require("./util");
|
||||
var b = require("./builders");
|
||||
var _ = require("lodash");
|
||||
@ -21,11 +22,12 @@ File.normaliseOptions = function (opts) {
|
||||
opts = opts || {};
|
||||
|
||||
_.defaults(opts, {
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
sourceMap: false,
|
||||
filename: "unknown",
|
||||
modules: "common"
|
||||
whitespace: true,
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
sourceMap: false,
|
||||
filename: "unknown",
|
||||
modules: "common"
|
||||
});
|
||||
|
||||
_.defaults(opts, {
|
||||
@ -78,10 +80,25 @@ File.prototype.addDeclaration = function (name) {
|
||||
return uid;
|
||||
};
|
||||
|
||||
File.prototype.errorWithNode = function (node, msg) {
|
||||
var loc
|
||||
|
||||
if (node.loc) {
|
||||
loc = node.loc.start;
|
||||
} else {
|
||||
loc = acorn.getLineInfo(this.code, node.start);
|
||||
}
|
||||
|
||||
var err = new SyntaxError("Line " + loc.line + ": " + msg);
|
||||
err.lineNumber = loc.line;
|
||||
err.column = loc.column;
|
||||
return err;
|
||||
};
|
||||
|
||||
File.prototype.parse = function (code) {
|
||||
var self = this;
|
||||
this.code = code;
|
||||
|
||||
this.code = code;
|
||||
code = this.parseShebang(code);
|
||||
|
||||
return util.parse(this.opts, code, function (tree) {
|
||||
|
||||
@ -73,16 +73,6 @@ exports.getIds = function (node) {
|
||||
return ids;
|
||||
};
|
||||
|
||||
exports.errorWithNode = function (node, msg) {
|
||||
var line = node.loc.start.line;
|
||||
var col = node.loc.start.column;
|
||||
|
||||
var err = new SyntaxError("Line " + line + ": " + msg);
|
||||
err.lineNumber = line;
|
||||
err.column = col;
|
||||
return err;
|
||||
};
|
||||
|
||||
exports.canCompile = function (filename) {
|
||||
var ext = path.extname(filename);
|
||||
return _.contains([".js", ".es6"], ext);
|
||||
@ -202,20 +192,14 @@ exports.repeat = function (width, cha) {
|
||||
|
||||
exports.parse = function (opts, code, callback) {
|
||||
try {
|
||||
var recastOpts = {};
|
||||
|
||||
if (opts.sourceMap) {
|
||||
recastOpts.sourceFileName = opts.sourceFileName;
|
||||
recastOpts.sourceRoot = opts.sourceRoot;
|
||||
}
|
||||
|
||||
var comments = [];
|
||||
var tokens = [];
|
||||
|
||||
var ast = acorn.parse(code, {
|
||||
ecmaVersion: Infinity,
|
||||
strictMode: true,
|
||||
onComment: comments,
|
||||
locations: true,
|
||||
locations: opts.sourceMap,
|
||||
onToken: tokens,
|
||||
ranges: true
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user