]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/dot-notation.js
import 8.23.1 source
[pve-eslint.git] / eslint / lib / rules / dot-notation.js
1 /**
2 * @fileoverview Rule to warn about using dot notation instead of square bracket notation when possible.
3 * @author Josh Perez
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const astUtils = require("./utils/ast-utils");
12 const keywords = require("./utils/keywords");
13
14 //------------------------------------------------------------------------------
15 // Rule Definition
16 //------------------------------------------------------------------------------
17
18 const validIdentifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/u;
19
20 // `null` literal must be handled separately.
21 const literalTypesToCheck = new Set(["string", "boolean"]);
22
23 /** @type {import('../shared/types').Rule} */
24 module.exports = {
25 meta: {
26 type: "suggestion",
27
28 docs: {
29 description: "Enforce dot notation whenever possible",
30 recommended: false,
31 url: "https://eslint.org/docs/rules/dot-notation"
32 },
33
34 schema: [
35 {
36 type: "object",
37 properties: {
38 allowKeywords: {
39 type: "boolean",
40 default: true
41 },
42 allowPattern: {
43 type: "string",
44 default: ""
45 }
46 },
47 additionalProperties: false
48 }
49 ],
50
51 fixable: "code",
52
53 messages: {
54 useDot: "[{{key}}] is better written in dot notation.",
55 useBrackets: ".{{key}} is a syntax error."
56 }
57 },
58
59 create(context) {
60 const options = context.options[0] || {};
61 const allowKeywords = options.allowKeywords === void 0 || options.allowKeywords;
62 const sourceCode = context.getSourceCode();
63
64 let allowPattern;
65
66 if (options.allowPattern) {
67 allowPattern = new RegExp(options.allowPattern, "u");
68 }
69
70 /**
71 * Check if the property is valid dot notation
72 * @param {ASTNode} node The dot notation node
73 * @param {string} value Value which is to be checked
74 * @returns {void}
75 */
76 function checkComputedProperty(node, value) {
77 if (
78 validIdentifier.test(value) &&
79 (allowKeywords || !keywords.includes(String(value))) &&
80 !(allowPattern && allowPattern.test(value))
81 ) {
82 const formattedValue = node.property.type === "Literal" ? JSON.stringify(value) : `\`${value}\``;
83
84 context.report({
85 node: node.property,
86 messageId: "useDot",
87 data: {
88 key: formattedValue
89 },
90 *fix(fixer) {
91 const leftBracket = sourceCode.getTokenAfter(node.object, astUtils.isOpeningBracketToken);
92 const rightBracket = sourceCode.getLastToken(node);
93 const nextToken = sourceCode.getTokenAfter(node);
94
95 // Don't perform any fixes if there are comments inside the brackets.
96 if (sourceCode.commentsExistBetween(leftBracket, rightBracket)) {
97 return;
98 }
99
100 // Replace the brackets by an identifier.
101 if (!node.optional) {
102 yield fixer.insertTextBefore(
103 leftBracket,
104 astUtils.isDecimalInteger(node.object) ? " ." : "."
105 );
106 }
107 yield fixer.replaceTextRange(
108 [leftBracket.range[0], rightBracket.range[1]],
109 value
110 );
111
112 // Insert a space after the property if it will be connected to the next token.
113 if (
114 nextToken &&
115 rightBracket.range[1] === nextToken.range[0] &&
116 !astUtils.canTokensBeAdjacent(String(value), nextToken)
117 ) {
118 yield fixer.insertTextAfter(node, " ");
119 }
120 }
121 });
122 }
123 }
124
125 return {
126 MemberExpression(node) {
127 if (
128 node.computed &&
129 node.property.type === "Literal" &&
130 (literalTypesToCheck.has(typeof node.property.value) || astUtils.isNullLiteral(node.property))
131 ) {
132 checkComputedProperty(node, node.property.value);
133 }
134 if (
135 node.computed &&
136 node.property.type === "TemplateLiteral" &&
137 node.property.expressions.length === 0
138 ) {
139 checkComputedProperty(node, node.property.quasis[0].value.cooked);
140 }
141 if (
142 !allowKeywords &&
143 !node.computed &&
144 node.property.type === "Identifier" &&
145 keywords.includes(String(node.property.name))
146 ) {
147 context.report({
148 node: node.property,
149 messageId: "useBrackets",
150 data: {
151 key: node.property.name
152 },
153 *fix(fixer) {
154 const dotToken = sourceCode.getTokenBefore(node.property);
155
156 // A statement that starts with `let[` is parsed as a destructuring variable declaration, not a MemberExpression.
157 if (node.object.type === "Identifier" && node.object.name === "let" && !node.optional) {
158 return;
159 }
160
161 // Don't perform any fixes if there are comments between the dot and the property name.
162 if (sourceCode.commentsExistBetween(dotToken, node.property)) {
163 return;
164 }
165
166 // Replace the identifier to brackets.
167 if (!node.optional) {
168 yield fixer.remove(dotToken);
169 }
170 yield fixer.replaceText(node.property, `["${node.property.name}"]`);
171 }
172 });
173 }
174 }
175 };
176 }
177 };