diff --git a/src/Module.ts b/src/Module.ts index 7f27f22f156..531872eb121 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -947,24 +947,27 @@ export default class Module { props.id = this.id; props.pos = pos; let code = this.info.code; - let { column, line } = locate(code!, pos, { offsetLine: 1 }); - try { - ({ column, line } = getOriginalLocation(this.sourcemapChain, { column, line })); - code = this.originalCode; - } catch (e) { - this.options.onwarn({ - code: 'SOURCEMAP_ERROR', - id: this.id, - loc: { - column, - file: this.id, - line - }, - message: `Error when using sourcemap for reporting an error: ${e.message}`, - pos - }); + const location = locate(code!, pos, { offsetLine: 1 }); + if (location) { + let { column, line } = location; + try { + ({ column, line } = getOriginalLocation(this.sourcemapChain, { column, line })); + code = this.originalCode; + } catch (e) { + this.options.onwarn({ + code: 'SOURCEMAP_ERROR', + id: this.id, + loc: { + column, + file: this.id, + line + }, + message: `Error when using sourcemap for reporting an error: ${e.message}`, + pos + }); + } + augmentCodeLocation(props, { column, line }, code!, this.id); } - augmentCodeLocation(props, { column, line }, code!, this.id); } private addModulesToImportDescriptions(importDescription: { diff --git a/test/misc/misc.js b/test/misc/misc.js index 313811cfea2..296fa8ce9ad 100644 --- a/test/misc/misc.js +++ b/test/misc/misc.js @@ -242,4 +242,30 @@ console.log(x); assert.strictEqual(subfeature.fileName, 'base/main/feature/sub'); assert.ok(subfeature.code.startsWith("import { fn } from '../../main'")); }); + + it('throws the proper error on max call stack exception', async () => { + const count = 10000; + let source = ''; + for (let i = 0; i < count; i++) { + source += `if (foo) {`; + } + for (let i = 0; i < count; i++) { + source += '}'; + } + try { + await rollup.rollup({ + input: { + input: 'input' + }, + plugins: [ + loader({ + input: source + }) + ] + }); + } catch (err) { + assert.notDeepStrictEqual(err.message, 'Maximum call stack size exceeded'); + assert.strictEqual(err.name, 'Error'); + } + }); });