Skip to content

Commit

Permalink
feat: if code frame error is on a single line, highlight the w… (#10361)
Browse files Browse the repository at this point in the history
* feat: if code frame error is on a single line, highlight the whole path

* flow
  • Loading branch information
SimenB authored and nicolo-ribaudo committed Oct 29, 2019
1 parent b6ef968 commit f1bc6c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
23 changes: 19 additions & 4 deletions packages/babel-core/src/transformation/file/file.js
Expand Up @@ -19,6 +19,17 @@ const errorVisitor = {
},
};

export type NodeLocation = {
loc?: {
end?: { line: number, column: number },
start: { line: number, column: number },
},
_loc?: {
end?: { line: number, column: number },
start: { line: number, column: number },
},
};

export default class File {
_map: Map<any, any> = new Map();
opts: Object;
Expand Down Expand Up @@ -250,10 +261,7 @@ export default class File {
}

buildCodeFrameError(
node: ?{
loc?: { start: { line: number, column: number } },
_loc?: { start: { line: number, column: number } },
},
node: ?NodeLocation,
msg: string,
Error: typeof Error = SyntaxError,
): Error {
Expand Down Expand Up @@ -285,6 +293,13 @@ export default class File {
line: loc.start.line,
column: loc.start.column + 1,
},
end:
loc.end && loc.start.line === loc.end.line
? {
line: loc.end.line,
column: loc.end.column + 1,
}
: undefined,
},
{ highlightCode },
);
Expand Down
10 changes: 2 additions & 8 deletions packages/babel-core/src/transformation/plugin-pass.js
@@ -1,6 +1,7 @@
// @flow

import type File from "./file/file";
import type NodeLocation from "./file/file";

export default class PluginPass {
_map: Map<mixed, mixed> = new Map();
Expand Down Expand Up @@ -47,14 +48,7 @@ export default class PluginPass {
return this.file.getModuleName();
}

buildCodeFrameError(
node: ?{
loc?: { start: { line: number, column: number } },
_loc?: { start: { line: number, column: number } },
},
msg: string,
Error?: typeof Error,
) {
buildCodeFrameError(node: ?NodeLocation, msg: string, Error?: typeof Error) {
return this.file.buildCodeFrameError(node, msg, Error);
}
}

0 comments on commit f1bc6c4

Please sign in to comment.