Skip to content

Commit

Permalink
chore: Demo with only enhanced-resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
scagood committed Apr 17, 2024
1 parent a256b25 commit f9e454d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 46 deletions.
44 changes: 4 additions & 40 deletions lib/util/check-existence.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
*/
"use strict"

const path = require("path")
const exists = require("./exists")
const getAllowModules = require("./get-allow-modules")
const isTypescript = require("./is-typescript")
const { convertJsExtensionToTs } = require("../util/map-typescript-extension")

/**
* Reports a missing file from ImportTarget
Expand Down Expand Up @@ -41,49 +37,17 @@ function markMissing(context, target) {
* @returns {void}
*/
exports.checkExistence = function checkExistence(context, targets) {
/** @type {Set<string | undefined>} */
const allowed = new Set(getAllowModules(context))

target: for (const target of targets) {
if (
target.moduleName != null &&
!allowed.has(target.moduleName) &&
target.filePath == null
) {
markMissing(context, target)
continue
}

if (
target.moduleName != null ||
target.filePath == null ||
exists(target.filePath)
) {
for (const target of targets) {
if (allowed.has(target.moduleName)) {
continue
}

if (isTypescript(context) === false) {
if (target.resolveError != null) {
markMissing(context, target)
continue
}

const parsed = path.parse(target.filePath)
const pathWithoutExt = path.resolve(parsed.dir, parsed.name)

const reversedExtensions = convertJsExtensionToTs(
context,
target.filePath,
parsed.ext
)

for (const reversedExtension of reversedExtensions) {
const reversedPath = pathWithoutExt + reversedExtension

if (exists(reversedPath)) {
continue target
}
}

markMissing(context, target)
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib/util/import-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,18 @@ module.exports = class ImportTarget {
extensionAlias = getTypescriptExtensionMap(this.context).backward
}

const requireResolve = resolver.create.sync({
/** @type {import('enhanced-resolve').ResolveOptionsOptionalFS} */
this.resolverConfig = {
conditionNames,
extensions,
mainFields,
mainFiles,

extensionAlias,
alias,
})
}

const requireResolve = resolver.create.sync(this.resolverConfig)

const cwd = this.context.settings?.cwd ?? process.cwd()
for (const directory of this.getPaths()) {
Expand Down
9 changes: 5 additions & 4 deletions tests/lib/rules/no-missing-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ ruleTester.run("no-missing-import", rule, {
filename: fixture("test.js"),
code: "import a from './a.js';",
},
{
filename: fixture("test.ts"),
code: "import a from './a.js';",
},
// I dont think this should resolve, as it wont after a standard `tsc`
// {
// filename: fixture("test.ts"),
// code: "import a from './a.js';",
// },
{
filename: fixture("test.ts"),
code: "import a from './d.js';",
Expand Down

0 comments on commit f9e454d

Please sign in to comment.