Skip to content

Commit ff01930

Browse files
authoredNov 7, 2023
feat: Export the moduleType from ImportTarget (#132)
1 parent 79c8afd commit ff01930

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed
 

‎lib/rules/file-extension-in-import.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ const path = require("path")
88
const fs = require("fs")
99
const mapTypescriptExtension = require("../util/map-typescript-extension")
1010
const visitImport = require("../util/visit-import")
11-
const packageNamePattern = /^(?:@[^/\\]+[/\\])?[^/\\]+$/u
12-
const corePackageOverridePattern =
13-
/^(?:assert|async_hooks|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|http2|https|inspector|module|net|os|path|perf_hooks|process|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|trace_events|tty|url|util|v8|vm|worker_threads|zlib)[/\\]$/u
1411

1512
/**
1613
* Get all file extensions of the files which have the same basename.
@@ -67,13 +64,13 @@ module.exports = {
6764
const defaultStyle = context.options[0] || "always"
6865
const overrideStyle = context.options[1] || {}
6966

70-
function verify({ filePath, name, node }) {
67+
/**
68+
* @param {import("../util/import-target.js")} target
69+
* @returns {void}
70+
*/
71+
function verify({ filePath, name, node, moduleType }) {
7172
// Ignore if it's not resolved to a file or it's a bare module.
72-
if (
73-
!filePath ||
74-
packageNamePattern.test(name) ||
75-
corePackageOverridePattern.test(name)
76-
) {
73+
if (moduleType !== "relative" && moduleType !== "absolute") {
7774
return
7875
}
7976

‎lib/util/import-target.js

+27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const path = require("path")
88
const { pathToFileURL, fileURLToPath } = require("url")
9+
const { isBuiltin } = require("module")
910
const resolve = require("resolve")
1011
const {
1112
defaultResolve: importResolve,
@@ -72,6 +73,14 @@ function getFilePath(isModule, id, options, moduleType) {
7273
}
7374
}
7475

76+
function isNodeModule(name, options) {
77+
try {
78+
return require.resolve(name, options).startsWith(path.sep)
79+
} catch {
80+
return false
81+
}
82+
}
83+
7584
/**
7685
* Gets the module name of a given path.
7786
*
@@ -115,6 +124,24 @@ module.exports = class ImportTarget {
115124
*/
116125
this.name = name
117126

127+
/**
128+
* What type of module is this
129+
* @type {'unknown'|'relative'|'absolute'|'node'|'npm'|'http'|void}
130+
*/
131+
this.moduleType = "unknown"
132+
133+
if (name.startsWith("./") || name.startsWith(".\\")) {
134+
this.moduleType = "relative"
135+
} else if (name.startsWith("/") || name.startsWith("\\")) {
136+
this.moduleType = "absolute"
137+
} else if (isBuiltin(name)) {
138+
this.moduleType = "node"
139+
} else if (isNodeModule(name, options)) {
140+
this.moduleType = "npm"
141+
} else if (name.startsWith("http://") || name.startsWith("https://")) {
142+
this.moduleType = "http"
143+
}
144+
118145
/**
119146
* The full path of this import target.
120147
* If the target is a module and it does not exist then this is `null`.

‎tests/lib/rules/file-extension-in-import.js

+17
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ new RuleTester({
155155
options: ["never", { ".json": "always" }],
156156
},
157157

158+
// Ignore sub-paths of modules
159+
{
160+
filename: fixture("test.js"),
161+
code: "import '@apollo/client/core'",
162+
options: ["always"],
163+
},
164+
{
165+
filename: fixture("test.js"),
166+
code: "import 'yargs/helpers'",
167+
options: ["always"],
168+
},
169+
{
170+
filename: fixture("test.js"),
171+
code: "import 'firebase-functions/v1/auth'",
172+
options: ["always"],
173+
},
174+
158175
// typescriptExtensionMap
159176
{
160177
filename: fixture("test.tsx"),

0 commit comments

Comments
 (0)
Please sign in to comment.