Skip to content

useJsonImportAttributes

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"correctness": {
"useJsonImportAttributes": "error"
}
}
}
}

¥Description

强制 JSON 模块导入使用 with { type: "json" }

¥Enforces the use of with { type: "json" } for JSON module imports.

ECMAScript 模块可以导入 JSON 模块。需要特定的导入断言 with { type: "json" } 来告知 JavaScript 运行时,导入的文件应解析为 JSON。省略此断言可能会导致运行时错误或对导入模块的误解。

¥ECMAScript modules can import JSON modules. However, the specific import assertion with { type: "json" } is required to inform the JavaScript runtime that the imported file should be parsed as JSON. Omitting this assertion can lead to runtime errors or misinterpretation of the imported module.

此规则确保所有 .json 文件的导入都包含此断言。

¥This rule ensures that all imports of .json files include this assertion.

¥Examples

¥Invalid

import jsonData from './data.json';
code-block.js:1:1 lint/correctness/useJsonImportAttributes  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This JSON import is missing the type: “json” import attribute.

> 1 │ import jsonData from ‘./data.json’;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

To explicitly declare the module type for JSON imports, add with { type: “json” } to this import statement.

Safe fix: Add ‘type: “json”’ import attribute.

1 │ import·jsonData·from·‘./data.json’·with·{·type:·json·};
++++++++++++++++++++++
import jsonData from './data.json' with { someOtherAttribute: "value" };
code-block.js:1:36 lint/correctness/useJsonImportAttributes  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The import attributes for this JSON module are missing type: “json”.

> 1 │ import jsonData from ‘./data.json’ with { someOtherAttribute: “value” };
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

Ensure the with clause includes type: “json” for this JSON import.

Safe fix: Add ‘type: “json”’ import attribute.

1 │ import·jsonData·from·‘./data.json’·with·{·type:·json,·someOtherAttribute:·“value”·};
++++++++++++++

¥Valid

import jsonData from './data.json' with { type: "json" };
import jsonData from './data.json' with { type: "json", other: "value" };
import code from './script.js'; // Not a JSON import

¥Related links