Skip to content

Commit

Permalink
Merge pull request #11 from kendallgassner/addLocalDependencyChecker
Browse files Browse the repository at this point in the history
Add local-dependency rule
  • Loading branch information
JoshuaKGoldberg authored Sep 19, 2023
2 parents fdb73fb + deda2d3 commit 8b09206
Show file tree
Hide file tree
Showing 8 changed files with 3,596 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Or, individually configure the rules you want to use under the rules section.
- [`package-json/order-properties`](docs/rules/order-properties.md): Require top-level properties to be in a conventional order (`"name"`first, etc.).
- [`package-json/sort-collections`](docs/rules/sort-collections.md): Keep sub-collections like `"dependencies"` and `"scripts"` in alphabetical order.
- [`package-json/valid-package-def`](docs/rules/valid-package-def.md): Validate `package.json` files against the NPM specification.
- [`package-json/valid-local-dependency`](docs/rules/valid-local-dependency.md): Validates the casing for `file:` and `link:` dependencies in the `package.json` files.

These rules only run on `package.json` files; they will ignore all other files being linted. They lint `package.json` files at project root, and in any subfolder of the project, making this plugin great for monorepos.

Expand Down
25 changes: 25 additions & 0 deletions docs/rules/valid-local-dependency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Ensures that local dependencies specified in a package.json exist

## Rule Details

This rule validates the path for `file:` and `link:` dependencies in a `package.json` file - including name casing. If a `link:` or `file:` path is incorrect yarn does not sub out the link when releasing.

Examples of **incorrect** code for this rule (when `../folder` is the correct path):

```json
"devDependencies": {
"some-package": "link:../Folder",
}
```

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

```json
"devDependencies": {
"some-package": "link:../folder",
}
```

### Options

This rule has no options.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports.configs = {
rules: {
'package-json/sort-collections': 'error',
'package-json/order-properties': 'warn',
'package-json/valid-package-def': 'error'
'package-json/valid-package-def': 'error',
'package-json/valid-local-dependency': 'error'
}
}
};
85 changes: 85 additions & 0 deletions lib/rules/valid-local-dependency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @fileoverview Checks existence of local dependencies in the package.json
* @author Kendall Gassner
*/
'use strict';
const path = require('path');
const {
isPackageJson,
extractPackageObjectFromAST
} = require('../processors/PackageJsonProcessor');

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description:
'Checks existence of local dependencies in the package.json',
category: 'Best Practices',
recommended: true
},
fixable: null // or "code" or "whitespace"
},

create: function(context) {
return {
'Program:exit': node => {
if (!isPackageJson(context.getFilename())) {
return;
}
const sourceCode = context.getSourceCode();
const packageRoot = extractPackageObjectFromAST(node);
const original = JSON.parse(sourceCode.getText(packageRoot));
const {
dependencies,
peerDependencies,
devDependencies
} = original;

const depObjs = [
Object.entries(dependencies || {}),
Object.entries(peerDependencies || {}),
Object.entries(devDependencies || {})
];

depObjs.forEach(obj =>
obj.forEach(([key, value]) => {
const response = localPath => {
const filePath = path.join(
context
.getFilename()
.replace(/package\.json/g, '/'),
value.replace(new RegExp(localPath, 'g'), ''),
'/package.json'
);

try {
if (!require.resolve(filePath)) {
context.report({
node: packageRoot,
message: `The package ${key} does not exist given the specified path: ${value}.`
});
}
} catch (e) {
context.report({
node: packageRoot,
message: `The package ${key} does not exist given the specified path: ${value}.`
});
}
};

if (value.startsWith('link:')) {
response('link:');
}
if (value.startsWith('file:')) {
response('file:');
}
})
);
}
};
}
};
Loading

0 comments on commit 8b09206

Please sign in to comment.