Skip to content

Commit

Permalink
fix: do not crash rule no-unpublished-require (#49)
Browse files Browse the repository at this point in the history
Fixes #48
  • Loading branch information
fasttime committed Aug 22, 2022
1 parent d7b975a commit 38b0298
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/util/check-publish.js
Expand Up @@ -49,16 +49,22 @@ exports.checkPublish = function checkPublish(context, filePath, targets) {
if (!npmignore.match(toRelative(filePath))) {
// This file is published, so this cannot import private files.
for (const target of targets) {
const isPrivateFile =
target.moduleName == null &&
npmignore.match(toRelative(target.filePath))
const isDevPackage =
const isPrivateFile = () => {
if (target.moduleName != null) {
return false
}
const relativeTargetPath = toRelative(target.filePath)
return (
relativeTargetPath !== "" &&
npmignore.match(relativeTargetPath)
)
}
const isDevPackage = () =>
target.moduleName != null &&
devDependencies.has(target.moduleName) &&
!dependencies.has(target.moduleName) &&
!allowed.has(target.moduleName)

if (isPrivateFile || isDevPackage) {
if (isPrivateFile() || isDevPackage()) {
context.report({
node: target.node,
loc: target.node.loc,
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/no-unpublished/issue48n/package.json
@@ -0,0 +1,5 @@
{
"files": [
"**/*.js"
]
}
17 changes: 17 additions & 0 deletions tests/lib/rules/no-unpublished-require.js
Expand Up @@ -223,6 +223,23 @@ ruleTester.run("no-unpublished-require", rule, {
env: { node: true },
},

// Should work fine if the target is the package directory.
{
filename: fixture("issue48n/test.js"),
code: "require('.');",
env: { node: true },
},
{
filename: fixture("issue48n/test.js"),
code: "require('./');",
env: { node: true },
},
{
filename: fixture("issue48n/test/test.js"),
code: "require('..');",
env: { node: true },
},

// allowModules option
{
filename: fixture("1/test.js"),
Expand Down

0 comments on commit 38b0298

Please sign in to comment.