]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/computed-property-spacing.js
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",
20 url
: "https://eslint.org/docs/rules/computed-property-spacing"
23 fixable
: "whitespace",
27 enum: ["always", "never"]
32 enforceForClassMembers
: {
37 additionalProperties
: false
42 unexpectedSpaceBefore
: "There should be no space before '{{tokenValue}}'.",
43 unexpectedSpaceAfter
: "There should be no space after '{{tokenValue}}'.",
45 missingSpaceBefore
: "A space is required before '{{tokenValue}}'.",
46 missingSpaceAfter
: "A space is required after '{{tokenValue}}'."
51 const sourceCode
= context
.getSourceCode();
52 const propertyNameMustBeSpaced
= context
.options
[0] === "always"; // default is "never"
53 const enforceForClassMembers
= !context
.options
[1] || context
.options
[1].enforceForClassMembers
;
55 //--------------------------------------------------------------------------
57 //--------------------------------------------------------------------------
60 * Reports that there shouldn't be a space after the first token
61 * @param {ASTNode} node The node to report in the event of an error.
62 * @param {Token} token The token to use for the report.
63 * @param {Token} tokenAfter The token after `token`.
66 function reportNoBeginningSpace(node
, token
, tokenAfter
) {
69 loc
: { start
: token
.loc
.end
, end
: tokenAfter
.loc
.start
},
70 messageId
: "unexpectedSpaceAfter",
72 tokenValue
: token
.value
75 return fixer
.removeRange([token
.range
[1], tokenAfter
.range
[0]]);
81 * Reports that there shouldn't be a space before the last token
82 * @param {ASTNode} node The node to report in the event of an error.
83 * @param {Token} token The token to use for the report.
84 * @param {Token} tokenBefore The token before `token`.
87 function reportNoEndingSpace(node
, token
, tokenBefore
) {
90 loc
: { start
: tokenBefore
.loc
.end
, end
: token
.loc
.start
},
91 messageId
: "unexpectedSpaceBefore",
93 tokenValue
: token
.value
96 return fixer
.removeRange([tokenBefore
.range
[1], token
.range
[0]]);
102 * Reports that there should be a space after the first token
103 * @param {ASTNode} node The node to report in the event of an error.
104 * @param {Token} token The token to use for the report.
107 function reportRequiredBeginningSpace(node
, token
) {
111 messageId
: "missingSpaceAfter",
113 tokenValue
: token
.value
116 return fixer
.insertTextAfter(token
, " ");
122 * Reports that there should be a space before the last token
123 * @param {ASTNode} node The node to report in the event of an error.
124 * @param {Token} token The token to use for the report.
127 function reportRequiredEndingSpace(node
, token
) {
131 messageId
: "missingSpaceBefore",
133 tokenValue
: token
.value
136 return fixer
.insertTextBefore(token
, " ");
142 * Returns a function that checks the spacing of a node on the property name
143 * that was passed in.
144 * @param {string} propertyName The property on the node to check for spacing
145 * @returns {Function} A function that will check spacing on a node
147 function checkSpacing(propertyName
) {
148 return function(node
) {
149 if (!node
.computed
) {
153 const property
= node
[propertyName
];
155 const before
= sourceCode
.getTokenBefore(property
, astUtils
.isOpeningBracketToken
),
156 first
= sourceCode
.getTokenAfter(before
, { includeComments
: true }),
157 after
= sourceCode
.getTokenAfter(property
, astUtils
.isClosingBracketToken
),
158 last
= sourceCode
.getTokenBefore(after
, { includeComments
: true });
160 if (astUtils
.isTokenOnSameLine(before
, first
)) {
161 if (propertyNameMustBeSpaced
) {
162 if (!sourceCode
.isSpaceBetweenTokens(before
, first
) && astUtils
.isTokenOnSameLine(before
, first
)) {
163 reportRequiredBeginningSpace(node
, before
);
166 if (sourceCode
.isSpaceBetweenTokens(before
, first
)) {
167 reportNoBeginningSpace(node
, before
, first
);
172 if (astUtils
.isTokenOnSameLine(last
, after
)) {
173 if (propertyNameMustBeSpaced
) {
174 if (!sourceCode
.isSpaceBetweenTokens(last
, after
) && astUtils
.isTokenOnSameLine(last
, after
)) {
175 reportRequiredEndingSpace(node
, after
);
178 if (sourceCode
.isSpaceBetweenTokens(last
, after
)) {
179 reportNoEndingSpace(node
, after
, last
);
187 //--------------------------------------------------------------------------
189 //--------------------------------------------------------------------------
192 Property
: checkSpacing("key"),
193 MemberExpression
: checkSpacing("property")
196 if (enforceForClassMembers
) {
197 listeners
.MethodDefinition
=
198 listeners
.PropertyDefinition
= listeners
.Property
;