2 * @fileoverview Validates spacing before and after semicolon
3 * @author Mathias Schreck
4 * @copyright 2015 Mathias Schreck
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 module.exports = function (context) {
15 var config = context.options[0],
16 requireSpaceBefore = false,
17 requireSpaceAfter = true;
19 if (typeof config === "object") {
20 if (config.hasOwnProperty("before")) {
21 requireSpaceBefore = config.before;
23 if (config.hasOwnProperty("after")) {
24 requireSpaceAfter = config.after;
29 * Determines whether two adjacent tokens are have whitespace between them.
30 * @param {Object} left - The left token object.
31 * @param {Object} right - The right token object.
32 * @returns {boolean} Whether or not there is space between the tokens.
34 function isSpaced(left, right) {
35 return left.range[1] < right.range[0];
39 * Checks whether two tokens are on the same line.
40 * @param {Object} left The leftmost token.
41 * @param {Object} right The rightmost token.
42 * @returns {boolean} True if the tokens are on the same line, false if not.
45 function isSameLine(left, right) {
46 return left.loc.end.line === right.loc.start.line;
50 * Checks if a given token has leading whitespace.
51 * @param {Object} token The token to check.
52 * @returns {boolean} True if the given token has leading space, false if not.
54 function hasLeadingSpace(token) {
55 var tokenBefore = context.getTokenBefore(token);
56 return tokenBefore && isSameLine(tokenBefore, token) && isSpaced(tokenBefore, token);
60 * Checks if a given token has trailing whitespace.
61 * @param {Object} token The token to check.
62 * @returns {boolean} True if the given token has trailing space, false if not.
64 function hasTrailingSpace(token) {
65 var tokenAfter = context.getTokenAfter(token);
66 return tokenAfter && isSameLine(token, tokenAfter) && isSpaced(token, tokenAfter);
70 * Checks if the given token is the last token in its line.
71 * @param {Token} token The token to check.
72 * @returns {boolean} Whether or not the token is the last in its line.
74 function isLastTokenInCurrentLine(token) {
75 var tokenAfter = context.getTokenAfter(token);
76 return !(tokenAfter && isSameLine(token, tokenAfter));
80 * Checks if the given token is a semicolon.
81 * @param {Token} token The token to check.
82 * @returns {boolean} Whether or not the given token is a semicolon.
84 function isSemicolon(token) {
85 return token.type === "Punctuator" && token.value === ";";
89 * Reports if the given token has invalid spacing.
90 * @param {Token} token The semicolon token to check.
91 * @param {ASTNode} node The corresponding node of the token.
94 function checkSemicolonSpacing(token, node) {
97 if (isSemicolon(token)) {
98 location = token.loc.start;
100 if (hasLeadingSpace(token)) {
101 if (!requireSpaceBefore) {
102 context.report(node, location, "Unexpected whitespace before semicolon.");
105 if (requireSpaceBefore) {
106 context.report(node, location, "Missing whitespace before semicolon.");
110 if (!isLastTokenInCurrentLine(token)) {
111 if (hasTrailingSpace(token)) {
112 if (!requireSpaceAfter) {
113 context.report(node, location, "Unexpected whitespace after semicolon.");
116 if (requireSpaceAfter) {
117 context.report(node, location, "Missing whitespace after semicolon.");
125 * Checks the spacing of the semicolon with the assumption that the last token is the semicolon.
126 * @param {ASTNode} node The node to check.
129 function checkNode(node) {
130 var token = context.getLastToken(node);
131 checkSemicolonSpacing(token, node);
135 "VariableDeclaration": checkNode,
136 "ExpressionStatement": checkNode,
137 "BreakStatement": checkNode,
138 "ContinueStatement": checkNode,
139 "DebuggerStatement": checkNode,
140 "ReturnStatement": checkNode,
141 "ThrowStatement": checkNode,
142 "ForStatement": function (node) {
144 checkSemicolonSpacing(context.getTokenAfter(node.init), node);
148 checkSemicolonSpacing(context.getTokenAfter(node.test), node);