2 * @fileoverview Disallows or enforces spaces inside computed properties.
3 * @author Jamund Ferguson
7 const astUtils = require("./utils/ast-utils");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description: "enforce consistent spacing inside computed property brackets",
19 category: "Stylistic Issues",
21 url: "https://eslint.org/docs/rules/computed-property-spacing"
24 fixable: "whitespace",
28 enum: ["always", "never"]
33 enforceForClassMembers: {
38 additionalProperties: false
43 unexpectedSpaceBefore: "There should be no space before '{{tokenValue}}'.",
44 unexpectedSpaceAfter: "There should be no space after '{{tokenValue}}'.",
46 missingSpaceBefore: "A space is required before '{{tokenValue}}'.",
47 missingSpaceAfter: "A space is required after '{{tokenValue}}'."
52 const sourceCode = context.getSourceCode();
53 const propertyNameMustBeSpaced = context.options[0] === "always"; // default is "never"
54 const enforceForClassMembers = context.options[1] && context.options[1].enforceForClassMembers;
56 //--------------------------------------------------------------------------
58 //--------------------------------------------------------------------------
61 * Reports that there shouldn't be a space after the first token
62 * @param {ASTNode} node The node to report in the event of an error.
63 * @param {Token} token The token to use for the report.
64 * @param {Token} tokenAfter The token after `token`.
67 function reportNoBeginningSpace(node, token, tokenAfter) {
71 messageId: "unexpectedSpaceAfter",
73 tokenValue: token.value
76 return fixer.removeRange([token.range[1], tokenAfter.range[0]]);
82 * Reports that there shouldn't be a space before the last token
83 * @param {ASTNode} node The node to report in the event of an error.
84 * @param {Token} token The token to use for the report.
85 * @param {Token} tokenBefore The token before `token`.
88 function reportNoEndingSpace(node, token, tokenBefore) {
92 messageId: "unexpectedSpaceBefore",
94 tokenValue: token.value
97 return fixer.removeRange([tokenBefore.range[1], token.range[0]]);
103 * Reports that there should be a space after the first token
104 * @param {ASTNode} node The node to report in the event of an error.
105 * @param {Token} token The token to use for the report.
108 function reportRequiredBeginningSpace(node, token) {
111 loc: token.loc.start,
112 messageId: "missingSpaceAfter",
114 tokenValue: token.value
117 return fixer.insertTextAfter(token, " ");
123 * Reports that there should be a space before the last token
124 * @param {ASTNode} node The node to report in the event of an error.
125 * @param {Token} token The token to use for the report.
128 function reportRequiredEndingSpace(node, token) {
131 loc: token.loc.start,
132 messageId: "missingSpaceBefore",
134 tokenValue: token.value
137 return fixer.insertTextBefore(token, " ");
143 * Returns a function that checks the spacing of a node on the property name
144 * that was passed in.
145 * @param {string} propertyName The property on the node to check for spacing
146 * @returns {Function} A function that will check spacing on a node
148 function checkSpacing(propertyName) {
149 return function(node) {
150 if (!node.computed) {
154 const property = node[propertyName];
156 const before = sourceCode.getTokenBefore(property, astUtils.isOpeningBracketToken),
157 first = sourceCode.getTokenAfter(before, { includeComments: true }),
158 after = sourceCode.getTokenAfter(property, astUtils.isClosingBracketToken),
159 last = sourceCode.getTokenBefore(after, { includeComments: true });
161 if (astUtils.isTokenOnSameLine(before, first)) {
162 if (propertyNameMustBeSpaced) {
163 if (!sourceCode.isSpaceBetweenTokens(before, first) && astUtils.isTokenOnSameLine(before, first)) {
164 reportRequiredBeginningSpace(node, before);
167 if (sourceCode.isSpaceBetweenTokens(before, first)) {
168 reportNoBeginningSpace(node, before, first);
173 if (astUtils.isTokenOnSameLine(last, after)) {
174 if (propertyNameMustBeSpaced) {
175 if (!sourceCode.isSpaceBetweenTokens(last, after) && astUtils.isTokenOnSameLine(last, after)) {
176 reportRequiredEndingSpace(node, after);
179 if (sourceCode.isSpaceBetweenTokens(last, after)) {
180 reportNoEndingSpace(node, after, last);
188 //--------------------------------------------------------------------------
190 //--------------------------------------------------------------------------
193 Property: checkSpacing("key"),
194 MemberExpression: checkSpacing("property")
197 if (enforceForClassMembers) {
198 listeners.MethodDefinition = checkSpacing("key");