From cbc5b3cb7aa8e5f6cf5c22e3430a8ade51c2bc7c Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 24 Mar 2023 06:49:05 +0100 Subject: [PATCH 1/2] Do not fail if the location of a warning is outside the original source --- src/utils/getCodeFrame.ts | 2 ++ .../_config.js | 34 +++++++++++++++++++ .../constants.js | 1 + .../main.js | 5 +++ 4 files changed, 42 insertions(+) create mode 100644 test/function/samples/warning-incorrect-sourcemap-location/_config.js create mode 100644 test/function/samples/warning-incorrect-sourcemap-location/constants.js create mode 100644 test/function/samples/warning-incorrect-sourcemap-location/main.js diff --git a/src/utils/getCodeFrame.ts b/src/utils/getCodeFrame.ts index 38d4a0f575f..ffdbf1470cc 100644 --- a/src/utils/getCodeFrame.ts +++ b/src/utils/getCodeFrame.ts @@ -14,6 +14,8 @@ const ELLIPSIS = '...'; export default function getCodeFrame(source: string, line: number, column: number): string { let lines = source.split('\n'); + // Needed if a plugin did not generate correct sourcemaps + if (line >= lines.length) return ''; const maxLineLength = Math.max( tabsToSpaces(lines[line - 1].slice(0, column)).length + MIN_CHARACTERS_SHOWN_AFTER_LOCATION + diff --git a/test/function/samples/warning-incorrect-sourcemap-location/_config.js b/test/function/samples/warning-incorrect-sourcemap-location/_config.js new file mode 100644 index 00000000000..3dba1354083 --- /dev/null +++ b/test/function/samples/warning-incorrect-sourcemap-location/_config.js @@ -0,0 +1,34 @@ +const { join } = require('node:path'); +const ID_MAIN = join(__dirname, 'main.js'); +const ID_CONSTANTS = join(__dirname, 'constants.js'); + +module.exports = { + description: 'does not fail if a warning has an incorrect location due to missing sourcemaps', + options: { + plugins: [ + { + name: 'test', + transform(code) { + return '/* injected */;\n\n\n\n\n\n\n\n' + code; + } + } + ] + }, + warnings: [ + { + binding: 'NON_EXISTENT', + code: 'MISSING_EXPORT', + exporter: ID_CONSTANTS, + frame: '', + id: ID_MAIN, + loc: { + column: 15, + file: ID_MAIN, + line: 12 + }, + message: '"NON_EXISTENT" is not exported by "constants.js", imported by "main.js".', + pos: 111, + url: 'https://rollupjs.org/troubleshooting/#error-name-is-not-exported-by-module' + } + ] +}; diff --git a/test/function/samples/warning-incorrect-sourcemap-location/constants.js b/test/function/samples/warning-incorrect-sourcemap-location/constants.js new file mode 100644 index 00000000000..a537ae96c16 --- /dev/null +++ b/test/function/samples/warning-incorrect-sourcemap-location/constants.js @@ -0,0 +1 @@ +export const q = 'Queue'; diff --git a/test/function/samples/warning-incorrect-sourcemap-location/main.js b/test/function/samples/warning-incorrect-sourcemap-location/main.js new file mode 100644 index 00000000000..409d9552d7f --- /dev/null +++ b/test/function/samples/warning-incorrect-sourcemap-location/main.js @@ -0,0 +1,5 @@ +import * as CONSTANTS from './constants'; + +export default class Sample { + x = CONSTANTS.NON_EXISTENT; +} From a5728880387101da3bc6a17215c4f572273d33e3 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 24 Mar 2023 07:08:39 +0100 Subject: [PATCH 2/2] Fix comparison --- src/utils/getCodeFrame.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/getCodeFrame.ts b/src/utils/getCodeFrame.ts index ffdbf1470cc..5c547c63ba8 100644 --- a/src/utils/getCodeFrame.ts +++ b/src/utils/getCodeFrame.ts @@ -15,7 +15,7 @@ const ELLIPSIS = '...'; export default function getCodeFrame(source: string, line: number, column: number): string { let lines = source.split('\n'); // Needed if a plugin did not generate correct sourcemaps - if (line >= lines.length) return ''; + if (line > lines.length) return ''; const maxLineLength = Math.max( tabsToSpaces(lines[line - 1].slice(0, column)).length + MIN_CHARACTERS_SHOWN_AFTER_LOCATION +