2 * @fileoverview Rule to disallow whitespace before the semicolon
3 * @author Jonathan Kingston
4 * @copyright 2015 Mathias Schreck
5 * @copyright 2014 Jonathan Kingston
10 //------------------------------------------------------------------------------
12 //------------------------------------------------------------------------------
14 module.exports = function(context) {
17 * Determines whether two adjacent tokens are have whitespace between them.
18 * @param {Object} left - The left token object.
19 * @param {Object} right - The right token object.
20 * @returns {boolean} Whether or not there is space between the tokens.
22 function isSpaced(left, right) {
23 return left.range[1] < right.range[0];
27 * Checks whether two tokens are on the same line.
28 * @param {Object} left The leftmost token.
29 * @param {Object} right The rightmost token.
30 * @returns {boolean} True if the tokens are on the same line, false if not.
33 function isSameLine(left, right) {
34 return left.loc.end.line === right.loc.start.line;
38 * Checks if a given token has leading whitespace.
39 * @param {Object} token The token to check.
40 * @returns {boolean} True if the given token has leading space, false if not.
42 function hasLeadingSpace(token) {
43 var tokenBefore = context.getTokenBefore(token);
44 return isSameLine(tokenBefore, token) && isSpaced(tokenBefore, token);
48 * Checks if the given token is a semicolon.
49 * @param {Token} token The token to check.
50 * @returns {boolean} Whether or not the given token is a semicolon.
52 function isSemicolon(token) {
53 return token.type === "Punctuator" && token.value === ";";
57 * Reports if the given token has leading space.
58 * @param {Token} token The semicolon token to check.
59 * @param {ASTNode} node The corresponding node of the token.
62 function checkSemiTokenForLeadingSpace(token, node) {
63 if (isSemicolon(token) && hasLeadingSpace(token)) {
64 context.report(node, token.loc.start, "Unexpected whitespace before semicolon.");
69 * Checks leading space before the semicolon with the assumption that the last token is the semicolon.
70 * @param {ASTNode} node The node to check.
73 function checkNode(node) {
74 var token = context.getLastToken(node);
75 checkSemiTokenForLeadingSpace(token, node);
79 "VariableDeclaration": checkNode,
80 "ExpressionStatement": checkNode,
81 "BreakStatement": checkNode,
82 "ContinueStatement": checkNode,
83 "DebuggerStatement": checkNode,
84 "ReturnStatement": checkNode,
85 "ThrowStatement": checkNode,
86 "ForStatement": function (node) {
88 checkSemiTokenForLeadingSpace(context.getTokenAfter(node.init), node);
92 checkSemiTokenForLeadingSpace(context.getTokenAfter(node.test), node);