Remove wrap function and calculate code frames earlier.
This commit is contained in:
parent
4a8137c6b4
commit
c1df126b83
@ -63,7 +63,7 @@ export function compile(filename, opts) {
|
||||
return babel.transformFileSync(filename, opts);
|
||||
} catch (err) {
|
||||
if (commander.watch) {
|
||||
console.error(toErrorStack(err));
|
||||
console.error(err);
|
||||
return { ignored: true };
|
||||
} else {
|
||||
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) {
|
||||
console.error(toErrorStack(err));
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
|
||||
@ -212,26 +212,38 @@ export default class File {
|
||||
msg: string,
|
||||
Error: typeof Error = SyntaxError,
|
||||
): 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) {
|
||||
err.loc = loc.start;
|
||||
} else {
|
||||
traverse(node, errorVisitor, this.scope, err);
|
||||
if (!loc && node) {
|
||||
const state = {
|
||||
loc: null,
|
||||
};
|
||||
traverse(node, errorVisitor, this.scope, state);
|
||||
loc = state.loc;
|
||||
|
||||
err.message +=
|
||||
" (This is an error on an internal node. Probably an internal error";
|
||||
let txt =
|
||||
"This is an error on an internal node. Probably an internal error.";
|
||||
if (loc) txt += " Location has been estimated.";
|
||||
|
||||
if (err.loc) {
|
||||
err.message += ". Location has been estimated.";
|
||||
msg += ` (${txt})`;
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -302,7 +314,28 @@ export default class File {
|
||||
}
|
||||
|
||||
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");
|
||||
return ast;
|
||||
}
|
||||
@ -365,47 +398,6 @@ export default class File {
|
||||
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) {
|
||||
code = (code || "") + "";
|
||||
code = this.parseInputSourceMap(code);
|
||||
|
||||
@ -10,11 +10,9 @@ export function transform(code: string, opts?: Object): BabelFileResult {
|
||||
if (config === null) return null;
|
||||
|
||||
const file = new File(config);
|
||||
return file.wrap(code, function() {
|
||||
file.addCode(code);
|
||||
file.parseCode(code);
|
||||
return file.transform();
|
||||
});
|
||||
}
|
||||
|
||||
export function transformFromAst(
|
||||
@ -32,11 +30,9 @@ export function transformFromAst(
|
||||
}
|
||||
|
||||
const file = new File(config);
|
||||
return file.wrap(code, function() {
|
||||
file.addCode(code);
|
||||
file.addAst(ast);
|
||||
return file.transform();
|
||||
});
|
||||
}
|
||||
|
||||
export function transformFile(
|
||||
@ -59,11 +55,9 @@ export function transformFile(
|
||||
if (!err) {
|
||||
try {
|
||||
const file = new File(config);
|
||||
result = file.wrap(code, function() {
|
||||
file.addCode(code);
|
||||
file.parseCode(code);
|
||||
return file.transform();
|
||||
});
|
||||
result = file.transform();
|
||||
} catch (_err) {
|
||||
err = _err;
|
||||
}
|
||||
@ -88,9 +82,7 @@ export function transformFileSync(
|
||||
const code = fs.readFileSync(filename, "utf8");
|
||||
const file = new File(config);
|
||||
|
||||
return file.wrap(code, function() {
|
||||
file.addCode(code);
|
||||
file.parseCode(code);
|
||||
return file.transform();
|
||||
});
|
||||
}
|
||||
|
||||
@ -176,8 +176,9 @@ function run(task) {
|
||||
try {
|
||||
resultExec = runCodeInTestContext(execCode, execOpts);
|
||||
} catch (err) {
|
||||
err.message = exec.loc + ": " + err.message;
|
||||
err.message += codeFrameColumns(execCode, exec.loc);
|
||||
// Pass empty location to include the whole file in the output.
|
||||
err.message =
|
||||
`${exec.loc}: ${err.message}\n` + codeFrameColumns(execCode, {});
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-template",
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"babel-code-frame": "7.0.0-beta.2",
|
||||
"babel-traverse": "7.0.0-beta.2",
|
||||
"babel-types": "7.0.0-beta.2",
|
||||
"babylon": "7.0.0-beta.27",
|
||||
|
||||
@ -2,6 +2,7 @@ import cloneDeep from "lodash/cloneDeep";
|
||||
import has from "lodash/has";
|
||||
import traverse from "babel-traverse";
|
||||
import * as babylon from "babylon";
|
||||
import { codeFrameColumns } from "babel-code-frame";
|
||||
|
||||
const FROM_TEMPLATE = new Set();
|
||||
|
||||
@ -42,7 +43,12 @@ export default function(code: string, opts?: Object): Function {
|
||||
preserveComments: opts.preserveComments,
|
||||
});
|
||||
} 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;
|
||||
}
|
||||
|
||||
|
||||
@ -77,14 +77,18 @@ export function replaceWithSourceString(replacement) {
|
||||
} catch (err) {
|
||||
const loc = err.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: {
|
||||
line: loc.line,
|
||||
column: loc.column + 1,
|
||||
},
|
||||
};
|
||||
err.message += " - make sure this is an expression.";
|
||||
err.message += "\n" + codeFrameColumns(replacement, location);
|
||||
});
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user