useJsonImportAttributes
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
lint/correctness/useJsonImportAttributes¥Diagnostic Category:
lint/correctness/useJsonImportAttributes -
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
此规则属于以下域:
¥This rule belongs to the following domains:
¥How to configure
{ "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
Biome v2.1 中文网 - 粤ICP备13048890号