Skip to content

Commit

Permalink
add ensure-workspaces rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden committed Apr 23, 2024
1 parent 5bd584c commit 5fd881a
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 44 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ eslint . --ext js,json
|:--------|:------------|
| [json-files/ensure-repository-directory](./docs/rules/ensure-repository-directory.md) | ensure repository/directory in package.json |
| [json-files/ensure-volta-extends](./docs/rules/ensure-volta-extends.md) | ensure volta-extends in package.json |
| [json-files/ensure-workspaces](./docs/rules/ensure-workspaces.md) | ensure workspace globs in package.json resolve to directories |
| [json-files/eol-last](./docs/rules/eol-last.md) | require or disallow newline at the end of package.json |
| [json-files/no-branch-in-dependencies](./docs/rules/no-branch-in-dependencies.md) | prevent branches in package.json dependencies |
| [json-files/require-engines](./docs/rules/require-engines.md) | require the engines field in package.json |
Expand Down
58 changes: 58 additions & 0 deletions docs/rules/ensure-workspaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Ensure workspace globs in package.json resolve to directories (ensure-workspaces)

Check that the monorepo workspace globs find dirs with package.json files.


## Rule Details

This rule aims to ensure the workspace globs find dirs with package.json files.

Examples of **incorrect** code for this rule:

```json
{
"workspace": [
"packages/*/missing-dir"
]
}
```

Examples of **correct** code for this rule:

```json
{
"workspace": [
"packages/*/dir-with-package-json"
]
}
```

```json
{
"workspace": [
"packages/dir-with-package-json"
]
}
```

```json
{
"workspace": {
"packages": [
"packages/*/dir-with-package-json"
]
}
}
```

### Options



## When Not To Use It

If workspace globs are placeholders for future packages.

## Further Reading

https://docs.npmjs.com/cli/v10/configuring-npm/package-json#workspaces
68 changes: 68 additions & 0 deletions lib/rules/ensure-workspaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const path = require('path');
const fg = require('fast-glob');

module.exports = {
meta: {
docs: {
description: 'ensure workspace globs in package.json resolve to directories'
},
schema: [
]
},

create(context) {
let filename = context.getFilename();
if (path.basename(filename) !== 'package.json') {
return {};
}

return {
AssignmentExpression(node) {
let json = node.right;
let property = json.properties.find(p => p.key.value === 'workspaces');
if (!property) {
return;
}

let workspaces = property.value;
if (workspaces.type === 'ObjectExpression') {
let property = workspaces.properties.find(p => p.key.value === 'packages');
if (!property) {
return;
}

workspaces = property.value;
}

if (workspaces.type !== 'ArrayExpression') {
return;
}

for (let node of workspaces.elements) {
if (node.type !== 'Literal') {
continue;
}

if (typeof node.value !== 'string') {
continue;
}

let glob = path.join(node.value, 'package.json');

let entries = fg.sync(glob, {
cwd: path.dirname(filename)
});

if (!entries.length) {
context.report({
node,
message: 'workspace path/glob does not match any workspaces with a package.json.'
});
}
}
}
};
}
};
87 changes: 43 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"ajv": "^8.2.0",
"better-ajv-errors": "^1.2.0",
"fast-glob": "^3.3.2",
"requireindex": "^1.2.0",
"semver": "^7.0.0",
"sort-package-json": "^1.22.1"
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/workspaces/foo/bar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Loading

0 comments on commit 5fd881a

Please sign in to comment.