rename errorWithNode to buildCodeFrameError and when erroring on dynamic nodes try and estimate a location

This commit is contained in:
Sebastian McKenzie
2015-09-15 06:31:51 +01:00
parent c00bf262a7
commit dff098e77b
3 changed files with 27 additions and 9 deletions

View File

@@ -20,6 +20,15 @@ import * as util from "../../util";
import path from "path"; import path from "path";
import * as t from "babel-types"; import * as t from "babel-types";
var errorVisitor = {
enter(path, state) {
var loc = path.node.loc;
if (loc) {
state.loc = loc;
path.stop();
}
}
};
export default class File { export default class File {
constructor(opts = {}, pipeline) { constructor(opts = {}, pipeline) {
this.pipeline = pipeline; this.pipeline = pipeline;
@@ -383,17 +392,26 @@ export default class File {
return uid; return uid;
} }
errorWithNode(node, msg, Error = SyntaxError) { buildCodeFrameError(node, msg, Error = SyntaxError) {
var err;
var loc = node && (node.loc || node._loc); var loc = node && (node.loc || node._loc);
var err = new Error(msg);
if (loc) { if (loc) {
err = new Error(`Line ${loc.start.line}: ${msg}`);
err.loc = loc.start; err.loc = loc.start;
} else { } else {
// todo: find errors with nodes inside to at least point to something traverse(node, errorVisitor, err);
err = new Error("There's been an error on a dynamic node. This is almost certainly an internal error. Please report it.");
err.message += " (This is an error on an internal node. Probably an internal error";
if (err.loc) {
err.message += ". Location has been estimated.";
}
err.message += ")";
} }
return err;
return err
} }
mergeSourceMap(map: Object) { mergeSourceMap(map: Object) {

View File

@@ -28,7 +28,7 @@ export function push(mutatorMap, node, kind, file) {
} }
if (map.value || map.initializer) { if (map.value || map.initializer) {
throw file.errorWithNode(node, "Key conflict with sibling node"); throw file.buildCodeFrameError(node, "Key conflict with sibling node");
} }
if (node.value) { if (node.value) {

View File

@@ -37,7 +37,7 @@ export default class DefaultFormatter {
var existingScope = this.sourceScopes[source]; var existingScope = this.sourceScopes[source];
if (existingScope && existingScope !== path.scope) { if (existingScope && existingScope !== path.scope) {
throw path.errorWithNode(messages.get("modulesDuplicateDeclarations")); throw path.buildCodeFrameError(messages.get("modulesDuplicateDeclarations"));
} }
this.sourceScopes[source] = path.scope; this.sourceScopes[source] = path.scope;
@@ -180,7 +180,7 @@ export default class DefaultFormatter {
checkExportIdentifier(node) { checkExportIdentifier(node) {
if (t.isIdentifier(node, { name: "__esModule" })) { if (t.isIdentifier(node, { name: "__esModule" })) {
throw this.file.errorWithNode(node, messages.get("modulesIllegalExportName", node.name)); throw this.file.buildCodeFrameError(node, messages.get("modulesIllegalExportName", node.name));
} }
} }