Skip to content

Commit 46ebfb5

Browse files
committed
2 parents ccc55c9 + d6620f4 commit 46ebfb5

14 files changed

+260
-43
lines changed

src/rules/prefer-alphabetical-bundledDependencies.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {isInAlphabeticalOrder} = require('./../validators/alphabetical-sort');
2+
const {exists} = require('../validators/property');
23
const LintIssue = require('./../LintIssue');
34

45
const lintId = 'prefer-alphabetical-bundledDependencies';
@@ -7,6 +8,10 @@ const message = 'Your bundledDependencies are not in alphabetical order.';
78
const ruleType = 'standard';
89

910
const lint = (packageJsonData, severity) => {
11+
if (!exists(packageJsonData, nodeName)) {
12+
return true;
13+
}
14+
1015
const result = isInAlphabeticalOrder(packageJsonData, nodeName);
1116

1217
if (!result.status) {
@@ -21,5 +26,7 @@ const lint = (packageJsonData, severity) => {
2126
return true;
2227
};
2328

24-
module.exports.lint = lint;
25-
module.exports.ruleType = ruleType;
29+
module.exports = {
30+
lint,
31+
ruleType
32+
};

src/rules/prefer-alphabetical-dependencies.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {isInAlphabeticalOrder} = require('./../validators/alphabetical-sort');
2+
const {exists} = require('../validators/property');
23
const LintIssue = require('./../LintIssue');
34

45
const lintId = 'prefer-alphabetical-dependencies';
@@ -7,6 +8,10 @@ const message = 'Your dependencies are not in alphabetical order.';
78
const ruleType = 'standard';
89

910
const lint = (packageJsonData, severity) => {
11+
if (!exists(packageJsonData, nodeName)) {
12+
return true;
13+
}
14+
1015
const result = isInAlphabeticalOrder(packageJsonData, nodeName);
1116

1217
if (!result.status) {
@@ -21,5 +26,7 @@ const lint = (packageJsonData, severity) => {
2126
return true;
2227
};
2328

24-
module.exports.lint = lint;
25-
module.exports.ruleType = ruleType;
29+
module.exports = {
30+
lint,
31+
ruleType
32+
};

src/rules/prefer-alphabetical-devDependencies.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {isInAlphabeticalOrder} = require('./../validators/alphabetical-sort');
2+
const {exists} = require('../validators/property');
23
const LintIssue = require('./../LintIssue');
34

45
const lintId = 'prefer-alphabetical-devDependencies';
@@ -7,6 +8,10 @@ const message = 'Your devDependencies are not in alphabetical order.';
78
const ruleType = 'standard';
89

910
const lint = (packageJsonData, severity) => {
11+
if (!exists(packageJsonData, nodeName)) {
12+
return true;
13+
}
14+
1015
const result = isInAlphabeticalOrder(packageJsonData, nodeName);
1116

1217
if (!result.status) {
@@ -21,5 +26,7 @@ const lint = (packageJsonData, severity) => {
2126
return true;
2227
};
2328

24-
module.exports.lint = lint;
25-
module.exports.ruleType = ruleType;
29+
module.exports = {
30+
lint,
31+
ruleType
32+
};

src/rules/prefer-alphabetical-optionalDependencies.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {isInAlphabeticalOrder} = require('./../validators/alphabetical-sort');
2+
const {exists} = require('../validators/property');
23
const LintIssue = require('./../LintIssue');
34

45
const lintId = 'prefer-alphabetical-optionalDependencies';
@@ -7,6 +8,10 @@ const message = 'Your optionalDependencies are not in alphabetical order.';
78
const ruleType = 'standard';
89

910
const lint = (packageJsonData, severity) => {
11+
if (!exists(packageJsonData, nodeName)) {
12+
return true;
13+
}
14+
1015
const result = isInAlphabeticalOrder(packageJsonData, nodeName);
1116

1217
if (!result.status) {
@@ -21,5 +26,7 @@ const lint = (packageJsonData, severity) => {
2126
return true;
2227
};
2328

24-
module.exports.lint = lint;
25-
module.exports.ruleType = ruleType;
29+
module.exports = {
30+
lint,
31+
ruleType
32+
};

src/rules/prefer-alphabetical-peerDependencies.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {isInAlphabeticalOrder} = require('./../validators/alphabetical-sort');
2+
const {exists} = require('../validators/property');
23
const LintIssue = require('./../LintIssue');
34

45
const lintId = 'prefer-alphabetical-peerDependencies';
@@ -7,6 +8,10 @@ const message = 'Your peerDependencies are not in alphabetical order.';
78
const ruleType = 'standard';
89

910
const lint = (packageJsonData, severity) => {
11+
if (!exists(packageJsonData, nodeName)) {
12+
return true;
13+
}
14+
1015
const result = isInAlphabeticalOrder(packageJsonData, nodeName);
1116

1217
if (!result.status) {
@@ -21,5 +26,7 @@ const lint = (packageJsonData, severity) => {
2126
return true;
2227
};
2328

24-
module.exports.lint = lint;
25-
module.exports.ruleType = ruleType;
29+
module.exports = {
30+
lint,
31+
ruleType
32+
};
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const {exists} = require('../validators/property');
12
const LintIssue = require('./../LintIssue');
23

34
const lintId = 'prefer-no-engineStrict';
@@ -6,12 +7,14 @@ const message = 'engineStrict was deprecated with npm v3.0.0. Please remove it f
67
const ruleType = 'standard';
78

89
const lint = (packageJsonData, severity) => {
9-
if (packageJsonData.hasOwnProperty(nodeName)) {
10+
if (exists(packageJsonData, nodeName)) {
1011
return new LintIssue(lintId, severity, nodeName, message);
1112
}
1213

1314
return true;
1415
};
1516

16-
module.exports.lint = lint;
17-
module.exports.ruleType = ruleType;
17+
module.exports = {
18+
lint,
19+
ruleType
20+
};

src/validators/alphabetical-sort.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ const increment = 1;
77
* @return {object} Object containing the status and the dependencies that are out of order, if applicable
88
*/
99
const isInAlphabeticalOrder = (packageJsonData, nodeName) => {
10-
if (!packageJsonData.hasOwnProperty(nodeName)) {
11-
return {
12-
status: true,
13-
data: {
14-
invalidNode: null,
15-
validNode: null
16-
}
17-
};
18-
}
19-
2010
let isValid = true;
2111
let data = {
2212
invalidNode: null,
@@ -42,4 +32,6 @@ const isInAlphabeticalOrder = (packageJsonData, nodeName) => {
4232
};
4333
};
4434

45-
module.exports.isInAlphabeticalOrder = isInAlphabeticalOrder;
35+
module.exports = {
36+
isInAlphabeticalOrder
37+
};

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
const ruleModule = require('./../../../src/rules/prefer-alphabetical-bundledDependencies');
2+
const alphabeticalSort = require('../../../src/validators/alphabetical-sort');
3+
const property = require('../../../src/validators/property');
24

35
const {lint, ruleType} = ruleModule;
46

7+
jest.mock('../../../src/validators/alphabetical-sort');
8+
jest.mock('../../../src/validators/property');
9+
10+
const nodeName = 'bundledDependencies';
11+
512
describe('prefer-alphabetical-bundledDependencies Unit Tests', () => {
613
describe('a rule type value should be exported', () => {
714
test('it should equal "standard"', () => {
@@ -11,6 +18,15 @@ describe('prefer-alphabetical-bundledDependencies Unit Tests', () => {
1118

1219
describe('when package.json has node with an invalid order', () => {
1320
test('LintIssue object should be returned', () => {
21+
alphabeticalSort.isInAlphabeticalOrder.mockReturnValue({
22+
status: false,
23+
data: {
24+
invalidNode: 'semver',
25+
validNode: 'chalk'
26+
}
27+
});
28+
property.exists.mockReturnValue(true);
29+
1430
const packageJsonData = {
1531
bundledDependencies: {
1632
semver: '^5.3.0',
@@ -26,11 +42,22 @@ describe('prefer-alphabetical-bundledDependencies Unit Tests', () => {
2642
expect(response.lintMessage).toStrictEqual(
2743
'Your bundledDependencies are not in alphabetical order. Please move semver after chalk.'
2844
);
45+
46+
expect(property.exists).toHaveBeenCalledTimes(1);
47+
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
48+
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledTimes(1);
49+
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledWith(packageJsonData, nodeName);
2950
});
3051
});
3152

3253
describe('when package.json has node with a valid order', () => {
33-
test('LintIssue object should be returned', () => {
54+
test('true should be returned', () => {
55+
alphabeticalSort.isInAlphabeticalOrder.mockReturnValue({
56+
status: true,
57+
data: {}
58+
});
59+
property.exists.mockReturnValue(true);
60+
3461
const packageJsonData = {
3562
bundledDependencies: {
3663
chalk: '^1.1.3',
@@ -41,15 +68,25 @@ describe('prefer-alphabetical-bundledDependencies Unit Tests', () => {
4168
const response = lint(packageJsonData, 'error');
4269

4370
expect(response).toBeTruthy();
71+
72+
expect(property.exists).toHaveBeenCalledTimes(1);
73+
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
74+
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledTimes(1);
75+
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledWith(packageJsonData, nodeName);
4476
});
4577
});
4678

4779
describe('when package.json does not have node', () => {
4880
test('true should be returned', () => {
81+
property.exists.mockReturnValue(false);
82+
4983
const packageJsonData = {};
5084
const response = lint(packageJsonData, 'error');
5185

5286
expect(response).toBeTruthy();
87+
88+
expect(property.exists).toHaveBeenCalledTimes(1);
89+
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
5390
});
5491
});
5592
});

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
const ruleModule = require('./../../../src/rules/prefer-alphabetical-dependencies');
2+
const alphabeticalSort = require('../../../src/validators/alphabetical-sort');
3+
const property = require('../../../src/validators/property');
24

35
const {lint, ruleType} = ruleModule;
46

7+
jest.mock('../../../src/validators/alphabetical-sort');
8+
jest.mock('../../../src/validators/property');
9+
10+
const nodeName = 'dependencies';
11+
512
describe('prefer-alphabetical-dependencies Unit Tests', () => {
613
describe('a rule type value should be exported', () => {
714
test('it should equal "standard"', () => {
@@ -11,6 +18,15 @@ describe('prefer-alphabetical-dependencies Unit Tests', () => {
1118

1219
describe('when package.json has node with an invalid order', () => {
1320
test('LintIssue object should be returned', () => {
21+
alphabeticalSort.isInAlphabeticalOrder.mockReturnValue({
22+
status: false,
23+
data: {
24+
invalidNode: 'semver',
25+
validNode: 'chalk'
26+
}
27+
});
28+
property.exists.mockReturnValue(true);
29+
1430
const packageJsonData = {
1531
dependencies: {
1632
semver: '^5.3.0',
@@ -26,11 +42,22 @@ describe('prefer-alphabetical-dependencies Unit Tests', () => {
2642
expect(response.lintMessage).toStrictEqual(
2743
'Your dependencies are not in alphabetical order. Please move semver after chalk.'
2844
);
45+
46+
expect(property.exists).toHaveBeenCalledTimes(1);
47+
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
48+
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledTimes(1);
49+
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledWith(packageJsonData, nodeName);
2950
});
3051
});
3152

3253
describe('when package.json has node with a valid order', () => {
33-
test('LintIssue object should be returned', () => {
54+
test('true should be returned', () => {
55+
alphabeticalSort.isInAlphabeticalOrder.mockReturnValue({
56+
status: true,
57+
data: {}
58+
});
59+
property.exists.mockReturnValue(true);
60+
3461
const packageJsonData = {
3562
dependencies: {
3663
chalk: '^1.1.3',
@@ -41,15 +68,25 @@ describe('prefer-alphabetical-dependencies Unit Tests', () => {
4168
const response = lint(packageJsonData, 'error');
4269

4370
expect(response).toBeTruthy();
71+
72+
expect(property.exists).toHaveBeenCalledTimes(1);
73+
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
74+
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledTimes(1);
75+
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledWith(packageJsonData, nodeName);
4476
});
4577
});
4678

4779
describe('when package.json does not have node', () => {
4880
test('true should be returned', () => {
81+
property.exists.mockReturnValue(false);
82+
4983
const packageJsonData = {};
5084
const response = lint(packageJsonData, 'error');
5185

5286
expect(response).toBeTruthy();
87+
88+
expect(property.exists).toHaveBeenCalledTimes(1);
89+
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
5390
});
5491
});
5592
});

0 commit comments

Comments
 (0)