This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
requireSpacesInsideParenthesizedExpression: new rule implementation
Ref #1069 (comment) Ref gh-812 Ref gh-431
- Loading branch information
Showing
3 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
lib/rules/require-spaces-inside-parenthesized-expression.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Requires space after opening grouping round bracket and before closing. | ||
* | ||
* Types: `Boolean` or `Object` | ||
* | ||
* Values: | ||
* - `true`: always require spaces inside grouping parentheses | ||
* - `Object`: | ||
* - `"allExcept"`: `[ "{", "}", "function" ]` Ignore parenthesized objects and functions | ||
* | ||
* #### Example | ||
* | ||
* ```js | ||
* "requireSpacesInsideParenthesizedExpression": true | ||
* | ||
* // or | ||
* | ||
* "requireSpacesInsideParenthesizedExpression": { | ||
* "allExcept": [ "{", "}" ] | ||
* } | ||
* ``` | ||
* | ||
* ##### Valid for mode `true` | ||
* | ||
* ```js | ||
* var x = ( 1 + obj.size ) * ( 2 ); | ||
* ``` | ||
* | ||
* ##### Valid for mode `{ allExcept": [ "{", "}", "function" ] }` | ||
* | ||
* ```js | ||
* var x = ( options || { x: true }).x; | ||
* var global = (function() { return this; })(); | ||
* ``` | ||
* | ||
* ##### Invalid | ||
* | ||
* ```js | ||
* var x = (1 + obj.size) * (2); | ||
* ``` | ||
*/ | ||
|
||
var assert = require('assert'); | ||
var utils = require('../utils'); | ||
|
||
module.exports = function() {}; | ||
|
||
module.exports.prototype = { | ||
configure: function(value) { | ||
var isObject = typeof value === 'object'; | ||
|
||
var error = this.getOptionName() + ' rule requires string value true or object'; | ||
|
||
if (isObject) { | ||
assert('allExcept' in value, error); | ||
} else { | ||
assert(value === true, error); | ||
} | ||
|
||
this._exceptions = {}; | ||
|
||
if (isObject) { | ||
(value.allExcept || []).forEach(function(value) { | ||
this._exceptions[value] = true; | ||
}, this); | ||
} | ||
}, | ||
|
||
getOptionName: function() { | ||
return 'requireSpacesInsideParenthesizedExpression'; | ||
}, | ||
|
||
check: function(file, errors) { | ||
var exceptions = this._exceptions; | ||
|
||
file.iterateTokenByValue('(', function(token) { | ||
var nextToken = file.getNextToken(token, {includeComments: true}); | ||
var value = nextToken.value; | ||
|
||
// Skip empty parentheses and explicit exceptions | ||
if (value === ')' || value in exceptions) { | ||
return; | ||
} | ||
|
||
// Skip non-expression parentheses | ||
var type = utils.categorizeOpenParen(token, file); | ||
if (type !== 'ParenthesizedExpression') { | ||
return; | ||
} | ||
|
||
errors.assert.whitespaceBetween({ | ||
token: token, | ||
nextToken: nextToken, | ||
message: 'Missing space after opening grouping round bracket' | ||
}); | ||
}); | ||
|
||
file.iterateTokenByValue(')', function(token) { | ||
var prevToken = file.getPrevToken(token, {includeComments: true}); | ||
var value = prevToken.value; | ||
|
||
// Skip empty parentheses and explicit exceptions | ||
if (value === '(' || value in exceptions) { | ||
return; | ||
} | ||
|
||
// Skip non-expression parentheses | ||
var type = utils.categorizeCloseParen(token, file); | ||
if (type !== 'ParenthesizedExpression') { | ||
return; | ||
} | ||
|
||
errors.assert.whitespaceBetween({ | ||
token: prevToken, | ||
nextToken: token, | ||
message: 'Missing space before closing grouping round bracket' | ||
}); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters