UNPKG

5.66 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const utils_1 = require("@typescript-eslint/utils");
4const util_1 = require("../util");
5exports.default = (0, util_1.createRule)({
6 name: 'comma-spacing',
7 meta: {
8 type: 'suggestion',
9 docs: {
10 description: 'Enforces consistent spacing before and after commas',
11 recommended: false,
12 extendsBaseRule: true,
13 },
14 fixable: 'whitespace',
15 schema: [
16 {
17 type: 'object',
18 properties: {
19 before: {
20 type: 'boolean',
21 default: false,
22 },
23 after: {
24 type: 'boolean',
25 default: true,
26 },
27 },
28 additionalProperties: false,
29 },
30 ],
31 messages: {
32 unexpected: `There should be no space {{loc}} ','.`,
33 missing: `A space is required {{loc}} ','.`,
34 },
35 },
36 defaultOptions: [
37 {
38 before: false,
39 after: true,
40 },
41 ],
42 create(context, [{ before: spaceBefore, after: spaceAfter }]) {
43 const sourceCode = context.getSourceCode();
44 const tokensAndComments = sourceCode.tokensAndComments;
45 const ignoredTokens = new Set();
46 /**
47 * Adds null elements of the ArrayExpression or ArrayPattern node to the ignore list
48 * @param node node to evaluate
49 */
50 function addNullElementsToIgnoreList(node) {
51 let previousToken = sourceCode.getFirstToken(node);
52 for (const element of node.elements) {
53 let token;
54 if (element === null) {
55 token = sourceCode.getTokenAfter(previousToken);
56 if (token && (0, util_1.isCommaToken)(token)) {
57 ignoredTokens.add(token);
58 }
59 }
60 else {
61 token = sourceCode.getTokenAfter(element);
62 }
63 previousToken = token;
64 }
65 }
66 /**
67 * Adds type parameters trailing comma token to the ignore list
68 * @param node node to evaluate
69 */
70 function addTypeParametersTrailingCommaToIgnoreList(node) {
71 const paramLength = node.params.length;
72 if (paramLength) {
73 const param = node.params[paramLength - 1];
74 const afterToken = sourceCode.getTokenAfter(param);
75 if (afterToken && (0, util_1.isCommaToken)(afterToken)) {
76 ignoredTokens.add(afterToken);
77 }
78 }
79 }
80 /**
81 * Validates the spacing around a comma token.
82 * @param commaToken The token representing the comma
83 * @param prevToken The last token before the comma
84 * @param nextToken The first token after the comma
85 */
86 function validateCommaSpacing(commaToken, prevToken, nextToken) {
87 if (prevToken &&
88 (0, util_1.isTokenOnSameLine)(prevToken, commaToken) &&
89 spaceBefore !== sourceCode.isSpaceBetweenTokens(prevToken, commaToken)) {
90 context.report({
91 node: commaToken,
92 data: {
93 loc: 'before',
94 },
95 messageId: spaceBefore ? 'missing' : 'unexpected',
96 fix: fixer => spaceBefore
97 ? fixer.insertTextBefore(commaToken, ' ')
98 : fixer.replaceTextRange([prevToken.range[1], commaToken.range[0]], ''),
99 });
100 }
101 if (nextToken && (0, util_1.isClosingParenToken)(nextToken)) {
102 return;
103 }
104 if (!spaceAfter && nextToken && nextToken.type === utils_1.AST_TOKEN_TYPES.Line) {
105 return;
106 }
107 if (nextToken &&
108 (0, util_1.isTokenOnSameLine)(commaToken, nextToken) &&
109 spaceAfter !== sourceCode.isSpaceBetweenTokens(commaToken, nextToken)) {
110 context.report({
111 node: commaToken,
112 data: {
113 loc: 'after',
114 },
115 messageId: spaceAfter ? 'missing' : 'unexpected',
116 fix: fixer => spaceAfter
117 ? fixer.insertTextAfter(commaToken, ' ')
118 : fixer.replaceTextRange([commaToken.range[1], nextToken.range[0]], ''),
119 });
120 }
121 }
122 return {
123 TSTypeParameterDeclaration: addTypeParametersTrailingCommaToIgnoreList,
124 ArrayExpression: addNullElementsToIgnoreList,
125 ArrayPattern: addNullElementsToIgnoreList,
126 'Program:exit'() {
127 tokensAndComments.forEach((token, i) => {
128 if (!(0, util_1.isCommaToken)(token)) {
129 return;
130 }
131 const prevToken = tokensAndComments[i - 1];
132 const nextToken = tokensAndComments[i + 1];
133 validateCommaSpacing(token, (0, util_1.isCommaToken)(prevToken) || ignoredTokens.has(token)
134 ? null
135 : prevToken, (0, util_1.isCommaToken)(nextToken) || ignoredTokens.has(token)
136 ? null
137 : nextToken);
138 });
139 },
140 };
141 },
142});
143//# sourceMappingURL=comma-spacing.js.map
\No newline at end of file