useJsonImportAttribute
Summary
Section titled “Summary”- Rule available since:
v2.0.0
- Diagnostic Category:
lint/nursery/useJsonImportAttribute
- This rule has a safe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
Description
Section titled “Description”Enforces the use of with { type: "json" }
for JSON module imports.
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.
This rule ensures that all imports of .json
files include this assertion.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”import jsonData from './data.json';
code-block.js:1:1 lint/nursery/useJsonImportAttribute 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/nursery/useJsonImportAttribute 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”·};
│ ++++++++++++++
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
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useJsonImportAttribute": "error" } } }}