From 85f0f6fb14f043f00a12a4a8bdbce5e906ecc44c Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 3 Nov 2014 11:09:58 +1100 Subject: [PATCH] add whitespace option and move util.errorWithNode to File --- lib/6to5/file.js | 29 +++++++++++++++++++++++------ lib/6to5/util.js | 20 ++------------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 49b75abd10..de9a22aacf 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -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) { diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 9e80919bc4..7c35633cf1 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -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 });