Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

*stickedOperators: remove documentation in preparation of deprecation. #366

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
164 changes: 33 additions & 131 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1381,137 +1381,6 @@ x = y
? 1 : 2;
```

### disallowLeftStickedOperators

Disallows sticking operators to the left.

Type: `Array`

Values: Array of quoted operators

#### Example

```js
"disallowLeftStickedOperators": [
"?",
"=",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
]
```

##### Valid

```js
x = y ? 1 : 2;
```

##### Invalid

```js
x = y? 1 : 2;
```

### requireRightStickedOperators

Requires sticking operators to the right.

Type: `Array`

Values: Array of quoted operators

#### Example

```js
"requireRightStickedOperators": ["!"]
```

##### Valid

```js
x = !y;
```

##### Invalid

```js
x = ! y;
```

### disallowRightStickedOperators

Disallows sticking operators to the right.

Type: `Array`

Values: Array of quoted operators

#### Example

```js
"disallowRightStickedOperators": [
"?",
"=",
"+",
"/",
"*",
":",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
]
```

##### Valid
```js
x = y + 1;
```
##### Invalid
```js
x = y +1;
```

### requireLeftStickedOperators

Requires sticking operators to the left.

Type: `Array`

Values: Array of quoted operators

#### Example

```js
"requireLeftStickedOperators": [","]
```

##### Valid

```js
x = [1, 2];
```

##### Invalid

```js
x = [1 , 2];
```

### disallowSpaceAfterPrefixUnaryOperators

Requires sticking unary operators to the right.
Expand Down Expand Up @@ -2552,6 +2421,39 @@ Values: `true`
// A comment
```

## Removed Rules

### ~~disallowLeftStickedOperators~~

Use the following rules instead:

* requireSpaceBeforeBinaryOperators
* requireSpaceBeforePostfixUnaryOperators
* requireSpacesInConditionalExpression

### ~~disallowRightStickedOperators~~

Use the following rules instead:

* requireSpaceAfterBinaryOperators
* requireSpaceAfterPrefixUnaryOperators
* requireSpacesInConditionalExpression

### ~~requireLeftStickedOperators~~

Use the following rules instead:

* disallowSpaceBeforeBinaryOperators
* disallowSpaceBeforePostfixUnaryOperators
* disallowSpacesInConditionalExpressions

### ~~requireRightStickedOperators~~

Use the following rules instead:

* disallowSpaceAfterBinaryOperators
* disallowSpaceAfterPrefixUnaryOperators
* disallowSpacesInConditionalExpressions

## Browser Usage

Expand Down
33 changes: 11 additions & 22 deletions lib/rules/disallow-left-sticked-operators.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(operators) {
assert(Array.isArray(operators), 'disallowLeftStickedOperators option requires array value');
this._operatorIndex = {};
for (var i = 0, l = operators.length; i < l; i++) {
this._operatorIndex[operators[i]] = true;
}
},
configure: function() {},

getOptionName: function() {
return 'disallowLeftStickedOperators';
},

check: function(file, errors) {
var operators = this._operatorIndex;

file.iterateTokensByType('Punctuator', function(token, i, tokens) {
if (operators[token.value]) {
var prevToken = tokens[i - 1];
if (prevToken && prevToken.range[1] === token.range[0]) {
errors.add(
'Operator ' + token.value + ' should not stick to preceding expression',
token.loc.start
);
}
}
});
errors.add(
'The disallowLeftStickedOperators rule is no longer supported.' +
'\nPlease use the following rules instead:' +
'\n' +
'\nrequireSpaceBeforeBinaryOperators' +
'\nrequireSpaceBeforePostfixUnaryOperators' +
'\nrequireSpacesInConditionalExpression',
1,
0
);
}

};
33 changes: 11 additions & 22 deletions lib/rules/disallow-right-sticked-operators.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(operators) {
assert(Array.isArray(operators), 'disallowRightStickedOperators option requires array value');
this._operatorIndex = {};
for (var i = 0, l = operators.length; i < l; i++) {
this._operatorIndex[operators[i]] = true;
}
},
configure: function() {},

getOptionName: function() {
return 'disallowRightStickedOperators';
},

