From f1bc6c4e18dbff8aad8e0b53d80c787c9dc9870c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 29 Oct 2019 18:23:54 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20if=20code=20frame=20error=20is=20on=20a?= =?UTF-8?q?=20single=20line,=20highlight=20the=20w=E2=80=A6=20(#10361)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: if code frame error is on a single line, highlight the whole path * flow --- .../src/transformation/file/file.js | 23 +++++++++++++++---- .../src/transformation/plugin-pass.js | 10 ++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/babel-core/src/transformation/file/file.js b/packages/babel-core/src/transformation/file/file.js index 1aa6070c73c2..ed86452c64ff 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 { @@ -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 }, ); 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); } }