2 * @fileoverview Rule to enforce the number of spaces after certain keywords
4 * @copyright 2014 Nick Fisher. All rights reserved.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 module.exports = function(context) {
14 // unless the first option is `"never"`, then a space is required
15 var requiresSpace = context.options[0] !== "never";
18 * Check if the separation of two adjacent tokens meets the spacing rules, and report a problem if not.
20 * @param {ASTNode} node The node to which the potential problem belongs.
21 * @param {Token} left The first token.
22 * @param {Token} right The second token
25 function checkTokens(node, left, right) {
26 var hasSpace = left.range[1] < right.range[0],
29 if (hasSpace !== requiresSpace) {
30 context.report(node, "Keyword \"{{value}}\" must {{not}}be followed by whitespace.", {
32 not: requiresSpace ? "" : "not "
38 * Check if the given node (`if`, `for`, `while`, etc), has the correct spacing after it.
39 * @param {ASTNode} node The node to check.
42 function check(node) {
43 var tokens = context.getFirstTokens(node, 2);
44 checkTokens(node, tokens[0], tokens[1]);
48 "IfStatement": function (node) {
51 if (node.alternate && node.alternate.type !== "IfStatement") {
52 checkTokens(node.alternate, context.getTokenBefore(node.alternate), context.getFirstToken(node.alternate));
55 "ForStatement": check,
56 "ForOfStatement": check,
57 "ForInStatement": check,
58 "WhileStatement": check,
59 "DoWhileStatement": function (node) {
62 var whileTokens = context.getTokensBefore(node.test, 2);
63 checkTokens(node, whileTokens[0], whileTokens[1]);
65 "SwitchStatement": check,
66 "TryStatement": function (node) {
68 // check the `finally`
70 checkTokens(node.finalizer, context.getTokenBefore(node.finalizer), context.getFirstToken(node.finalizer));
73 "CatchStatement": check,
74 "WithStatement": check