@roadmunk/eslint-plugin-roadmunk-custom
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -8,2 +8,4 @@ /** | ||
const { hasPropsWithValues } = require('../helper.js'); | ||
const DEFAULT_MAX_SPACES = 25; | ||
// ------------------------------------------------------------------------------ | ||
@@ -24,2 +26,4 @@ // Rule Definition | ||
create : function(context) { | ||
const maxSpaces = context.options && context.options[0] && context.options[0].maxSpaces || DEFAULT_MAX_SPACES; | ||
return { | ||
@@ -30,2 +34,3 @@ BlockStatement : checkFunction, | ||
function checkFunction(node) { | ||
@@ -41,3 +46,3 @@ // Make blocks out of statements that could be aligned | ||
(childNode.type === 'VariableDeclaration' && childNode.declarations.some(decl => decl.init !== null)) | ||
|| hasPropsWithValues(childNode, { 'type' : 'ExpressionStatement', 'expression.type' : 'AssignmentExpression', 'expression.operator' : '=' }) | ||
|| hasPropsWithValues(childNode, { 'type' : 'ExpressionStatement', 'expression.type' : 'AssignmentExpression' }) | ||
) | ||
@@ -75,7 +80,14 @@ | ||
// Figure out where the equals sign should go | ||
const equalsPositions = []; | ||
alignBlocks.forEach((block, index) => { | ||
equalsPositions[index] = 0; | ||
block.forEach(blockNode => { | ||
let tokenBeforeEquals = null; | ||
const alignBlockEqualsPositions = []; | ||
for (let blockIndex = 0; blockIndex < alignBlocks.length; blockIndex++) { | ||
const block = alignBlocks[blockIndex]; | ||
let prevEqualsPosition; | ||
let maxEqualsPosition = 0; | ||
let maxOperatorLength = 0; | ||
for (let nodeIndex = 0; nodeIndex < block.length; nodeIndex++) { | ||
const blockNode = block[nodeIndex]; | ||
let equalsPosition; | ||
let operatorLength; | ||
if (blockNode.type === 'VariableDeclaration') { | ||
@@ -87,14 +99,31 @@ // Find the first declaration that has an equals sign | ||
*/ | ||
tokenBeforeEquals = blockNode.declarations.find(decl => decl.init !== null).id; | ||
const tokenBeforeEquals = blockNode.declarations.find(decl => decl.init !== null).id; | ||
equalsPosition = tokenBeforeEquals.loc.end.column; | ||
operatorLength = 1; | ||
} | ||
else { | ||
tokenBeforeEquals = blockNode.expression.left; | ||
equalsPosition = blockNode.expression.left.loc.end.column; | ||
operatorLength = blockNode.expression.operator.length; | ||
} | ||
equalsPositions[index] = Math.max(tokenBeforeEquals.loc.end.column + 1, equalsPositions[index]); | ||
}); | ||
}); | ||
// if this node needs too many or too few spaces, make a new block for it | ||
if ( | ||
prevEqualsPosition | ||
&& (equalsPosition > prevEqualsPosition + maxSpaces || equalsPosition < prevEqualsPosition - maxSpaces) | ||
) { | ||
alignBlocks.splice(blockIndex + 1, 0, block.splice(nodeIndex)); | ||
break; | ||
} | ||
prevEqualsPosition = equalsPosition; | ||
maxOperatorLength = Math.max(maxOperatorLength, operatorLength); | ||
maxEqualsPosition = Math.max(maxEqualsPosition, equalsPosition); | ||
} | ||
alignBlockEqualsPositions[blockIndex] = maxEqualsPosition + maxOperatorLength; | ||
} | ||
// Make sure all the equals signs are in the right places | ||
alignBlocks.forEach((block, index) => { | ||
const equalsPosition = equalsPositions[index]; | ||
const equalsPosition = alignBlockEqualsPositions[index]; | ||
block.forEach(blockNode => { | ||
@@ -104,2 +133,3 @@ const currentEqualsPos = indexOfEquals(blockNode, sourceCode.getText(blockNode)) + blockNode.loc.start.column; | ||
if (numSpacesNeeded !== 0) { | ||
const operatorLength = blockNode.type === 'VariableDeclaration' ? 1 : blockNode.expression.operator.length; | ||
context.report({ | ||
@@ -110,7 +140,7 @@ node, | ||
line : blockNode.loc.start.line, | ||
column : currentEqualsPos + blockNode.loc.start.column, | ||
column : currentEqualsPos - operatorLength + 1, | ||
}, | ||
end : { | ||
line : blockNode.loc.start.line, | ||
column : currentEqualsPos + blockNode.loc.start.column + 1, | ||
column : currentEqualsPos + 1, | ||
}, | ||
@@ -117,0 +147,0 @@ }, |
{ | ||
"name": "@roadmunk/eslint-plugin-roadmunk-custom", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Plugin to hold custom ESLint rules for Roadmunk", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -55,1 +55,4 @@ # eslint-plugin-roadmunk-custom | ||
`assert-length` : Uses the correct assertion method to check for length of an array | ||
`no-lodash-deprecated-functions` : Prevents usage of deprecated lodash functions. | ||
@@ -87,5 +87,11 @@ /** | ||
` | ||
var a = 4; | ||
b += 7;`, | ||
var a = 4; | ||
b += 7;`, | ||
` | ||
asdf += 4; | ||
b = 7;`, | ||
` | ||
var a = 4; | ||
b >>>= 7;`, | ||
` | ||
var a = 1; | ||
@@ -104,2 +110,20 @@ var ab = function() { | ||
b = 2;`, | ||
` | ||
thisVariableHas27Characters = 2; | ||
b = 3; | ||
`, | ||
{ | ||
code : ` | ||
thisVariableHas22Chars = 2; | ||
b = 3; | ||
`, | ||
options : [ { maxSpaces : 40 } ], | ||
}, | ||
{ | ||
code : ` | ||
thisVariableHas22Chars = 2; | ||
b = 3; | ||
`, | ||
options : [ { maxSpaces : 20 } ], | ||
}, | ||
], | ||
@@ -186,3 +210,21 @@ invalid : [ | ||
}, | ||
{ | ||
code : ` | ||
thisVariableHas27Characters += 2; | ||
b = 3;`, | ||
errors : [ {} ], | ||
output : ` | ||
thisVariableHas27Characters += 2; | ||
b = 3;`, | ||
}, | ||
{ | ||
code : ` | ||
var a = 2; | ||
b += 3;`, | ||
errors : [ {}, {} ], | ||
output : ` | ||
var a = 2; | ||
b += 3;`, | ||
}, | ||
], | ||
}); |
64593
24
1855
58