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

Add new values and deprecate some for (require | disallow)SpacesInsideObjectBrackets #893

Closed
wants to merge 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 69 additions & 43 deletions lib/rules/disallow-spaces-inside-object-brackets.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/**
* Disallows space after opening object curly brace and before closing.
*
* Type: `Boolean` or `String`
* Type: `Object`, `Boolean` or `String`
*
* Values: `"all"` or `true` for strict mode, `"nested"` ignores closing brackets in a row.
* Values: `"all"` or `true` for strict mode, `"nested"` (*deprecated* use `"allExcept": ['}']`)
* ignores closing brackets in a row.
*
* #### Example
*
* ```js
* "disallowSpacesInsideObjectBrackets": "all"
* "disallowSpacesInsideObjectBrackets": {
* "allExcept": [ "}", ")" ]
* }
*
* // or
* * "disallowSpacesInsideObjectBrackets": true | "all" | "nested"
* ```
*
* ##### Valid for mode `"all"`
Expand All @@ -20,7 +26,13 @@
* ##### Valid for mode `"nested"`
*
* ```js
* var x = { a: {b: 1} };
* var x = {a: {b: 1} };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it was a mistake, see description of the rule

* ```
*
* ##### Valid for mode `"allExcept": ["}"]`
*
* ```js
* var x = {a: {b: 1} };
* ```
*
* ##### Invalid
Expand All @@ -36,59 +48,73 @@ module.exports = function() {};

module.exports.prototype = {

configure: function(disallowSpacesInsideObjectBrackets) {
assert(
disallowSpacesInsideObjectBrackets === true || disallowSpacesInsideObjectBrackets === 'all' ||
disallowSpacesInsideObjectBrackets === 'nested',
'disallowSpacesInsideObjectBrackets option requires "all"(true value) or "nested" string'
);
configure: function(value) {
var mode;
var modes = {
'all': true,
'nested': true
};
var isObject = typeof value === 'object';

var error = 'disallowSpacesInsideObjectBrackets rule' +
' requires string "all" or "nested", true value or object';

if (typeof value === 'string' || value === true) {
assert(modes[ value === true ? 'all' : value], error);

} else if (isObject) {
assert('allExcept' in value, error);
} else {
assert(false, error);
}

this._exceptions = {};

this._mode = disallowSpacesInsideObjectBrackets === true ? 'all' : disallowSpacesInsideObjectBrackets;
if (isObject) {
(value.allExcept || []).forEach(function(value) {
this._exceptions[value] = true;
}, this);

} else {
mode = value;
}

if (mode === 'nested') {
this._exceptions['}'] = true;
}
},

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

check: function(file, errors) {
var mode = this._mode;
var exceptions = this._exceptions;

file.iterateNodesByType('ObjectExpression', function(node) {
var tokens = file.getTokens();

var openingBracketPos = file.getTokenPosByRangeStart(node.range[0]);
var closingBracketPos = file.getTokenPosByRangeStart(node.range[1] - 1);
var openingBracket = file.getFirstNodeToken(node);
var nextToken = file.getNextToken(openingBracket);

var brackets = {
opening: tokens[openingBracketPos],
closing: tokens[closingBracketPos]
};
errors.assert.noWhitespaceBetween({
token: openingBracket,
nextToken: nextToken,
spaces: 1,
message: 'Illegal space after opening curly brace'
});

var insideTokens = {
prev: tokens[closingBracketPos - 1],
next: tokens[openingBracketPos + 1]
};
var closingBracket = file.getLastNodeToken(node);
var prevToken = file.getPrevToken(closingBracket);

if (mode === 'all') {
check(brackets, errors, insideTokens);

} else if (node.parentNode.type === 'Property') {
check(brackets, errors, insideTokens);
if (prevToken.value in exceptions) {
return;
}

errors.assert.noWhitespaceBetween({
token: prevToken,
nextToken: closingBracket,
spaces: 1,
message: 'Illegal space before closing curly brace'
});
});
}
};

function check(brackets, errors, insideTokens) {
if (brackets.opening.loc.start.line === insideTokens.next.loc.start.line &&
brackets.opening.range[1] !== insideTokens.next.range[0]
) {
errors.add('Illegal space after opening curly brace', brackets.opening.loc.end);
}

if (brackets.closing.loc.start.line === insideTokens.prev.loc.start.line &&
brackets.closing.range[0] !== insideTokens.prev.range[1]
) {
errors.add('Illegal space before closing curly brace', insideTokens.prev.loc.end);
}
}
98 changes: 69 additions & 29 deletions lib/rules/require-spaces-inside-object-brackets.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/**
* Requires space after opening object curly brace and before closing.
*
* Type: `String`
* Type: `Object` or `String`
*
* Values: `"all"` for strict mode, `"allButNested"` ignores closing brackets in a row.
* Values: `"all"` for strict mode, `"allButNested"` (*deprecated* use `"allExcept": ['}']`)
* ignores closing brackets in a row.
*
* #### Example
*
* ```js
* "requireSpacesInsideObjectBrackets": "all"
* "requireSpacesInsideObjectBrackets": {
* "allExcept": [ "}", ")" ]
* }
*
* // or
* "requireSpacesInsideObjectBrackets": true | "all" | "allButNested"
* ```
*
* ##### Valid for mode `"all"`
Expand All @@ -23,6 +29,13 @@
* var x = { a: { b: 1 }};
* ```
*
* ##### Valid for mode `"allExcept": [ "}", ")" ]`
*
* ```js
* var x = { a: (b ? 1 : 2)};
* var x = { a: { b: 1 }};
* ```
*
* ##### Invalid
*
* ```js
Expand All @@ -36,51 +49,78 @@ module.exports = function() {};

module.exports.prototype = {

configure: function(mode) {
configure: function(value) {
var mode;
var modes = {
'all': true,
'allButNested': true
};
assert(
typeof mode === 'string' &&
modes[mode],
'requireSpacesInsideObjectBrackets option requires string value \'all\' or \'allButNested\''
);
this._mode = mode;
var isObject = typeof value === 'object';

var error = 'requireSpacesInsideObjectBrackets rule' +
' requires string value \'all\' or \'allButNested\' or object';

if (typeof value === 'string') {
assert(modes[value], error);

} else if (isObject) {
assert('allExcept' in value, error);
} else {
assert(false, error);
}

this._exceptions = {};

if (isObject) {
(value.allExcept || []).forEach(function(value) {
this._exceptions[value] = true;
}, this);

} else {
mode = value;
}

if (mode === 'allButNested') {
this._exceptions['}'] = true;
}
},

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

check: function(file, errors) {
var mode = this._mode;
file.iterateNodesByType('ObjectExpression', function(node) {
var tokens = file.getTokens();
var openingBracketPos = file.getTokenPosByRangeStart(node.range[0]);
var exceptions = this._exceptions;

var openingBracket = tokens[openingBracketPos];
var nextToken = tokens[openingBracketPos + 1];
file.iterateNodesByType('ObjectExpression', function(node) {
var openingBracket = file.getFirstNodeToken(node);
var nextToken = file.getNextToken(openingBracket);

if (nextToken.type === 'Punctuator' && nextToken.value === '}') {
// Don't check empty object
if (nextToken.value === '}') {
return;
}

if (openingBracket.range[1] === nextToken.range[0]) {
errors.add('Missing space after opening curly brace', nextToken.loc.start);
}
errors.assert.whitespaceBetween({
token: openingBracket,
nextToken: nextToken,
spaces: 1,
message: 'Missing space after opening curly brace'
});

var closingBracketPos = file.getTokenPosByRangeStart(node.range[1] - 1);
var closingBracket = tokens[closingBracketPos];
var prevToken = tokens[closingBracketPos - 1];
var isNested = mode === 'allButNested' &&
prevToken.type === 'Punctuator' &&
prevToken.value === '}';
var closingBracket = file.getLastNodeToken(node);
var prevToken = file.getPrevToken(closingBracket);

if (closingBracket.range[0] === prevToken.range[1] && !isNested) {
errors.add('Missing space before closing curly brace', closingBracket.loc.start);
if (prevToken.value in exceptions) {
return;
}

errors.assert.whitespaceBetween({
token: prevToken,
nextToken: closingBracket,
spaces: 1,
message: 'Missing space before closing curly brace'
});
});
}

};
Loading