Skip to content

Commit

Permalink
fix(no-unpublished): allowModules option was disabled when using TS…
Browse files Browse the repository at this point in the history
… alias
  • Loading branch information
ota-meshi committed Mar 12, 2025
1 parent 067b9bf commit a8c47c4
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 15 deletions.
4 changes: 3 additions & 1 deletion lib/util/check-extraneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ exports.checkExtraneous = function checkExtraneous(context, filePath, targets) {
target.moduleName != null &&
target.filePath != null &&
!dependencies.has(target.moduleName) &&
!allowed.has(target.moduleName)
!allowed.has(target.moduleName) &&
// https://github.com/eslint-community/eslint-plugin-n/issues/379
!target.hasTSAlias()
) {
context.report({
node: target.node,
Expand Down
27 changes: 13 additions & 14 deletions lib/util/import-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,6 @@ module.exports = class ImportTarget {
return "node"
}

// This check should be done before the RegExp that checks npm packages,
// because `tsconfig.json` aliases may start with `~`, `@` and this will
// cause imports using aliases to be treated as npm-module imports
// https://github.com/eslint-community/eslint-plugin-n/issues/379
if (isTypescript(this.context)) {
const aliases = getTSConfigAliases(this.context)
if (
Array.isArray(aliases) &&
aliases.some(alias => this.name.startsWith(alias.name))
) {
return "relative"
}
}

if (/^(@[\w~-][\w.~-]*\/)?[\w~-][\w.~-]*/.test(this.name)) {
return "npm"
}
Expand All @@ -201,6 +187,19 @@ module.exports = class ImportTarget {
return "unknown"
}

hasTSAlias() {
if (isTypescript(this.context)) {
const aliases = getTSConfigAliases(this.context)
if (
Array.isArray(aliases) &&
aliases.some(alias => this.name.startsWith(alias.name))
) {
return true
}
}
return false
}

/**
* What module import style is used
* @param {'import' | 'require'} fallback
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/*
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// File needs to exists
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "test",
"version": "0.0.0",
"devDependencies": {
"@test/dev": "0.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"*": ["./node_modules/*"]
}
}
}
12 changes: 12 additions & 0 deletions tests/lib/rules/no-unpublished-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ ruleTester.run("no-unpublished-import", rule, {
code: "import type foo from 'foo';",
options: [{ ignoreTypeImport: true }],
},

// imports using `tsconfig.json > compilerOptions > paths` setting
// https://github.com/eslint-community/eslint-plugin-n/issues/421
{
filename: fixture("tsconfig-paths-wildcard/index.ts"),
code: "import foo from '@test/dev'",
options: [
{
allowModules: ["@test/dev"],
},
],
},
],
invalid: [
{
Expand Down

0 comments on commit a8c47c4

Please # to comment.