source: imaps-frontend/node_modules/eslint/lib/rules/arrow-parens.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: 7.6 KB
RevLine 
[d565449]1/**
2 * @fileoverview Rule to require parens in arrow function arguments.
3 * @author Jxck
4 * @deprecated in ESLint v8.53.0
5 */
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const astUtils = require("./utils/ast-utils");
13
14//------------------------------------------------------------------------------
15// Helpers
16//------------------------------------------------------------------------------
17
18/**
19 * Determines if the given arrow function has block body.
20 * @param {ASTNode} node `ArrowFunctionExpression` node.
21 * @returns {boolean} `true` if the function has block body.
22 */
23function hasBlockBody(node) {
24 return node.body.type === "BlockStatement";
25}
26
27//------------------------------------------------------------------------------
28// Rule Definition
29//------------------------------------------------------------------------------
30
31/** @type {import('../shared/types').Rule} */
32module.exports = {
33 meta: {
34 deprecated: true,
35 replacedBy: [],
36 type: "layout",
37
38 docs: {
39 description: "Require parentheses around arrow function arguments",
40 recommended: false,
41 url: "https://eslint.org/docs/latest/rules/arrow-parens"
42 },
43
44 fixable: "code",
45
46 schema: [
47 {
48 enum: ["always", "as-needed"]
49 },
50 {
51 type: "object",
52 properties: {
53 requireForBlockBody: {
54 type: "boolean",
55 default: false
56 }
57 },
58 additionalProperties: false
59 }
60 ],
61
62 messages: {
63 unexpectedParens: "Unexpected parentheses around single function argument.",
64 expectedParens: "Expected parentheses around arrow function argument.",
65
66 unexpectedParensInline: "Unexpected parentheses around single function argument having a body with no curly braces.",
67 expectedParensBlock: "Expected parentheses around arrow function argument having a body with curly braces."
68 }
69 },
70
71 create(context) {
72 const asNeeded = context.options[0] === "as-needed";
73 const requireForBlockBody = asNeeded && context.options[1] && context.options[1].requireForBlockBody === true;
74
75 const sourceCode = context.sourceCode;
76
77 /**
78 * Finds opening paren of parameters for the given arrow function, if it exists.
79 * It is assumed that the given arrow function has exactly one parameter.
80 * @param {ASTNode} node `ArrowFunctionExpression` node.
81 * @returns {Token|null} the opening paren, or `null` if the given arrow function doesn't have parens of parameters.
82 */
83 function findOpeningParenOfParams(node) {
84 const tokenBeforeParams = sourceCode.getTokenBefore(node.params[0]);
85
86 if (
87 tokenBeforeParams &&
88 astUtils.isOpeningParenToken(tokenBeforeParams) &&
89 node.range[0] <= tokenBeforeParams.range[0]
90 ) {
91 return tokenBeforeParams;
92 }
93
94 return null;
95 }
96
97 /**
98 * Finds closing paren of parameters for the given arrow function.
99 * It is assumed that the given arrow function has parens of parameters and that it has exactly one parameter.
100 * @param {ASTNode} node `ArrowFunctionExpression` node.
101 * @returns {Token} the closing paren of parameters.
102 */
103 function getClosingParenOfParams(node) {
104 return sourceCode.getTokenAfter(node.params[0], astUtils.isClosingParenToken);
105 }
106
107 /**
108 * Determines whether the given arrow function has comments inside parens of parameters.
109 * It is assumed that the given arrow function has parens of parameters.
110 * @param {ASTNode} node `ArrowFunctionExpression` node.
111 * @param {Token} openingParen Opening paren of parameters.
112 * @returns {boolean} `true` if the function has at least one comment inside of parens of parameters.
113 */
114 function hasCommentsInParensOfParams(node, openingParen) {
115 return sourceCode.commentsExistBetween(openingParen, getClosingParenOfParams(node));
116 }
117
118 /**
119 * Determines whether the given arrow function has unexpected tokens before opening paren of parameters,
120 * in which case it will be assumed that the existing parens of parameters are necessary.
121 * Only tokens within the range of the arrow function (tokens that are part of the arrow function) are taken into account.
122 * Example: <T>(a) => b
123 * @param {ASTNode} node `ArrowFunctionExpression` node.
124 * @param {Token} openingParen Opening paren of parameters.
125 * @returns {boolean} `true` if the function has at least one unexpected token.
126 */
127 function hasUnexpectedTokensBeforeOpeningParen(node, openingParen) {
128 const expectedCount = node.async ? 1 : 0;
129
130 return sourceCode.getFirstToken(node, { skip: expectedCount }) !== openingParen;
131 }
132
133 return {
134 "ArrowFunctionExpression[params.length=1]"(node) {
135 const shouldHaveParens = !asNeeded || requireForBlockBody && hasBlockBody(node);
136 const openingParen = findOpeningParenOfParams(node);
137 const hasParens = openingParen !== null;
138 const [param] = node.params;
139
140 if (shouldHaveParens && !hasParens) {
141 context.report({
142 node,
143 messageId: requireForBlockBody ? "expectedParensBlock" : "expectedParens",
144 loc: param.loc,
145 *fix(fixer) {
146 yield fixer.insertTextBefore(param, "(");
147 yield fixer.insertTextAfter(param, ")");
148 }
149 });
150 }
151
152 if (
153 !shouldHaveParens &&
154 hasParens &&
155 param.type === "Identifier" &&
156 !param.typeAnnotation &&
157 !node.returnType &&
158 !hasCommentsInParensOfParams(node, openingParen) &&
159 !hasUnexpectedTokensBeforeOpeningParen(node, openingParen)
160 ) {
161 context.report({
162 node,
163 messageId: requireForBlockBody ? "unexpectedParensInline" : "unexpectedParens",
164 loc: param.loc,
165 *fix(fixer) {
166 const tokenBeforeOpeningParen = sourceCode.getTokenBefore(openingParen);
167 const closingParen = getClosingParenOfParams(node);
168
169 if (
170 tokenBeforeOpeningParen &&
171 tokenBeforeOpeningParen.range[1] === openingParen.range[0] &&
172 !astUtils.canTokensBeAdjacent(tokenBeforeOpeningParen, sourceCode.getFirstToken(param))
173 ) {
174 yield fixer.insertTextBefore(openingParen, " ");
175 }
176
177 // remove parens, whitespace inside parens, and possible trailing comma
178 yield fixer.removeRange([openingParen.range[0], param.range[0]]);
179 yield fixer.removeRange([param.range[1], closingParen.range[1]]);
180 }
181 });
182 }
183 }
184 };
185 }
186};
Note: See TracBrowser for help on using the repository browser.