-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from kendallgassner/addLocalDependencyChecker
Add local-dependency rule
- Loading branch information
Showing
8 changed files
with
3,596 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:'); | ||
} | ||
}) | ||
); | ||
} | ||
}; | ||
} | ||
}; |
Oops, something went wrong.