This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathterIndentRule.ts
1234 lines (1104 loc) · 43.1 KB
/
terIndentRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This rule is a port of eslint:
*
* source file: https://github.com/eslint/eslint/blob/master/lib/rules/indent.js
* git commit hash: 0643bfeff68979044ca57a2b392d855d18be7d08
*
*/
import * as ts from 'typescript';
import * as Lint from 'tslint';
const RULE_NAME = 'ter-indent';
const DEFAULT_VARIABLE_INDENT = 1;
const DEFAULT_PARAMETER_INDENT = null;
const DEFAULT_FUNCTION_BODY_INDENT = 1;
let indentType = 'space';
let indentSize = 4;
let OPTIONS: any;
interface INodeIndent {
contentStart: number;
firstInLine: boolean;
space: number;
tab: number;
goodChar: number;
badChar: number;
}
function assign(target: any, ...sources: any[]): any {
sources.forEach((source) => {
if (source !== undefined && source !== null) {
for (const nextKey in source) {
if (source.hasOwnProperty(nextKey)) {
target[nextKey] = source[nextKey];
}
}
}
});
return target;
}
function isKind<T extends ts.Node>(node: ts.Node, kind: string): node is T {
return node.kind === ts.SyntaxKind[kind];
}
function isOneOf(node: ts.Node, kinds: string[]) {
return kinds.some(kind => node.kind === ts.SyntaxKind[kind]);
}
// TODO: Look for `.getFirstToken()!` and try to remove the `!`.
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: RULE_NAME,
hasFix: true,
description: 'enforce consistent indentation',
rationale: Lint.Utils.dedent`
Using only one of tabs or spaces for indentation leads to more consistent editor behavior,
cleaner diffs in version control, and easier programmatic manipulation.
`,
optionsDescription: Lint.Utils.dedent`
The string 'tab' or an integer indicating the number of spaces to use per tab.
An object may be provided to fine tune the indentation rules:
* \`"SwitchCase"\` (default: 0) enforces indentation level for \`case\` clauses in
\`switch\` statements
* \`"VariableDeclarator"\` (default: 1) enforces indentation level for \`var\` declarators;
can also take an object to define separate rules for \`var\`,
\`let\` and \`const\` declarations.
* \`"outerIIFEBody"\` (default: 1) enforces indentation level for file-level IIFEs.
* \`"MemberExpression"\` (off by default) enforces indentation level for multi-line
property chains (except in variable declarations and assignments)
* \`"FunctionDeclaration"\` takes an object to define rules for function declarations.
* \`"parameters"\` (off by default) enforces indentation level for parameters in a
function declaration. This can either be a number indicating
indentation level, or the string \`"first"\` indicating that all
parameters of the declaration must be aligned with the first parameter.
* \`"body"\` (default: 1) enforces indentation level for the body of a function expression.
* \`"FunctionExpression"\` takes an object to define rules for function declarations.
* \`"parameters"\` (off by default) enforces indentation level for parameters in a
function declaration. This can either be a number indicating
indentation level, or the string \`"first"\` indicating that all
parameters of the declaration must be aligned with the first parameter.
* \`"body"\` (default: 1) enforces indentation level for the body of a function expression.
* \`"CallExpression"\` takes an object to define rules for function call expressions.
* \`"arguments"\` (off by default) enforces indentation level for arguments in a call
expression. This can either be a number indicating indentation level,
or the string \`"first"\` indicating that all arguments of the
expression must be aligned with the first argument.
`,
options: {
type: 'array',
items: [{
type: 'number',
minimum: '0'
}, {
type: 'string',
enum: ['tab']
}, {
type: 'object',
properties: {
SwitchCase: {
type: 'number',
minimum: 0
},
VariableDeclarator: {
type: 'object',
properties: {
var: {
type: 'number',
minimum: 0
},
let: {
type: 'number',
minimum: 0
},
const: {
type: 'number',
minimum: 0
}
}
},
outerIIFEBody: {
type: 'number'
},
FunctionDeclaration: {
type: 'object',
properties: {
parameters: {
type: 'number',
minimum: 0
},
body: {
type: 'number',
minimum: 0
}
}
},
FunctionExpression: {
type: 'object',
properties: {
parameters: {
type: 'number',
minimum: 0
},
body: {
type: 'number',
minimum: 0
}
}
},
MemberExpression: {
type: 'number'
},
CallExpression: {
type: 'object',
properties: {
arguments: {
type: 'number',
minimum: 0
}
}
}
},
additionalProperties: false
}],
minLength: 1,
maxLength: 2
},
optionExamples: [
Lint.Utils.dedent`
"${RULE_NAME}": [true, "tab"]
`,
Lint.Utils.dedent`
"${RULE_NAME}": [true, 2]
`,
Lint.Utils.dedent`
"${RULE_NAME}": [
true,
2,
{
"FunctionExpression": {
"parameters": 1,
"body": 1
}
}
]
`
],
typescriptOnly: false,
type: 'maintainability'
};
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const walker = new IndentWalker(sourceFile, this.getOptions());
return this.applyWithWalker(walker);
}
}
class IndentWalker extends Lint.RuleWalker {
private srcFile: ts.SourceFile;
private srcText: string;
private caseIndentStore: { [key: number]: number } = {};
private varIndentStore: { [key: number]: number } = {};
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
OPTIONS = {
SwitchCase: 0,
VariableDeclarator: {
var: DEFAULT_VARIABLE_INDENT,
let: DEFAULT_VARIABLE_INDENT,
const: DEFAULT_VARIABLE_INDENT
},
outerIIFEBody: null,
FunctionDeclaration: {
parameters: DEFAULT_PARAMETER_INDENT,
body: DEFAULT_FUNCTION_BODY_INDENT
},
FunctionExpression: {
parameters: DEFAULT_PARAMETER_INDENT,
body: DEFAULT_FUNCTION_BODY_INDENT
},
CallExpression: {
arguments: DEFAULT_PARAMETER_INDENT
}
};
const firstParam = this.getOptions()[0];
if (firstParam === 'tab') {
indentSize = 1;
indentType = 'tab';
} else {
indentSize = firstParam || 4;
indentType = 'space';
}
const userOptions = this.getOptions()[1];
if (userOptions) {
OPTIONS.SwitchCase = userOptions.SwitchCase || 0;
if (typeof userOptions.VariableDeclarator === 'number') {
OPTIONS.VariableDeclarator = {
var: userOptions.VariableDeclarator,
let: userOptions.VariableDeclarator,
const: userOptions.VariableDeclarator
};
} else if (typeof userOptions.VariableDeclarator === 'object') {
assign(OPTIONS.VariableDeclarator, userOptions.VariableDeclarator);
}
if (typeof userOptions.outerIIFEBody === 'number') {
OPTIONS.outerIIFEBody = userOptions.outerIIFEBody;
}
if (typeof userOptions.MemberExpression === 'number') {
OPTIONS.MemberExpression = userOptions.MemberExpression;
}
if (typeof userOptions.FunctionDeclaration === 'object') {
assign(OPTIONS.FunctionDeclaration, userOptions.FunctionDeclaration);
}
if (typeof userOptions.FunctionExpression === 'object') {
assign(OPTIONS.FunctionExpression, userOptions.FunctionExpression);
}
if (typeof userOptions.CallExpression === 'object') {
assign(OPTIONS.CallExpression, userOptions.CallExpression);
}
}
this.srcFile = sourceFile;
this.srcText = sourceFile.getFullText();
}
private getSourceSubstr(start: number, end: number) {
return this.srcText.substr(start, end - start);
}
private getLineAndCharacter(node: ts.Node, byEndLocation: boolean = false) {
const index = byEndLocation ? node.getEnd() : node.getStart();
return this.srcFile.getLineAndCharacterOfPosition(index);
}
private getLine(node: ts.Node, byEndLocation: boolean = false) {
return this.getLineAndCharacter(node, byEndLocation).line;
}
/**
* Creates an error message for a line.
*
* expectedAmount: The expected amount of indentation characters for this line
* actualSpaces: The actual number of indentation spaces that were found on this line
* actualTabs: The actual number of indentation tabs that were found on this line
*/
private createErrorMessage(expectedAmount: number, actualSpaces: number, actualTabs: number): string {
const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? '' : 's'}`;
const foundSpacesWord = `space${actualSpaces === 1 ? '' : 's'}`;
const foundTabsWord = `tab${actualTabs === 1 ? '' : 's'}`;
let foundStatement;
if (actualSpaces > 0 && actualTabs > 0) {
foundStatement = `${actualSpaces} ${foundSpacesWord} and ${actualTabs} ${foundTabsWord}`;
} else if (actualSpaces > 0) {
// Abbreviate the message if the expected indentation is also spaces.
// e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces'
foundStatement = indentType === 'space' ? actualSpaces : `${actualSpaces} ${foundSpacesWord}`;
} else if (actualTabs > 0) {
foundStatement = indentType === 'tab' ? actualTabs : `${actualTabs} ${foundTabsWord}`;
} else {
foundStatement = '0';
}
return `Expected indentation of ${expectedStatement} but found ${foundStatement}.`;
}
/**
* Reports a given indent violation
* node: Node violating the indent rule
* needed: Expected indentation character count
* gottenSpaces: Indentation space count in the actual node/code
* gottenTabs: Indentation tab count in the actual node/code
*/
private report(node: ts.Node, needed: number, gottenSpaces: number, gottenTabs: number, loc?: number) {
if (gottenSpaces && gottenTabs) {
// Don't report lines that have both spaces and tabs to avoid conflicts with rules that
// report a mix of tabs and spaces.
return;
}
const msg = this.createErrorMessage(needed, gottenSpaces, gottenTabs);
const width = gottenSpaces + gottenTabs;
const start = (loc !== undefined ? loc : node.getStart()) - width;
const desiredIndent = ((indentType === 'space' ? ' ' : '\t') as any).repeat(needed);
const fix = this.createReplacement(start, width, desiredIndent);
this.addFailure(this.createFailure(start, width, msg, fix));
}
/**
* Checks node is the first in its own start line. By default it looks by start line.
* [byEndLocation=false]: Lookup based on start position or end
*/
private isNodeFirstInLine(node: ts.Node, byEndLocation: boolean = false) {
const token = byEndLocation ? node.getLastToken() : node.getFirstToken();
// TODO: How can there be no token?
let pos = token!.getStart() - 1;
while ([' ', '\t'].indexOf(this.srcText.charAt(pos)) !== -1) {
pos -= 1;
}
return this.srcText.charAt(pos) === '\n' || this._firstInLineCommentHelper(node);
}
/**
* Checks to see a leading comment is blocking the start of the node. For instance:
*
* /* comment *\/ {
*
* is allowed and in this case `{` would be first in line.
*/
private _firstInLineCommentHelper(node: ts.Node) {
let pos;
let firstInLine = false;
const comments = ts.getLeadingCommentRanges(node.getFullText(), 0);
if (comments && comments.length) {
const offset = node.getFullStart();
const lastComment = comments[comments.length - 1];
const comment = this.getSourceSubstr(lastComment.pos + offset, lastComment.end + offset);
if (comment.indexOf('\n') !== -1) {
firstInLine = true;
} else {
pos = lastComment.pos + offset;
while (pos > 0 && this.srcText.charAt(pos) !== '\n') {
pos -= 1;
}
const content = this.getSourceSubstr(pos + 1, lastComment.pos + offset);
if (content.trim() === '') {
firstInLine = true;
}
}
}
return firstInLine;
}
/**
* Returns the node's indent. Contains keys `space` and `tab`, representing the indent of each
* character. Also contains keys `goodChar` and `badChar`, where `goodChar` is the amount of the
* user's desired indentation character, and `badChar` is the amount of the other indentation
* character.
*/
private getNodeIndent(node: ts.Node): INodeIndent {
if (node === this.getSourceFile()) {
return { contentStart: 0, firstInLine: true, space: 0, tab: 0, goodChar: 0, badChar: 0 };
}
if (node.kind === ts.SyntaxKind.SyntaxList && node.parent) {
return this.getNodeIndent(node.parent);
}
const endIndex = node.getStart(this.srcFile);
let pos = endIndex - 1;
while (pos > 0 && this.srcText.charAt(pos) !== '\n') {
pos -= 1;
}
const str = this.getSourceSubstr(pos + 1, endIndex);
const whiteSpace = (str.match(/^\s+/) || [''])[0];
const indentChars = whiteSpace.split('');
const spaces = indentChars.filter(char => char === ' ').length;
const tabs = indentChars.filter(char => char === '\t').length;
return {
contentStart: pos + spaces + tabs + 1,
firstInLine: spaces + tabs === str.length || this._firstInLineCommentHelper(node),
space: spaces,
tab: tabs,
goodChar: indentType === 'space' ? spaces : tabs,
badChar: indentType === 'space' ? tabs : spaces
};
}
private checkNodeIndent(node: ts.Node, neededIndent: number): void {
const actualIndent = this.getNodeIndent(node);
if (
!isKind(node, 'ArrayLiteralExpression') &&
!isKind(node, 'ObjectLiteralExpression') &&
(actualIndent.goodChar !== neededIndent || actualIndent.badChar !== 0) &&
actualIndent.firstInLine
) {
this.report(node, neededIndent, actualIndent.space, actualIndent.tab, actualIndent.contentStart);
}
if (isKind<ts.IfStatement>(node, 'IfStatement')) {
const elseStatement = node.elseStatement;
if (elseStatement) {
const elseKeyword = node.getChildren().filter(ch => isKind(ch, 'ElseKeyword')).shift()!;
this.checkNodeIndent(elseKeyword, neededIndent);
if (!this.isNodeFirstInLine(elseStatement)) {
this.checkNodeIndent(elseStatement, neededIndent);
}
}
} else if (isKind<ts.TryStatement>(node, 'TryStatement')) {
const handler = node.catchClause;
if (handler) {
const catchKeyword = handler.getChildren().filter(ch => isKind(ch, 'CatchKeyword')).shift()!;
this.checkNodeIndent(catchKeyword, neededIndent);
if (!this.isNodeFirstInLine(handler)) {
this.checkNodeIndent(handler, neededIndent);
}
}
const finalizer = node.finallyBlock;
if (finalizer) {
const finallyKeyword = node.getChildren().filter(ch => isKind(ch, 'FinallyKeyword')).shift()!;
this.checkNodeIndent(finallyKeyword, neededIndent);
}
} else if (isKind(node, 'DoStatement')) {
const whileKeyword = node.getChildren().filter(ch => isKind(ch, 'WhileKeyword')).shift()!;
this.checkNodeIndent(whileKeyword, neededIndent);
}
}
private isSingleLineNode(node: ts.Node): boolean {
// Note: all the tests would pass using `node.getFullText()` but we should only use this for
// the syntax list nodes, otherwise nodes which are single line may say they are multiline
// and this will make us do unnecessary checks.
const text = node.kind === ts.SyntaxKind.SyntaxList ? node.getFullText() : node.getText();
return text.indexOf('\n') === -1;
}
/**
* Check indentation for blocks
*/
private blockIndentationCheck(node: ts.BlockLike | ts.IterationStatement): void {
if (this.isSingleLineNode(node)) {
return;
}
const functionLike = [
'FunctionExpression',
'FunctionDeclaration',
'MethodDeclaration',
'Constructor',
'ArrowFunction'
];
if (node.parent && isOneOf(node.parent, functionLike)) {
// TODO: nope, can't figure this last check, shutting it down for now.
this.checkIndentInFunctionBlock(node as any);
return;
}
let indent;
let nodesToCheck: ts.Node[] = [];
/* For these statements we should check indent from statement beginning, not from the beginning
of the block.
*/
const statementsWithProperties = [
'IfStatement',
'WhileStatement',
'ForStatement',
'ForInStatement',
'ForOfStatement',
'DoStatement',
'ClassDeclaration',
'ClassExpression',
'InterfaceDeclaration',
'TypeLiteral',
'TryStatement',
'SourceFile'
];
if (node.parent && isOneOf(node.parent, statementsWithProperties) && this.isNodeBodyBlock(node)) {
indent = this.getNodeIndent(node.parent!).goodChar;
} else if (node.parent && isKind(node.parent, 'CatchClause')) {
indent = this.getNodeIndent(node.parent.parent!).goodChar;
} else {
indent = this.getNodeIndent(node).goodChar;
}
if (isKind<ts.IfStatement>(node, 'IfStatement') && !isKind<ts.Block>(node.thenStatement, 'Block')) {
nodesToCheck = [node.thenStatement];
} else {
if (isKind<ts.Block>(node, 'Block')) {
nodesToCheck = node.getChildren()[1].getChildren();
} else if (
node.parent &&
isOneOf(node.parent, [
'ClassDeclaration',
'ClassExpression',
'InterfaceDeclaration',
'TypeLiteral'
])
) {
nodesToCheck = node.getChildren();
} else {
// TODO: previously we had it as ts.IterationStatement, need to find out what type of node
// we have here. Doing `any` for now.
nodesToCheck = [(node as any).statement];
}
}
this.checkNodeIndent(node, indent);
if (nodesToCheck.length > 0) {
this.checkNodesIndent(nodesToCheck, indent + indentSize);
}
if (isKind<ts.Block>(node, 'Block')) {
this.checkLastNodeLineIndent(node, indent);
} else if (node.parent && this.isNodeBodyBlock(node)) {
this.checkLastNodeLineIndent(node.parent!, indent);
}
}
/**
* Check if node is an assignment expression, i.e. the binary expression contains the equal token.
*/
private isAssignment(node: ts.Node): boolean {
if (!isKind(node, 'BinaryExpression')) {
return false;
}
return (node as ts.BinaryExpression).operatorToken.getText() === '=';
}
/**
* Check if the node or node body is a BlockStatement or not
*/
private isNodeBodyBlock(node: ts.Node): node is ts.BlockLike {
const hasBlock = [
'ClassDeclaration',
'ClassExpression',
'InterfaceDeclaration',
'TypeLiteral'
];
return isKind<ts.Block>(node, 'Block') || (
isKind<ts.SyntaxList>(node, 'SyntaxList') &&
isOneOf(node.parent!, hasBlock)
);
}
/**
* Check that the start of the node has the correct level of indentation.
*/
private checkFirstNodeLineIndent(node: ts.Node, firstLineIndent: number): void {
const startIndent = this.getNodeIndent(node);
const firstInLine = startIndent.firstInLine;
if (firstInLine && (startIndent.goodChar !== firstLineIndent || startIndent.badChar !== 0)) {
this.report(node, firstLineIndent, startIndent.space, startIndent.tab, startIndent.contentStart);
}
}
/**
* Check last line of the node has the correct level of indentation.
*/
private checkLastNodeLineIndent(node: ts.Node, lastLineIndent: number): void {
const lastToken = node.getLastToken()!;
const endIndent = this.getNodeIndent(lastToken);
const firstInLine = endIndent.firstInLine;
if (firstInLine && (endIndent.goodChar !== lastLineIndent || endIndent.badChar !== 0)) {
this.report(lastToken, lastLineIndent, endIndent.space, endIndent.tab);
}
}
/**
* Check to see if the node function is a file level IIFE
*/
private isOuterIIFE(node: ts.FunctionExpression): boolean {
if (!node.parent) return false;
let parent = node.parent as ts.ParenthesizedExpression;
let expressionIsNode = parent.expression !== node;
if (isKind(parent, 'ParenthesizedExpression')) {
parent = parent.parent as ts.ParenthesizedExpression;
}
// Verify that the node is an IIEF
if (!isKind(parent, 'CallExpression') || expressionIsNode) {
return false;
}
let stmt: ts.Node = parent;
let condition: boolean;
do {
stmt = stmt.parent!;
condition = (
isKind<ts.PrefixUnaryExpression>(stmt, 'PrefixUnaryExpression') && (
stmt.operator === ts.SyntaxKind.ExclamationToken ||
stmt.operator === ts.SyntaxKind.TildeToken ||
stmt.operator === ts.SyntaxKind.PlusToken ||
stmt.operator === ts.SyntaxKind.MinusToken
) ||
isKind(stmt, 'BinaryExpression') ||
isKind(stmt, 'SyntaxList') ||
isKind(stmt, 'VariableDeclaration') ||
isKind(stmt, 'VariableDeclarationList') ||
isKind(stmt, 'ParenthesizedExpression')
);
} while (condition);
return ((
isKind<ts.ExpressionStatement>(stmt, 'ExpressionStatement') ||
isKind<ts.VariableStatement>(stmt, 'VariableStatement')) &&
!!stmt.parent && isKind(stmt.parent, 'SourceFile')
);
}
/**
* Check to see if the argument before the callee node is multi-line and
* there should only be 1 argument before the callee node
*/
private isArgBeforeCalleeNodeMultiline(node: ts.Node): boolean {
const parent = node.parent;
if (parent && parent['arguments'].length >= 2 && parent['arguments'][1] === node) {
const firstArg = parent['arguments'][0];
return this.getLine(firstArg, true) > this.getLine(firstArg);
}
return false;
}
/**
* Check indent for function block content
*/
private checkIndentInFunctionBlock(node: ts.BlockLike): void {
const calleeNode = node.parent as ts.FunctionExpression;
let indent = this.getNodeIndent(calleeNode).goodChar;
if (calleeNode.parent && calleeNode.parent.kind === ts.SyntaxKind.CallExpression) {
const calleeParent = calleeNode.parent as ts.CallExpression;
if (calleeNode.kind !== ts.SyntaxKind.FunctionExpression && calleeNode.kind !== ts.SyntaxKind.ArrowFunction) {
if (calleeParent && this.getLine(calleeParent) < this.getLine(node)) {
indent = this.getNodeIndent(calleeParent).goodChar;
}
} else {
const callee = calleeParent.expression;
if (
this.isArgBeforeCalleeNodeMultiline(calleeNode) &&
this.getLine(callee) === this.getLine(callee, true) &&
!this.isNodeFirstInLine(calleeNode)
) {
indent = this.getNodeIndent(calleeParent).goodChar;
}
}
}
// function body indent should be indent + indent size, unless this
// is a FunctionDeclaration, FunctionExpression, or outer IIFE and the corresponding options are enabled.
let functionOffset = indentSize;
if (OPTIONS.outerIIFEBody !== null && this.isOuterIIFE(calleeNode)) {
functionOffset = OPTIONS.outerIIFEBody * indentSize;
} else if (calleeNode.kind === ts.SyntaxKind.FunctionExpression) {
functionOffset = OPTIONS.FunctionExpression.body * indentSize;
} else if (calleeNode.kind === ts.SyntaxKind.FunctionDeclaration) {
functionOffset = OPTIONS.FunctionDeclaration.body * indentSize;
} else if (isOneOf(calleeNode, ['MethodDeclaration', 'Constructor'])) {
functionOffset = OPTIONS.FunctionExpression.body * indentSize;
}
indent += functionOffset;
// check if the node is inside a variable
const parentVarNode = this.getVariableDeclaratorNode(node);
if (parentVarNode && this.isNodeInVarOnTop(node, parentVarNode) && parentVarNode.parent) {
const varKind = parentVarNode.parent.getFirstToken()!.getText();
indent += indentSize * OPTIONS.VariableDeclarator[varKind];
}
this.checkFirstNodeLineIndent(node, indent - functionOffset);
if (node.statements.length) {
this.checkNodesIndent(node.statements, indent);
}
this.checkLastNodeLineIndent(node, indent - functionOffset);
}
/**
* Check indent for nodes list.
*/
protected checkNodesIndent(nodes: ReadonlyArray<ts.Node>, indent: number): void {
nodes.forEach(node => this.checkNodeIndent(node, indent));
}
/**
* Returns the expected indentation for the case statement.
*/
private expectedCaseIndent(node: ts.Node, switchIndent?: number) {
const switchNode = (node.kind === ts.SyntaxKind.SwitchStatement) ? node : node.parent!;
const line = this.getLine(switchNode);
let caseIndent;
if (this.caseIndentStore[line]) {
return this.caseIndentStore[line];
} else {
if (typeof switchIndent === 'undefined') {
switchIndent = this.getNodeIndent(switchNode).goodChar;
}
caseIndent = switchIndent + (indentSize * OPTIONS.SwitchCase);
this.caseIndentStore[line] = caseIndent;
return caseIndent;
}
}
/**
* Returns the expected indentation for the variable declarations.
*/
private expectedVarIndent(node: ts.VariableDeclaration, varIndent?: number) {
// VariableStatement -> VariableDeclarationList -> VariableDeclaration
const varNode = node.parent!;
const line = this.getLine(varNode);
let indent;
if (this.varIndentStore[line]) {
return this.varIndentStore[line];
} else {
if (typeof varIndent === 'undefined') {
varIndent = this.getNodeIndent(varNode).goodChar;
}
const varKind = varNode.getFirstToken()!.getText();
indent = varIndent + (indentSize * OPTIONS.VariableDeclarator[varKind]);
this.varIndentStore[line] = indent;
return indent;
}
}
/**
* Returns a parent node of given node based on a specified type
* if not present then return null
*/
private getParentNodeByType<T extends ts.Node>(
node: ts.Node,
kind: number,
stopAtList: number[] = [ts.SyntaxKind.SourceFile]
): T | null {
let parent = node.parent;
while (
parent
&& parent.kind !== kind
&& stopAtList.indexOf(parent.kind) === -1
&& parent.kind !== ts.SyntaxKind.SourceFile
) {
parent = parent.parent;
}
return parent && parent.kind === kind ? parent as T : null;
}
/**
* Returns the VariableDeclarator based on the current node if not present then return null.
*/
protected getVariableDeclaratorNode(node: ts.Node): ts.VariableDeclaration | null {
return this.getParentNodeByType<ts.VariableDeclaration>(node, ts.SyntaxKind.VariableDeclaration);
}
/**
* Returns the BinaryExpression based on the current node if not present then return null.
*/
protected getBinaryExpressionNode(node: ts.Node): ts.BinaryExpression | null {
return this.getParentNodeByType<ts.BinaryExpression>(node, ts.SyntaxKind.BinaryExpression);
}
/**
* Check indent for array block content or object block content
*/
protected checkIndentInArrayOrObjectBlock(node: ts.Node): void {
if (this.isSingleLineNode(node)) {
return;
}
let elements: ts.Node[] = isKind(node, 'ObjectLiteralExpression') ? node['properties'] : node['elements'];
// filter out empty elements, an example would be [ , 2]
elements = elements.filter(elem => elem.getText() !== '');
const nodeLine = this.getLine(node);
const nodeEndLine = this.getLine(node, true);
let nodeIndent;
let elementsIndent;
let varKind;
const parentVarNode = this.getVariableDeclaratorNode(node);
if (this.isNodeFirstInLine(node) && node.parent) {
const parent = node.parent;
nodeIndent = this.getNodeIndent(parent).goodChar;
if (parentVarNode && this.getLine(parentVarNode) !== nodeLine) {
// TODO: parentVarNode.parent can be a CatchClause and this one may not have a declarations list
if (!isKind(parent, 'VariableDeclaration') || parentVarNode === (parentVarNode.parent as ts.VariableDeclarationList).declarations[0]) {
const parentVarLine = this.getLine(parentVarNode);
const parentLine = this.getLine(parent);
if (isKind(parent, 'VariableDeclaration') && parentVarLine === parentLine && parentVarNode.parent) {
varKind = parentVarNode.parent.getFirstToken()!.getText();
nodeIndent = nodeIndent + (indentSize * OPTIONS.VariableDeclarator[varKind]);
} else if (
isOneOf(parent, [
'ObjectLiteralExpression',
'ArrayLiteralExpression',
'CallExpression',
'ArrowFunction',
'NewExpression',
'BinaryExpression'
])
) {
nodeIndent = nodeIndent + indentSize;
}
}
} else if (
!parentVarNode &&
!this.isFirstArrayElementOnSameLine(parent) &&
parent.kind !== ts.SyntaxKind.PropertyAccessExpression &&
parent.kind !== ts.SyntaxKind.ExpressionStatement &&
parent.kind !== ts.SyntaxKind.PropertyAssignment &&
!(this.isAssignment(parent))
) {
nodeIndent = nodeIndent + indentSize;
}
elementsIndent = nodeIndent + indentSize;
this.checkFirstNodeLineIndent(node, nodeIndent);
} else {
nodeIndent = this.getNodeIndent(node).goodChar;
elementsIndent = nodeIndent + indentSize;
}
/*
* Check if the node is a multiple variable declaration; if so, then
* make sure indentation takes that into account.
*/
if (parentVarNode && this.isNodeInVarOnTop(node, parentVarNode) && parentVarNode.parent) {
varKind = parentVarNode.parent.getFirstToken()!.getText();
elementsIndent += indentSize * OPTIONS.VariableDeclarator[varKind];
}
this.checkNodesIndent(elements, elementsIndent);
if (elements.length > 0) {
const lastLine = this.getLine(elements[elements.length - 1], true);
// Skip last block line check if last item in same line
if (lastLine === nodeEndLine) {
return;
}
}
this.checkLastNodeLineIndent(node, elementsIndent - indentSize);
}
/**
* Check to see if the first element inside an array is an object and on the same line as the node
*/
private isFirstArrayElementOnSameLine(node: ts.Node): boolean {
if (isKind(node, 'ArrayLiteralExpression')) {
const ele = (node as ts.ArrayLiteralExpression).elements[0];
if (ele) {
return isKind(ele, 'ObjectLiteralExpression') && this.getLine(ele) === this.getLine(node);
}
}
return false;
}
/**
* Check to see if the node is part of the multi-line variable declaration.
* Also if its on the same line as the varNode
* @param {ASTNode} node node to check
* @param {ASTNode} varNode variable declaration node to check against
* @returns {boolean} True if all the above condition satisfy
*/
protected isNodeInVarOnTop(node: ts.Node, varNode: ts.VariableDeclaration) {
const nodeLine = this.getLine(node);
const parentLine = this.getLine(varNode.parent!);
return varNode &&
parentLine === nodeLine &&
(varNode.parent! as ts.VariableDeclarationList).declarations.length > 1;
}
/**
* Check and decide whether to check for indentation for blockless nodes
* Scenarios are for or while statements without braces around them
*/
private blockLessNodes(node: ts.IterationStatement): void {
if (!isKind<ts.Block>(node.statement, 'Block')) {
this.blockIndentationCheck(node);
}
}
/**
* Check indentation for variable declarations
*/
private checkIndentInVariableDeclarations(node: ts.VariableDeclaration) {
const indent = this.expectedVarIndent(node);
this.checkNodeIndent(node, indent);
}
/**
* Check indentation for case and default clauses in switch statements.
*/
private visitCase(node: ts.CaseClause | ts.DefaultClause) {
if (this.isSingleLineNode(node)) {
return;
}
const caseIndent = this.expectedCaseIndent(node);
this.checkNodesIndent(node.statements, caseIndent + indentSize);
}
/**
* Check last node line indent this detects, that block closed correctly
* This function for more complicated return statement case, where closing parenthesis may be
* followed by ';'
*/
private checkLastReturnStatementLineIndent(node: ts.ReturnStatement, firstLineIndent: number): void {
if (!node.expression) {
return;
}
const lastToken = node.expression.getLastToken()!;
const endIndex = lastToken.getStart();
let pos = endIndex - 1;
while (pos > 0 && this.srcText.charAt(pos) !== '\n') {
pos -= 1;
}
const textBeforeClosingParenthesis = this.getSourceSubstr(pos + 1, endIndex);
if (textBeforeClosingParenthesis.trim()) {
// There are tokens before the closing paren, don't report this case
return;
}
const endIndent = this.getNodeIndent(lastToken);
if (endIndent.goodChar !== firstLineIndent) {
this.report(node, firstLineIndent, endIndent.space, endIndent.tab, lastToken.getStart());
}
}
protected visitClassDeclaration(node: ts.ClassDeclaration) {
const len = node.getChildCount();
this.blockIndentationCheck(node.getChildAt(len - 2) as ts.BlockLike);
super.visitClassDeclaration(node);
}
protected visitClassExpression(node: ts.ClassExpression) {
const len = node.getChildCount();
this.blockIndentationCheck(node.getChildAt(len - 2) as ts.BlockLike);
super.visitClassExpression(node);
}
protected visitInterfaceDeclaration(node: ts.InterfaceDeclaration) {
const len = node.getChildCount();
this.blockIndentationCheck(node.getChildAt(len - 2) as ts.BlockLike);
super.visitInterfaceDeclaration(node);
}
protected visitTypeLiteral(node: ts.TypeLiteralNode) {
const len = node.getChildCount();
this.blockIndentationCheck(node.getChildAt(len - 2) as ts.BlockLike);
super.visitTypeLiteral(node);
}
protected visitBlock(node: ts.Block) {
this.blockIndentationCheck(node);
super.visitBlock(node);
}
protected visitIfStatement(node: ts.IfStatement) {
const thenLine = this.getLine(node.thenStatement);
const line = this.getLine(node);