UNPKG

17.9 kBJavaScriptView Raw
1"use strict";
2/**
3 * Note this file is rather type-unsafe in its current state.
4 * This is due to some really funky type conversions between different node types.
5 * This is done intentionally based on the internal implementation of the base indent rule.
6 */
7/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment */
8var __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}));
15var __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});
20var __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};
27Object.defineProperty(exports, "__esModule", { value: true });
28const utils_1 = require("@typescript-eslint/utils");
29const getESLintCoreRule_1 = require("../util/getESLintCoreRule");
30const util = __importStar(require("../util"));
31const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('indent');
32const KNOWN_NODES = new Set([
33 // Class properties aren't yet supported by eslint...
34 utils_1.AST_NODE_TYPES.PropertyDefinition,
35 // ts keywords
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 // ts specific nodes we want to support
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]);
97exports.default = util.createRule({
98 name: 'indent',
99 meta: {
100 type: 'layout',
101 docs: {
102 description: 'Enforce consistent indentation',
103 // too opinionated to be recommended
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 // typescript docs and playground use 4 space indent
114 4,
115 {
116 // typescript docs indent the case from the switch
117 // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-8.html#example-4
118 SwitchCase: 1,
119 flatTernaryExpressions: false,
120 ignoredNodes: [],
121 },
122 ],
123 create(context, optionsWithDefaults) {
124 // because we extend the base rule, have to update opts on the context
125 // the context defines options as readonly though...
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 * Converts from a TSPropertySignature to a Property
136 * @param node a TSPropertySignature node
137 * @param [type] the type to give the new node
138 * @returns a Property node
139 */
140 function TSPropertySignatureToProperty(node, type = utils_1.AST_NODE_TYPES.Property) {
141 const base = {
142 // indent doesn't actually use these
143 key: null,
144 value: null,
145 // Property flags
146 computed: false,
147 method: false,
148 kind: 'init',
149 // this will stop eslint from interrogating the type literal
150 shorthand: true,
151 // location data
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 // overwrite the base rule here so we can use our KNOWN_NODES list instead
165 '*:exit'(node) {
166 // For nodes we care about, skip the default handling, because it just marks the node as ignored...
167 if (!KNOWN_NODES.has(node.type)) {
168 rules['*:exit'](node);
169 }
170 },
171 VariableDeclaration(node) {
172 // https://github.com/typescript-eslint/typescript-eslint/issues/441
173 if (node.declarations.length === 0) {
174 return;
175 }
176 return rules.VariableDeclaration(node);
177 },
178 TSAsExpression(node) {
179 // transform it to a BinaryExpression
180 return rules['BinaryExpression, LogicalExpression']({
181 type: utils_1.AST_NODE_TYPES.BinaryExpression,
182 operator: 'as',
183 left: node.expression,
184 // the first typeAnnotation includes the as token
185 right: node.typeAnnotation,
186 // location data
187 parent: node.parent,
188 range: node.range,
189 loc: node.loc,
190 });
191 },
192 TSConditionalType(node) {
193 // transform it to a ConditionalExpression
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 // location data
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 // location data
211 parent: node.parent,
212 range: node.range,
213 loc: node.loc,
214 });
215 },
216 'TSEnumDeclaration, TSTypeLiteral'(node) {
217 // transform it to an ObjectExpression
218 return rules['ObjectExpression, ObjectPattern']({
219 type: utils_1.AST_NODE_TYPES.ObjectExpression,
220 properties: node.members.map(member => TSPropertySignatureToProperty(member)),
221 // location data
222 parent: node.parent,
223 range: node.range,
224 loc: node.loc,
225 });
226 },
227 TSImportEqualsDeclaration(node) {
228 // transform it to an VariableDeclaration
229 // use VariableDeclaration instead of ImportDeclaration because it's essentially the same thing
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 // location data
264 range: moduleReference.range,
265 loc: moduleReference.loc,
266 },
267 },
268 ],
269 // location data
270 parent: node.parent,
271 range: node.range,
272 loc: node.loc,
273 });
274 },
275 TSIndexedAccessType(node) {
276 // convert to a MemberExpression
277 return rules['MemberExpression, JSXMemberExpression, MetaProperty']({
278 type: utils_1.AST_NODE_TYPES.MemberExpression,
279 object: node.objectType,
280 property: node.indexType,
281 // location data
282 parent: node.parent,
283 range: node.range,
284 loc: node.loc,
285 optional: false,
286 computed: true,
287 });
288 },
289 TSInterfaceBody(node) {
290 // transform it to an ClassBody
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 // location data
295 parent: node.parent,
296 range: node.range,
297 loc: node.loc,
298 });
299 },
300 'TSInterfaceDeclaration[extends.length > 0]'(node) {
301 // transform it to a ClassDeclaration
302 return rules['ClassDeclaration[superClass], ClassExpression[superClass]']({
303 type: utils_1.AST_NODE_TYPES.ClassDeclaration,
304 body: node.body,
305 id: null,
306 // TODO: This is invalid, there can be more than one extends in interface
307 superClass: node.extends[0].expression,
308 // location data
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 // transform it to an ObjectExpression
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 // location data
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 // location data
345 parent: node.parent,
346 range: node.range,
347 loc: node.loc,
348 });
349 },
350 TSModuleBlock(node) {
351 // transform it to a BlockStatement
352 return rules['BlockStatement, ClassBody']({
353 type: utils_1.AST_NODE_TYPES.BlockStatement,
354 body: node.body,
355 // location data
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 // location data
367 parent: node.parent,
368 range: node.range,
369 loc: node.loc,
370 optional: false,
371 computed: false,
372 });
373 },
374 TSTupleType(node) {
375 // transform it to an ArrayExpression
376 return rules['ArrayExpression, ArrayPattern']({
377 type: utils_1.AST_NODE_TYPES.ArrayExpression,
378 elements: node.elementTypes,
379 // location data
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 // JSX is about the closest we can get because the angle brackets
391 // it's not perfect but it works!
392 return rules.JSXOpeningElement({
393 type: utils_1.AST_NODE_TYPES.JSXOpeningElement,
394 selfClosing: false,
395 name: name,
396 attributes: attributes,
397 // location data
398 parent: node.parent,
399 range: node.range,
400 loc: node.loc,
401 });
402 },
403 });
404 },
405});
406//# sourceMappingURL=indent.js.map
\No newline at end of file