diff --git a/src/Module.ts b/src/Module.ts index 7065fa31aff..a88cf041bc9 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -37,11 +37,11 @@ import { PreserveEntrySignaturesOption, ResolvedIdMap, RollupError, + RollupLogProps, RollupWarning, TransformModuleJSON } from './rollup/types'; -import { error, Errors } from './utils/error'; -import getCodeFrame from './utils/getCodeFrame'; +import { augmentCodeLocation, error, Errors } from './utils/error'; import { getId } from './utils/getId'; import { getOriginalLocation } from './utils/getOriginalLocation'; import { makeLegal } from './utils/identifierHelpers'; @@ -89,7 +89,7 @@ export interface AstContext { addImportMeta: (node: MetaProperty) => void; code: string; deoptimizationTracker: PathTracker; - error: (props: RollupError, pos?: number) => never; + error: (props: RollupError, pos: number) => never; fileName: string; getExports: () => string[]; getModuleExecIndex: () => number; @@ -107,7 +107,7 @@ export interface AstContext { traceExport: (name: string) => Variable; traceVariable: (name: string) => Variable | null; usesTopLevelAwait: boolean; - warn: (warning: RollupWarning, pos?: number) => void; + warn: (warning: RollupWarning, pos: number) => void; } function tryParse(module: Module, Parser: typeof acorn.Parser, acornOptions: acorn.Options) { @@ -256,33 +256,8 @@ export default class Module { this.ast.bind(); } - error(props: RollupError, pos?: number): never { - if (typeof pos === 'number') { - props.pos = pos; - let location: { column: number; line: number } = locate(this.code, pos, { offsetLine: 1 }); - try { - location = getOriginalLocation(this.sourcemapChain, location); - } catch (e) { - this.warn({ - code: 'SOURCEMAP_ERROR', - loc: { - column: location.column, - file: this.id, - line: location.line - }, - message: `Error when using sourcemap for reporting an error: ${e.message}`, - pos - }); - } - - props.loc = { - column: location.column, - file: this.id, - line: location.line - }; - props.frame = getCodeFrame(this.originalCode, location.line, location.column); - } - + error(props: RollupError, pos: number): never { + this.addLocationToLogProps(props, pos); return error(props); } @@ -484,7 +459,7 @@ export default class Module { return this.exportShimVariable; } const name = exportDeclaration.localName; - return this.traceVariable(name) || this.graph.scope.findVariable(name); + return this.traceVariable(name)!; } if (name !== 'default') { @@ -756,18 +731,9 @@ export default class Module { return null; } - warn(warning: RollupWarning, pos?: number) { - if (typeof pos === 'number') { - warning.pos = pos; - - const { line, column } = locate(this.code, pos, { offsetLine: 1 }); // TODO trace sourcemaps, cf. error() - - warning.loc = { file: this.id, line, column }; - warning.frame = getCodeFrame(this.code, line, column); - } - - warning.id = this.id; - this.options.onwarn(warning); + warn(props: RollupWarning, pos: number) { + this.addLocationToLogProps(props, pos); + this.options.onwarn(props); } private addDynamicImport(node: ImportExpression) { @@ -877,6 +843,28 @@ export default class Module { this.importMetas.push(node); } + private addLocationToLogProps(props: RollupLogProps, pos: number): void { + props.id = this.id; + props.pos = pos; + let { column, line } = locate(this.code, pos, { offsetLine: 1 }); + try { + ({ column, line } = getOriginalLocation(this.sourcemapChain, { column, line })); + } 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 }, this.originalCode, this.id); + } + private addModulesToImportDescriptions(importDescription: { [name: string]: ImportDescription | ReexportDescription; }) { diff --git a/src/utils/error.ts b/src/utils/error.ts index 0b08036c9c6..aa6ede7e27b 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -3,6 +3,7 @@ import Module from '../Module'; import { NormalizedInputOptions, RollupError, + RollupLogProps, RollupWarning, WarningHandler } from '../rollup/types'; @@ -15,23 +16,23 @@ export function error(base: Error | RollupError): never { } export function augmentCodeLocation( - object: RollupError | RollupWarning, + props: RollupLogProps, pos: number | { column: number; line: number }, source: string, id: string ): void { if (typeof pos === 'object') { const { line, column } = pos; - object.loc = { file: id, line, column }; + props.loc = { file: id, line, column }; } else { - object.pos = pos; + props.pos = pos; const { line, column } = locate(source, pos, { offsetLine: 1 }); - object.loc = { file: id, line, column }; + props.loc = { file: id, line, column }; } - if (object.frame === undefined) { - const { line, column } = object.loc; - object.frame = getCodeFrame(source, line, column); + if (props.frame === undefined) { + const { line, column } = props.loc; + props.frame = getCodeFrame(source, line, column); } } diff --git a/test/function/samples/circular-missed-reexports-2/_config.js b/test/function/samples/circular-missed-reexports-2/_config.js index e17a190410c..04075a8bb86 100644 --- a/test/function/samples/circular-missed-reexports-2/_config.js +++ b/test/function/samples/circular-missed-reexports-2/_config.js @@ -8,6 +8,7 @@ module.exports = { }, error: { code: 'MISSING_EXPORT', + id: path.resolve(__dirname, 'dep2.js'), frame: ` 1: export { doesNotExist } from './dep1.js'; ^`, diff --git a/test/function/samples/default-not-reexported/_config.js b/test/function/samples/default-not-reexported/_config.js index be866dd0b30..c0800c241c5 100644 --- a/test/function/samples/default-not-reexported/_config.js +++ b/test/function/samples/default-not-reexported/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'MISSING_EXPORT', message: `'default' is not exported by foo.js, imported by main.js`, + id: path.resolve(__dirname, 'main.js'), pos: 7, watchFiles: [ path.resolve(__dirname, 'main.js'), diff --git a/test/function/samples/double-default-export/_config.js b/test/function/samples/double-default-export/_config.js index 792dad2d2db..9a928a5e040 100644 --- a/test/function/samples/double-default-export/_config.js +++ b/test/function/samples/double-default-export/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'PARSE_ERROR', message: `Duplicate export 'default'`, + id: path.resolve(__dirname, 'foo.js'), parserError: { loc: { column: 7, diff --git a/test/function/samples/double-named-export/_config.js b/test/function/samples/double-named-export/_config.js index e9d7d5d5f54..7c24f8d04db 100644 --- a/test/function/samples/double-named-export/_config.js +++ b/test/function/samples/double-named-export/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'PARSE_ERROR', message: `Duplicate export 'foo'`, + id: path.resolve(__dirname, 'foo.js'), parserError: { loc: { column: 9, diff --git a/test/function/samples/double-named-reexport/_config.js b/test/function/samples/double-named-reexport/_config.js index 8fae7ea261a..597181042c6 100644 --- a/test/function/samples/double-named-reexport/_config.js +++ b/test/function/samples/double-named-reexport/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'PARSE_ERROR', message: `Duplicate export 'foo'`, + id: path.resolve(__dirname, 'foo.js'), parserError: { loc: { column: 9, diff --git a/test/function/samples/duplicate-import-fails/_config.js b/test/function/samples/duplicate-import-fails/_config.js index e31695fba5f..a52a5446a20 100644 --- a/test/function/samples/duplicate-import-fails/_config.js +++ b/test/function/samples/duplicate-import-fails/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'PARSE_ERROR', message: `Identifier 'a' has already been declared`, + id: path.resolve(__dirname, 'main.js'), parserError: { loc: { column: 9, diff --git a/test/function/samples/duplicate-import-specifier-fails/_config.js b/test/function/samples/duplicate-import-specifier-fails/_config.js index 02e51693dc4..a62bacddad9 100644 --- a/test/function/samples/duplicate-import-specifier-fails/_config.js +++ b/test/function/samples/duplicate-import-specifier-fails/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'PARSE_ERROR', message: `Identifier 'a' has already been declared`, + id: path.resolve(__dirname, 'main.js'), parserError: { loc: { column: 12, diff --git a/test/function/samples/error-after-transform-should-throw-correct-location/_config.js b/test/function/samples/error-after-transform-should-throw-correct-location/_config.js index 58c2caa9550..9cc9f582a25 100644 --- a/test/function/samples/error-after-transform-should-throw-correct-location/_config.js +++ b/test/function/samples/error-after-transform-should-throw-correct-location/_config.js @@ -21,6 +21,7 @@ module.exports = { error: { code: 'MISSING_EXPORT', message: `'default' is not exported by empty.js, imported by main.js`, + id: path.resolve(__dirname, 'main.js'), pos: 44, watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'empty.js')], loc: { diff --git a/test/function/samples/error-parse-json/_config.js b/test/function/samples/error-parse-json/_config.js index 62dbc19fa45..a7d15c32b53 100644 --- a/test/function/samples/error-parse-json/_config.js +++ b/test/function/samples/error-parse-json/_config.js @@ -6,6 +6,7 @@ module.exports = { error: { code: 'PARSE_ERROR', message: 'Unexpected token (Note that you need @rollup/plugin-json to import JSON files)', + id: path.resolve(__dirname, 'file.json'), parserError: { loc: { column: 8, diff --git a/test/function/samples/error-parse-unknown-extension/_config.js b/test/function/samples/error-parse-unknown-extension/_config.js index a9949efb4ef..3f8a81d873b 100644 --- a/test/function/samples/error-parse-unknown-extension/_config.js +++ b/test/function/samples/error-parse-unknown-extension/_config.js @@ -7,6 +7,7 @@ module.exports = { code: 'PARSE_ERROR', message: 'Unexpected token (Note that you need plugins to import files that are not JavaScript)', + id: path.resolve(__dirname, 'file.css'), parserError: { loc: { column: 0, diff --git a/test/function/samples/export-not-at-top-level-fails/_config.js b/test/function/samples/export-not-at-top-level-fails/_config.js index 17680e24306..3ce0c368be7 100644 --- a/test/function/samples/export-not-at-top-level-fails/_config.js +++ b/test/function/samples/export-not-at-top-level-fails/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'PARSE_ERROR', message: `'import' and 'export' may only appear at the top level`, + id: path.resolve(__dirname, 'main.js'), parserError: { loc: { column: 2, diff --git a/test/function/samples/import-not-at-top-level-fails/_config.js b/test/function/samples/import-not-at-top-level-fails/_config.js index a6da4dffaad..2aef5c09f88 100644 --- a/test/function/samples/import-not-at-top-level-fails/_config.js +++ b/test/function/samples/import-not-at-top-level-fails/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'PARSE_ERROR', message: `'import' and 'export' may only appear at the top level`, + id: path.resolve(__dirname, 'main.js'), parserError: { loc: { column: 2, diff --git a/test/function/samples/import-of-unexported-fails/_config.js b/test/function/samples/import-of-unexported-fails/_config.js index 6f5fa5552b2..f5f8c261651 100644 --- a/test/function/samples/import-of-unexported-fails/_config.js +++ b/test/function/samples/import-of-unexported-fails/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'MISSING_EXPORT', message: `'default' is not exported by empty.js, imported by main.js`, + id: path.resolve(__dirname, 'main.js'), pos: 7, watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'empty.js')], loc: { diff --git a/test/function/samples/reassign-import-fails/_config.js b/test/function/samples/reassign-import-fails/_config.js index 1f8b3bf9027..f0d28451839 100644 --- a/test/function/samples/reassign-import-fails/_config.js +++ b/test/function/samples/reassign-import-fails/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'ILLEGAL_REASSIGNMENT', message: `Illegal reassignment to import 'x'`, + id: path.resolve(__dirname, 'main.js'), pos: 113, watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'foo.js')], loc: { diff --git a/test/function/samples/reassign-import-not-at-top-level-fails/_config.js b/test/function/samples/reassign-import-not-at-top-level-fails/_config.js index e826b171f13..65c3b8a6170 100644 --- a/test/function/samples/reassign-import-not-at-top-level-fails/_config.js +++ b/test/function/samples/reassign-import-not-at-top-level-fails/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'ILLEGAL_REASSIGNMENT', message: `Illegal reassignment to import 'x'`, + id: path.resolve(__dirname, 'main.js'), pos: 95, watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'foo.js')], loc: { diff --git a/test/function/samples/reexport-missing-error/_config.js b/test/function/samples/reexport-missing-error/_config.js index 5a17f6c2bd5..bf769eccade 100644 --- a/test/function/samples/reexport-missing-error/_config.js +++ b/test/function/samples/reexport-missing-error/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'MISSING_EXPORT', message: `'foo' is not exported by empty.js, imported by main.js`, + id: path.resolve(__dirname, 'main.js'), pos: 9, watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'empty.js')], loc: { diff --git a/test/function/samples/update-expression-of-import-fails/_config.js b/test/function/samples/update-expression-of-import-fails/_config.js index cff3fb53b1d..4911b8316ef 100644 --- a/test/function/samples/update-expression-of-import-fails/_config.js +++ b/test/function/samples/update-expression-of-import-fails/_config.js @@ -5,6 +5,7 @@ module.exports = { error: { code: 'ILLEGAL_REASSIGNMENT', message: `Illegal reassignment to import 'a'`, + id: path.resolve(__dirname, 'main.js'), pos: 28, watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'foo.js')], loc: {