1 | /**
|
---|
2 | * @fileoverview Disallows or enforces spaces inside computed properties.
|
---|
3 | * @author Jamund Ferguson
|
---|
4 | * @deprecated in ESLint v8.53.0
|
---|
5 | */
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const astUtils = require("./utils/ast-utils");
|
---|
9 |
|
---|
10 | //------------------------------------------------------------------------------
|
---|
11 | // Rule Definition
|
---|
12 | //------------------------------------------------------------------------------
|
---|
13 |
|
---|
14 | /** @type {import('../shared/types').Rule} */
|
---|
15 | module.exports = {
|
---|
16 | meta: {
|
---|
17 | deprecated: true,
|
---|
18 | replacedBy: [],
|
---|
19 | type: "layout",
|
---|
20 |
|
---|
21 | docs: {
|
---|
22 | description: "Enforce consistent spacing inside computed property brackets",
|
---|
23 | recommended: false,
|
---|
24 | url: "https://eslint.org/docs/latest/rules/computed-property-spacing"
|
---|
25 | },
|
---|
26 |
|
---|
27 | fixable: "whitespace",
|
---|
28 |
|
---|
29 | schema: [
|
---|
30 | {
|
---|
31 | enum: ["always", "never"]
|
---|
32 | },
|
---|
33 | {
|
---|
34 | type: "object",
|
---|
35 | properties: {
|
---|
36 | enforceForClassMembers: {
|
---|
37 | type: "boolean",
|
---|
38 | default: true
|
---|
39 | }
|
---|
40 | },
|
---|
41 | additionalProperties: false
|
---|
42 | }
|
---|
43 | ],
|
---|
44 |
|
---|
45 | messages: {
|
---|
46 | unexpectedSpaceBefore: "There should be no space before '{{tokenValue}}'.",
|
---|
47 | unexpectedSpaceAfter: "There should be no space after '{{tokenValue}}'.",
|
---|
48 |
|
---|
49 | missingSpaceBefore: "A space is required before '{{tokenValue}}'.",
|
---|
50 | missingSpaceAfter: "A space is required after '{{tokenValue}}'."
|
---|
51 | }
|
---|
52 | },
|
---|
53 |
|
---|
54 | create(context) {
|
---|
55 | const sourceCode = context.sourceCode;
|
---|
56 | const propertyNameMustBeSpaced = context.options[0] === "always"; // default is "never"
|
---|
57 | const enforceForClassMembers = !context.options[1] || context.options[1].enforceForClassMembers;
|
---|
58 |
|
---|
59 | //--------------------------------------------------------------------------
|
---|
60 | // Helpers
|
---|
61 | //--------------------------------------------------------------------------
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Reports that there shouldn't be a space after the first token
|
---|
65 | * @param {ASTNode} node The node to report in the event of an error.
|
---|
66 | * @param {Token} token The token to use for the report.
|
---|
67 | * @param {Token} tokenAfter The token after `token`.
|
---|
68 | * @returns {void}
|
---|
69 | */
|
---|
70 | function reportNoBeginningSpace(node, token, tokenAfter) {
|
---|
71 | context.report({
|
---|
72 | node,
|
---|
73 | loc: { start: token.loc.end, end: tokenAfter.loc.start },
|
---|
74 | messageId: "unexpectedSpaceAfter",
|
---|
75 | data: {
|
---|
76 | tokenValue: token.value
|
---|
77 | },
|
---|
78 | fix(fixer) {
|
---|
79 | return fixer.removeRange([token.range[1], tokenAfter.range[0]]);
|
---|
80 | }
|
---|
81 | });
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Reports that there shouldn't be a space before the last token
|
---|
86 | * @param {ASTNode} node The node to report in the event of an error.
|
---|
87 | * @param {Token} token The token to use for the report.
|
---|
88 | * @param {Token} tokenBefore The token before `token`.
|
---|
89 | * @returns {void}
|
---|
90 | */
|
---|
91 | function reportNoEndingSpace(node, token, tokenBefore) {
|
---|
92 | context.report({
|
---|
93 | node,
|
---|
94 | loc: { start: tokenBefore.loc.end, end: token.loc.start },
|
---|
95 | messageId: "unexpectedSpaceBefore",
|
---|
96 | data: {
|
---|
97 | tokenValue: token.value
|
---|
98 | },
|
---|
99 | fix(fixer) {
|
---|
100 | return fixer.removeRange([tokenBefore.range[1], token.range[0]]);
|
---|
101 | }
|
---|
102 | });
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Reports that there should be a space after the first token
|
---|
107 | * @param {ASTNode} node The node to report in the event of an error.
|
---|
108 | * @param {Token} token The token to use for the report.
|
---|
109 | * @returns {void}
|
---|
110 | */
|
---|
111 | function reportRequiredBeginningSpace(node, token) {
|
---|
112 | context.report({
|
---|
113 | node,
|
---|
114 | loc: token.loc,
|
---|
115 | messageId: "missingSpaceAfter",
|
---|
116 | data: {
|
---|
117 | tokenValue: token.value
|
---|
118 | },
|
---|
119 | fix(fixer) {
|
---|
120 | return fixer.insertTextAfter(token, " ");
|
---|
121 | }
|
---|
122 | });
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Reports that there should be a space before the last token
|
---|
127 | * @param {ASTNode} node The node to report in the event of an error.
|
---|
128 | * @param {Token} token The token to use for the report.
|
---|
129 | * @returns {void}
|
---|
130 | */
|
---|
131 | function reportRequiredEndingSpace(node, token) {
|
---|
132 | context.report({
|
---|
133 | node,
|
---|
134 | loc: token.loc,
|
---|
135 | messageId: "missingSpaceBefore",
|
---|
136 | data: {
|
---|
137 | tokenValue: token.value
|
---|
138 | },
|
---|
139 | fix(fixer) {
|
---|
140 | return fixer.insertTextBefore(token, " ");
|
---|
141 | }
|
---|
142 | });
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Returns a function that checks the spacing of a node on the property name
|
---|
147 | * that was passed in.
|
---|
148 | * @param {string} propertyName The property on the node to check for spacing
|
---|
149 | * @returns {Function} A function that will check spacing on a node
|
---|
150 | */
|
---|
151 | function checkSpacing(propertyName) {
|
---|
152 | return function(node) {
|
---|
153 | if (!node.computed) {
|
---|
154 | return;
|
---|
155 | }
|
---|
156 |
|
---|
157 | const property = node[propertyName];
|
---|
158 |
|
---|
159 | const before = sourceCode.getTokenBefore(property, astUtils.isOpeningBracketToken),
|
---|
160 | first = sourceCode.getTokenAfter(before, { includeComments: true }),
|
---|
161 | after = sourceCode.getTokenAfter(property, astUtils.isClosingBracketToken),
|
---|
162 | last = sourceCode.getTokenBefore(after, { includeComments: true });
|
---|
163 |
|
---|
164 | if (astUtils.isTokenOnSameLine(before, first)) {
|
---|
165 | if (propertyNameMustBeSpaced) {
|
---|
166 | if (!sourceCode.isSpaceBetweenTokens(before, first) && astUtils.isTokenOnSameLine(before, first)) {
|
---|
167 | reportRequiredBeginningSpace(node, before);
|
---|
168 | }
|
---|
169 | } else {
|
---|
170 | if (sourceCode.isSpaceBetweenTokens(before, first)) {
|
---|
171 | reportNoBeginningSpace(node, before, first);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (astUtils.isTokenOnSameLine(last, after)) {
|
---|
177 | if (propertyNameMustBeSpaced) {
|
---|
178 | if (!sourceCode.isSpaceBetweenTokens(last, after) && astUtils.isTokenOnSameLine(last, after)) {
|
---|
179 | reportRequiredEndingSpace(node, after);
|
---|
180 | }
|
---|
181 | } else {
|
---|
182 | if (sourceCode.isSpaceBetweenTokens(last, after)) {
|
---|
183 | reportNoEndingSpace(node, after, last);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | };
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | //--------------------------------------------------------------------------
|
---|
192 | // Public
|
---|
193 | //--------------------------------------------------------------------------
|
---|
194 |
|
---|
195 | const listeners = {
|
---|
196 | Property: checkSpacing("key"),
|
---|
197 | MemberExpression: checkSpacing("property")
|
---|
198 | };
|
---|
199 |
|
---|
200 | if (enforceForClassMembers) {
|
---|
201 | listeners.MethodDefinition =
|
---|
202 | listeners.PropertyDefinition = listeners.Property;
|
---|
203 | }
|
---|
204 |
|
---|
205 | return listeners;
|
---|
206 |
|
---|
207 | }
|
---|
208 | };
|
---|