2 * @fileoverview Rule to flag use of unnecessary semicolons
3 * @author Nicholas C. Zakas
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const FixTracker = require("./utils/fix-tracker");
13 const astUtils = require("./utils/ast-utils");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
24 description: "disallow unnecessary semicolons",
25 category: "Possible Errors",
27 url: "https://eslint.org/docs/rules/no-extra-semi"
34 unexpected: "Unnecessary semicolon."
39 const sourceCode = context.getSourceCode();
42 * Reports an unnecessary semicolon error.
43 * @param {Node|Token} nodeOrToken A node or a token to be reported.
46 function report(nodeOrToken) {
49 messageId: "unexpected",
53 * Expand the replacement range to include the surrounding
54 * tokens to avoid conflicting with semi.
55 * https://github.com/eslint/eslint/issues/7928
57 return new FixTracker(fixer, context.getSourceCode())
58 .retainSurroundingTokens(nodeOrToken)
65 * Checks for a part of a class body.
66 * This checks tokens from a specified token to a next MethodDefinition or the end of class body.
67 * @param {Token} firstToken The first token to check.
70 function checkForPartOfClassBody(firstToken) {
71 for (let token = firstToken;
72 token.type === "Punctuator" && !astUtils.isClosingBraceToken(token);
73 token = sourceCode.getTokenAfter(token)
75 if (astUtils.isSemicolonToken(token)) {
84 * Reports this empty statement, except if the parent node is a loop.
85 * @param {Node} node A EmptyStatement node to be reported.
88 EmptyStatement(node) {
89 const parent = node.parent,
90 allowedParentTypes = [
101 if (allowedParentTypes.indexOf(parent.type) === -1) {
107 * Checks tokens from the head of this class body to the first MethodDefinition or the end of this class body.
108 * @param {Node} node A ClassBody node to check.
112 checkForPartOfClassBody(sourceCode.getFirstToken(node, 1)); // 0 is `{`.
116 * Checks tokens from this MethodDefinition to the next MethodDefinition or the end of this class body.
117 * @param {Node} node A MethodDefinition node of the start point.
120 MethodDefinition(node) {
121 checkForPartOfClassBody(sourceCode.getTokenAfter(node));