From cc54a2439bb274d91c74cc74a70e4c25e3dbb206 Mon Sep 17 00:00:00 2001 From: TrickyPi <530257315@qq.com> Date: Fri, 9 Dec 2022 04:24:11 +0800 Subject: [PATCH] fix: provide json hint when importing a no export json file (#4741) * fix: add json hint for missing export error * fix: tweak logical & add test Co-authored-by: Lukas Taegert-Atkinson --- src/utils/error.ts | 4 ++- .../_config.js | 28 +++++++++++++++++++ .../array.json | 1 + .../main.js | 2 ++ 4 files changed, 34 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 90f16ca6830..2abf1cadcc6 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(exporter) === '.json'; 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` }; } 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;