Skip to content

Commit

Permalink
Alpha rule updates (#119)
Browse files Browse the repository at this point in the history
* Remove validator and add ajv-errors

* Add new property exist module

* Add the ability to add exceptions

Closes #93

* Update format.test.js

* Update format.js

* Update format.js

* Enhance name format rule

Closes #115

* Update alpha rules
  • Loading branch information
tclindner committed Jun 29, 2019
1 parent 1dd158f commit d6620f4
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 43 deletions.
11 changes: 9 additions & 2 deletions src/rules/prefer-alphabetical-bundledDependencies.js
@@ -1,4 +1,5 @@
const {isInAlphabeticalOrder} = require('./../validators/alphabetical-sort');
const {exists} = require('../validators/property');
const LintIssue = require('./../LintIssue');

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

const lint = (packageJsonData, severity) => {
if (!exists(packageJsonData, nodeName)) {
return true;
}

const result = isInAlphabeticalOrder(packageJsonData, nodeName);

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

module.exports.lint = lint;
module.exports.ruleType = ruleType;
module.exports = {
lint,
ruleType
};
11 changes: 9 additions & 2 deletions src/rules/prefer-alphabetical-dependencies.js
@@ -1,4 +1,5 @@
const {isInAlphabeticalOrder} = require('./../validators/alphabetical-sort');
const {exists} = require('../validators/property');
const LintIssue = require('./../LintIssue');

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

const lint = (packageJsonData, severity) => {
if (!exists(packageJsonData, nodeName)) {
return true;
}

const result = isInAlphabeticalOrder(packageJsonData, nodeName);

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

module.exports.lint = lint;
module.exports.ruleType = ruleType;
module.exports = {
lint,
ruleType
};
11 changes: 9 additions & 2 deletions src/rules/prefer-alphabetical-devDependencies.js
@@ -1,4 +1,5 @@
const {isInAlphabeticalOrder} = require('./../validators/alphabetical-sort');
const {exists} = require('../validators/property');
const LintIssue = require('./../LintIssue');

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

const lint = (packageJsonData, severity) => {
if (!exists(packageJsonData, nodeName)) {
return true;
}

const result = isInAlphabeticalOrder(packageJsonData, nodeName);

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

module.exports.lint = lint;
module.exports.ruleType = ruleType;
module.exports = {
lint,
ruleType
};
11 changes: 9 additions & 2 deletions src/rules/prefer-alphabetical-optionalDependencies.js
@@ -1,4 +1,5 @@
const {isInAlphabeticalOrder} = require('./../validators/alphabetical-sort');
const {exists} = require('../validators/property');
const LintIssue = require('./../LintIssue');

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

const lint = (packageJsonData, severity) => {
if (!exists(packageJsonData, nodeName)) {
return true;
}

const result = isInAlphabeticalOrder(packageJsonData, nodeName);

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

module.exports.lint = lint;
module.exports.ruleType = ruleType;
module.exports = {
lint,
ruleType
};
11 changes: 9 additions & 2 deletions src/rules/prefer-alphabetical-peerDependencies.js
@@ -1,4 +1,5 @@
const {isInAlphabeticalOrder} = require('./../validators/alphabetical-sort');
const {exists} = require('../validators/property');
const LintIssue = require('./../LintIssue');

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

const lint = (packageJsonData, severity) => {
if (!exists(packageJsonData, nodeName)) {
return true;
}

const result = isInAlphabeticalOrder(packageJsonData, nodeName);

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

module.exports.lint = lint;
module.exports.ruleType = ruleType;
module.exports = {
lint,
ruleType
};
9 changes: 6 additions & 3 deletions src/rules/prefer-no-engineStrict.js
@@ -1,3 +1,4 @@
const {exists} = require('../validators/property');
const LintIssue = require('./../LintIssue');

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

const lint = (packageJsonData, severity) => {
if (packageJsonData.hasOwnProperty(nodeName)) {
if (exists(packageJsonData, nodeName)) {
return new LintIssue(lintId, severity, nodeName, message);
}

return true;
};

module.exports.lint = lint;
module.exports.ruleType = ruleType;
module.exports = {
lint,
ruleType
};
14 changes: 3 additions & 11 deletions src/validators/alphabetical-sort.js
Expand Up @@ -7,16 +7,6 @@ const increment = 1;
* @return {object} Object containing the status and the dependencies that are out of order, if applicable
*/
const isInAlphabeticalOrder = (packageJsonData, nodeName) => {
if (!packageJsonData.hasOwnProperty(nodeName)) {
return {
status: true,
data: {
invalidNode: null,
validNode: null
}
};
}

let isValid = true;
let data = {
invalidNode: null,
Expand All @@ -42,4 +32,6 @@ const isInAlphabeticalOrder = (packageJsonData, nodeName) => {
};
};

module.exports.isInAlphabeticalOrder = isInAlphabeticalOrder;
module.exports = {
isInAlphabeticalOrder
};
39 changes: 38 additions & 1 deletion test/unit/rules/prefer-alphabetical-bundledDependencies.test.js
@@ -1,7 +1,14 @@
const ruleModule = require('./../../../src/rules/prefer-alphabetical-bundledDependencies');
const alphabeticalSort = require('../../../src/validators/alphabetical-sort');
const property = require('../../../src/validators/property');

const {lint, ruleType} = ruleModule;

jest.mock('../../../src/validators/alphabetical-sort');
jest.mock('../../../src/validators/property');

const nodeName = 'bundledDependencies';

describe('prefer-alphabetical-bundledDependencies Unit Tests', () => {
describe('a rule type value should be exported', () => {
test('it should equal "standard"', () => {
Expand All @@ -11,6 +18,15 @@ describe('prefer-alphabetical-bundledDependencies Unit Tests', () => {

describe('when package.json has node with an invalid order', () => {
test('LintIssue object should be returned', () => {
alphabeticalSort.isInAlphabeticalOrder.mockReturnValue({
status: false,
data: {
invalidNode: 'semver',
validNode: 'chalk'
}
});
property.exists.mockReturnValue(true);

const packageJsonData = {
bundledDependencies: {
semver: '^5.3.0',
Expand All @@ -26,11 +42,22 @@ describe('prefer-alphabetical-bundledDependencies Unit Tests', () => {
expect(response.lintMessage).toStrictEqual(
'Your bundledDependencies are not in alphabetical order. Please move semver after chalk.'
);

expect(property.exists).toHaveBeenCalledTimes(1);
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledTimes(1);
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledWith(packageJsonData, nodeName);
});
});

describe('when package.json has node with a valid order', () => {
test('LintIssue object should be returned', () => {
test('true should be returned', () => {
alphabeticalSort.isInAlphabeticalOrder.mockReturnValue({
status: true,
data: {}
});
property.exists.mockReturnValue(true);

const packageJsonData = {
bundledDependencies: {
chalk: '^1.1.3',
Expand All @@ -41,15 +68,25 @@ describe('prefer-alphabetical-bundledDependencies Unit Tests', () => {
const response = lint(packageJsonData, 'error');

expect(response).toBeTruthy();

expect(property.exists).toHaveBeenCalledTimes(1);
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledTimes(1);
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledWith(packageJsonData, nodeName);
});
});

describe('when package.json does not have node', () => {
test('true should be returned', () => {
property.exists.mockReturnValue(false);

const packageJsonData = {};
const response = lint(packageJsonData, 'error');

expect(response).toBeTruthy();

expect(property.exists).toHaveBeenCalledTimes(1);
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
});
});
});
39 changes: 38 additions & 1 deletion test/unit/rules/prefer-alphabetical-dependencies.test.js
@@ -1,7 +1,14 @@
const ruleModule = require('./../../../src/rules/prefer-alphabetical-dependencies');
const alphabeticalSort = require('../../../src/validators/alphabetical-sort');
const property = require('../../../src/validators/property');

const {lint, ruleType} = ruleModule;

jest.mock('../../../src/validators/alphabetical-sort');
jest.mock('../../../src/validators/property');

const nodeName = 'dependencies';

describe('prefer-alphabetical-dependencies Unit Tests', () => {
describe('a rule type value should be exported', () => {
test('it should equal "standard"', () => {
Expand All @@ -11,6 +18,15 @@ describe('prefer-alphabetical-dependencies Unit Tests', () => {

describe('when package.json has node with an invalid order', () => {
test('LintIssue object should be returned', () => {
alphabeticalSort.isInAlphabeticalOrder.mockReturnValue({
status: false,
data: {
invalidNode: 'semver',
validNode: 'chalk'
}
});
property.exists.mockReturnValue(true);

const packageJsonData = {
dependencies: {
semver: '^5.3.0',
Expand All @@ -26,11 +42,22 @@ describe('prefer-alphabetical-dependencies Unit Tests', () => {
expect(response.lintMessage).toStrictEqual(
'Your dependencies are not in alphabetical order. Please move semver after chalk.'
);

expect(property.exists).toHaveBeenCalledTimes(1);
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledTimes(1);
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledWith(packageJsonData, nodeName);
});
});

describe('when package.json has node with a valid order', () => {
test('LintIssue object should be returned', () => {
test('true should be returned', () => {
alphabeticalSort.isInAlphabeticalOrder.mockReturnValue({
status: true,
data: {}
});
property.exists.mockReturnValue(true);

const packageJsonData = {
dependencies: {
chalk: '^1.1.3',
Expand All @@ -41,15 +68,25 @@ describe('prefer-alphabetical-dependencies Unit Tests', () => {
const response = lint(packageJsonData, 'error');

expect(response).toBeTruthy();

expect(property.exists).toHaveBeenCalledTimes(1);
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledTimes(1);
expect(alphabeticalSort.isInAlphabeticalOrder).toHaveBeenCalledWith(packageJsonData, nodeName);
});
});

describe('when package.json does not have node', () => {
test('true should be returned', () => {
property.exists.mockReturnValue(false);

const packageJsonData = {};
const response = lint(packageJsonData, 'error');

expect(response).toBeTruthy();

expect(property.exists).toHaveBeenCalledTimes(1);
expect(property.exists).toHaveBeenCalledWith(packageJsonData, nodeName);
});
});
});

0 comments on commit d6620f4

Please sign in to comment.