Skip to content

Commit 2a85ec9

Browse files
authored
Feature/improve alphabetical rule message (#37)
* Closes #33 Improved the message when prefer-alphabetical-dependencies, prefer-alphabetical-devDependencies, prefer-alphabetical-bundledDependencies, prefer-alphabetical-optionalDependencies, or prefer-alphabetical-peerDependencies are triggered. Now failing dependency is communicated as well as the dependency it must be placed after. * Bump dips
1 parent cd78666 commit 2a85ec9

14 files changed

+68
-33
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
### Removed
1313

14+
## [2.4.0] - 2017-05-23
15+
### Changed
16+
- Improved the message when prefer-alphabetical-dependencies, prefer-alphabetical-devDependencies, prefer-alphabetical-bundledDependencies, prefer-alphabetical-optionalDependencies, or prefer-alphabetical-peerDependencies are triggered. Now failing dependency is communicated as well as the dependency it must be placed after.
17+
1418
## [2.3.0] - 2017-04-22
1519
### Added
1620
- New rule: [prefer-alphabetical-dependencies](https://github.com/tclindner/npm-package-json-lint/wiki/prefer-alphabetical-dependencies)

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-package-json-lint",
3-
"version": "2.3.0",
3+
"version": "2.4.0",
44
"description": "CLI app for linting package.json files.",
55
"keywords": [
66
"lint",
@@ -51,9 +51,9 @@
5151
"grunt-jscs": "^3.0.1",
5252
"grunt-jsonlint": "^1.1.0",
5353
"grunt-mocha-test": "^0.13.2",
54-
"mocha": "^3.2.0",
54+
"mocha": "^3.4.1",
5555
"should": "^11.2.1",
56-
"sinon": "^2.1.0",
56+
"sinon": "^2.3.0",
5757
"time-grunt": "^1.4.0"
5858
},
5959
"engines": {

src/rules/prefer-alphabetical-bundledDependencies.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ const isInAlphabeticalOrder = require('./../validators/alphabetical-sort').isInA
44
const LintIssue = require('./../LintIssue');
55
const lintId = 'prefer-alphabetical-bundledDependencies';
66
const nodeName = 'bundledDependencies';
7-
const message = 'Your bundledDependencies are not in alphabetical order. Please update the order.';
7+
const message = 'Your bundledDependencies are not in alphabetical order.';
88
const ruleType = 'bundledDependencies-alphabetical-order';
99

1010
const lint = function(packageJsonData, lintType) {
11-
if (!isInAlphabeticalOrder(packageJsonData, nodeName)) {
12-
return new LintIssue(lintId, lintType, nodeName, message);
11+
const result = isInAlphabeticalOrder(packageJsonData, nodeName);
12+
13+
if (!result.status) {
14+
return new LintIssue(lintId, lintType, nodeName, `${message} Please move ${result.data.invalidNode} after ${result.data.validNode}.`);
1315
}
1416

1517
return true;

src/rules/prefer-alphabetical-dependencies.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ const isInAlphabeticalOrder = require('./../validators/alphabetical-sort').isInA
44
const LintIssue = require('./../LintIssue');
55
const lintId = 'prefer-alphabetical-dependencies';
66
const nodeName = 'dependencies';
7-
const message = 'Your dependencies are not in alphabetical order. Please update the order.';
7+
const message = 'Your dependencies are not in alphabetical order.';
88
const ruleType = 'dependencies-alphabetical-order';
99

1010
const lint = function(packageJsonData, lintType) {
11-
if (!isInAlphabeticalOrder(packageJsonData, nodeName)) {
12-
return new LintIssue(lintId, lintType, nodeName, message);
11+
const result = isInAlphabeticalOrder(packageJsonData, nodeName);
12+
13+
if (!result.status) {
14+
return new LintIssue(lintId, lintType, nodeName, `${message} Please move ${result.data.invalidNode} after ${result.data.validNode}.`);
1315
}
1416

1517
return true;

src/rules/prefer-alphabetical-devDependencies.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ const isInAlphabeticalOrder = require('./../validators/alphabetical-sort').isInA
44
const LintIssue = require('./../LintIssue');
55
const lintId = 'prefer-alphabetical-devDependencies';
66
const nodeName = 'devDependencies';
7-
const message = 'Your devDependencies are not in alphabetical order. Please update the order.';
7+
const message = 'Your devDependencies are not in alphabetical order.';
88
const ruleType = 'devDependencies-alphabetical-order';
99

1010
const lint = function(packageJsonData, lintType) {
11-
if (!isInAlphabeticalOrder(packageJsonData, nodeName)) {
12-
return new LintIssue(lintId, lintType, nodeName, message);
11+
const result = isInAlphabeticalOrder(packageJsonData, nodeName);
12+
13+
if (!result.status) {
14+
return new LintIssue(lintId, lintType, nodeName, `${message} Please move ${result.data.invalidNode} after ${result.data.validNode}.`);
1315
}
1416

1517
return true;

src/rules/prefer-alphabetical-optionalDependencies.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ const isInAlphabeticalOrder = require('./../validators/alphabetical-sort').isInA
44
const LintIssue = require('./../LintIssue');
55
const lintId = 'prefer-alphabetical-optionalDependencies';
66
const nodeName = 'optionalDependencies';
7-
const message = 'Your optionalDependencies are not in alphabetical order. Please update the order.';
7+
const message = 'Your optionalDependencies are not in alphabetical order.';
88
const ruleType = 'optionalDependencies-alphabetical-order';
99

1010
const lint = function(packageJsonData, lintType) {
11-
if (!isInAlphabeticalOrder(packageJsonData, nodeName)) {
12-
return new LintIssue(lintId, lintType, nodeName, message);
11+
const result = isInAlphabeticalOrder(packageJsonData, nodeName);
12+
13+
if (!result.status) {
14+
return new LintIssue(lintId, lintType, nodeName, `${message} Please move ${result.data.invalidNode} after ${result.data.validNode}.`);
1315
}
1416

1517
return true;

src/rules/prefer-alphabetical-peerDependencies.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ const isInAlphabeticalOrder = require('./../validators/alphabetical-sort').isInA
44
const LintIssue = require('./../LintIssue');
55
const lintId = 'prefer-alphabetical-peerDependencies';
66
const nodeName = 'peerDependencies';
7-
const message = 'Your peerDependencies are not in alphabetical order. Please update the order.';
7+
const message = 'Your peerDependencies are not in alphabetical order.';
88
const ruleType = 'peerDependencies-alphabetical-order';
99

1010
const lint = function(packageJsonData, lintType) {
11-
if (!isInAlphabeticalOrder(packageJsonData, nodeName)) {
12-
return new LintIssue(lintId, lintType, nodeName, message);
11+
const result = isInAlphabeticalOrder(packageJsonData, nodeName);
12+
13+
if (!result.status) {
14+
return new LintIssue(lintId, lintType, nodeName, `${message} Please move ${result.data.invalidNode} after ${result.data.validNode}.`);
1315
}
1416

1517
return true;
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
'use strict';
22

33
// jscs:disable requireSpacesInForStatement, requireSpacesInForStatement
4+
const increment = 1;
45

56
/**
67
* Determines whether an array is in alphabetical order
78
* @param {object} packageJsonData Valid JSON
89
* @param {string} nodeName Name of a node in the package.json file
9-
* @return {boolean} True if the node is in alphabetical order or is missing. False if it is not.
10+
* @return {object} Object containing the status and the dependencies that are out of order, if applicable
1011
*/
1112
const isInAlphabeticalOrder = function(packageJsonData, nodeName) {
1213
if (!packageJsonData.hasOwnProperty(nodeName)) {
13-
return true;
14+
return {
15+
status: true,
16+
data: {
17+
invalidNode: null,
18+
validNode: null
19+
}
20+
};
1421
}
1522

1623
let isValid = true;
17-
const increment = 1;
24+
let data = {
25+
invalidNode: null,
26+
validNode: null
27+
};
1828
const nodeKeysOriginal = Object.keys(packageJsonData[nodeName]);
19-
let nodeKeysSorted = Object.keys(packageJsonData[nodeName]);
20-
21-
nodeKeysSorted = nodeKeysSorted.sort();
29+
const nodeKeysSorted = Object.keys(packageJsonData[nodeName]).sort();
2230

2331
for (let keyIndex = 0;keyIndex <= nodeKeysOriginal.length;keyIndex += increment) {
2432
if (nodeKeysOriginal[keyIndex] !== nodeKeysSorted[keyIndex]) {
2533
isValid = false;
34+
data = {
35+
invalidNode: nodeKeysOriginal[keyIndex],
36+
validNode: nodeKeysSorted[keyIndex]
37+
};
2638
break;
2739
}
2840
}
2941

30-
return isValid;
42+
return {
43+
status: isValid,
44+
data
45+
};
3146
};
3247

3348
module.exports.isInAlphabeticalOrder = isInAlphabeticalOrder;

tests/unit/rules/prefer-alphabetical-bundledDependencies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('prefer-alphabetical-bundledDependencies Unit Tests', function() {
2222
response.lintId.should.equal('prefer-alphabetical-bundledDependencies');
2323
response.lintType.should.equal('error');
2424
response.node.should.equal('bundledDependencies');
25-
response.lintMessage.should.equal('Your bundledDependencies are not in alphabetical order. Please update the order.');
25+
response.lintMessage.should.equal('Your bundledDependencies are not in alphabetical order. Please move semver after chalk.');
2626
});
2727
});
2828

tests/unit/rules/prefer-alphabetical-dependencies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('prefer-alphabetical-dependencies Unit Tests', function() {
2222
response.lintId.should.equal('prefer-alphabetical-dependencies');
2323
response.lintType.should.equal('error');
2424
response.node.should.equal('dependencies');
25-
response.lintMessage.should.equal('Your dependencies are not in alphabetical order. Please update the order.');
25+
response.lintMessage.should.equal('Your dependencies are not in alphabetical order. Please move semver after chalk.');
2626
});
2727
});
2828

0 commit comments

Comments
 (0)