1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const astUtils = require("../ast-utils");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const 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 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function 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 |
|
49 |
|
50 |
|
51 |
|
52 | function isLastChild(node) {
|
53 | const t = node.parent.type;
|
54 |
|
55 | if (t === "IfStatement" && node.parent.consequent === node && node.parent.alternate) {
|
56 | return true;
|
57 | }
|
58 | if (t === "DoWhileStatement") {
|
59 | return true;
|
60 | }
|
61 | const nodeList = getChildren(node.parent);
|
62 |
|
63 | return nodeList !== null && nodeList[nodeList.length - 1] === node;
|
64 | }
|
65 |
|
66 | module.exports = {
|
67 | meta: {
|
68 | docs: {
|
69 | description: "enforce location of semicolons",
|
70 | category: "Stylistic Issues",
|
71 | recommended: false,
|
72 | url: "https://eslint.org/docs/rules/semi-style"
|
73 | },
|
74 | schema: [{ enum: ["last", "first"] }],
|
75 | fixable: "whitespace"
|
76 | },
|
77 |
|
78 | create(context) {
|
79 | const sourceCode = context.getSourceCode();
|
80 | const option = context.options[0] || "last";
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function check(semiToken, expected) {
|
89 | const prevToken = sourceCode.getTokenBefore(semiToken);
|
90 | const nextToken = sourceCode.getTokenAfter(semiToken);
|
91 | const prevIsSameLine = !prevToken || astUtils.isTokenOnSameLine(prevToken, semiToken);
|
92 | const nextIsSameLine = !nextToken || astUtils.isTokenOnSameLine(semiToken, nextToken);
|
93 |
|
94 | if ((expected === "last" && !prevIsSameLine) || (expected === "first" && !nextIsSameLine)) {
|
95 | context.report({
|
96 | loc: semiToken.loc,
|
97 | message: "Expected this semicolon to be at {{pos}}.",
|
98 | data: {
|
99 | pos: (expected === "last")
|
100 | ? "the end of the previous line"
|
101 | : "the beginning of the next line"
|
102 | },
|
103 | fix(fixer) {
|
104 | if (prevToken && nextToken && sourceCode.commentsExistBetween(prevToken, nextToken)) {
|
105 | return null;
|
106 | }
|
107 |
|
108 | const start = prevToken ? prevToken.range[1] : semiToken.range[0];
|
109 | const end = nextToken ? nextToken.range[0] : semiToken.range[1];
|
110 | const text = (expected === "last") ? ";\n" : "\n;";
|
111 |
|
112 | return fixer.replaceTextRange([start, end], text);
|
113 | }
|
114 | });
|
115 | }
|
116 | }
|
117 |
|
118 | return {
|
119 | [SELECTOR](node) {
|
120 | if (option === "first" && isLastChild(node)) {
|
121 | return;
|
122 | }
|
123 |
|
124 | const lastToken = sourceCode.getLastToken(node);
|
125 |
|
126 | if (astUtils.isSemicolonToken(lastToken)) {
|
127 | check(lastToken, option);
|
128 | }
|
129 | },
|
130 |
|
131 | ForStatement(node) {
|
132 | const firstSemi = node.init && sourceCode.getTokenAfter(node.init, astUtils.isSemicolonToken);
|
133 | const secondSemi = node.test && sourceCode.getTokenAfter(node.test, astUtils.isSemicolonToken);
|
134 |
|
135 | if (firstSemi) {
|
136 | check(firstSemi, "last");
|
137 | }
|
138 | if (secondSemi) {
|
139 | check(secondSemi, "last");
|
140 | }
|
141 | }
|
142 | };
|
143 | }
|
144 | };
|