Remove wrap function and calculate code frames earlier.

This commit is contained in:
Logan Smyth 2017-10-01 14:23:32 -07:00
parent 4a8137c6b4
commit c1df126b83
7 changed files with 85 additions and 97 deletions

View File

@ -63,7 +63,7 @@ export function compile(filename, opts) {
return babel.transformFileSync(filename, opts); return babel.transformFileSync(filename, opts);
} catch (err) { } catch (err) {
if (commander.watch) { if (commander.watch) {
console.error(toErrorStack(err)); console.error(err);
return { ignored: true }; return { ignored: true };
} else { } else {
throw err; throw err;
@ -87,16 +87,8 @@ export function deleteDir(path) {
} }
} }
function toErrorStack(err) {
if (err._babel && err instanceof SyntaxError) {
return `${err.name}: ${err.message}\n${err.codeFrame}`;
} else {
return err.stack;
}
}
process.on("uncaughtException", function(err) { process.on("uncaughtException", function(err) {
console.error(toErrorStack(err)); console.error(err);
process.exit(1); process.exit(1);
}); });

View File

@ -212,26 +212,38 @@ export default class File {
msg: string, msg: string,
Error: typeof Error = SyntaxError, Error: typeof Error = SyntaxError,
): Error { ): Error {
const loc = node && (node.loc || node._loc); let loc = node && (node.loc || node._loc);
const err = new Error(msg); msg = `${this.opts.filename}: ${msg}`;
if (loc) { if (!loc && node) {
err.loc = loc.start; const state = {
} else { loc: null,
traverse(node, errorVisitor, this.scope, err); };
traverse(node, errorVisitor, this.scope, state);
loc = state.loc;
err.message += let txt =
" (This is an error on an internal node. Probably an internal error"; "This is an error on an internal node. Probably an internal error.";
if (loc) txt += " Location has been estimated.";
if (err.loc) { msg += ` (${txt})`;
err.message += ". Location has been estimated.";
} }
err.message += ")"; msg +=
} "\n" +
codeFrameColumns(
this.code,
{
start: {
line: loc.line,
column: loc.column + 1,
},
},
this.opts,
);
return err; return new Error(msg);
} }
mergeSourceMap(map: Object) { mergeSourceMap(map: Object) {
@ -302,7 +314,28 @@ export default class File {
} }
debug(this.opts, "Parse start"); debug(this.opts, "Parse start");
const ast = parseCode(code, parserOpts || this.parserOpts); let ast;
try {
ast = parseCode(code, parserOpts || this.parserOpts);
} catch (err) {
const loc = err.loc;
if (loc) {
err.loc = null;
err.message =
`${this.opts.filename}: ${err.message}\n` +
codeFrameColumns(
this.code,
{
start: {
line: loc.line,
column: loc.column + 1,
},
},
this.opts,
);
}
throw err;
}
debug(this.opts, "Parse stop"); debug(this.opts, "Parse stop");
return ast; return ast;
} }
@ -365,47 +398,6 @@ export default class File {
return this.generate(); return this.generate();
} }
wrap(code: string, callback: Function): BabelFileResult {
code = code + "";
try {
return callback();
} catch (err) {
if (err._babel) {
throw err;
} else {
err._babel = true;
}
let message = (err.message = `${this.opts.filename}: ${err.message}`);
const loc = err.loc;
if (loc) {
const location = {
start: {
line: loc.line,
column: loc.column + 1,
},
};
err.codeFrame = codeFrameColumns(code, location, this.opts);
message += "\n" + err.codeFrame;
}
if (process.browser) {
// chrome has it's own pretty stringifier which doesn't use the stack property
// https://github.com/babel/babel/issues/2175
err.message = message;
}
if (err.stack) {
const newStack = err.stack.replace(err.message, message);
err.stack = newStack;
}
throw err;
}
}
addCode(code: string) { addCode(code: string) {
code = (code || "") + ""; code = (code || "") + "";
code = this.parseInputSourceMap(code); code = this.parseInputSourceMap(code);

View File

@ -10,11 +10,9 @@ export function transform(code: string, opts?: Object): BabelFileResult {
if (config === null) return null; if (config === null) return null;
const file = new File(config); const file = new File(config);
return file.wrap(code, function() {
file.addCode(code); file.addCode(code);
file.parseCode(code); file.parseCode(code);
return file.transform(); return file.transform();
});
} }
export function transformFromAst( export function transformFromAst(
@ -32,11 +30,9 @@ export function transformFromAst(
} }
const file = new File(config); const file = new File(config);
return file.wrap(code, function() {
file.addCode(code); file.addCode(code);
file.addAst(ast); file.addAst(ast);
return file.transform(); return file.transform();
});
} }
export function transformFile( export function transformFile(
@ -59,11 +55,9 @@ export function transformFile(
if (!err) { if (!err) {
try { try {
const file = new File(config); const file = new File(config);
result = file.wrap(code, function() {
file.addCode(code); file.addCode(code);
file.parseCode(code); file.parseCode(code);
return file.transform(); result = file.transform();
});
} catch (_err) { } catch (_err) {
err = _err; err = _err;
} }
@ -88,9 +82,7 @@ export function transformFileSync(
const code = fs.readFileSync(filename, "utf8"); const code = fs.readFileSync(filename, "utf8");
const file = new File(config); const file = new File(config);
return file.wrap(code, function() {
file.addCode(code); file.addCode(code);
file.parseCode(code); file.parseCode(code);
return file.transform(); return file.transform();
});
} }

View File

@ -176,8 +176,9 @@ function run(task) {
try { try {
resultExec = runCodeInTestContext(execCode, execOpts); resultExec = runCodeInTestContext(execCode, execOpts);
} catch (err) { } catch (err) {
err.message = exec.loc + ": " + err.message; // Pass empty location to include the whole file in the output.
err.message += codeFrameColumns(execCode, exec.loc); err.message =
`${exec.loc}: ${err.message}\n` + codeFrameColumns(execCode, {});
throw err; throw err;
} }
} }

View File

@ -8,6 +8,7 @@
"repository": "https://github.com/babel/babel/tree/master/packages/babel-template", "repository": "https://github.com/babel/babel/tree/master/packages/babel-template",
"main": "lib/index.js", "main": "lib/index.js",
"dependencies": { "dependencies": {
"babel-code-frame": "7.0.0-beta.2",
"babel-traverse": "7.0.0-beta.2", "babel-traverse": "7.0.0-beta.2",
"babel-types": "7.0.0-beta.2", "babel-types": "7.0.0-beta.2",
"babylon": "7.0.0-beta.27", "babylon": "7.0.0-beta.27",

View File

@ -2,6 +2,7 @@ import cloneDeep from "lodash/cloneDeep";
import has from "lodash/has"; import has from "lodash/has";
import traverse from "babel-traverse"; import traverse from "babel-traverse";
import * as babylon from "babylon"; import * as babylon from "babylon";
import { codeFrameColumns } from "babel-code-frame";
const FROM_TEMPLATE = new Set(); const FROM_TEMPLATE = new Set();
@ -42,7 +43,12 @@ export default function(code: string, opts?: Object): Function {
preserveComments: opts.preserveComments, preserveComments: opts.preserveComments,
}); });
} catch (err) { } catch (err) {
err.stack = `${err.stack}from\n${stack}`; const loc = err.loc;
if (loc) {
err.loc = null;
err.message += "\n" + codeFrameColumns(code, { start: loc });
}
err.stack = `${err.stack}\n ==========================\n${stack}`;
throw err; throw err;
} }

View File

@ -77,14 +77,18 @@ export function replaceWithSourceString(replacement) {
} catch (err) { } catch (err) {
const loc = err.loc; const loc = err.loc;
if (loc) { if (loc) {
const location = { // Set the location to null or else the re-thrown exception could
// incorrectly interpret the location as referencing the file being
// transformed.
err.loc = null;
err.message +=
" - make sure this is an expression.\n" +
codeFrameColumns(replacement, {
start: { start: {
line: loc.line, line: loc.line,
column: loc.column + 1, column: loc.column + 1,
}, },
}; });
err.message += " - make sure this is an expression.";
err.message += "\n" + codeFrameColumns(replacement, location);
} }
throw err; throw err;
} }