UNPKG

6.89 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const util = __importStar(require("../util"));
23const getESLintCoreRule_1 = require("../util/getESLintCoreRule");
24const utils_1 = require("@typescript-eslint/utils");
25const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('comma-dangle');
26const OPTION_VALUE_SCHEME = [
27 'always-multiline',
28 'always',
29 'never',
30 'only-multiline',
31];
32const DEFAULT_OPTION_VALUE = 'never';
33function normalizeOptions(options) {
34 var _a, _b, _c;
35 if (typeof options === 'string') {
36 return {
37 enums: options,
38 generics: options,
39 tuples: options,
40 };
41 }
42 return {
43 enums: (_a = options.enums) !== null && _a !== void 0 ? _a : DEFAULT_OPTION_VALUE,
44 generics: (_b = options.generics) !== null && _b !== void 0 ? _b : DEFAULT_OPTION_VALUE,
45 tuples: (_c = options.tuples) !== null && _c !== void 0 ? _c : DEFAULT_OPTION_VALUE,
46 };
47}
48exports.default = util.createRule({
49 name: 'comma-dangle',
50 meta: {
51 type: 'layout',
52 docs: {
53 description: 'Require or disallow trailing comma',
54 recommended: false,
55 extendsBaseRule: true,
56 },
57 schema: {
58 definitions: {
59 value: {
60 enum: OPTION_VALUE_SCHEME,
61 },
62 valueWithIgnore: {
63 enum: [...OPTION_VALUE_SCHEME, 'ignore'],
64 },
65 },
66 type: 'array',
67 items: [
68 {
69 oneOf: [
70 {
71 $ref: '#/definitions/value',
72 },
73 {
74 type: 'object',
75 properties: {
76 arrays: { $ref: '#/definitions/valueWithIgnore' },
77 objects: { $ref: '#/definitions/valueWithIgnore' },
78 imports: { $ref: '#/definitions/valueWithIgnore' },
79 exports: { $ref: '#/definitions/valueWithIgnore' },
80 functions: { $ref: '#/definitions/valueWithIgnore' },
81 enums: { $ref: '#/definitions/valueWithIgnore' },
82 generics: { $ref: '#/definitions/valueWithIgnore' },
83 tuples: { $ref: '#/definitions/valueWithIgnore' },
84 },
85 additionalProperties: false,
86 },
87 ],
88 },
89 ],
90 additionalProperties: false,
91 },
92 fixable: 'code',
93 hasSuggestions: baseRule.meta.hasSuggestions,
94 messages: baseRule.meta.messages,
95 },
96 defaultOptions: ['never'],
97 create(context, [options]) {
98 const rules = baseRule.create(context);
99 const sourceCode = context.getSourceCode();
100 const normalizedOptions = normalizeOptions(options);
101 const predicate = {
102 always: forceComma,
103 'always-multiline': forceCommaIfMultiline,
104 'only-multiline': allowCommaIfMultiline,
105 never: forbidComma,
106 ignore: () => { },
107 };
108 function last(nodes) {
109 var _a;
110 return (_a = nodes[nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
111 }
112 function getLastItem(node) {
113 switch (node.type) {
114 case utils_1.AST_NODE_TYPES.TSEnumDeclaration:
115 return last(node.members);
116 case utils_1.AST_NODE_TYPES.TSTypeParameterDeclaration:
117 return last(node.params);
118 case utils_1.AST_NODE_TYPES.TSTupleType:
119 return last(node.elementTypes);
120 default:
121 return null;
122 }
123 }
124 function getTrailingToken(node) {
125 const last = getLastItem(node);
126 const trailing = last && sourceCode.getTokenAfter(last);
127 return trailing;
128 }
129 function isMultiline(node) {
130 const last = getLastItem(node);
131 const lastToken = sourceCode.getLastToken(node);
132 return (last === null || last === void 0 ? void 0 : last.loc.end.line) !== (lastToken === null || lastToken === void 0 ? void 0 : lastToken.loc.end.line);
133 }
134 function forbidComma(node) {
135 const last = getLastItem(node);
136 const trailing = getTrailingToken(node);
137 if (last && trailing && util.isCommaToken(trailing)) {
138 context.report({
139 node,
140 messageId: 'unexpected',
141 fix(fixer) {
142 return fixer.remove(trailing);
143 },
144 });
145 }
146 }
147 function forceComma(node) {
148 const last = getLastItem(node);
149 const trailing = getTrailingToken(node);
150 if (last && trailing && !util.isCommaToken(trailing)) {
151 context.report({
152 node,
153 messageId: 'missing',
154 fix(fixer) {
155 return fixer.insertTextAfter(last, ',');
156 },
157 });
158 }
159 }
160 function allowCommaIfMultiline(node) {
161 if (!isMultiline(node)) {
162 forbidComma(node);
163 }
164 }
165 function forceCommaIfMultiline(node) {
166 if (isMultiline(node)) {
167 forceComma(node);
168 }
169 else {
170 forbidComma(node);
171 }
172 }
173 return Object.assign(Object.assign({}, rules), { TSEnumDeclaration: predicate[normalizedOptions.enums], TSTypeParameterDeclaration: predicate[normalizedOptions.generics], TSTupleType: predicate[normalizedOptions.tuples] });
174 },
175});
176//# sourceMappingURL=comma-dangle.js.map
\No newline at end of file