UNPKG

5.09 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to enforce location of semicolons.
3 * @author Toru Nagashima
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const astUtils = require("./utils/ast-utils");
13
14//------------------------------------------------------------------------------
15// Rule Definition
16//------------------------------------------------------------------------------
17
18const SELECTOR = `:matches(${
19 [
20 "BreakStatement", "ContinueStatement", "DebuggerStatement",
21 "DoWhileStatement", "ExportAllDeclaration",
22 "ExportDefaultDeclaration", "ExportNamedDeclaration",
23 "ExpressionStatement", "ImportDeclaration", "ReturnStatement",
24 "ThrowStatement", "VariableDeclaration"
25 ].join(",")
26})`;
27
28/**
29 * Get the child node list of a given node.
30 * This returns `Program#body`, `BlockStatement#body`, or `SwitchCase#consequent`.
31 * This is used to check whether a node is the first/last child.
32 * @param {Node} node A node to get child node list.
33 * @returns {Node[]|null} The child node list.
34 */
35function getChildren(node) {
36 const t = node.type;
37
38 if (t === "BlockStatement" || t === "Program") {
39 return node.body;
40 }
41 if (t === "SwitchCase") {
42 return node.consequent;
43 }
44 return null;
45}
46
47/**
48 * Check whether a given node is the last statement in the parent block.
49 * @param {Node} node A node to check.
50 * @returns {boolean} `true` if the node is the last statement in the parent block.
51 */
52function isLastChild(node) {
53 const t = node.parent.type;
54
55 if (t === "IfStatement" && node.parent.consequent === node && node.parent.alternate) { // before `else` keyword.
56 return true;
57 }
58 if (t === "DoWhileStatement") { // before `while` keyword.
59 return true;
60 }
61 const nodeList = getChildren(node.parent);
62
63 return nodeList !== null && nodeList[nodeList.length - 1] === node; // before `}` or etc.
64}
65
66module.exports = {
67 meta: {
68 type: "layout",
69
70 docs: {
71 description: "enforce location of semicolons",
72 category: "Stylistic Issues",
73 recommended: false,
74 url: "https://eslint.org/docs/rules/semi-style"
75 },
76
77 schema: [{ enum: ["last", "first"] }],
78 fixable: "whitespace"
79 },
80
81 create(context) {
82 const sourceCode = context.getSourceCode();
83 const option = context.options[0] || "last";
84
85 /**
86 * Check the given semicolon token.
87 * @param {Token} semiToken The semicolon token to check.
88 * @param {"first"|"last"} expected The expected location to check.
89 * @returns {void}
90 */
91 function check(semiToken, expected) {
92 const prevToken = sourceCode.getTokenBefore(semiToken);
93 const nextToken = sourceCode.getTokenAfter(semiToken);
94 const prevIsSameLine = !prevToken || astUtils.isTokenOnSameLine(prevToken, semiToken);
95 const nextIsSameLine = !nextToken || astUtils.isTokenOnSameLine(semiToken, nextToken);
96
97 if ((expected === "last" && !prevIsSameLine) || (expected === "first" && !nextIsSameLine)) {
98 context.report({
99 loc: semiToken.loc,
100 message: "Expected this semicolon to be at {{pos}}.",
101 data: {
102 pos: (expected === "last")
103 ? "the end of the previous line"
104 : "the beginning of the next line"
105 },
106 fix(fixer) {
107 if (prevToken && nextToken && sourceCode.commentsExistBetween(prevToken, nextToken)) {
108 return null;
109 }
110
111 const start = prevToken ? prevToken.range[1] : semiToken.range[0];
112 const end = nextToken ? nextToken.range[0] : semiToken.range[1];
113 const text = (expected === "last") ? ";\n" : "\n;";
114
115 return fixer.replaceTextRange([start, end], text);
116 }
117 });
118 }
119 }
120
121 return {
122 [SELECTOR](node) {
123 if (option === "first" && isLastChild(node)) {
124 return;
125 }
126
127 const lastToken = sourceCode.getLastToken(node);
128
129 if (astUtils.isSemicolonToken(lastToken)) {
130 check(lastToken, option);
131 }
132 },
133
134 ForStatement(node) {
135 const firstSemi = node.init && sourceCode.getTokenAfter(node.init, astUtils.isSemicolonToken);
136 const secondSemi = node.test && sourceCode.getTokenAfter(node.test, astUtils.isSemicolonToken);
137
138 if (firstSemi) {
139 check(firstSemi, "last");
140 }
141 if (secondSemi) {
142 check(secondSemi, "last");
143 }
144 }
145 };
146 }
147};