check: function(file, errors) {
var operators = this._operatorIndex;

file.iterateTokensByType('Punctuator', function(token, i, tokens) {
if (operators[token.value]) {
var nextToken = tokens[i + 1];
if (nextToken && nextToken.range[0] === token.range[1]) {
errors.add(
'Operator ' + token.value + ' should not stick to following expression',
token.loc.start
);
}
}
});
errors.add(
'The disallowRightStickedOperators rule is no longer supported.' +
'\nPlease use the following rules instead:' +
'\n' +
'\nrequireSpaceAfterBinaryOperators' +
'\nrequireSpaceAfterPrefixUnaryOperators' +
'\nrequireSpacesInConditionalExpression',
1,
0
);
}

};
33 changes: 11 additions & 22 deletions lib/rules/require-left-sticked-operators.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(operators) {
assert(Array.isArray(operators), 'requireLeftStickedOperators option requires array value');
this._operatorIndex = {};
for (var i = 0, l = operators.length; i < l; i++) {
this._operatorIndex[operators[i]] = true;
}
},
configure: function() {},

getOptionName: function() {
return 'requireLeftStickedOperators';
},

check: function(file, errors) {
var operators = this._operatorIndex;

file.iterateTokensByType('Punctuator', function(token, i, tokens) {
if (operators[token.value]) {
var prevToken = tokens[i - 1];
if (prevToken && prevToken.range[1] !== token.range[0]) {
errors.add(
'Operator ' + token.value + ' should stick to preceding expression',
token.loc.start
);
}
}
});
errors.add(
'The requireLeftStickedOperators rule is no longer supported.' +
'\nPlease use the following rules instead:' +
'\n' +
'\ndisallowSpaceBeforeBinaryOperators' +
'\ndisallowSpaceBeforePostfixUnaryOperators' +
'\ndisallowSpacesInConditionalExpressions',
1,
0
);
}

};
33 changes: 11 additions & 22 deletions lib/rules/require-right-sticked-operators.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(operators) {
assert(Array.isArray(operators), 'requireRightStickedOperators option requires array value');
this._operatorIndex = {};
for (var i = 0, l = operators.length; i < l; i++) {
this._operatorIndex[operators[i]] = true;
}
},
configure: function() {},

getOptionName: function() {
return 'requireRightStickedOperators';
},

check: function(file, errors) {
var operators = this._operatorIndex;

file.iterateTokensByType('Punctuator', function(token, i, tokens) {
if (operators[token.value]) {
var nextToken = tokens[i + 1];
if (nextToken && nextToken.range[0] !== token.range[1]) {
errors.add(
'Operator ' + token.value + ' should stick to following expression',
token.loc.start
);
}
}
});
errors.add(
'The requireRightStickedOperators rule is no longer supported.' +
'\nPlease use the following rules instead:' +
'\n' +
'\ndisallowSpaceAfterBinaryOperators' +
'\ndisallowSpaceAfterPrefixUnaryOperators' +
'\ndisallowSpacesInConditionalExpressions',
1,
0
);
}

};
20 changes: 13 additions & 7 deletions test/rules/disallow-left-sticked-operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ describe('rules/disallow-left-sticked-operators', function() {
beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
checker.configure({ disallowLeftStickedOperators: ['+'] });
});
it('should report sticky operator', function() {
checker.configure({ disallowLeftStickedOperators: ['?'] });
assert(checker.checkString('var x = y? z : w;').getErrorCount() === 1);
});
it('should not report separated operator', function() {
checker.configure({ disallowLeftStickedOperators: ['?'] });
assert(checker.checkString('var x = y ? z : w;').isEmpty());
it('should output correct deprecation notice', function() {
var errors = checker.checkString('var a = b+ c; var a = b+ c;').getErrorList();
assert(errors.length === 1);

var error = errors[0];
assert(error.line === 1 && error.column === 0);
assert(error.message === 'The disallowLeftStickedOperators rule is no longer supported.' +
'\nPlease use the following rules instead:' +
'\n' +
'\nrequireSpaceBeforeBinaryOperators' +
'\nrequireSpaceBeforePostfixUnaryOperators' +
'\nrequireSpacesInConditionalExpression');
});
});