blob: 799770537dd12299cacb047dda6ebe33d6db4711 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/**
* @fileoverview A rule to ensure whitespace before blocks.
* @author Mathias Schreck <https://github.com/lo1tuma>
* @copyright 2014 Mathias Schreck. All rights reserved.
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function (context) {
var requireSpace = context.options[0] !== "never";
/**
* Determines whether two adjacent tokens are have whitespace between them.
* @param {Object} left - The left token object.
* @param {Object} right - The right token object.
* @returns {boolean} Whether or not there is space between the tokens.
*/
function isSpaced(left, right) {
return left.range[1] < right.range[0];
}
/**
* Determines whether two adjacent tokens are on the same line.
* @param {Object} left - The left token object.
* @param {Object} right - The right token object.
* @returns {boolean} Whether or not the tokens are on the same line.
*/
function isSameLine(left, right) {
return left.loc.start.line === right.loc.start.line;
}
/**
* Checks the given BlockStatement node has a preceding space if it doesn’t start on a new line.
* @param {ASTNode|Token} node The AST node of a BlockStatement.
* @returns {void} undefined.
*/
function checkPrecedingSpace(node) {
var precedingToken = context.getTokenBefore(node),
hasSpace;
if (precedingToken && isSameLine(precedingToken, node)) {
hasSpace = isSpaced(precedingToken, node);
if (requireSpace) {
if (!hasSpace) {
context.report(node, "Missing space before opening brace.");
}
} else {
if (hasSpace) {
context.report(node, "Unexpected space before opening brace.");
}
}
}
}
/**
* Checks if the CaseBlock of an given SwitchStatement node has a preceding space.
* @param {ASTNode} node The node of a SwitchStatement.
* @returns {void} undefined.
*/
function checkSpaceBeforeCaseBlock(node) {
var cases = node.cases,
firstCase,
openingBrace;
if (cases.length > 0) {
firstCase = cases[0];
openingBrace = context.getTokenBefore(firstCase);
} else {
openingBrace = context.getLastToken(node, 1);
}
checkPrecedingSpace(openingBrace);
}
return {
"BlockStatement": checkPrecedingSpace,
"SwitchStatement": checkSpaceBeforeCaseBlock
};
};
module.exports.schema = [
{
"enum": ["always", "never"]
}
];
|