forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
padding-line-between-statements.ts
809 lines (744 loc) · 23.1 KB
/
padding-line-between-statements.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
import {
AST_NODE_TYPES,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
/**
* This rule is a replica of padding-line-between-statements.
*
* Ideally we would want to extend the rule support typescript specific support.
* But since not all the state is exposed by the eslint and eslint has frozen stylistic rules,
* (see - https://eslint.org/blog/2020/05/changes-to-rules-policies for details.)
* we are forced to re-implement the rule here.
*
* We have tried to keep the implementation as close as possible to the eslint implementation, to make
* patching easier for future contributors.
*
* Reference rule - https://github.com/eslint/eslint/blob/main/lib/rules/padding-line-between-statements.js
*/
type NodeTest = (
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
) => boolean;
interface NodeTestObject {
test: NodeTest;
}
interface PaddingOption {
blankLine: keyof typeof PaddingTypes;
prev: string | string[];
next: string | string[];
}
type MessageIds = 'expectedBlankLine' | 'unexpectedBlankLine';
type Options = PaddingOption[];
const LT = `[${Array.from(
new Set(['\r\n', '\r', '\n', '\u2028', '\u2029']),
).join('')}]`;
const PADDING_LINE_SEQUENCE = new RegExp(
String.raw`^(\s*?${LT})\s*${LT}(\s*;?)$`,
'u',
);
/**
* Creates tester which check if a node starts with specific keyword with the
* appropriate AST_NODE_TYPES.
* @param keyword The keyword to test.
* @returns the created tester.
* @private
*/
function newKeywordTester(
type: AST_NODE_TYPES | AST_NODE_TYPES[],
keyword: string,
): NodeTestObject {
return {
test(node, sourceCode): boolean {
const isSameKeyword = sourceCode.getFirstToken(node)?.value === keyword;
const isSameType = Array.isArray(type)
? type.some(val => val === node.type)
: type === node.type;
return isSameKeyword && isSameType;
},
};
}
/**
* Creates tester which check if a node starts with specific keyword and spans a single line.
* @param keyword The keyword to test.
* @returns the created tester.
* @private
*/
function newSinglelineKeywordTester(keyword: string): NodeTestObject {
return {
test(node, sourceCode): boolean {
return (
node.loc.start.line === node.loc.end.line &&
sourceCode.getFirstToken(node)!.value === keyword
);
},
};
}
/**
* Creates tester which check if a node starts with specific keyword and spans multiple lines.
* @param keyword The keyword to test.
* @returns the created tester.
* @private
*/
function newMultilineKeywordTester(keyword: string): NodeTestObject {
return {
test(node, sourceCode): boolean {
return (
node.loc.start.line !== node.loc.end.line &&
sourceCode.getFirstToken(node)!.value === keyword
);
},
};
}
/**
* Creates tester which check if a node is specific type.
* @param type The node type to test.
* @returns the created tester.
* @private
*/
function newNodeTypeTester(type: AST_NODE_TYPES): NodeTestObject {
return {
test: (node): boolean => node.type === type,
};
}
/**
* Skips a chain expression node
* @param node The node to test
* @returnsA non-chain expression
* @private
*/
function skipChainExpression(node: TSESTree.Node): TSESTree.Node {
return node && node.type === AST_NODE_TYPES.ChainExpression
? node.expression
: node;
}
/**
* Checks the given node is an expression statement of IIFE.
* @param node The node to check.
* @returns `true` if the node is an expression statement of IIFE.
* @private
*/
function isIIFEStatement(node: TSESTree.Node): boolean {
if (node.type === AST_NODE_TYPES.ExpressionStatement) {
let expression = skipChainExpression(node.expression);
if (expression.type === AST_NODE_TYPES.UnaryExpression) {
expression = skipChainExpression(expression.argument);
}
if (expression.type === AST_NODE_TYPES.CallExpression) {
let node: TSESTree.Node = expression.callee;
while (node.type === AST_NODE_TYPES.SequenceExpression) {
node = node.expressions[node.expressions.length - 1];
}
return util.isFunction(node);
}
}
return false;
}
/**
* Checks the given node is a CommonJS require statement
* @param node The node to check.
* @returns `true` if the node is a CommonJS require statement.
* @private
*/
function isCJSRequire(node: TSESTree.Node): boolean {
if (node.type === AST_NODE_TYPES.VariableDeclaration) {
const declaration = node.declarations[0];
if (declaration?.init) {
let call = declaration?.init;
while (call.type === AST_NODE_TYPES.MemberExpression) {
call = call.object;
}
if (
call.type === AST_NODE_TYPES.CallExpression &&
call.callee.type === AST_NODE_TYPES.Identifier
) {
return call.callee.name === 'require';
}
}
}
return false;
}
/**
* Checks whether the given node is a block-like statement.
* This checks the last token of the node is the closing brace of a block.
* @param sourceCode The source code to get tokens.
* @param node The node to check.
* @returns `true` if the node is a block-like statement.
* @private
*/
function isBlockLikeStatement(
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
): boolean {
// do-while with a block is a block-like statement.
if (
node.type === AST_NODE_TYPES.DoWhileStatement &&
node.body.type === AST_NODE_TYPES.BlockStatement
) {
return true;
}
/**
* IIFE is a block-like statement specially from
* JSCS#disallowPaddingNewLinesAfterBlocks.
*/
if (isIIFEStatement(node)) {
return true;
}
// Checks the last token is a closing brace of blocks.
const lastToken = sourceCode.getLastToken(node, util.isNotSemicolonToken);
const belongingNode =
lastToken && util.isClosingBraceToken(lastToken)
? sourceCode.getNodeByRangeIndex(lastToken.range[0])
: null;
return (
!!belongingNode &&
(belongingNode.type === AST_NODE_TYPES.BlockStatement ||
belongingNode.type === AST_NODE_TYPES.SwitchStatement)
);
}
/**
* Check whether the given node is a directive or not.
* @param node The node to check.
* @param sourceCode The source code object to get tokens.
* @returns `true` if the node is a directive.
*/
function isDirective(
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
): boolean {
return (
node.type === AST_NODE_TYPES.ExpressionStatement &&
(node.parent?.type === AST_NODE_TYPES.Program ||
(node.parent?.type === AST_NODE_TYPES.BlockStatement &&
util.isFunction(node.parent.parent))) &&
node.expression.type === AST_NODE_TYPES.Literal &&
typeof node.expression.value === 'string' &&
!util.isParenthesized(node.expression, sourceCode)
);
}
/**
* Check whether the given node is a part of directive prologue or not.
* @param node The node to check.
* @param sourceCode The source code object to get tokens.
* @returns `true` if the node is a part of directive prologue.
*/
function isDirectivePrologue(
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
): boolean {
if (
isDirective(node, sourceCode) &&
node.parent &&
'body' in node.parent &&
Array.isArray(node.parent.body)
) {
for (const sibling of node.parent.body) {
if (sibling === node) {
break;
}
if (!isDirective(sibling, sourceCode)) {
return false;
}
}
return true;
}
return false;
}
/**
* Checks the given node is a CommonJS export statement
* @param node The node to check.
* @returns `true` if the node is a CommonJS export statement.
* @private
*/
function isCJSExport(node: TSESTree.Node): boolean {
if (node.type === AST_NODE_TYPES.ExpressionStatement) {
const expression = node.expression;
if (expression.type === AST_NODE_TYPES.AssignmentExpression) {
let left = expression.left;
if (left.type === AST_NODE_TYPES.MemberExpression) {
while (left.object.type === AST_NODE_TYPES.MemberExpression) {
left = left.object;
}
return (
left.object.type === AST_NODE_TYPES.Identifier &&
(left.object.name === 'exports' ||
(left.object.name === 'module' &&
left.property.type === AST_NODE_TYPES.Identifier &&
left.property.name === 'exports'))
);
}
}
}
return false;
}
/**
* Check whether the given node is an expression
* @param node The node to check.
* @param sourceCode The source code object to get tokens.
* @returns `true` if the node is an expression
*/
function isExpression(
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
): boolean {
return (
node.type === AST_NODE_TYPES.ExpressionStatement &&
!isDirectivePrologue(node, sourceCode)
);
}
/**
* Gets the actual last token.
*
* If a semicolon is semicolon-less style's semicolon, this ignores it.
* For example:
*
* foo()
* ;[1, 2, 3].forEach(bar)
* @param sourceCode The source code to get tokens.
* @param node The node to get.
* @returns The actual last token.
* @private
*/
function getActualLastToken(
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
): TSESTree.Token | null {
const semiToken = sourceCode.getLastToken(node)!;
const prevToken = sourceCode.getTokenBefore(semiToken);
const nextToken = sourceCode.getTokenAfter(semiToken);
const isSemicolonLessStyle =
prevToken &&
nextToken &&
prevToken.range[0] >= node.range[0] &&
util.isSemicolonToken(semiToken) &&
semiToken.loc.start.line !== prevToken.loc.end.line &&
semiToken.loc.end.line === nextToken.loc.start.line;
return isSemicolonLessStyle ? prevToken : semiToken;
}
/**
* This returns the concatenation of the first 2 captured strings.
* @param _ Unused. Whole matched string.
* @param trailingSpaces The trailing spaces of the first line.
* @param indentSpaces The indentation spaces of the last line.
* @returns The concatenation of trailingSpaces and indentSpaces.
* @private
*/
function replacerToRemovePaddingLines(
_: string,
trailingSpaces: string,
indentSpaces: string,
): string {
return trailingSpaces + indentSpaces;
}
/**
* Check and report statements for `any` configuration.
* It does nothing.
*
* @private
*/
function verifyForAny(): void {
// Empty
}
/**
* Check and report statements for `never` configuration.
* This autofix removes blank lines between the given 2 statements.
* However, if comments exist between 2 blank lines, it does not remove those
* blank lines automatically.
* @param context The rule context to report.
* @param _ Unused. The previous node to check.
* @param nextNode The next node to check.
* @param paddingLines The array of token pairs that blank
* lines exist between the pair.
*
* @private
*/
function verifyForNever(
context: TSESLint.RuleContext<MessageIds, Options>,
_: TSESTree.Node,
nextNode: TSESTree.Node,
paddingLines: [TSESTree.Token, TSESTree.Token][],
): void {
if (paddingLines.length === 0) {
return;
}
context.report({
node: nextNode,
messageId: 'unexpectedBlankLine',
fix(fixer) {
if (paddingLines.length >= 2) {
return null;
}
const prevToken = paddingLines[0][0];
const nextToken = paddingLines[0][1];
const start = prevToken.range[1];
const end = nextToken.range[0];
const text = context
.getSourceCode()
.text.slice(start, end)
.replace(PADDING_LINE_SEQUENCE, replacerToRemovePaddingLines);
return fixer.replaceTextRange([start, end], text);
},
});
}
/**
* Check and report statements for `always` configuration.
* This autofix inserts a blank line between the given 2 statements.
* If the `prevNode` has trailing comments, it inserts a blank line after the
* trailing comments.
* @param context The rule context to report.
* @param prevNode The previous node to check.
* @param nextNode The next node to check.
* @param paddingLines The array of token pairs that blank
* lines exist between the pair.
*
* @private
*/
function verifyForAlways(
context: TSESLint.RuleContext<MessageIds, Options>,
prevNode: TSESTree.Node,
nextNode: TSESTree.Node,
paddingLines: [TSESTree.Token, TSESTree.Token][],
): void {
if (paddingLines.length > 0) {
return;
}
context.report({
node: nextNode,
messageId: 'expectedBlankLine',
fix(fixer) {
const sourceCode = context.getSourceCode();
let prevToken = getActualLastToken(
prevNode,
sourceCode,
) as TSESTree.Token;
const nextToken =
(sourceCode.getFirstTokenBetween(prevToken, nextNode, {
includeComments: true,
/**
* Skip the trailing comments of the previous node.
* This inserts a blank line after the last trailing comment.
*
* For example:
*
* foo(); // trailing comment.
* // comment.
* bar();
*
* Get fixed to:
*
* foo(); // trailing comment.
*
* // comment.
* bar();
* @param token The token to check.
* @returns `true` if the token is not a trailing comment.
* @private
*/
filter(token) {
if (util.isTokenOnSameLine(prevToken, token)) {
prevToken = token;
return false;
}
return true;
},
}) as TSESTree.Token) || nextNode;
const insertText = util.isTokenOnSameLine(prevToken, nextToken)
? '\n\n'
: '\n';
return fixer.insertTextAfter(prevToken, insertText);
},
});
}
/**
* Types of blank lines.
* `any`, `never`, and `always` are defined.
* Those have `verify` method to check and report statements.
* @private
*/
const PaddingTypes = {
any: { verify: verifyForAny },
never: { verify: verifyForNever },
always: { verify: verifyForAlways },
};
/**
* Types of statements.
* Those have `test` method to check it matches to the given statement.
* @private
*/
const StatementTypes: Record<string, NodeTestObject> = {
'*': { test: (): boolean => true },
'block-like': { test: isBlockLikeStatement },
exports: { test: isCJSExport },
require: { test: isCJSRequire },
directive: { test: isDirectivePrologue },
expression: { test: isExpression },
iife: { test: isIIFEStatement },
'multiline-block-like': {
test: (node, sourceCode) =>
node.loc.start.line !== node.loc.end.line &&
isBlockLikeStatement(node, sourceCode),
},
'multiline-expression': {
test: (node, sourceCode) =>
node.loc.start.line !== node.loc.end.line &&
node.type === AST_NODE_TYPES.ExpressionStatement &&
!isDirectivePrologue(node, sourceCode),
},
'multiline-const': newMultilineKeywordTester('const'),
'multiline-let': newMultilineKeywordTester('let'),
'multiline-var': newMultilineKeywordTester('var'),
'singleline-const': newSinglelineKeywordTester('const'),
'singleline-let': newSinglelineKeywordTester('let'),
'singleline-var': newSinglelineKeywordTester('var'),
block: newNodeTypeTester(AST_NODE_TYPES.BlockStatement),
empty: newNodeTypeTester(AST_NODE_TYPES.EmptyStatement),
function: newNodeTypeTester(AST_NODE_TYPES.FunctionDeclaration),
break: newKeywordTester(AST_NODE_TYPES.BreakStatement, 'break'),
case: newKeywordTester(AST_NODE_TYPES.SwitchCase, 'case'),
class: newKeywordTester(AST_NODE_TYPES.ClassDeclaration, 'class'),
const: newKeywordTester(AST_NODE_TYPES.VariableDeclaration, 'const'),
continue: newKeywordTester(AST_NODE_TYPES.ContinueStatement, 'continue'),
debugger: newKeywordTester(AST_NODE_TYPES.DebuggerStatement, 'debugger'),
default: newKeywordTester(
[AST_NODE_TYPES.SwitchCase, AST_NODE_TYPES.ExportDefaultDeclaration],
'default',
),
do: newKeywordTester(AST_NODE_TYPES.DoWhileStatement, 'do'),
export: newKeywordTester(
[
AST_NODE_TYPES.ExportDefaultDeclaration,
AST_NODE_TYPES.ExportNamedDeclaration,
],
'export',
),
for: newKeywordTester(
[
AST_NODE_TYPES.ForStatement,
AST_NODE_TYPES.ForInStatement,
AST_NODE_TYPES.ForOfStatement,
],
'for',
),
if: newKeywordTester(AST_NODE_TYPES.IfStatement, 'if'),
import: newKeywordTester(AST_NODE_TYPES.ImportDeclaration, 'import'),
let: newKeywordTester(AST_NODE_TYPES.VariableDeclaration, 'let'),
return: newKeywordTester(AST_NODE_TYPES.ReturnStatement, 'return'),
switch: newKeywordTester(AST_NODE_TYPES.SwitchStatement, 'switch'),
throw: newKeywordTester(AST_NODE_TYPES.ThrowStatement, 'throw'),
try: newKeywordTester(AST_NODE_TYPES.TryStatement, 'try'),
var: newKeywordTester(AST_NODE_TYPES.VariableDeclaration, 'var'),
while: newKeywordTester(
[AST_NODE_TYPES.WhileStatement, AST_NODE_TYPES.DoWhileStatement],
'while',
),
with: newKeywordTester(AST_NODE_TYPES.WithStatement, 'with'),
// Additional Typescript constructs
interface: newKeywordTester(
AST_NODE_TYPES.TSInterfaceDeclaration,
'interface',
),
type: newKeywordTester(AST_NODE_TYPES.TSTypeAliasDeclaration, 'type'),
};
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
export default util.createRule<Options, MessageIds>({
name: 'padding-line-between-statements',
meta: {
type: 'layout',
docs: {
description: 'require or disallow padding lines between statements',
recommended: false,
extendsBaseRule: true,
},
fixable: 'whitespace',
hasSuggestions: true,
schema: {
definitions: {
paddingType: {
enum: Object.keys(PaddingTypes),
},
statementType: {
anyOf: [
{ enum: Object.keys(StatementTypes) },
{
type: 'array',
items: { enum: Object.keys(StatementTypes) },
minItems: 1,
uniqueItems: true,
additionalItems: false,
},
],
},
},
type: 'array',
items: {
type: 'object',
properties: {
blankLine: { $ref: '#/definitions/paddingType' },
prev: { $ref: '#/definitions/statementType' },
next: { $ref: '#/definitions/statementType' },
},
additionalProperties: false,
required: ['blankLine', 'prev', 'next'],
},
additionalItems: false,
},
messages: {
unexpectedBlankLine: 'Unexpected blank line before this statement.',
expectedBlankLine: 'Expected blank line before this statement.',
},
},
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode();
const configureList = context.options || [];
type Scope = null | {
upper: Scope;
prevNode: TSESTree.Node | null;
};
let scopeInfo: Scope = null;
/**
* Processes to enter to new scope.
* This manages the current previous statement.
*
* @private
*/
function enterScope(): void {
scopeInfo = {
upper: scopeInfo,
prevNode: null,
};
}
/**
* Processes to exit from the current scope.
*
* @private
*/
function exitScope(): void {
if (scopeInfo) {
scopeInfo = scopeInfo.upper;
}
}
/**
* Checks whether the given node matches the given type.
* @param node The statement node to check.
* @param type The statement type to check.
* @returns `true` if the statement node matched the type.
* @private
*/
function match(node: TSESTree.Node, type: string | string[]): boolean {
let innerStatementNode = node;
while (innerStatementNode.type === AST_NODE_TYPES.LabeledStatement) {
innerStatementNode = innerStatementNode.body;
}
if (Array.isArray(type)) {
return type.some(match.bind(null, innerStatementNode));
}
return StatementTypes[type].test(innerStatementNode, sourceCode);
}
/**
* Finds the last matched configure from configureList.
* @paramprevNode The previous statement to match.
* @paramnextNode The current statement to match.
* @returns The tester of the last matched configure.
* @private
*/
function getPaddingType(
prevNode: TSESTree.Node,
nextNode: TSESTree.Node,
): typeof PaddingTypes[keyof typeof PaddingTypes] {
for (let i = configureList.length - 1; i >= 0; --i) {
const configure = configureList[i];
if (
match(prevNode, configure.prev) &&
match(nextNode, configure.next)
) {
return PaddingTypes[configure.blankLine];
}
}
return PaddingTypes.any;
}
/**
* Gets padding line sequences between the given 2 statements.
* Comments are separators of the padding line sequences.
* @paramprevNode The previous statement to count.
* @paramnextNode The current statement to count.
* @returns The array of token pairs.
* @private
*/
function getPaddingLineSequences(
prevNode: TSESTree.Node,
nextNode: TSESTree.Node,
): [TSESTree.Token, TSESTree.Token][] {
const pairs: [TSESTree.Token, TSESTree.Token][] = [];
let prevToken: TSESTree.Token = getActualLastToken(prevNode, sourceCode)!;
if (nextNode.loc.start.line - prevToken.loc.end.line >= 2) {
do {
const token: TSESTree.Token = sourceCode.getTokenAfter(prevToken, {
includeComments: true,
})!;
if (token.loc.start.line - prevToken.loc.end.line >= 2) {
pairs.push([prevToken, token]);
}
prevToken = token;
} while (prevToken.range[0] < nextNode.range[0]);
}
return pairs;
}
/**
* Verify padding lines between the given node and the previous node.
* @param node The node to verify.
*
* @private
*/
function verify(node: TSESTree.Node): void {
if (
!node.parent ||
![
AST_NODE_TYPES.BlockStatement,
AST_NODE_TYPES.Program,
AST_NODE_TYPES.SwitchCase,
AST_NODE_TYPES.SwitchStatement,
AST_NODE_TYPES.TSModuleBlock,
].includes(node.parent.type)
) {
return;
}
// Save this node as the current previous statement.
const prevNode = scopeInfo!.prevNode;
// Verify.
if (prevNode) {
const type = getPaddingType(prevNode, node);
const paddingLines = getPaddingLineSequences(prevNode, node);
type.verify(context, prevNode, node, paddingLines);
}
scopeInfo!.prevNode = node;
}
/**
* Verify padding lines between the given node and the previous node.
* Then process to enter to new scope.
* @param node The node to verify.
*
* @private
*/
function verifyThenEnterScope(node: TSESTree.Node): void {
verify(node);
enterScope();
}
return {
Program: enterScope,
BlockStatement: enterScope,
SwitchStatement: enterScope,
TSModuleBlock: enterScope,
'Program:exit': exitScope,
'BlockStatement:exit': exitScope,
'SwitchStatement:exit': exitScope,
'TSModuleBlock:exit': exitScope,
':statement': verify,
SwitchCase: verifyThenEnterScope,
'SwitchCase:exit': exitScope,
};
},
});