Skip to content

Commit 69ea12b

Browse files
authored
fix(no-missing-import): Support data imports (#465)
1 parent 047d914 commit 69ea12b

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

lib/util/import-target.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function getTSConfigAliases(context) {
7474
* @property {Partial<import('enhanced-resolve').ResolveOptions>} [resolverConfig]
7575
* @property {string} basedir
7676
*/
77-
/** @typedef { 'unknown' | 'relative' | 'absolute' | 'node' | 'npm' | 'http' } ModuleType */
77+
/** @typedef { 'unknown' | 'relative' | 'absolute' | 'node' | 'npm' | 'http' | 'data' } ModuleType */
7878
/** @typedef { 'import' | 'require' | 'type' } ModuleStyle */
7979

8080
/**
@@ -176,6 +176,10 @@ module.exports = class ImportTarget {
176176
return "node"
177177
}
178178

179+
if (/^data:/.test(this.name)) {
180+
return "data"
181+
}
182+
179183
if (/^(@[\w~-][\w.~-]*\/)?[\w~-][\w.~-]*/.test(this.name)) {
180184
return "npm"
181185
}
@@ -242,7 +246,7 @@ module.exports = class ImportTarget {
242246
* @returns {string | undefined}
243247
*/
244248
getModuleName() {
245-
if (this.moduleType === "relative") return
249+
if (this.moduleType === "relative" || this.moduleType === "data") return
246250

247251
if (this.moduleType === "npm") {
248252
if (this.name.startsWith("@")) {
@@ -296,6 +300,10 @@ module.exports = class ImportTarget {
296300
* @returns {string | null} The resolved path.
297301
*/
298302
getFilePath() {
303+
if (this.moduleType === "data" && this.moduleStyle === "import") {
304+
return this.name
305+
}
306+
299307
const conditionNames = ["node", "require"]
300308

301309
const mainFields = []

tests/lib/rules/no-missing-import.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ ruleTester.run("no-missing-import", rule, {
366366
options: [{ ignoreTypeImport: true }],
367367
},
368368

369+
// data import
370+
{
371+
filename: fixture("test.js"),
372+
code: "import 'data:text/javascript,const x = 123;';",
373+
},
374+
369375
// import()
370376
...(DynamicImportSupported
371377
? [

tests/lib/rules/no-missing-require.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,14 @@ ruleTester.run("no-missing-require", rule, {
463463
code: "require('virtual:package-scope/name');",
464464
errors: cantResolve("virtual:package-scope/name"),
465465
},
466+
467+
// Sanity test for (wrong) attempt to require data
468+
// (this should only be supported in imports)
469+
{
470+
filename: fixture("test.js"),
471+
code: "require('data:text/javascript,const x = 123;');",
472+
errors: cantResolve("data:text/javascript,const x = 123;"),
473+
},
466474
],
467475
})
468476

0 commit comments

Comments
 (0)