Skip to content

Commit 13a568d

Browse files
authored
Closes #57 and #58 (#60)
* Closes #57 and #58 This change gives better recommendations for what change is required by the user to resolve the lint issue. It also no longer throws an error when a property exists in the package.json file that doesn't exist in the preferred property order array. Thanks @moshest for the input. * Remove unused variable
1 parent 8c95822 commit 13a568d

File tree

5 files changed

+62
-34
lines changed

5 files changed

+62
-34
lines changed

CHANGELOG.md

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

1414

15+
## [2.10.0] - 2017-09-02
16+
### Changed
17+
- Addressed issues, from @moshest, [#57](https://github.com/tclindner/npm-package-json-lint/issues/57) and [#58](https://github.com/tclindner/npm-package-json-lint/issues/58). This change gives better recommendations for what change is required by the user to resolve the lint issue. It also no longer throws an error when a property exists in the package.json file that doesn't exist in the preferred property order array. Thanks @moshest.
18+
1519
## [2.9.0] - 2017-08-29
1620
### Changed
1721
- Update all rules to export the type of rule they are. Current valid values are "standard" and "array". The rules loader has been updated to references the ruleType export rather than trying to maintain a separate list of array style rules. This change closes [issue #56](https://github.com/tclindner/npm-package-json-lint/issues/56) and should prevent the issue discussed in [issue #53](https://github.com/tclindner/npm-package-json-lint/issues/53) from occurring again.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-package-json-lint",
3-
"version": "2.9.0",
3+
"version": "2.10.0",
44
"description": "CLI app for linting package.json files.",
55
"keywords": [
66
"lint",
@@ -42,17 +42,17 @@
4242
"plur": "^2.1.2",
4343
"semver": "^5.4.1",
4444
"user-home": "^2.0.0",
45-
"validator": "^8.0.0"
45+
"validator": "^8.1.0"
4646
},
4747
"devDependencies": {
48-
"chai": "^4.1.1",
48+
"chai": "^4.1.2",
4949
"eslint": "^4.4.1",
5050
"eslint-config-tc": "^2.1.0",
5151
"eslint-formatter-pretty": "^1.1.0",
5252
"figures": "^2.0.0",
5353
"mocha": "^3.5.0",
5454
"nyc": "^11.1.0",
55-
"sinon": "^3.1.0"
55+
"sinon": "^3.2.1"
5656
},
5757
"engines": {
5858
"node": ">=4.2.0",

src/validators/property-order.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const notFound = -1;
66
const empty = 0;
7+
const one = 1;
78
const increment = 1;
89
const defaultPreferredNodeOrder = [
910
'name',
@@ -62,29 +63,26 @@ const defaultPreferredNodeOrder = [
6263
const isInPreferredOrder = function(packageJsonData, userPreferredNodeOrder) {
6364
let isValid = true;
6465
let msg = null;
65-
const actualNodeList = Object.keys(packageJsonData);
6666
const preferredNodeOrder = userPreferredNodeOrder.length === empty ? Array.from(defaultPreferredNodeOrder) : Array.from(userPreferredNodeOrder);
67-
const preferredNodeOrderCopy = Array.from(preferredNodeOrder);
67+
const fltrdPreferredNodeOrder = preferredNodeOrder.filter((property) => packageJsonData.hasOwnProperty(property));
68+
const actualNodeList = Object.keys(packageJsonData);
69+
const fltrdActualNodeList = actualNodeList.filter((property) => preferredNodeOrder.indexOf(property) !== notFound);
70+
const filteredPreferredOrderMap = new Map();
6871

69-
for (let keyIndex = 0;keyIndex < actualNodeList.length;keyIndex += increment) {
70-
let preferredNodeOrderItem = null;
72+
fltrdPreferredNodeOrder.forEach((property, index) => {
73+
filteredPreferredOrderMap.set(property, index);
74+
});
7175

72-
if (preferredNodeOrder.indexOf(actualNodeList[keyIndex]) === notFound) {
73-
isValid = false;
74-
msg = `${actualNodeList[keyIndex]} is not in the preferred property list.`;
75-
break;
76-
}
76+
for (let keyIndex = 0;keyIndex < fltrdActualNodeList.length;keyIndex += increment) {
77+
const currentPkgJsonProperty = fltrdActualNodeList[keyIndex];
78+
79+
const preferredOrderPosition = filteredPreferredOrderMap.get(currentPkgJsonProperty);
7780

78-
if (preferredNodeOrderCopy.indexOf(actualNodeList[keyIndex]) === notFound) {
81+
if (preferredOrderPosition !== keyIndex) {
7982
isValid = false;
80-
msg = `Please move ${actualNodeList[keyIndex]} before ${actualNodeList[keyIndex - increment]}.`;
83+
msg = `Please move "${currentPkgJsonProperty}" after "${fltrdPreferredNodeOrder[preferredOrderPosition - one]}".`;
8184
break;
8285
}
83-
84-
do {
85-
preferredNodeOrderItem = preferredNodeOrderCopy.shift();
86-
87-
} while (actualNodeList[keyIndex] !== preferredNodeOrderItem);
8886
}
8987

9088
return {

tests/unit/rules/prefer-property-order.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('prefer-property-order Unit Tests', function() {
6666
response.lintId.should.equal('prefer-property-order');
6767
response.lintType.should.equal('error');
6868
response.node.should.equal('');
69-
response.lintMessage.should.equal('Your package.json properties are not in the desired order. Please move version before description.');
69+
response.lintMessage.should.equal('Your package.json properties are not in the desired order. Please move "description" after "version".');
7070
});
7171
});
7272
});

tests/unit/validators/property-order.test.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('property-order Unit Tests', function() {
4242
});
4343

4444
context('when the actual node list does not have the same number of nodes as the desired list', function() {
45-
it('false should be returned', function() {
45+
it('true should be returned', function() {
4646
const packageJson = {
4747
name: 'awesome-module',
4848
version: '1.0.0'
@@ -74,7 +74,7 @@ describe('property-order Unit Tests', function() {
7474
const response = propertyOrder.isInPreferredOrder(packageJson, preferredOrder);
7575

7676
response.status.should.be.false;
77-
response.msg.should.equal('Please move version before description.');
77+
response.msg.should.equal('Please move "description" after "version".');
7878
});
7979
});
8080

@@ -93,12 +93,12 @@ describe('property-order Unit Tests', function() {
9393
const response = propertyOrder.isInPreferredOrder(packageJson, preferredOrder);
9494

9595
response.status.should.be.false;
96-
response.msg.should.equal('Please move name before version.');
96+
response.msg.should.equal('Please move "version" after "name".');
9797
});
9898
});
9999

100100
context('when the actual node list is in a different order than desired', function() {
101-
it('false should be returned', function() {
101+
it('true should be returned', function() {
102102
const packageJson = {
103103
name: 'awesome-module',
104104
version: '1.0.0',
@@ -118,7 +118,7 @@ describe('property-order Unit Tests', function() {
118118
});
119119

120120
context('when the actual node list is in a different order than desired', function() {
121-
it('false should be returned', function() {
121+
it('true should be returned', function() {
122122
const packageJson = {
123123
name: 'awesome-module',
124124
version: '1.0.0',
@@ -163,8 +163,34 @@ describe('property-order Unit Tests', function() {
163163
});
164164
});
165165

166+
context('when the actual node list is in correct order, but has extra values in preferred order', function() {
167+
it('false should be returned', function() {
168+
const packageJson = {
169+
name: 'awesome-module',
170+
version: '1.0.0',
171+
description: 'description',
172+
homepage: 'https://github.com/tclindner/npm-package-json-lint',
173+
license: 'MIT',
174+
keywords: ['awesome']
175+
};
176+
const preferredOrder = [
177+
'name',
178+
'version',
179+
'description',
180+
'scripts',
181+
'bin',
182+
'keywords',
183+
'homepage'
184+
];
185+
const response = propertyOrder.isInPreferredOrder(packageJson, preferredOrder);
186+
187+
response.status.should.be.false;
188+
response.msg.should.equal('Please move "homepage" after "keywords".');
189+
});
190+
});
191+
166192
context('when the actual node list is not in correct order and also has extra values in preferred order', function() {
167-
it('true should be returned', function() {
193+
it('false should be returned', function() {
168194
const packageJson = {
169195
name: 'awesome-module',
170196
version: '1.0.0',
@@ -184,12 +210,12 @@ describe('property-order Unit Tests', function() {
184210
const response = propertyOrder.isInPreferredOrder(packageJson, preferredOrder);
185211

186212
response.status.should.be.false;
187-
response.msg.should.equal('Please move keywords before homepage.');
213+
response.msg.should.equal('Please move "homepage" after "keywords".');
188214
});
189215
});
190216

191217
context('when node is not in the preferred node list', function() {
192-
it('false should be returned', function() {
218+
it('true should be returned', function() {
193219
const packageJson = {
194220
name: 'awesome-module',
195221
version: '1.0.0',
@@ -204,13 +230,13 @@ describe('property-order Unit Tests', function() {
204230
];
205231
const response = propertyOrder.isInPreferredOrder(packageJson, preferredOrder);
206232

207-
response.status.should.be.false;
208-
response.msg.should.equal('description is not in the preferred property list.');
233+
response.status.should.be.true;
234+
(response.msg === null).should.be.true;
209235
});
210236
});
211237

212238
context('when node is not in the preferred node list', function() {
213-
it('false should be returned', function() {
239+
it('true should be returned', function() {
214240
const packageJson = {
215241
name: 'awesome-module',
216242
version: '1.0.0',
@@ -224,8 +250,8 @@ describe('property-order Unit Tests', function() {
224250
];
225251
const response = propertyOrder.isInPreferredOrder(packageJson, preferredOrder);
226252

227-
response.status.should.be.false;
228-
response.msg.should.equal('name is not in the preferred property list.');
253+
response.status.should.be.true;
254+
(response.msg === null).should.be.true;
229255
});
230256
});
231257
});

0 commit comments

Comments
 (0)