Skip to content

Commit

Permalink
feat(require-dependency): peerDependencies & optionalDependencies
Browse files Browse the repository at this point in the history
Now `peerDependencies` and `optionalDependencies` are also considered in the `require-dependency` rule.
  • Loading branch information
demensky committed Jun 7, 2023
1 parent 302a28b commit 67c2bab
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rules/require-dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ module.exports.create = (context) => {
context.getFilename(),
({ node, value, path, currentWorkspace }) => {
workspaces.forEach(({ package: { name }, location }) => {
const { dependencies = {}, devDependencies = {} } =
currentWorkspace.package;
const {
dependencies = {},
peerDependencies = {},
optionalDependencies = {},
devDependencies = {},
} = currentWorkspace.package;

if (
name !== currentWorkspace.package.name &&
(isSubPath(name, value) || isSubPath(location, path)) &&
!Object.keys(dependencies).includes(name) &&
!Object.keys(peerDependencies).includes(name) &&
!Object.keys(optionalDependencies).includes(name) &&
!Object.keys(devDependencies).includes(name)
) {
context.report({
Expand Down
14 changes: 14 additions & 0 deletions tests/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ module.exports.findWorkspacesMock = () => [
name: "@test/third-workspace",
},
},
{
location: "/test/peer-dependencies",
package: {
name: "@test/peer-dependencies",
peerDependencies: { "@test/peer-workspace": "^1.0.0" },
},
},
{
location: "/test/optional-dependencies",
package: {
name: "@test/optional-dependencies",
optionalDependencies: { "@test/optional-workspace": "^1.0.0" },
},
},
{
location: "/test/scope/shared",
package: {
Expand Down
8 changes: 8 additions & 0 deletions tests/rules/require-dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ describe("require-dependency", () => {
filename: "/test/workspace/test.js",
code: "require(undefined)",
},
{
filename: "/test/peer-dependencies/test.js",
code: "import '@test/peer-workspace'",
},
{
filename: "/test/optional-dependencies/test.js",
code: "import '@test/optional-workspace'",
},
],
invalid: [
{
Expand Down

0 comments on commit 67c2bab

Please sign in to comment.