Skip to content

Commit

Permalink
Standard (#16)
Browse files Browse the repository at this point in the history
* - Linting: Apply "standard" config

* - Linting (ESLint): Add `no-var` rule

* - Linting: Add `prefer-destructuring` rule

* - Linting: Add `object-shorthand` rule

* - Use exisitng pattern in config of 2 for error

* - Use `includes` over `indexOf` where possible (`prefer-includes` from unicorn config without its config)

* - npm: Add `standard` to devDeps
  • Loading branch information
brettz9 authored Mar 6, 2020
1 parent 9adea3d commit 423bc25
Show file tree
Hide file tree
Showing 41 changed files with 2,189 additions and 1,659 deletions.
8 changes: 6 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"extends": "eslint:recommended",
"extends": "standard",
"rules": {
"no-var": [2],
"prefer-destructuring": [2],
"object-shorthand": [2],
"quotes": [2, "double"]
},
"overrides": [{
Expand All @@ -9,6 +12,7 @@
}],
"env": {
"browser": true,
"node": true
"node": true,
"es6": true
}
}
17 changes: 8 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
* @copyright 2015 onechiporenko. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict";
"use strict"

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
var path = require("path");
var requireIndex = require("requireindex");
// ------------------------------------------------------------------------------
const path = require("path")
const requireIndex = require("requireindex")

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Plugin Definition
//------------------------------------------------------------------------------

// ------------------------------------------------------------------------------

// import all rules in lib/rules
module.exports = {
Expand Down Expand Up @@ -58,4 +57,4 @@ module.exports = {
}
}
}
};
}
70 changes: 35 additions & 35 deletions lib/rules/asserts-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,73 @@
* @copyright 2015 onechiporenko. All rights reserved.
*/

"use strict";
"use strict"

var n = require("../utils/node.js");
var isSkipSkipped = require("../utils/options.js").isSkipSkipped;
const n = require("../utils/node.js")
const { isSkipSkipped } = require("../utils/options.js")

var hasOwnProperty = Object.prototype.hasOwnProperty;
const { hasOwnProperty } = Object.prototype

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

module.exports = {
create: function (context) {
var insideIt = false;
var assertsCounter = 0;
var options = context.options[0] || {};
var assertsLimit = options.assertsLimit >= 1 ? options.assertsLimit : 3;
var skipSkipped = isSkipSkipped(options, context);
var ignoreZeroAssertionsIfDoneExists = hasOwnProperty.call(options, "ignoreZeroAssertionsIfDoneExists") ? options.ignoreZeroAssertionsIfDoneExists : true;
var nodeSkipped;
var doneExists;
create (context) {
let insideIt = false
let assertsCounter = 0
const options = context.options[0] || {}
const assertsLimit = options.assertsLimit >= 1 ? options.assertsLimit : 3
const skipSkipped = isSkipSkipped(options, context)
const ignoreZeroAssertionsIfDoneExists = hasOwnProperty.call(options, "ignoreZeroAssertionsIfDoneExists") ? options.ignoreZeroAssertionsIfDoneExists : true
let nodeSkipped
let doneExists

function check(node) {
function check (node) {
if (!insideIt || nodeSkipped) {
return;
return
}
if (n.isAssertion(node)) {
return assertsCounter++;
return assertsCounter++
}
}

function fEnter(node) {
function fEnter (node) {
if (n.isTestBody(node)) {
if (skipSkipped) {
nodeSkipped = n.tryDetectSkipInParent(node);
nodeSkipped = n.tryDetectSkipInParent(node)
}
doneExists = node.params.length > 0;
insideIt = true;
doneExists = node.params.length > 0
insideIt = true
}
}

function fExit(node) {
function fExit (node) {
if (n.isTestBody(node)) {
if (assertsCounter > assertsLimit) {
context.report(node.parent, "Too many assertions ({{num}}). Maximum allowed is {{max}}.", {
max: assertsLimit,
num: assertsCounter
});
})
}
if (assertsCounter === 0 && !nodeSkipped && !(ignoreZeroAssertionsIfDoneExists && doneExists)) {
context.report(node.parent, "Test without assertions is not allowed.");
context.report(node.parent, "Test without assertions is not allowed.")
}
insideIt = false;
nodeSkipped = false;
doneExists = false;
assertsCounter = 0;
insideIt = false
nodeSkipped = false
doneExists = false
assertsCounter = 0
}
}

return {
"FunctionExpression": fEnter,
"ArrowFunctionExpression": fEnter,
FunctionExpression: fEnter,
ArrowFunctionExpression: fEnter,
"FunctionExpression:exit": fExit,
"ArrowFunctionExpression:exit": fExit,
"MemberExpression": check,
"CallExpression": check
};
MemberExpression: check,
CallExpression: check
}
},

meta: {
Expand All @@ -93,4 +93,4 @@ module.exports = {
}
]
}
};
}
93 changes: 46 additions & 47 deletions lib/rules/complexity-it.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,41 @@
* @copyright 2015 onechiporenko. All rights reserved.
*/

