From c3e0152daa3081b3c567008b6fb7b379fdc8be3e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 22 Aug 2019 14:38:27 +0200 Subject: [PATCH 1/2] feat: if code frame error is on a single line, highlight the whole path --- packages/babel-core/src/transformation/file/file.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/babel-core/src/transformation/file/file.js b/packages/babel-core/src/transformation/file/file.js index a0a9f65bcdf4..d07dead742dd 100644 --- a/packages/babel-core/src/transformation/file/file.js +++ b/packages/babel-core/src/transformation/file/file.js @@ -287,6 +287,13 @@ export default class File { line: loc.start.line, column: loc.start.column + 1, }, + end: + loc.start.line === loc.end.line + ? { + line: loc.end.line, + column: loc.end.column + 1, + } + : undefined, }, { highlightCode }, ); From 509e952b506ba2614c9c6604e73658d59f8cba78 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 22 Aug 2019 15:07:45 +0200 Subject: [PATCH 2/2] flow --- .../babel-core/src/transformation/file/file.js | 18 +++++++++++++----- .../src/transformation/plugin-pass.js | 10 ++-------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/babel-core/src/transformation/file/file.js b/packages/babel-core/src/transformation/file/file.js index d07dead742dd..db788ac51e69 100644 --- a/packages/babel-core/src/transformation/file/file.js +++ b/packages/babel-core/src/transformation/file/file.js @@ -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 = new Map(); opts: Object; @@ -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 { @@ -288,7 +296,7 @@ export default class File { column: loc.start.column + 1, }, end: - loc.start.line === loc.end.line + loc.end && loc.start.line === loc.end.line ? { line: loc.end.line, column: loc.end.column + 1, diff --git a/packages/babel-core/src/transformation/plugin-pass.js b/packages/babel-core/src/transformation/plugin-pass.js index 6e62c42bc786..e11f97804745 100644 --- a/packages/babel-core/src/transformation/plugin-pass.js +++ b/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 = new Map(); @@ -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); } }