From 81c2e75d75f982beed776d53bc875a9ac80704c6 Mon Sep 17 00:00:00 2001 From: TrickyPi <530257315@qq.com> Date: Thu, 8 Dec 2022 10:46:26 +0800 Subject: [PATCH 1/2] fix: add json hint for missing export error --- src/utils/error.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/error.ts b/src/utils/error.ts index 90f16ca6830..60c393912df 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -7,6 +7,7 @@ import type { RollupWarning, WarningHandler } from '../rollup/types'; +import { extname } from '../utils/path'; import getCodeFrame from './getCodeFrame'; import { printQuotedStringList } from './printStringList'; import relativeId from './relativeId'; @@ -528,6 +529,7 @@ export function errorMissingExport( importingModule: string, exporter: string ): RollupLog { + const isJson = extname(importingModule); return { binding, code: MISSING_EXPORT, @@ -535,7 +537,7 @@ export function errorMissingExport( id: importingModule, message: `"${binding}" is not exported by "${relativeId(exporter)}", imported by "${relativeId( importingModule - )}".`, + )}".${isJson ? ' (Note that you need @rollup/plugin-json to import JSON files)' : ''}`, url: `https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module` }; } From 4ff7e64d78e340e20ff674c894d8ef7b81b3c314 Mon Sep 17 00:00:00 2001 From: TrickyPi <530257315@qq.com> Date: Thu, 8 Dec 2022 15:06:06 +0800 Subject: [PATCH 2/2] fix: tweak logical & add test --- src/utils/error.ts | 2 +- .../_config.js | 28 +++++++++++++++++++ .../array.json | 1 + .../main.js | 2 ++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/_config.js create mode 100644 test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/array.json create mode 100644 test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/main.js diff --git a/src/utils/error.ts b/src/utils/error.ts index 60c393912df..2abf1cadcc6 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -529,7 +529,7 @@ export function errorMissingExport( importingModule: string, exporter: string ): RollupLog { - const isJson = extname(importingModule); + const isJson = extname(exporter) === '.json'; return { binding, code: MISSING_EXPORT, diff --git a/test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/_config.js b/test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/_config.js new file mode 100644 index 00000000000..0a3e7c33cb2 --- /dev/null +++ b/test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/_config.js @@ -0,0 +1,28 @@ +const path = require('node:path'); +const ID_MAIN = path.join(__dirname, 'main.js'); +const ID_ARRAY_JSON = path.join(__dirname, 'array.json'); + +module.exports = { + description: 'should provide json hint when importing a no export json file', + error: { + binding: 'default', + code: 'MISSING_EXPORT', + exporter: ID_ARRAY_JSON, + id: ID_MAIN, + url: 'https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module', + pos: 7, + loc: { + column: 7, + file: ID_MAIN, + line: 1 + }, + frame: ` +1: import theArray from './array.json'; + ^ +2: export default theArray; + `, + watchFiles: [ID_ARRAY_JSON, ID_MAIN], + message: + '"default" is not exported by "array.json", imported by "main.js". (Note that you need @rollup/plugin-json to import JSON files)' + } +}; diff --git a/test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/array.json b/test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/array.json new file mode 100644 index 00000000000..a5fb3048965 --- /dev/null +++ b/test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/array.json @@ -0,0 +1 @@ +["foo", "bar"] diff --git a/test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/main.js b/test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/main.js new file mode 100644 index 00000000000..9c42986130e --- /dev/null +++ b/test/function/samples/adds-json-hint-for-missing-export-if-is-json-file/main.js @@ -0,0 +1,2 @@ +import theArray from './array.json'; +export default theArray;