"use strict";
"use strict"

var n = require("../utils/node.js");
var isSkipSkipped = require("../utils/options.js").isSkipSkipped;
var hasOwnProperty = Object.prototype.hasOwnProperty;
const n = require("../utils/node.js")
const { isSkipSkipped } = require("../utils/options.js")
const { hasOwnProperty } = Object.prototype

//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------

module.exports = {
create: function (context) {
create (context) {
let insideIt = false
let currentComplexityCount = 0
const options = context.options[0] || {}
const maxAllowedComplexity = hasOwnProperty.call(options, "maxAllowedComplexity") ? options.maxAllowedComplexity : 40
const skipSkipped = isSkipSkipped(options, context)
let nodeSkipped

var insideIt = false;
var currentComplexityCount = 0;
var options = context.options[0] || {};
var maxAllowedComplexity = hasOwnProperty.call(options, "maxAllowedComplexity") ? options.maxAllowedComplexity : 40;
var skipSkipped = isSkipSkipped(options, context);
var nodeSkipped;

function increaseComplexity() {
function increaseComplexity () {
if (!insideIt) {
return;
return
}
if (nodeSkipped) {
return;
return
}
currentComplexityCount++;
currentComplexityCount++
}

function fEnter (node) {
if (n.isTestBody(node)) {
if (skipSkipped) {
nodeSkipped = n.tryDetectSkipInParent(node);
nodeSkipped = n.tryDetectSkipInParent(node)
}
insideIt = true;
insideIt = true
}
}

Expand All @@ -51,51 +50,51 @@ module.exports = {
max: maxAllowedComplexity,
num: currentComplexityCount,
name: n.getCaller(node.parent)
});
})
}
insideIt = false;
nodeSkipped = false;
currentComplexityCount = 0;
insideIt = false
nodeSkipped = false
currentComplexityCount = 0
}
}

return {
"FunctionExpression": fEnter,
"ArrowFunctionExpression": fEnter,
FunctionExpression: fEnter,
ArrowFunctionExpression: fEnter,
"FunctionExpression:exit": fExit,
"ArrowFunctionExpression:exit": fExit,

"MemberExpression": function (node) {
MemberExpression (node) {
if (n.isSinonAssert(node) || n.isChaiAssert(node) || n.isChaiShould(node) || n.isChaiChainable(node)) {
return;
return
}
if (node.parent.type === "MemberExpression") {
return;
return
}
increaseComplexity();
increaseComplexity()
},

"CallExpression": function (node) {
CallExpression (node) {
if (n.isChaiExpect(node)) {
return;
return
}
increaseComplexity();
increaseComplexity()
},

"FunctionDeclaration": increaseComplexity,
"ExpressionStatement": increaseComplexity,
"CatchClause": increaseComplexity,
"ConditionalExpression": increaseComplexity,
"LogicalExpression": increaseComplexity,
"ForStatement": increaseComplexity,
"ForInStatement": increaseComplexity,
"ForOfStatement": increaseComplexity,
"IfStatement": increaseComplexity,
"SwitchCase": increaseComplexity,
"WhileStatement": increaseComplexity,
"DoWhileStatement": increaseComplexity
FunctionDeclaration: increaseComplexity,
ExpressionStatement: increaseComplexity,
CatchClause: increaseComplexity,
ConditionalExpression: increaseComplexity,
LogicalExpression: increaseComplexity,
ForStatement: increaseComplexity,
ForInStatement: increaseComplexity,
ForOfStatement: increaseComplexity,
IfStatement: increaseComplexity,
SwitchCase: increaseComplexity,
WhileStatement: increaseComplexity,
DoWhileStatement: increaseComplexity

};
}
},

meta: {
Expand All @@ -115,4 +114,4 @@ module.exports = {
}
]
}
};
}
Loading

0 comments on commit 423bc25

Please sign in to comment.