1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports = function(context) {
|
13 |
|
14 | var MESSAGE = "Opening curly brace does not appear on the same line as controlling statement.";
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function checkBlock() {
|
21 | var blockProperties = arguments;
|
22 | return function(node) {
|
23 | var tokens, block;
|
24 | [].forEach.call(blockProperties, function(blockProp) {
|
25 | block = node[blockProp];
|
26 | if(block && block.type === "BlockStatement") {
|
27 | tokens = context.getTokens(block, 1);
|
28 | if (tokens[0].loc.start.line !== tokens[1].loc.start.line) {
|
29 | context.report(node, MESSAGE);
|
30 | }
|
31 | }
|
32 | });
|
33 | };
|
34 | }
|
35 |
|
36 | function checkSwitchStatement(node) {
|
37 | var tokens;
|
38 | if(node.cases && node.cases.length) {
|
39 | tokens = context.getTokens(node.cases[0], 2);
|
40 | if (tokens[0].loc.start.line !== tokens[1].loc.start.line) {
|
41 | context.report(node, MESSAGE);
|
42 | }
|
43 | } else {
|
44 | tokens = context.getTokens(node);
|
45 | tokens.pop();
|
46 | if (tokens.pop().loc.start.line !== tokens.pop().loc.start.line) {
|
47 | context.report(node, MESSAGE);
|
48 | }
|
49 | }
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | return {
|
57 | "FunctionDeclaration": checkBlock("body"),
|
58 | "IfStatement": checkBlock("consequent", "alternate"),
|
59 | "TryStatement": checkBlock("block", "finalizer"),
|
60 | "CatchClause": checkBlock("body"),
|
61 | "DoWhileStatement": checkBlock("body"),
|
62 | "WhileStatement": checkBlock("body"),
|
63 | "WithStatement": checkBlock("body"),
|
64 | "ForStatement": checkBlock("body"),
|
65 | "ForInStatement": checkBlock("body"),
|
66 | "SwitchStatement": checkSwitchStatement
|
67 | };
|
68 |
|
69 | };
|