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

feat: Add flag ignorePrivate to no-unpublished-x rules #298

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/rules/no-unpublished-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ In this way, the following code will not be reported:
import type foo from "foo";
```

### ignorePrivate

In a private package you sometimes want to disable checking for unpublished dependencies, e.g. if the package is not published.

However, there are situations where you want to mark it as private, though still ensure only published dependencies are used in your source code.
An example, for such a case would be a package that is deployed to a server.

Defaults to `true`.

package.json:

```json
{
"private": true,
...
}
```

```json
{
"rules": {
"n/no-unpublished-import": ["error", {
"ignorePrivate": true
}]
}
}
```

## 🔎 Implementation

- [Rule source](../../lib/rules/no-unpublished-import.js)
Expand Down
28 changes: 28 additions & 0 deletions docs/rules/no-unpublished-require.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ Please see the shared settings documentation for more information.
This can be configured in the rule options or as a shared setting [`settings.tryExtensions`](../shared-settings.md#tryextensions).
Please see the shared settings documentation for more information.

### ignorePrivate

In a private package you sometimes want to disable checking for unpublished dependencies, e.g. if the package is not published.

However, there are situations where you want to mark it as private, though still ensure only published dependencies are used in your source code.
An example, for such a case would be a package that is deployed to a server.

Defaults to `true`.

package.json:

```json
{
"private": true,
...
}
```

```json
{
"rules": {
"n/no-unpublished-import": ["error", {
"ignorePrivate": true
}]
}
}
```

## 🔎 Implementation

- [Rule source](../../lib/rules/no-unpublished-require.js)
Expand Down
9 changes: 4 additions & 5 deletions lib/rules/no-unpublished-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
convertPath: getConvertPath.schema,
resolvePaths: getResolvePaths.schema,
ignoreTypeImport: { type: "boolean", default: false },
ignorePrivate: { type: "boolean", default: true },
},
additionalProperties: false,
},
Expand All @@ -38,17 +39,15 @@ module.exports = {
create(context) {
const filePath = context.filename ?? context.getFilename()
const options = context.options[0] || {}
const ignoreTypeImport =
options.ignoreTypeImport === void 0
? false
: options.ignoreTypeImport
const ignoreTypeImport = options.ignoreTypeImport ?? false
const ignorePrivate = options.ignorePrivate ?? true

if (filePath === "<input>") {
return {}
}

return visitImport(context, { ignoreTypeImport }, targets => {
checkPublish(context, filePath, targets)
checkPublish(context, filePath, targets, { ignorePrivate })
})
},
}
6 changes: 5 additions & 1 deletion lib/rules/no-unpublished-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
convertPath: getConvertPath.schema,
resolvePaths: getResolvePaths.schema,
tryExtensions: getTryExtensions.schema,
ignorePrivate: { type: "boolean", default: true },
},
additionalProperties: false,
},
Expand All @@ -38,12 +39,15 @@ module.exports = {
},
create(context) {
const filePath = context.filename ?? context.getFilename()
const options = context.options[0] || {}
const ignorePrivate = options.ignorePrivate ?? true

if (filePath === "<input>") {
return {}
}

return visitRequire(context, {}, targets => {
checkPublish(context, filePath, targets)
checkPublish(context, filePath, targets, { ignorePrivate })
})
},
}
14 changes: 11 additions & 3 deletions lib/util/check-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@ const { getPackageJson } = require("./get-package-json")
* @param {import('eslint').Rule.RuleContext} context - A context to report.
* @param {string} filePath - The current file path.
* @param {import('./import-target.js')[]} targets - A list of target information to check.
* @param {{ignorePrivate: boolean}} options - Configuration options for checking for published files.
* @returns {void}
*/
exports.checkPublish = function checkPublish(context, filePath, targets) {
exports.checkPublish = function checkPublish(
context,
filePath,
targets,
options
) {
const packageJson = getPackageJson(filePath)
if (typeof packageJson?.filePath !== "string") {
return
}

// Private packages are never published so we don't need to check the imported dependencies either.
// Flag to ignore checking imported dependencies in private packages.
// For projects that need to be deployed to a server checking for imported dependencies may still be desireable
// while making it a private package.
// More information: https://docs.npmjs.com/cli/v8/configuring-npm/package-json#private
if (packageJson.private === true) {
if (options.ignorePrivate && packageJson.private === true) {
return
}

Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/no-unpublished-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,13 @@ ruleTester.run("no-unpublished-import", rule, {
code: "import type foo from 'foo';",
errors: [{ messageId: "notPublished" }],
},

// devDependency in a private package
{
filename: fixture("private-package/index.js"),
code: "import bbb from 'bbb';",
errors: ['"bbb" is not published.'],
options: [{ ignorePrivate: false }],
},
],
})
8 changes: 8 additions & 0 deletions tests/lib/rules/no-unpublished-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,5 +359,13 @@ ruleTester.run("no-unpublished-require", rule, {
code: "require('../2/a.js');",
errors: ['"../2/a.js" is not published.'],
},

// devDependency in a private package
{
filename: fixture("private-package/index.js"),
code: "require('bbb');",
errors: ['"bbb" is not published.'],
options: [{ ignorePrivate: false }],
},
],
})