Skip to content

Commit 0609431

Browse files
authoredJun 14, 2024··
feat: Add flag ignorePrivate to no-unpublished-x rules (#298)
* feat: Add flag ignorePrivate to no-unpublished-x rules This rule make it possible to selectively disable/enable the rule in private packages * docs: Add documentation of ignorePrivate * Simplify property access and fallback
1 parent 1bcb006 commit 0609431

7 files changed

+92
-9
lines changed
 

‎docs/rules/no-unpublished-import.md

+28
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ In this way, the following code will not be reported:
6666
import type foo from "foo";
6767
```
6868

69+
### ignorePrivate
70+
71+
In a private package you sometimes want to disable checking for unpublished dependencies, e.g. if the package is not published.
72+
73+
However, there are situations where you want to mark it as private, though still ensure only published dependencies are used in your source code.
74+
An example, for such a case would be a package that is deployed to a server.
75+
76+
Defaults to `true`.
77+
78+
package.json:
79+
80+
```json
81+
{
82+
"private": true,
83+
...
84+
}
85+
```
86+
87+
```json
88+
{
89+
"rules": {
90+
"n/no-unpublished-import": ["error", {
91+
"ignorePrivate": true
92+
}]
93+
}
94+
}
95+
```
96+
6997
## 🔎 Implementation
7098

7199
- [Rule source](../../lib/rules/no-unpublished-import.js)

‎docs/rules/no-unpublished-require.md

+28
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ Please see the shared settings documentation for more information.
5353
This can be configured in the rule options or as a shared setting [`settings.tryExtensions`](../shared-settings.md#tryextensions).
5454
Please see the shared settings documentation for more information.
5555

56+
### ignorePrivate
57+
58+
In a private package you sometimes want to disable checking for unpublished dependencies, e.g. if the package is not published.
59+
60+
However, there are situations where you want to mark it as private, though still ensure only published dependencies are used in your source code.
61+
An example, for such a case would be a package that is deployed to a server.
62+
63+
Defaults to `true`.
64+
65+
package.json:
66+
67+
```json
68+
{
69+
"private": true,
70+
...
71+
}
72+
```
73+
74+
```json
75+
{
76+
"rules": {
77+
"n/no-unpublished-import": ["error", {
78+
"ignorePrivate": true
79+
}]
80+
}
81+
}
82+
```
83+
5684
## 🔎 Implementation
5785

5886
- [Rule source](../../lib/rules/no-unpublished-require.js)

‎lib/rules/no-unpublished-import.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = {
2929
convertPath: getConvertPath.schema,
3030
resolvePaths: getResolvePaths.schema,
3131
ignoreTypeImport: { type: "boolean", default: false },
32+
ignorePrivate: { type: "boolean", default: true },
3233
},
3334
additionalProperties: false,
3435
},
@@ -38,17 +39,15 @@ module.exports = {
3839
create(context) {
3940
const filePath = context.filename ?? context.getFilename()
4041
const options = context.options[0] || {}
41-
const ignoreTypeImport =
42-
options.ignoreTypeImport === void 0
43-
? false
44-
: options.ignoreTypeImport
42+
const ignoreTypeImport = options.ignoreTypeImport ?? false
43+
const ignorePrivate = options.ignorePrivate ?? true
4544

4645
if (filePath === "<input>") {
4746
return {}
4847
}
4948

5049
return visitImport(context, { ignoreTypeImport }, targets => {
51-
checkPublish(context, filePath, targets)
50+
checkPublish(context, filePath, targets, { ignorePrivate })
5251
})
5352
},
5453
}

‎lib/rules/no-unpublished-require.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = {
3030
convertPath: getConvertPath.schema,
3131
resolvePaths: getResolvePaths.schema,
3232
tryExtensions: getTryExtensions.schema,
33+
ignorePrivate: { type: "boolean", default: true },
3334
},
3435
additionalProperties: false,
3536
},
@@ -38,12 +39,15 @@ module.exports = {
3839
},
3940
create(context) {
4041
const filePath = context.filename ?? context.getFilename()
42+
const options = context.options[0] || {}
43+
const ignorePrivate = options.ignorePrivate ?? true
44+
4145
if (filePath === "<input>") {
4246
return {}
4347
}
4448

4549
return visitRequire(context, {}, targets => {
46-
checkPublish(context, filePath, targets)
50+
checkPublish(context, filePath, targets, { ignorePrivate })
4751
})
4852
},
4953
}

‎lib/util/check-publish.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,25 @@ const { getPackageJson } = require("./get-package-json")
1818
* @param {import('eslint').Rule.RuleContext} context - A context to report.
1919
* @param {string} filePath - The current file path.
2020
* @param {import('./import-target.js')[]} targets - A list of target information to check.
21+
* @param {{ignorePrivate: boolean}} options - Configuration options for checking for published files.
2122
* @returns {void}
2223
*/
23-
exports.checkPublish = function checkPublish(context, filePath, targets) {
24+
exports.checkPublish = function checkPublish(
25+
context,
26+
filePath,
27+
targets,
28+
options
29+
) {
2430
const packageJson = getPackageJson(filePath)
2531
if (typeof packageJson?.filePath !== "string") {
2632
return
2733
}
2834

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

‎tests/lib/rules/no-unpublished-import.js

+8
Original file line numberDiff line numberDiff line change
@@ -292,5 +292,13 @@ ruleTester.run("no-unpublished-import", rule, {
292292
code: "import type foo from 'foo';",
293293
errors: [{ messageId: "notPublished" }],
294294
},
295+
296+
// devDependency in a private package
297+
{
298+
filename: fixture("private-package/index.js"),
299+
code: "import bbb from 'bbb';",
300+
errors: ['"bbb" is not published.'],
301+
options: [{ ignorePrivate: false }],
302+
},
295303
],
296304
})

‎tests/lib/rules/no-unpublished-require.js

+8
Original file line numberDiff line numberDiff line change
@@ -359,5 +359,13 @@ ruleTester.run("no-unpublished-require", rule, {
359359
code: "require('../2/a.js');",
360360
errors: ['"../2/a.js" is not published.'],
361361
},
362+
363+
// devDependency in a private package
364+
{
365+
filename: fixture("private-package/index.js"),
366+
code: "require('bbb');",
367+
errors: ['"bbb" is not published.'],
368+
options: [{ ignorePrivate: false }],
369+
},
362370
],
363371
})

0 commit comments

Comments
 (0)
Please sign in to comment.