Skip to content

fix(no-missing-import): Support data imports #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/util/import-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function getTSConfigAliases(context) {
* @property {Partial<import('enhanced-resolve').ResolveOptions>} [resolverConfig]
* @property {string} basedir
*/
/** @typedef { 'unknown' | 'relative' | 'absolute' | 'node' | 'npm' | 'http' } ModuleType */
/** @typedef { 'unknown' | 'relative' | 'absolute' | 'node' | 'npm' | 'http' | 'data' } ModuleType */
/** @typedef { 'import' | 'require' | 'type' } ModuleStyle */

/**
Expand Down Expand Up @@ -176,6 +176,10 @@ module.exports = class ImportTarget {
return "node"
}

if (/^data:/.test(this.name)) {
return "data"
}

if (/^(@[\w~-][\w.~-]*\/)?[\w~-][\w.~-]*/.test(this.name)) {
return "npm"
}
Expand Down Expand Up @@ -242,7 +246,7 @@ module.exports = class ImportTarget {
* @returns {string | undefined}
*/
getModuleName() {
if (this.moduleType === "relative") return
if (this.moduleType === "relative" || this.moduleType === "data") return

if (this.moduleType === "npm") {
if (this.name.startsWith("@")) {
Expand Down Expand Up @@ -296,6 +300,10 @@ module.exports = class ImportTarget {
* @returns {string | null} The resolved path.
*/
getFilePath() {
if (this.moduleType === "data" && this.moduleStyle === "import") {
return this.name
}

const conditionNames = ["node", "require"]

const mainFields = []
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-missing-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ ruleTester.run("no-missing-import", rule, {
options: [{ ignoreTypeImport: true }],
},

// data import
{
filename: fixture("test.js"),
code: "import 'data:text/javascript,const x = 123;';",
},

// import()
...(DynamicImportSupported
? [
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/no-missing-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,14 @@ ruleTester.run("no-missing-require", rule, {
code: "require('virtual:package-scope/name');",
errors: cantResolve("virtual:package-scope/name"),
},

// Sanity test for (wrong) attempt to require data
// (this should only be supported in imports)
{
filename: fixture("test.js"),
code: "require('data:text/javascript,const x = 123;');",
errors: cantResolve("data:text/javascript,const x = 123;"),
},
],
})

Expand Down
Loading