Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not crash rule no-unpublished-require #49

Merged
merged 1 commit into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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