From c3f6f7fe2847f9bc4e771421a3f401237794ae78 Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Wed, 3 Jan 2018 02:38:04 +0100 Subject: [PATCH] fix buildCodeFrameError empty code frames --- .../babel-core/src/transformation/file/file.js | 8 ++++---- .../babel-core/src/transformation/plugin-pass.js | 4 ++-- .../plugins/build-code-frame-error/exec.js | 16 ++++++++++++++++ .../plugins/build-code-frame-error/options.json | 3 +++ 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 packages/babel-core/test/fixtures/plugins/build-code-frame-error/exec.js create mode 100644 packages/babel-core/test/fixtures/plugins/build-code-frame-error/options.json diff --git a/packages/babel-core/src/transformation/file/file.js b/packages/babel-core/src/transformation/file/file.js index 71186978a9..044c6eb85d 100644 --- a/packages/babel-core/src/transformation/file/file.js +++ b/packages/babel-core/src/transformation/file/file.js @@ -179,8 +179,8 @@ export default class File { buildCodeFrameError( node: ?{ - loc?: { line: number, column: number }, - _loc?: { line: number, column: number }, + loc?: { start: { line: number, column: number } }, + _loc?: { start: { line: number, column: number } }, }, msg: string, Error: typeof Error = SyntaxError, @@ -212,8 +212,8 @@ export default class File { this.code, { start: { - line: loc.line, - column: loc.column + 1, + line: loc.start.line, + column: loc.start.column + 1, }, }, { highlightCode }, diff --git a/packages/babel-core/src/transformation/plugin-pass.js b/packages/babel-core/src/transformation/plugin-pass.js index c526d4256b..bd44495cfe 100644 --- a/packages/babel-core/src/transformation/plugin-pass.js +++ b/packages/babel-core/src/transformation/plugin-pass.js @@ -39,8 +39,8 @@ export default class PluginPass { buildCodeFrameError( node: ?{ - loc?: { line: number, column: number }, - _loc?: { line: number, column: number }, + loc?: { start: { line: number, column: number } }, + _loc?: { start: { line: number, column: number } }, }, msg: string, Error?: typeof Error, diff --git a/packages/babel-core/test/fixtures/plugins/build-code-frame-error/exec.js b/packages/babel-core/test/fixtures/plugins/build-code-frame-error/exec.js new file mode 100644 index 0000000000..3462e87a19 --- /dev/null +++ b/packages/babel-core/test/fixtures/plugins/build-code-frame-error/exec.js @@ -0,0 +1,16 @@ +var code = "function f() {}"; +transform(code, { + plugins: [ + function() { + return { + visitor: { + FunctionDeclaration: function(path) { + throw path.buildCodeFrameError("someMsg"); + }, + }, + }; + }, + ], + // hard to assert on ANSI escape codes + highlightCode: false, +}); diff --git a/packages/babel-core/test/fixtures/plugins/build-code-frame-error/options.json b/packages/babel-core/test/fixtures/plugins/build-code-frame-error/options.json new file mode 100644 index 0000000000..b24f27d786 --- /dev/null +++ b/packages/babel-core/test/fixtures/plugins/build-code-frame-error/options.json @@ -0,0 +1,3 @@ +{ + "throws": "undefined: someMsg\n> 1 | function f() {}" +}