1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
9 | if (k2 === undefined) k2 = k;
|
10 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
11 | }) : (function(o, m, k, k2) {
|
12 | if (k2 === undefined) k2 = k;
|
13 | o[k2] = m[k];
|
14 | }));
|
15 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
16 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
17 | }) : function(o, v) {
|
18 | o["default"] = v;
|
19 | });
|
20 | var __importStar = (this && this.__importStar) || function (mod) {
|
21 | if (mod && mod.__esModule) return mod;
|
22 | var result = {};
|
23 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
24 | __setModuleDefault(result, mod);
|
25 | return result;
|
26 | };
|
27 | Object.defineProperty(exports, "__esModule", { value: true });
|
28 | const utils_1 = require("@typescript-eslint/utils");
|
29 | const getESLintCoreRule_1 = require("../util/getESLintCoreRule");
|
30 | const util = __importStar(require("../util"));
|
31 | const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('indent');
|
32 | const KNOWN_NODES = new Set([
|
33 |
|
34 | utils_1.AST_NODE_TYPES.PropertyDefinition,
|
35 |
|
36 | utils_1.AST_NODE_TYPES.TSAbstractKeyword,
|
37 | utils_1.AST_NODE_TYPES.TSAnyKeyword,
|
38 | utils_1.AST_NODE_TYPES.TSBooleanKeyword,
|
39 | utils_1.AST_NODE_TYPES.TSNeverKeyword,
|
40 | utils_1.AST_NODE_TYPES.TSNumberKeyword,
|
41 | utils_1.AST_NODE_TYPES.TSStringKeyword,
|
42 | utils_1.AST_NODE_TYPES.TSSymbolKeyword,
|
43 | utils_1.AST_NODE_TYPES.TSUndefinedKeyword,
|
44 | utils_1.AST_NODE_TYPES.TSUnknownKeyword,
|
45 | utils_1.AST_NODE_TYPES.TSVoidKeyword,
|
46 | utils_1.AST_NODE_TYPES.TSNullKeyword,
|
47 |
|
48 | utils_1.AST_NODE_TYPES.TSAbstractPropertyDefinition,
|
49 | utils_1.AST_NODE_TYPES.TSAbstractMethodDefinition,
|
50 | utils_1.AST_NODE_TYPES.TSArrayType,
|
51 | utils_1.AST_NODE_TYPES.TSAsExpression,
|
52 | utils_1.AST_NODE_TYPES.TSCallSignatureDeclaration,
|
53 | utils_1.AST_NODE_TYPES.TSConditionalType,
|
54 | utils_1.AST_NODE_TYPES.TSConstructorType,
|
55 | utils_1.AST_NODE_TYPES.TSConstructSignatureDeclaration,
|
56 | utils_1.AST_NODE_TYPES.TSDeclareFunction,
|
57 | utils_1.AST_NODE_TYPES.TSEmptyBodyFunctionExpression,
|
58 | utils_1.AST_NODE_TYPES.TSEnumDeclaration,
|
59 | utils_1.AST_NODE_TYPES.TSEnumMember,
|
60 | utils_1.AST_NODE_TYPES.TSExportAssignment,
|
61 | utils_1.AST_NODE_TYPES.TSExternalModuleReference,
|
62 | utils_1.AST_NODE_TYPES.TSFunctionType,
|
63 | utils_1.AST_NODE_TYPES.TSImportType,
|
64 | utils_1.AST_NODE_TYPES.TSIndexedAccessType,
|
65 | utils_1.AST_NODE_TYPES.TSIndexSignature,
|
66 | utils_1.AST_NODE_TYPES.TSInferType,
|
67 | utils_1.AST_NODE_TYPES.TSInterfaceBody,
|
68 | utils_1.AST_NODE_TYPES.TSInterfaceDeclaration,
|
69 | utils_1.AST_NODE_TYPES.TSInterfaceHeritage,
|
70 | utils_1.AST_NODE_TYPES.TSIntersectionType,
|
71 | utils_1.AST_NODE_TYPES.TSImportEqualsDeclaration,
|
72 | utils_1.AST_NODE_TYPES.TSLiteralType,
|
73 | utils_1.AST_NODE_TYPES.TSMappedType,
|
74 | utils_1.AST_NODE_TYPES.TSMethodSignature,
|
75 | 'TSMinusToken',
|
76 | utils_1.AST_NODE_TYPES.TSModuleBlock,
|
77 | utils_1.AST_NODE_TYPES.TSModuleDeclaration,
|
78 | utils_1.AST_NODE_TYPES.TSNonNullExpression,
|
79 | utils_1.AST_NODE_TYPES.TSParameterProperty,
|
80 | 'TSPlusToken',
|
81 | utils_1.AST_NODE_TYPES.TSPropertySignature,
|
82 | utils_1.AST_NODE_TYPES.TSQualifiedName,
|
83 | 'TSQuestionToken',
|
84 | utils_1.AST_NODE_TYPES.TSRestType,
|
85 | utils_1.AST_NODE_TYPES.TSThisType,
|
86 | utils_1.AST_NODE_TYPES.TSTupleType,
|
87 | utils_1.AST_NODE_TYPES.TSTypeAnnotation,
|
88 | utils_1.AST_NODE_TYPES.TSTypeLiteral,
|
89 | utils_1.AST_NODE_TYPES.TSTypeOperator,
|
90 | utils_1.AST_NODE_TYPES.TSTypeParameter,
|
91 | utils_1.AST_NODE_TYPES.TSTypeParameterDeclaration,
|
92 | utils_1.AST_NODE_TYPES.TSTypeParameterInstantiation,
|
93 | utils_1.AST_NODE_TYPES.TSTypeReference,
|
94 | utils_1.AST_NODE_TYPES.TSUnionType,
|
95 | utils_1.AST_NODE_TYPES.Decorator,
|
96 | ]);
|
97 | exports.default = util.createRule({
|
98 | name: 'indent',
|
99 | meta: {
|
100 | type: 'layout',
|
101 | docs: {
|
102 | description: 'Enforce consistent indentation',
|
103 |
|
104 | recommended: false,
|
105 | extendsBaseRule: true,
|
106 | },
|
107 | fixable: 'whitespace',
|
108 | hasSuggestions: baseRule.meta.hasSuggestions,
|
109 | schema: baseRule.meta.schema,
|
110 | messages: baseRule.meta.messages,
|
111 | },
|
112 | defaultOptions: [
|
113 |
|
114 | 4,
|
115 | {
|
116 |
|
117 |
|
118 | SwitchCase: 1,
|
119 | flatTernaryExpressions: false,
|
120 | ignoredNodes: [],
|
121 | },
|
122 | ],
|
123 | create(context, optionsWithDefaults) {
|
124 |
|
125 |
|
126 | const contextWithDefaults = Object.create(context, {
|
127 | options: {
|
128 | writable: false,
|
129 | configurable: false,
|
130 | value: optionsWithDefaults,
|
131 | },
|
132 | });
|
133 | const rules = baseRule.create(contextWithDefaults);
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | function TSPropertySignatureToProperty(node, type = utils_1.AST_NODE_TYPES.Property) {
|
141 | const base = {
|
142 |
|
143 | key: null,
|
144 | value: null,
|
145 |
|
146 | computed: false,
|
147 | method: false,
|
148 | kind: 'init',
|
149 |
|
150 | shorthand: true,
|
151 |
|
152 | parent: node.parent,
|
153 | range: node.range,
|
154 | loc: node.loc,
|
155 | };
|
156 | if (type === utils_1.AST_NODE_TYPES.Property) {
|
157 | return Object.assign({ type }, base);
|
158 | }
|
159 | else {
|
160 | return Object.assign({ type, static: false, readonly: false, declare: false }, base);
|
161 | }
|
162 | }
|
163 | return Object.assign({}, rules, {
|
164 |
|
165 | '*:exit'(node) {
|
166 |
|
167 | if (!KNOWN_NODES.has(node.type)) {
|
168 | rules['*:exit'](node);
|
169 | }
|
170 | },
|
171 | VariableDeclaration(node) {
|
172 |
|
173 | if (node.declarations.length === 0) {
|
174 | return;
|
175 | }
|
176 | return rules.VariableDeclaration(node);
|
177 | },
|
178 | TSAsExpression(node) {
|
179 |
|
180 | return rules['BinaryExpression, LogicalExpression']({
|
181 | type: utils_1.AST_NODE_TYPES.BinaryExpression,
|
182 | operator: 'as',
|
183 | left: node.expression,
|
184 |
|
185 | right: node.typeAnnotation,
|
186 |
|
187 | parent: node.parent,
|
188 | range: node.range,
|
189 | loc: node.loc,
|
190 | });
|
191 | },
|
192 | TSConditionalType(node) {
|
193 |
|
194 | return rules.ConditionalExpression({
|
195 | type: utils_1.AST_NODE_TYPES.ConditionalExpression,
|
196 | test: {
|
197 | type: utils_1.AST_NODE_TYPES.BinaryExpression,
|
198 | operator: 'extends',
|
199 | left: node.checkType,
|
200 | right: node.extendsType,
|
201 |
|
202 | range: [node.checkType.range[0], node.extendsType.range[1]],
|
203 | loc: {
|
204 | start: node.checkType.loc.start,
|
205 | end: node.extendsType.loc.end,
|
206 | },
|
207 | },
|
208 | consequent: node.trueType,
|
209 | alternate: node.falseType,
|
210 |
|
211 | parent: node.parent,
|
212 | range: node.range,
|
213 | loc: node.loc,
|
214 | });
|
215 | },
|
216 | 'TSEnumDeclaration, TSTypeLiteral'(node) {
|
217 |
|
218 | return rules['ObjectExpression, ObjectPattern']({
|
219 | type: utils_1.AST_NODE_TYPES.ObjectExpression,
|
220 | properties: node.members.map(member => TSPropertySignatureToProperty(member)),
|
221 |
|
222 | parent: node.parent,
|
223 | range: node.range,
|
224 | loc: node.loc,
|
225 | });
|
226 | },
|
227 | TSImportEqualsDeclaration(node) {
|
228 |
|
229 |
|
230 | const { id, moduleReference } = node;
|
231 | return rules.VariableDeclaration({
|
232 | type: utils_1.AST_NODE_TYPES.VariableDeclaration,
|
233 | kind: 'const',
|
234 | declarations: [
|
235 | {
|
236 | type: utils_1.AST_NODE_TYPES.VariableDeclarator,
|
237 | range: [id.range[0], moduleReference.range[1]],
|
238 | loc: {
|
239 | start: id.loc.start,
|
240 | end: moduleReference.loc.end,
|
241 | },
|
242 | id: id,
|
243 | init: {
|
244 | type: utils_1.AST_NODE_TYPES.CallExpression,
|
245 | callee: {
|
246 | type: utils_1.AST_NODE_TYPES.Identifier,
|
247 | name: 'require',
|
248 | range: [
|
249 | moduleReference.range[0],
|
250 | moduleReference.range[0] + 'require'.length,
|
251 | ],
|
252 | loc: {
|
253 | start: moduleReference.loc.start,
|
254 | end: {
|
255 | line: moduleReference.loc.end.line,
|
256 | column: moduleReference.loc.start.line + 'require'.length,
|
257 | },
|
258 | },
|
259 | },
|
260 | arguments: 'expression' in moduleReference
|
261 | ? [moduleReference.expression]
|
262 | : [],
|
263 |
|
264 | range: moduleReference.range,
|
265 | loc: moduleReference.loc,
|
266 | },
|
267 | },
|
268 | ],
|
269 |
|
270 | parent: node.parent,
|
271 | range: node.range,
|
272 | loc: node.loc,
|
273 | });
|
274 | },
|
275 | TSIndexedAccessType(node) {
|
276 |
|
277 | return rules['MemberExpression, JSXMemberExpression, MetaProperty']({
|
278 | type: utils_1.AST_NODE_TYPES.MemberExpression,
|
279 | object: node.objectType,
|
280 | property: node.indexType,
|
281 |
|
282 | parent: node.parent,
|
283 | range: node.range,
|
284 | loc: node.loc,
|
285 | optional: false,
|
286 | computed: true,
|
287 | });
|
288 | },
|
289 | TSInterfaceBody(node) {
|
290 |
|
291 | return rules['BlockStatement, ClassBody']({
|
292 | type: utils_1.AST_NODE_TYPES.ClassBody,
|
293 | body: node.body.map(p => TSPropertySignatureToProperty(p, utils_1.AST_NODE_TYPES.PropertyDefinition)),
|
294 |
|
295 | parent: node.parent,
|
296 | range: node.range,
|
297 | loc: node.loc,
|
298 | });
|
299 | },
|
300 | 'TSInterfaceDeclaration[extends.length > 0]'(node) {
|
301 |
|
302 | return rules['ClassDeclaration[superClass], ClassExpression[superClass]']({
|
303 | type: utils_1.AST_NODE_TYPES.ClassDeclaration,
|
304 | body: node.body,
|
305 | id: null,
|
306 |
|
307 | superClass: node.extends[0].expression,
|
308 |
|
309 | parent: node.parent,
|
310 | range: node.range,
|
311 | loc: node.loc,
|
312 | });
|
313 | },
|
314 | TSMappedType(node) {
|
315 | const sourceCode = context.getSourceCode();
|
316 | const squareBracketStart = sourceCode.getTokenBefore(node.typeParameter);
|
317 |
|
318 | return rules['ObjectExpression, ObjectPattern']({
|
319 | type: utils_1.AST_NODE_TYPES.ObjectExpression,
|
320 | properties: [
|
321 | {
|
322 | type: utils_1.AST_NODE_TYPES.Property,
|
323 | key: node.typeParameter,
|
324 | value: node.typeAnnotation,
|
325 |
|
326 | range: [
|
327 | squareBracketStart.range[0],
|
328 | node.typeAnnotation
|
329 | ? node.typeAnnotation.range[1]
|
330 | : squareBracketStart.range[0],
|
331 | ],
|
332 | loc: {
|
333 | start: squareBracketStart.loc.start,
|
334 | end: node.typeAnnotation
|
335 | ? node.typeAnnotation.loc.end
|
336 | : squareBracketStart.loc.end,
|
337 | },
|
338 | kind: 'init',
|
339 | computed: false,
|
340 | method: false,
|
341 | shorthand: false,
|
342 | },
|
343 | ],
|
344 |
|
345 | parent: node.parent,
|
346 | range: node.range,
|
347 | loc: node.loc,
|
348 | });
|
349 | },
|
350 | TSModuleBlock(node) {
|
351 |
|
352 | return rules['BlockStatement, ClassBody']({
|
353 | type: utils_1.AST_NODE_TYPES.BlockStatement,
|
354 | body: node.body,
|
355 |
|
356 | parent: node.parent,
|
357 | range: node.range,
|
358 | loc: node.loc,
|
359 | });
|
360 | },
|
361 | TSQualifiedName(node) {
|
362 | return rules['MemberExpression, JSXMemberExpression, MetaProperty']({
|
363 | type: utils_1.AST_NODE_TYPES.MemberExpression,
|
364 | object: node.left,
|
365 | property: node.right,
|
366 |
|
367 | parent: node.parent,
|
368 | range: node.range,
|
369 | loc: node.loc,
|
370 | optional: false,
|
371 | computed: false,
|
372 | });
|
373 | },
|
374 | TSTupleType(node) {
|
375 |
|
376 | return rules['ArrayExpression, ArrayPattern']({
|
377 | type: utils_1.AST_NODE_TYPES.ArrayExpression,
|
378 | elements: node.elementTypes,
|
379 |
|
380 | parent: node.parent,
|
381 | range: node.range,
|
382 | loc: node.loc,
|
383 | });
|
384 | },
|
385 | TSTypeParameterDeclaration(node) {
|
386 | if (!node.params.length) {
|
387 | return;
|
388 | }
|
389 | const [name, ...attributes] = node.params;
|
390 |
|
391 |
|
392 | return rules.JSXOpeningElement({
|
393 | type: utils_1.AST_NODE_TYPES.JSXOpeningElement,
|
394 | selfClosing: false,
|
395 | name: name,
|
396 | attributes: attributes,
|
397 |
|
398 | parent: node.parent,
|
399 | range: node.range,
|
400 | loc: node.loc,
|
401 | });
|
402 | },
|
403 | });
|
404 | },
|
405 | });
|
406 |
|
\ | No newline at end of file |