Socket
Socket
Sign inDemoInstall

jscs

Package Overview
Dependencies
Maintainers
3
Versions
95
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jscs - npm Package Compare versions

Comparing version 1.4.3 to 1.4.4

lib/rules/disallow-spaces-in-conditional-expression.js

2

lib/cli.js

@@ -35,3 +35,3 @@ /**

} else {
console.error('Default configuration source was not found.');
console.error('No configuration found. Add a .jscsrc file to your project root or use the -c option.');
}

@@ -38,0 +38,0 @@

@@ -26,9 +26,13 @@ var assert = require('assert');

if (operators[node.operator]) {
// get token before right part of expression
var tokenBeforeRightPart = tokenHelper.getTokenByRangeStart(file, node.right.range[0] - 1, true);
var part = tokenHelper.getTokenByRangeStartIfPunctuator(
file,
node.right.range[0] - 1,
node.operator,
true
);
if (!tokenHelper.tokenIsPunctuator(tokenBeforeRightPart, node.operator)) {
if (!part) {
errors.add(
'Operator ' + node.operator + ' should stick to following expression',
node.right.loc.start
tokenHelper.findOperatorByRangeStart(file, node.right.range[0], node.operator).loc.start
);

@@ -38,4 +42,22 @@ }

});
// Comma and assignment
if (operators[','] || operators['=']) {
file.iterateTokensByType('Punctuator', function(token, i, tokens) {
var operator = token.value;
if (operator !== ',' && operator !== '=') {
return;
}
var nextToken = tokens[i + 1];
if (nextToken && nextToken.range[0] !== token.range[1]) {
errors.add(
'Operator ' + operator + ' should stick to following expression',
token.loc.start
);
}
});
}
}
};

@@ -26,9 +26,12 @@ var assert = require('assert');

if (operators[node.operator]) {
// get token after left part of expression
var tokenAfterLeftPart = tokenHelper.getTokenByRangeStart(file, node.left.range[1]);
var part = tokenHelper.getTokenByRangeStartIfPunctuator(
file,
node.left.range[1],
node.operator
);
if (!tokenHelper.tokenIsPunctuator(tokenAfterLeftPart, node.operator)) {
if (!part) {
errors.add(
'Operator ' + node.operator + ' should stick to following expression',
node.left.loc.start
'Operator ' + node.operator + ' should stick to preceding expression',
tokenHelper.findOperatorByRangeStart(file, node.right.range[0], node.operator).loc.start
);

@@ -38,4 +41,22 @@ }

});
// Comma and assignment
if (operators[','] || operators['=']) {
file.iterateTokensByType('Punctuator', function(token, i, tokens) {
var operator = token.value;
if (operator !== ',' && operator !== '=') {
return;
}
var prevToken = tokens[i - 1];
if (prevToken && prevToken.range[1] !== token.range[0]) {
errors.add(
'Operator ' + operator + ' should stick to preceding expression',
token.loc.start
);
}
});
}
}
};

@@ -47,3 +47,3 @@ var assert = require('assert');

file.iterateNodesByType([ 'FunctionExpression' ], function(node) {
file.iterateNodesByType(['FunctionExpression'], function(node) {

@@ -50,0 +50,0 @@ // anonymous function expressions only

@@ -46,3 +46,3 @@ var assert = require('assert');

file.iterateNodesByType([ 'FunctionDeclaration' ], function(node) {
file.iterateNodesByType(['FunctionDeclaration'], function(node) {

@@ -49,0 +49,0 @@ if (beforeOpeningRoundBrace) {

@@ -46,3 +46,3 @@ var assert = require('assert');

file.iterateNodesByType([ 'FunctionDeclaration', 'FunctionExpression' ], function(node) {
file.iterateNodesByType(['FunctionDeclaration', 'FunctionExpression'], function(node) {

@@ -49,0 +49,0 @@ if (beforeOpeningRoundBrace) {

@@ -47,3 +47,3 @@ var assert = require('assert');

file.iterateNodesByType([ 'FunctionExpression' ], function(node) {
file.iterateNodesByType(['FunctionExpression'], function(node) {

@@ -50,0 +50,0 @@ // named function expressions only

@@ -19,3 +19,3 @@ var assert = require('assert');

function onevar(file, errors) {
file.iterateNodesByType([ 'Program', 'FunctionDeclaration', 'FunctionExpression' ], function(node) {
file.iterateNodesByType(['Program', 'FunctionDeclaration', 'FunctionExpression'], function(node) {
var firstVar = true;

@@ -28,3 +28,3 @@ var firstParent = true;

// Don't go in nested scopes
if ( !firstParent && type && [ 'FunctionDeclaration', 'FunctionExpression' ].indexOf(type) > -1) {
if ( !firstParent && type && ['FunctionDeclaration', 'FunctionExpression'].indexOf(type) > -1) {
return false;

@@ -31,0 +31,0 @@ }

@@ -34,3 +34,3 @@ var assert = require('assert');

if (openingBracket.loc.start.line + 2 !== nextToken.loc.start.line) {
if (nextToken.loc.start.line - openingBracket.loc.start.line < 2) {
errors.add('Expected a padding newline after opening curly brace', openingBracket.loc.end);

@@ -43,5 +43,19 @@ }

if (closingBracket.loc.start.line !== prevToken.loc.start.line + 2) {
if (closingBracket.loc.start.line - prevToken.loc.start.line < 2) {
errors.add('Expected a padding newline before closing curly brace', prevToken.loc.end);
}
file.getComments().some(function(comment) {
if (comment.range[1] < openingBracket.range[0] || closingBracket.range[1] < comment.range[0]) {
return false;
}
if (comment.loc.start.line - openingBracket.loc.start.line < 2) {
errors.add('Expected a padding newline after opening curly brace', comment.loc.start);
} else if (closingBracket.loc.start.line - comment.loc.end.line < 2) {
errors.add('Expected a padding newline before closing curly brace', comment.loc.end);
}
return false;
});
});

@@ -48,0 +62,0 @@ }

@@ -26,9 +26,13 @@ var assert = require('assert');

if (operators[node.operator]) {
// get token before right part of expression
var tokenBeforeRightPart = tokenHelper.getTokenByRangeStart(file, node.right.range[0] - 1, true);
var part = tokenHelper.getTokenByRangeStartIfPunctuator(
file,
node.right.range[0] - 1,
node.operator,
true
);
if (tokenHelper.tokenIsPunctuator(tokenBeforeRightPart, node.operator)) {
if (part) {
errors.add(
'Operator ' + node.operator + ' should not stick to following expression',
tokenBeforeRightPart.loc.start
part.loc.start
);

@@ -38,4 +42,22 @@ }

});
// Comma and assignment
if (operators[','] || operators['=']) {
file.iterateTokensByType('Punctuator', function(token, i, tokens) {
var operator = token.value;
if (operator !== ',' && operator !== '=') {
return;
}
var nextToken = tokens[i + 1];
if (nextToken && nextToken.range[0] === token.range[1]) {
errors.add(
'Operator ' + operator + ' should not stick to following expression',
token.loc.start
);
}
});
}
}
};

@@ -22,3 +22,3 @@ var assert = require('assert');

file.iterateTokensByType([ 'Keyword' ], function(token, i, tokens) {
file.iterateTokensByType(['Keyword'], function(token, i, tokens) {
if (keywordIndex[token.value]) {

@@ -25,0 +25,0 @@ var nextToken = tokens[i + 1];

@@ -26,9 +26,12 @@ var assert = require('assert');

if (operators[node.operator]) {
// get token after left part of expression
var tokenAfterLeftPart = tokenHelper.getTokenByRangeStart(file, node.left.range[1]);
var part = tokenHelper.getTokenByRangeStartIfPunctuator(
file,
node.left.range[1],
node.operator
);
if (tokenHelper.tokenIsPunctuator(tokenAfterLeftPart, node.operator)) {
if (part) {
errors.add(
'Operator ' + node.operator + ' should not stick to preceding expression',
tokenAfterLeftPart.loc.start
part.loc.start
);

@@ -38,4 +41,22 @@ }

});
// Comma and assignment
if (operators[','] || operators['=']) {
file.iterateTokensByType('Punctuator', function(token, i, tokens) {
var operator = token.value;
if (operator !== ',' && operator !== '=') {
return;
}
var prevToken = tokens[i - 1];
if (prevToken && prevToken.range[1] === token.range[0]) {
errors.add(
'Operator ' + operator + ' should not stick to preceding expression',
token.loc.start
);
}
});
}
}
};

@@ -47,3 +47,3 @@ var assert = require('assert');

file.iterateNodesByType([ 'FunctionExpression' ], function(node) {
file.iterateNodesByType(['FunctionExpression'], function(node) {

@@ -50,0 +50,0 @@ if (!node.id) {

@@ -46,3 +46,3 @@ var assert = require('assert');

file.iterateNodesByType([ 'FunctionDeclaration' ], function(node) {
file.iterateNodesByType(['FunctionDeclaration'], function(node) {

@@ -49,0 +49,0 @@ if (beforeOpeningRoundBrace) {

@@ -46,3 +46,3 @@ var assert = require('assert');

file.iterateNodesByType([ 'FunctionDeclaration', 'FunctionExpression' ], function(node) {
file.iterateNodesByType(['FunctionDeclaration', 'FunctionExpression'], function(node) {

@@ -49,0 +49,0 @@ if (beforeOpeningRoundBrace) {

@@ -47,3 +47,3 @@ var assert = require('assert');

file.iterateNodesByType([ 'FunctionExpression' ], function(node) {
file.iterateNodesByType(['FunctionExpression'], function(node) {

@@ -50,0 +50,0 @@ if (node.id) {

@@ -22,3 +22,3 @@ var assert = require('assert');

check: function(file, errors) {
var keywords = typeof this._keywords === 'string' ? [ this._keywords ] : this._keywords;
var keywords = typeof this._keywords === 'string' ? [this._keywords] : this._keywords;

@@ -25,0 +25,0 @@ // var that = this

@@ -86,34 +86,4 @@ var assert = require('assert');

function getIndents(pushNode, indents) {
if (!pushNode.parentNode || !pushNode.parentNode.parentNode) {
return indents;
}
var parent = pushNode.parentNode;
var grandParent = pushNode.parentNode.parentNode;
var parentStart = parent.loc.start.line;
var grandParentStart = grandParent.loc.start.line;
if (parent.type !== 'VariableDeclarator' ) {
return indents;
}
if (parentStart !== grandParentStart) {
return indents;
}
var actualIndentation = getIndentationFromLine(grandParentStart -1);
var newIndentation;
if (grandParent.declarations.length > 1) {
newIndentation = getIndentationFromLine(grandParent.declarations[1].loc.start.line - 1);
} else {
newIndentation = getIndentationFromLine(pushNode.loc.end.line - 1);
}
return ((newIndentation - actualIndentation) / indentSize) + 1;
}
function markPushAndCheck(pushNode, indents) {
linesToCheck[pushNode.loc.start.line - 1].push = getIndents(pushNode, indents);
linesToCheck[pushNode.loc.start.line - 1].push = indents;
linesToCheck[pushNode.loc.end.line - 1].check = true;

@@ -161,3 +131,3 @@ }

if (line.pop !== false) {
if (line.pop !== null) {
expectedIndentation = indentStack.pop() - (indentSize * line.pop);

@@ -184,3 +154,3 @@ } else {

if (line.push !== false) {
if (line.push !== null) {
indentStack.push(actualIndentation + (indentSize * line.push));

@@ -223,11 +193,2 @@ }

file.iterateNodesByType('IfStatement', function(node) {
checkAlternateBlockStatement(node, 'alternate');
});
file.iterateNodesByType('TryStatement', function(node) {
checkAlternateBlockStatement(node, 'handler');
checkAlternateBlockStatement(node, 'finalizer');
});
file.iterateNodesByType('BlockStatement', function(node) {

@@ -243,2 +204,19 @@ if (!isMultiline(node)) {

file.iterateNodesByType('IfStatement', function(node) {
checkAlternateBlockStatement(node, 'alternate');
var test = node.test,
endLine = test.loc.end.line - 1;
if (isMultiline(test) && linesToCheck[endLine].pop !== null) {
linesToCheck[endLine].push = 1;
}
});
file.iterateNodesByType('TryStatement', function(node) {
checkAlternateBlockStatement(node, 'handler');
checkAlternateBlockStatement(node, 'finalizer');
});
file.iterateNodesByType('SwitchStatement', function(node) {

@@ -271,3 +249,3 @@ if (!isMultiline(node)) {

if ( children.length > 1 || children[0].type !== 'BlockStatement') {
if (children.length > 1 || children[0].type !== 'BlockStatement') {
markChildren(node);

@@ -278,2 +256,25 @@ markPop(node, 1, true);

});
file.iterateNodesByType('VariableDeclaration', function(node) {
var startLine = node.loc.start.line - 1;
var decls = node.declarations;
var declStartLine = decls[0].loc.start.line - 1;
var actualIndents;
var newIndents;
if (startLine !== declStartLine) {
return;
}
if (linesToCheck[startLine].push !== null) {
actualIndents = getIndentationFromLine(startLine);
if (decls.length > 1) {
newIndents = getIndentationFromLine(decls[1].loc.start.line - 1);
} else {
newIndents = getIndentationFromLine(decls[0].loc.end.line - 1);
}
linesToCheck[startLine].push = ((newIndents - actualIndents) / indentSize) + 1;
}
});
}

@@ -289,6 +290,6 @@

return {
push: false,
pop: false,
popAfter: false,
check: false
push: null,
pop: null,
popAfter: null,
check: null
};

@@ -295,0 +296,0 @@ });

@@ -99,2 +99,5 @@ var esprima = require('esprima');

this.registerRule(new (require('./rules/require-spaces-in-conditional-expression'))());
this.registerRule(new (require('./rules/disallow-spaces-in-conditional-expression'))());
this.registerRule(new (require('./rules/require-spaces-in-function-expression'))());

@@ -101,0 +104,0 @@ this.registerRule(new (require('./rules/disallow-spaces-in-function-expression'))());

@@ -9,3 +9,3 @@ /**

*/
module.exports.getTokenByRangeStart = function(file, range, backward) {
exports.getTokenByRangeStart = function(file, range, backward) {
var tokens = file.getTokens();

@@ -44,4 +44,47 @@

*/
module.exports.tokenIsPunctuator = function(token, punctuator) {
exports.tokenIsPunctuator = function(token, punctuator) {
return token && token.type === 'Punctuator' && token.value === punctuator;
};
/**
* Find previous operator by range start
*
* @param {JsFile} file
* @param {Number} range
* @param {String} operator
* @returns {Object|null}
*/
exports.findOperatorByRangeStart = function(file, range, operator) {
var tokens = file.getTokens();
var index = file.getTokenPosByRangeStart(range);
while (index) {
if (tokens[ index ].value === operator) {
return tokens[ index ];
}
index--;
}
return null;
};
/**
* Returns token or false if there is a punctuator in a given range
*
* @param {JsFile} file
* @param {Number} range
* @param {String} operator
* @param {Boolean} [backward=false] Direction
* @returns {Object|null}
*/
exports.getTokenByRangeStartIfPunctuator = function(file, range, operator, backward) {
var part = this.getTokenByRangeStart(file, range, backward);
if (this.tokenIsPunctuator(part, operator)) {
return part;
}
return null;
};

@@ -74,3 +74,3 @@ module.exports = {

} else {
iterate(contents, cb, node, [ contents ]);
iterate(contents, cb, node, [contents]);
}

@@ -77,0 +77,0 @@ }

@@ -5,3 +5,3 @@ {

"name": "jscs",
"version": "1.4.3",
"version": "1.4.4",
"main": "lib/checker",

@@ -8,0 +8,0 @@ "homepage": "https://github.com/mdevils/node-jscs",

@@ -21,2 +21,3 @@ {

},
"disallowSpacesInsideArrayBrackets": true,
"validateJSDoc": {

@@ -23,0 +24,0 @@ "checkParamNames": true,

@@ -363,2 +363,75 @@ # node-jscs [![Build Status](https://travis-ci.org/mdevils/node-jscs.svg?branch=master)](https://travis-ci.org/mdevils/node-jscs) [![Dependency Status](https://david-dm.org/mdevils/node-jscs.svg?theme=shields.io)](https://david-dm.org/mdevils/node-jscs) [![devDependency Status](https://david-dm.org/mdevils/node-jscs/dev-status.svg?theme=shields.io)](https://david-dm.org/mdevils/node-jscs#info=devDependencies)

### requireSpacesInConditionalExpression
Requires space before and/or after `?` or `:` in conditional expressions.
Type: `Object` or `true`
Values: `afterTest`, `beforeConsequent`, `afterConsequent`, `beforeAlternate` as child properties, or `true` to set all properties to true. Child properties must be set to `true`.
#### Example
```js
"requireSpacesInConditionalExpression": {
"afterTest": true,
"beforeConsequent": true,
"afterConsequent": true,
"beforeAlternate": true
}
```
##### Valid
```js
var a = b ? c : d;
var a= b ? c : d;
```
##### Invalid
```js
var a = b? c : d;
var a = b ?c : d;
var a = b ? c: d;
var a = b ? c :d;
```
### disallowSpacesInConditionalExpression
Disallows space before and/or after `?` or `:` in conditional expressions.
Type: `Object` or `true`
Values: `afterTest`, `beforeConsequent`, `afterConsequent`, `beforeAlternate` as child properties, or `true` to set all properties to true. Child properties must be set to `true`.
#### Example
```js
"disallowSpacesInConditionalExpression": {
"afterTest": true,
"beforeConsequent": true,
"afterConsequent": true,
"beforeAlternate": true
}
```
##### Valid
```js
var a = b?c:d;
var a= b?c:d;
```
##### Invalid
```js
var a = b ?c:d;
var a = b? c:d;
var a = b?c :d;
var a = b?c: d;
```
### requireSpacesInFunctionExpression

@@ -723,3 +796,3 @@

### requirePaddingNewlinesInBlock
### requirePaddingNewlinesInBlocks

@@ -735,3 +808,3 @@ Requires blocks to begin and end with 2 newlines

```js
"requirePaddingNewlinesInBlock": true
"requirePaddingNewlinesInBlocks": true
```

@@ -1505,2 +1578,4 @@

"disallowSpaceBeforeBinaryOperators": [
"=",
",",
"+",

@@ -1542,2 +1617,4 @@ "-",

"requireSpaceBeforeBinaryOperators": [
"=",
",",
"+",

@@ -1579,2 +1656,4 @@ "-",

"disallowSpaceAfterBinaryOperators": [
"=",
",",
"+",

@@ -1616,2 +1695,4 @@ "-",

"requireSpaceAfterBinaryOperators": [
"=",
",",
"+",

@@ -1618,0 +1699,0 @@ "-",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc