source: imaps-frontend/node_modules/eslint/lib/rules/no-extra-semi.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/**
2 * @fileoverview Rule to flag use of unnecessary semicolons
3 * @author Nicholas C. Zakas
4 * @deprecated in ESLint v8.53.0
5 */
6
7"use strict";
8
9//------------------------------------------------------------------------------
10// Requirements
11//------------------------------------------------------------------------------
12
13const FixTracker = require("./utils/fix-tracker");
14const astUtils = require("./utils/ast-utils");
15
16//------------------------------------------------------------------------------
17// Rule Definition
18//------------------------------------------------------------------------------
19
20/** @type {import('../shared/types').Rule} */
21module.exports = {
22 meta: {
23 deprecated: true,
24 replacedBy: [],
25 type: "suggestion",
26
27 docs: {
28 description: "Disallow unnecessary semicolons",
29 recommended: true,
30 url: "https://eslint.org/docs/latest/rules/no-extra-semi"
31 },
32
33 fixable: "code",
34 schema: [],
35
36 messages: {
37 unexpected: "Unnecessary semicolon."
38 }
39 },
40
41 create(context) {
42 const sourceCode = context.sourceCode;
43
44 /**
45 * Checks if a node or token is fixable.
46 * A node is fixable if it can be removed without turning a subsequent statement into a directive after fixing other nodes.
47 * @param {Token} nodeOrToken The node or token to check.
48 * @returns {boolean} Whether or not the node is fixable.
49 */
50 function isFixable(nodeOrToken) {
51 const nextToken = sourceCode.getTokenAfter(nodeOrToken);
52
53 if (!nextToken || nextToken.type !== "String") {
54 return true;
55 }
56 const stringNode = sourceCode.getNodeByRangeIndex(nextToken.range[0]);
57
58 return !astUtils.isTopLevelExpressionStatement(stringNode.parent);
59 }
60
61 /**
62 * Reports an unnecessary semicolon error.
63 * @param {Node|Token} nodeOrToken A node or a token to be reported.
64 * @returns {void}
65 */
66 function report(nodeOrToken) {
67 context.report({
68 node: nodeOrToken,
69 messageId: "unexpected",
70 fix: isFixable(nodeOrToken)
71 ? fixer =>
72
73 /*
74 * Expand the replacement range to include the surrounding
75 * tokens to avoid conflicting with semi.
76 * https://github.com/eslint/eslint/issues/7928
77 */
78 new FixTracker(fixer, context.sourceCode)
79 .retainSurroundingTokens(nodeOrToken)
80 .remove(nodeOrToken)
81 : null
82 });
83 }
84
85 /**
86 * Checks for a part of a class body.
87 * This checks tokens from a specified token to a next MethodDefinition or the end of class body.
88 * @param {Token} firstToken The first token to check.
89 * @returns {void}
90 */
91 function checkForPartOfClassBody(firstToken) {
92 for (let token = firstToken;
93 token.type === "Punctuator" && !astUtils.isClosingBraceToken(token);
94 token = sourceCode.getTokenAfter(token)
95 ) {
96 if (astUtils.isSemicolonToken(token)) {
97 report(token);
98 }
99 }
100 }
101
102 return {
103
104 /**
105 * Reports this empty statement, except if the parent node is a loop.
106 * @param {Node} node A EmptyStatement node to be reported.
107 * @returns {void}
108 */
109 EmptyStatement(node) {
110 const parent = node.parent,
111 allowedParentTypes = [
112 "ForStatement",
113 "ForInStatement",
114 "ForOfStatement",
115 "WhileStatement",
116 "DoWhileStatement",
117 "IfStatement",
118 "LabeledStatement",
119 "WithStatement"
120 ];
121
122 if (!allowedParentTypes.includes(parent.type)) {
123 report(node);
124 }
125 },
126
127 /**
128 * Checks tokens from the head of this class body to the first MethodDefinition or the end of this class body.
129 * @param {Node} node A ClassBody node to check.
130 * @returns {void}
131 */
132 ClassBody(node) {
133 checkForPartOfClassBody(sourceCode.getFirstToken(node, 1)); // 0 is `{`.
134 },
135
136 /**
137 * Checks tokens from this MethodDefinition to the next MethodDefinition or the end of this class body.
138 * @param {Node} node A MethodDefinition node of the start point.
139 * @returns {void}
140 */
141 "MethodDefinition, PropertyDefinition, StaticBlock"(node) {
142 checkForPartOfClassBody(sourceCode.getTokenAfter(node));
143 }
144 };
145
146 }
147};
Note: See TracBrowser for help on using the repository browser.