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

Commit

Permalink
requireCurlyBraces: support the with statement
Browse files Browse the repository at this point in the history
Closes gh-661
  • Loading branch information
mrjoelkemp committed Oct 9, 2014
1 parent bd62246 commit d0150e0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/rules/require-curly-braces.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports.prototype = {
}

var typeIndex = this._typeIndex;

if (typeIndex['if'] || typeIndex['else']) {
file.iterateNodesByType('IfStatement', function(node) {
if (typeIndex.if && node.consequent && node.consequent.type !== 'BlockStatement') {
Expand All @@ -57,6 +58,7 @@ module.exports.prototype = {
}
});
}

if (typeIndex['case'] || typeIndex['default']) {
file.iterateNodesByType('SwitchCase', function(node) {
// empty case statement
Expand All @@ -77,16 +79,23 @@ module.exports.prototype = {
}
});
}

if (typeIndex['while']) {
checkBody('WhileStatement', 'While');
}

if (typeIndex['for']) {
checkBody('ForStatement', 'For');
checkBody('ForInStatement', 'For in');
}

if (typeIndex['do']) {
checkBody('DoWhileStatement', 'Do while');
}

if (typeIndex['with']) {
checkBody('WithStatement', 'With');
}
}

};
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ exports.curlyBracedKeywords = [
'while',
'do',
'case',
'default'
'default',
'with'
];

/**
Expand Down
6 changes: 3 additions & 3 deletions test/rules/require-curly-braces.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ describe('rules/require-curly-braces', function() {
checker.configure({ requireCurlyBraces: true });

assert(!checker.checkString('if (x) x++;').isEmpty());
assert(!checker.checkString('if (x) {x++} else x--;;').isEmpty());
assert(!checker.checkString('if (x) {x++} else x--;').isEmpty());
assert(!checker.checkString('for (x = 0; x < 10; x++) x++;').isEmpty());
assert(!checker.checkString('while (x) x++;').isEmpty());
assert(!checker.checkString('do x++;').isEmpty());
assert(!checker.checkString('try x++;').isEmpty());
assert(!checker.checkString('do x++; while(x < 5);').isEmpty());
assert(!checker.checkString('try {x++;} catch(e) throw e;').isEmpty());
assert(!checker.checkString('switch(\'4\'){ case \'4\': break; }').isEmpty());
assert(!checker.checkString('switch(\'4\'){ case \'4\': {break;} default: 1; }').isEmpty());
assert(!checker.checkString('with(x) console.log(toString());').isEmpty());
});
});

0 comments on commit d0150e0

Please sign in to comment.