-
Notifications
You must be signed in to change notification settings - Fork 4
/
Formatter.ts
1256 lines (1144 loc) · 49 KB
/
Formatter.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
import * as trimRight from 'trim-right';
import { Lexer, Token, TokenKind, AllowedLocalIdentifiers, Parser, createVisitor, WalkMode } from 'brighterscript';
import { SourceNode } from 'source-map';
import { FormattingOptions, normalizeOptions } from './FormattingOptions';
import { IfStatement, AALiteralExpression, AAMemberExpression } from 'brighterscript/dist/parser';
export class Formatter {
/**
* Construct a new formatter. The options provided here will be normalized exactly once,
* and stored on the formatter instance.
*/
public constructor(formattingOptions?: FormattingOptions) {
if (formattingOptions) {
this.formattingOptions = normalizeOptions(formattingOptions);
}
}
/**
* The formatting options provided in the constructor. Can be undefined if none were provided
*/
public formattingOptions?: FormattingOptions;
/**
* The default number of spaces when indenting with spaces
*/
public static DEFAULT_INDENT_SPACE_COUNT = 4;
/**
* Format the given input.
* @param inputText the text to format
* @param formattingOptions options specifying formatting preferences
*/
public format(inputText: string, formattingOptions?: FormattingOptions) {
let tokens = this.getFormattedTokens(inputText, formattingOptions);
//join all tokens back together into a single string
let outputText = '';
for (let token of tokens) {
outputText += token.text;
}
return outputText;
}
/**
* Format the given input and return the formatted text as well as a source map
* @param inputText the text to format
* @param sourcePath the path to the file being formatted (used for sourcemap generator)
* @param formattingOptions options specifying formatting preferences
* @returns an object with property `code` holding the formatted code, and `map` holding the source map.
*/
public formatWithSourceMap(inputText: string, sourcePath: string, formattingOptions?: FormattingOptions) {
let tokens = this.getFormattedTokens(inputText, formattingOptions);
let chunks = [] as Array<string | SourceNode>;
for (let token of tokens) {
if (token.range) {
chunks.push(
new SourceNode(
//BrighterScript line numbers are 0-based, but source-map expects 1-based
token.range.start.line + 1,
token.range.start.character,
sourcePath,
token.text
)
);
} else {
chunks.push(token.text);
}
}
return new SourceNode(null, null, sourcePath, chunks).toStringWithSourceMap();
}
/**
* Format the given input.
* @param inputText the text to format
* @param formattingOptions options specifying formatting preferences
*/
public getFormattedTokens(inputText: string, formattingOptions?: FormattingOptions) {
/**
* Choose options in this order:
* 1. The provided options
* 2. The options from this instance property
* 3. The default options
*/
let options = normalizeOptions({
...this.formattingOptions,
...formattingOptions
});
let { tokens } = Lexer.scan(inputText, {
includeWhitespace: true
});
let parser = Parser.parse(
//strip out whitespace because the parser can't handle that
tokens.filter(x => x.kind !== TokenKind.Whitespace)
);
if (options.formatMultiLineObjectsAndArrays) {
tokens = this.formatMultiLineObjectsAndArrays(tokens);
}
if (options.compositeKeywords) {
tokens = this.formatCompositeKeywords(tokens, options);
}
tokens = this.formatKeywordCase(tokens, options);
if (options.removeTrailingWhiteSpace) {
tokens = this.formatTrailingWhiteSpace(tokens, options);
}
if (options.formatInteriorWhitespace) {
tokens = this.formatInteriorWhitespace(tokens, parser, options);
}
//dedupe side-by-side Whitespace tokens
this.dedupeWhitespace(tokens);
if (options.formatIndent) {
tokens = this.formatIndentation(tokens, options, parser);
}
return tokens;
}
/**
* Determines if the current index is the start of a single-line array or AA.
* Walks forward until we find the equal number of open and close curlies/squares, or a newline
*/
private isStartofSingleLineArrayOrAA(tokens: Token[], currentIndex: number, openKind: TokenKind, closeKind: TokenKind) {
let openCount = 0;
for (let i = currentIndex; i < tokens.length; i++) {
let token = tokens[i];
if (token.kind === openKind) {
openCount++;
} else if (token.kind === closeKind) {
openCount--;
}
if (openCount === 0) {
return true;
} else if (token.kind === TokenKind.Newline) {
return false;
}
}
return false;
}
/**
* Find the matching closing token for open square or open curly
*/
private getClosingToken(tokens: Token[], currentIndex: number, openKind: TokenKind, closeKind: TokenKind) {
let openCount = 0;
for (let i = currentIndex; i < tokens.length; i++) {
let token = tokens[i];
if (token.kind === openKind) {
openCount++;
} else if (token.kind === closeKind) {
openCount--;
}
if (openCount === 0) {
return token;
}
}
}
/**
* Given a kind like `}` or `]`, walk backwards until we find its match
*/
private getOpeningToken(tokens: Token[], currentIndex: number, openKind: TokenKind, closeKind: TokenKind) {
let openCount = 0;
for (let i = currentIndex; i >= 0; i--) {
let token = tokens[i];
if (token.kind === openKind) {
openCount++;
} else if (token.kind === closeKind) {
openCount--;
}
if (openCount === 0) {
return token;
}
}
}
private isMatchingDoubleArrayOrArrayCurly(tokens: Token[], currentIndex: number) {
let token = tokens[currentIndex];
let nextNonWhitespaceToken = this.getNextNonWhitespaceToken(tokens, currentIndex, true);
//don't separate multiple open/close pairs
if (
//is open array
token.kind === TokenKind.LeftSquareBracket &&
//there is another token on this line
nextNonWhitespaceToken &&
//is next token an open array or open object
(nextNonWhitespaceToken.kind === TokenKind.LeftSquareBracket || nextNonWhitespaceToken.kind === TokenKind.LeftCurlyBrace)
) {
let closingToken = this.getClosingToken(tokens, currentIndex, TokenKind.LeftSquareBracket, TokenKind.RightSquareBracket);
//look at the previous token
let previous = closingToken && this.getPreviousNonWhitespaceToken(tokens, tokens.indexOf(closingToken), true);
/* istanbul ignore else (because I can't figure out how to make this happen but I think it's still necessary) */
if (previous && (previous.kind === TokenKind.RightSquareBracket || previous.kind === TokenKind.RightCurlyBrace)) {
return true;
}
}
}
/**
* Standardize multi-line objects and arrays by inserting newlines after leading and before trailing.
*/
private formatMultiLineObjectsAndArrays(tokens: Token[]) {
for (let i = 0; i < tokens.length; i++) {
let token = tokens[i];
let openKind: TokenKind | undefined;
let closeKind: TokenKind | undefined;
if (token.kind === TokenKind.LeftCurlyBrace) {
openKind = TokenKind.LeftCurlyBrace;
closeKind = TokenKind.RightCurlyBrace;
} else if (token.kind === TokenKind.LeftSquareBracket) {
openKind = TokenKind.LeftSquareBracket;
closeKind = TokenKind.RightSquareBracket;
}
let nextNonWhitespaceToken = this.getNextNonWhitespaceToken(tokens, i, true);
//move contents to new line if this is a multi-line array or AA
if (
//is open curly or open square
openKind && closeKind &&
//is a multi-line array or AA
!this.isStartofSingleLineArrayOrAA(tokens, i, openKind, closeKind) &&
//there is extra stuff on this line that is not the end of the file
nextNonWhitespaceToken && nextNonWhitespaceToken.kind !== TokenKind.Eof &&
//is NOT array like `[[ ...\n ]]`, or `[{ ...\n }]`)
!this.isMatchingDoubleArrayOrArrayCurly(tokens, i)
) {
tokens.splice(i + 1, 0, <any>{
kind: TokenKind.Newline,
text: '\n'
});
let closingToken = this.getClosingToken(tokens, i, openKind, closeKind);
/* istanbul ignore next */
let closingTokenKindex = closingToken ? tokens.indexOf(closingToken) : -1;
i++;
//if there's stuff before the closer, move it to a newline
if (this.getPreviousNonWhitespaceToken(tokens, closingTokenKindex, true)) {
tokens.splice(closingTokenKindex, 0, <any>{
kind: TokenKind.Newline,
text: '\n'
});
}
}
}
return tokens;
}
private dedupeWhitespace(tokens: Token[]) {
for (let i = 0; i < tokens.length; i++) {
let currentToken = tokens[i];
let nextToken = tokens[i + 1] ? tokens[i + 1] : { kind: undefined, text: '' };
if (currentToken.kind === TokenKind.Whitespace && nextToken.kind === TokenKind.Whitespace) {
currentToken.text += nextToken.text;
tokens.splice(i + 1, 1);
//decrement the counter so we process this token again so it can absorb more Whitespace tokens
i--;
}
}
}
private formatCompositeKeywords(tokens: Token[], options: FormattingOptions) {
let indexOffset = 0;
for (let i = 0; i < tokens.length; i++) {
let token = tokens[i];
(token as any).startIndex += indexOffset;
let previousNonWhitespaceToken = this.getPreviousNonWhitespaceToken(tokens, i);
let nextNonWhitespaceToken = this.getNextNonWhitespaceToken(tokens, i);
if (
//is this a composite token
CompositeKeywords.includes(token.kind) &&
//is not being used as a key in an AA literal
(!nextNonWhitespaceToken || nextNonWhitespaceToken.kind !== TokenKind.Colon) &&
//is not being used as an object key
previousNonWhitespaceToken?.kind !== TokenKind.Dot
) {
let parts = this.getCompositeKeywordParts(token);
let tokenValue = token.text;
//remove separating Whitespace
if (options.compositeKeywords === 'combine') {
token.text = parts[0] + parts[1];
//separate with exactly 1 space
} else if (options.compositeKeywords === 'split') {
token.text = parts[0] + ' ' + parts[1];
} else {
//do nothing
}
let offsetDifference = token.text.length - tokenValue.length;
indexOffset += offsetDifference;
}
}
return tokens;
}
private getCompositeKeywordParts(token: Token) {
let lowerValue = token.text.toLowerCase();
//split the parts of the token, but retain their case
if (lowerValue.startsWith('end')) {
return [token.text.substring(0, 3), token.text.substring(3).trim()];
} else if (lowerValue.startsWith('#else')) {
return [token.text.substring(0, 5), token.text.substring(5).trim()];
} else {
return [token.text.substring(0, 4), token.text.substring(4).trim()];
}
}
/**
* Determine if the token is a type keyword (meaing preceeded by `as` token)
* @param token
*/
private isType(tokens: Token[], token: Token) {
let previousToken = this.getPreviousNonWhitespaceToken(tokens, tokens.indexOf(token));
if (previousToken && previousToken.text.toLowerCase() === 'as') {
return true;
} else {
return false;
}
}
private formatKeywordCase(tokens: Token[], options: FormattingOptions) {
for (let token of tokens) {
//if this token is a keyword
if (Keywords.includes(token.kind)) {
let keywordCase: FormattingOptions['keywordCase'];
let lowerKind = token.kind.toLowerCase();
//a token is a type if it's preceeded by an `as` token
if (this.isType(tokens, token)) {
//options.typeCase is always set to options.keywordCase when not provided
keywordCase = options.typeCase;
//if this is an overridden type keyword, use that override instead
if (options.typeCaseOverride && options.typeCaseOverride[lowerKind] !== undefined) {
keywordCase = options.typeCaseOverride[lowerKind];
}
} else {
keywordCase = options.keywordCase;
//if this is an overridable keyword, use that override instead
if (options.keywordCaseOverride && options.keywordCaseOverride[lowerKind] !== undefined) {
keywordCase = options.keywordCaseOverride[lowerKind];
}
}
switch (keywordCase) {
case 'lower':
token.text = token.text.toLowerCase();
break;
case 'upper':
token.text = token.text.toUpperCase();
break;
case 'title':
let lowerValue = token.text.toLowerCase();
//format the first letter (conditional compile composite-keywords start with hash)
let charIndex = token.text.startsWith('#') ? 1 : 0;
token.text = this.upperCaseLetter(token.text, charIndex);
//if this is a composite keyword, format the first letter of the second word
if (CompositeKeywords.includes(token.kind)) {
let spaceCharCount = (/\s+/.exec(lowerValue) ?? []).length;
let firstWordLength = CompositeKeywordStartingWords.find(x => lowerValue.startsWith(x))!.length;
let nextWordFirstCharIndex = firstWordLength + spaceCharCount;
token.text = this.upperCaseLetter(token.text, nextWordFirstCharIndex);
}
break;
case 'original':
default:
//do nothing
break;
}
}
}
return tokens;
}
private formatIndentation(tokens: Token[], options: FormattingOptions, parser: Parser) {
let tabCount = 0;
const ifStatements = new Map<Token, IfStatement>();
//create a map of all if statements for easier lookups
parser.ast.walk(createVisitor({
IfStatement: (statement) => {
ifStatements.set(statement.tokens.if, statement);
}
}), {
walkMode: WalkMode.visitAllRecursive
});
let nextLineStartTokenIndex = 0;
//the list of output tokens
let outputTokens: Token[] = [];
//set the loop to run for a max of double the number of tokens we found so we don't end up with an infinite loop
for (let outerLoopCounter = 0; outerLoopCounter <= tokens.length * 2; outerLoopCounter++) {
let lineObj = this.getLineTokens(nextLineStartTokenIndex, tokens);
nextLineStartTokenIndex = lineObj.stopIndex + 1;
let lineTokens = lineObj.tokens;
let thisTabCount = tabCount;
let foundIndentorThisLine = false;
let foundNonWhitespaceThisLine = false;
for (let i = 0; i < lineTokens.length; i++) {
let token = lineTokens[i];
let previousNonWhitespaceToken = this.getPreviousNonWhitespaceToken(lineTokens, i);
let nextNonWhitespaceToken = this.getNextNonWhitespaceToken(lineTokens, i);
//keep track of whether we found a non-Whitespace (or Newline) character
if (![TokenKind.Whitespace, TokenKind.Newline].includes(token.kind)) {
foundNonWhitespaceThisLine = true;
}
if (
//if this is an indentor token
IndentSpacerTokenKinds.includes(token.kind) &&
//is not being used as a key in an AA literal
nextNonWhitespaceToken && nextNonWhitespaceToken.kind !== TokenKind.Colon
) {
//skip indent for 'function'|'sub' used as type (preceeded by `as` keyword)
if (
CallableKeywordTokenKinds.includes(token.kind) &&
//the previous token will be Whitespace, so verify that previousPrevious is 'as'
previousNonWhitespaceToken?.kind === TokenKind.As
) {
continue;
}
//skip indent for single-line if statements
let ifStatement = ifStatements.get(token);
if (
ifStatement &&
(
//does not have an end if
!ifStatement.tokens.endIf ||
//end if is on same line as if
ifStatement.tokens.if.range.end.line === ifStatement.tokens.endIf.range.end.line
)
) {
//if there's an `else`, skip past it since it'll cause de-indent otherwise
if (ifStatement.tokens.else) {
i = tokens.indexOf(ifStatement.tokens.else);
//if there's no else, but there is an `else if`, skip past it since it'll cause de-indent otherwise
} else if (ifStatement.elseIfs && ifStatement.elseIfs.length > 0) {
i = tokens.indexOf(ifStatement.elseIfs[ifStatement.elseIfs.length - 1].elseIfToken);
}
continue;
}
tabCount++;
foundIndentorThisLine = true;
//don't double indent if this is `[[...\n...]]` or `[{...\n...}]`
if (
//is open square
token.kind === TokenKind.LeftSquareBracket &&
//next is an open curly or square
(nextNonWhitespaceToken.kind === TokenKind.LeftCurlyBrace || nextNonWhitespaceToken.kind === TokenKind.LeftSquareBracket) &&
//both tokens are on the same line
token.range.start.line === nextNonWhitespaceToken.range.start.line
) {
//find the closer
let closer = this.getClosingToken(tokens, tokens.indexOf(token), TokenKind.LeftSquareBracket, TokenKind.RightSquareBracket);
let expectedClosingPreviousKind = nextNonWhitespaceToken.kind === TokenKind.LeftSquareBracket ? TokenKind.RightSquareBracket : TokenKind.RightCurlyBrace;
let closingPrevious = closer && this.getPreviousNonWhitespaceToken(tokens, tokens.indexOf(closer), true);
/* istanbul ignore else (because I can't figure out how to make this happen but I think it's still necessary) */
if (closingPrevious && closingPrevious.kind === expectedClosingPreviousKind) {
//skip the next token
i++;
}
}
} else if (
//this is an outdentor token
OutdentSpacerTokenKinds.includes(token.kind) &&
//is not being used as a key in an AA literal
nextNonWhitespaceToken && nextNonWhitespaceToken.kind !== TokenKind.Colon &&
//is not a method call
!(
//certain symbols may appear next to an open paren, so exclude those
![TokenKind.RightSquareBracket].includes(token.kind) &&
//open paren means method call
nextNonWhitespaceToken.kind === TokenKind.LeftParen
)
) {
//do not un-indent if this is a `next` or `endclass` token preceeded by a period
if (
[TokenKind.Next, TokenKind.EndClass, TokenKind.Namespace, TokenKind.EndNamespace].includes(token.kind) &&
previousNonWhitespaceToken && previousNonWhitespaceToken.kind === TokenKind.Dot
) {
continue;
}
tabCount--;
if (foundIndentorThisLine === false) {
thisTabCount--;
}
//don't double un-indent if this is `[[...\n...]]` or `[{...\n...}]`
if (
//is closing curly or square
(token.kind === TokenKind.RightCurlyBrace || token.kind === TokenKind.RightSquareBracket) &&
//next is closing square
nextNonWhitespaceToken.kind === TokenKind.RightSquareBracket &&
//both tokens are on the same line
token.range.start.line === nextNonWhitespaceToken.range.start.line
) {
let opener = this.getOpeningToken(
tokens,
tokens.indexOf(nextNonWhitespaceToken),
TokenKind.LeftSquareBracket,
TokenKind.RightSquareBracket
);
let openerNext = opener && this.getNextNonWhitespaceToken(tokens, tokens.indexOf(opener), true);
if (openerNext && (openerNext.kind === TokenKind.LeftCurlyBrace || openerNext.kind === TokenKind.LeftSquareBracket)) {
//skip the next token
i += 1;
continue;
}
}
//this is an interum token
} else if (InterumSpacingTokenKinds.includes(token.kind)) {
//these need outdented, but don't change the tabCount
thisTabCount--;
}
// else if (token.kind === TokenKind.return && foundIndentorThisLine) {
// //a return statement on the same line as an indentor means we don't want to indent
// tabCount--;
// }
}
//if the tab counts are less than zero, something is wrong. However, at least try to do formatting as best we can by resetting to 0
thisTabCount = thisTabCount < 0 ? 0 : thisTabCount;
tabCount = tabCount < 0 ? 0 : tabCount;
let leadingWhitespace: string;
if (options.indentStyle === 'spaces') {
let indentSpaceCount = options.indentSpaceCount
? options.indentSpaceCount
: Formatter.DEFAULT_INDENT_SPACE_COUNT;
let spaceCount = thisTabCount * indentSpaceCount;
leadingWhitespace = Array(spaceCount + 1).join(' ');
} else {
leadingWhitespace = Array(thisTabCount + 1).join('\t');
}
//create a Whitespace token if there isn't one
if (lineTokens[0] && lineTokens[0].kind !== TokenKind.Whitespace) {
lineTokens.unshift(<any>{
startIndex: -1,
kind: TokenKind.Whitespace,
text: ''
});
}
//replace the Whitespace with the formatted Whitespace
lineTokens[0].text = leadingWhitespace;
//if this is a line filled only with Whitespace, throw out the Whitespace
if (foundNonWhitespaceThisLine === false) {
//only keep the traling Newline
lineTokens = [lineTokens.pop() as Token];
}
//add this list of tokens
outputTokens.push.apply(outputTokens, lineTokens);
let lastLineToken = lineTokens[lineTokens.length - 1];
//if we have found the end of file
if (
lastLineToken && lastLineToken.kind === TokenKind.Eof
) {
break;
}
/* istanbul ignore next */
if (outerLoopCounter === tokens.length * 2) {
throw new Error('Something went terribly wrong');
}
}
return outputTokens;
}
/**
* Force all Whitespace between tokens to be exactly 1 space wide
*/
private formatInteriorWhitespace(
tokens: Token[],
parser: Parser,
options: FormattingOptions
) {
let addBoth = [
//assignments
TokenKind.Equal,
TokenKind.PlusEqual,
TokenKind.MinusEqual,
TokenKind.StarEqual,
TokenKind.ForwardslashEqual,
TokenKind.BackslashEqual,
TokenKind.LeftShiftEqual,
TokenKind.RightShiftEqual,
//operators
TokenKind.Plus,
TokenKind.Minus,
TokenKind.Star,
TokenKind.Forwardslash,
TokenKind.Backslash,
TokenKind.Caret,
TokenKind.LessGreater,
TokenKind.LessEqual,
TokenKind.GreaterEqual,
TokenKind.Greater,
TokenKind.Less
];
let addLeft = [
...addBoth,
TokenKind.RightCurlyBrace
];
let addRight = [
...addBoth,
TokenKind.LeftCurlyBrace,
TokenKind.Comma,
TokenKind.Colon
];
let removeBoth = [];
let removeLeft = [
...removeBoth,
TokenKind.Comma,
TokenKind.RightSquareBracket,
TokenKind.RightParen,
TokenKind.PlusPlus,
TokenKind.MinusMinus
];
let removeRight = [
...removeBoth,
TokenKind.LeftSquareBracket,
TokenKind.LeftParen
];
let isPastFirstTokenOfLine = false;
for (let i = 0; i < tokens.length; i++) {
let token = tokens[i];
let nextTokenType: TokenKind | undefined = (tokens[i + 1] ? tokens[i + 1].kind : undefined);
let previousTokenType: TokenKind | undefined = (tokens[i - 1] ? tokens[i - 1].kind : undefined);
//reset token indicator on Newline
if (token.kind === TokenKind.Newline) {
isPastFirstTokenOfLine = false;
continue;
}
//skip past leading Whitespace
if (token.kind === TokenKind.Whitespace && isPastFirstTokenOfLine === false) {
continue;
}
isPastFirstTokenOfLine = true;
//force token to be exactly 1 space
if (token.kind === TokenKind.Whitespace) {
token.text = ' ';
}
//pad any of these token types with a space to the right
if (addRight.includes(token.kind)) {
//special case: we want the negative sign to be directly beside a numeric, in certain cases.
//we can't handle every case, but we can get close
if (this.looksLikeNegativeNumericLiteral(tokens, i)) {
//throw out the space to the right of the minus symbol if present
if (i + 1 < tokens.length && tokens[i + 1].kind === TokenKind.Whitespace) {
this.removeWhitespace(tokens, i + 1);
}
//ensure a space token to the right, only if we have more tokens to the right available
} else if (nextTokenType && ![TokenKind.Whitespace, TokenKind.Newline, TokenKind.Eof].includes(nextTokenType)) {
//don't add Whitespace if the next token is the Newline
tokens.splice(i + 1, 0, <any>{
startIndex: -1,
kind: TokenKind.Whitespace,
text: ' '
});
}
}
//pad any of these tokens with a space to the left
if (
addLeft.includes(token.kind) &&
//don't add left for negative sign preceeded by a square brace or paren
!(token.kind === TokenKind.Minus && previousTokenType && [TokenKind.LeftSquareBracket, TokenKind.LeftParen].includes(previousTokenType))
) {
//ensure a space token to the left
if (previousTokenType && previousTokenType !== TokenKind.Whitespace) {
tokens.splice(i, 0, <any>{
startIndex: -1,
kind: TokenKind.Whitespace,
text: ' '
});
//increment i by 1 since we added a token
i++;
}
}
//remove any space tokens on the right
if (removeRight.includes(token.kind)) {
if (nextTokenType === TokenKind.Whitespace) {
//remove the next token, which is the Whitespace token
tokens.splice(i + 1, 1);
}
}
//remove any space tokens on the left
if (removeLeft.includes(token.kind)) {
if (previousTokenType === TokenKind.Whitespace) {
//remove the previous token, which is the Whitespace token
tokens.splice(i - 1, 1);
//backtrack the index since we just shifted the array
i--;
}
}
}
tokens = this.formatTokenSpacing(tokens, parser, options);
return tokens;
}
/**
* Ensure exactly 1 or 0 spaces between all literal associative array keys and the colon after it
*/
private formatSpaceBetweenAssociativeArrayLiteralKeyAndColon(tokens: Token[], parser: Parser, options: FormattingOptions) {
const aaLiterals = [] as AALiteralExpression[];
parser.ast.walk(createVisitor({
AALiteralExpression: (expression) => {
aaLiterals.push(expression);
}
}), {
walkMode: WalkMode.visitAllRecursive
});
//find all of the AA literals
for (let aaLiteral of aaLiterals) {
for (let element of (aaLiteral.elements as AAMemberExpression[])) {
//our target elements should have both `key` and `colon` and they should both be on the same line
if (element.keyToken && element.colonToken && element.keyToken.range.end.line === element.colonToken.range.end.line) {
let whitespaceToken: Token;
let idx = tokens.indexOf(element.keyToken);
let nextToken = tokens[idx + 1];
if (nextToken.kind === TokenKind.Whitespace) {
whitespaceToken = nextToken;
} else {
whitespaceToken = <any>{
kind: TokenKind.Whitespace,
text: ''
};
tokens.splice(idx + 1, 0, whitespaceToken);
}
whitespaceToken.text = options.insertSpaceBetweenAssociativeArrayLiteralKeyAndColon === true ? ' ' : '';
}
}
}
return tokens;
}
/**
* Format spacing between various tokens that are more specific than `formatInteriorWhitespace`
*/
private formatTokenSpacing(
tokens: Token[],
parser: Parser,
options: FormattingOptions
) {
let i = 0;
let token: Token = undefined as any;
let nextNonWhitespaceToken: Token | undefined;
const setIndex = (newValue) => {
i = newValue;
token = tokens[i];
nextNonWhitespaceToken = this.getNextNonWhitespaceToken(tokens, i);
};
//handle special cases
for (i; i < tokens.length; i++) {
setIndex(i);
//space to left of function parens?
{
let parenToken: Token | undefined;
//look for anonymous functions
if (token.kind === TokenKind.Function && nextNonWhitespaceToken && nextNonWhitespaceToken.kind === TokenKind.LeftParen) {
parenToken = nextNonWhitespaceToken;
//look for named functions
} else if (token.kind === TokenKind.Function && nextNonWhitespaceToken && nextNonWhitespaceToken.kind === TokenKind.Identifier) {
//get the next non-Whitespace token, which SHOULD be the paren
let parenCandidate = this.getNextNonWhitespaceToken(tokens, tokens.indexOf(nextNonWhitespaceToken));
if (parenCandidate && parenCandidate.kind === TokenKind.LeftParen) {
parenToken = parenCandidate;
}
}
//if we found the paren token, handle spacing
if (parenToken) {
//walk backwards, removing any Whitespace tokens found
this.removeWhitespaceTokensBackwards(tokens, tokens.indexOf(parenToken));
if (options.insertSpaceBeforeFunctionParenthesis) {
//insert a Whitespace token
tokens.splice(tokens.indexOf(parenToken), 0, <any>{
kind: TokenKind.Whitespace,
text: ' ',
startIndex: -1
});
}
//next loop iteration should be after the open paren
setIndex(
tokens.indexOf(parenToken)
);
}
}
//add/remove whitespace around curly braces
{
//start of non empty object
if (
//is start of object
token.kind === TokenKind.LeftCurlyBrace &&
//there is some non-whitespace token to our right
this.getNextNonWhitespaceToken(tokens, i, true)?.kind
) {
let whitespaceToken = tokens[i + 1];
//this is never called because formatInteriorWhitespace already handles inserting this space
// //ensure there is a whitespace token in that position (make it 0-length for now)
// if (whitespaceToken && whitespaceToken.kind !== TokenKind.Whitespace) {
// whitespaceToken = <any>{
// kind: TokenKind.Whitespace,
// startIndex: -1,
// text: ''
// };
// tokens.splice(i, 0, whitespaceToken);
// }
//insert the space only if so configured
whitespaceToken.text = options.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces ? ' ' : '';
}
//end of non-empty object
if (
//is end of object
token.kind === TokenKind.RightCurlyBrace &&
//there is some non-whitespace token to our left
this.getPreviousNonWhitespaceToken(tokens, i, true)
) {
let whitespaceToken = tokens[i - 1];
//this is never called because formatInteriorWhitespace already handles inserting this space
// //ensure there is a whitespace token in that position (make it 0-length for now)
// if (whitespaceToken && whitespaceToken.kind !== TokenKind.Whitespace) {
// whitespaceToken = <any>{
// kind: TokenKind.Whitespace,
// startIndex: -1,
// text: ''
// };
// tokens.splice(i - 1, 0, whitespaceToken);
// }
//insert the space only if so configured
whitespaceToken.text = options.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces ? ' ' : '';
//next loop iteration should be after the closing curly brace
setIndex(
tokens.indexOf(token)
);
}
}
//empty curly braces
if (token.kind === TokenKind.RightCurlyBrace && this.getPreviousNonWhitespaceToken(tokens, i, true)?.kind === TokenKind.LeftCurlyBrace) {
this.removeWhitespaceTokensBackwards(tokens, i);
tokens.splice(tokens.indexOf(token), 0, <any>{
kind: TokenKind.Whitespace,
startIndex: -1,
text: options.insertSpaceBetweenEmptyCurlyBraces ? ' ' : ''
});
//next loop iteration should be after the closing curly brace
setIndex(
tokens.indexOf(token)
);
}
//empty parenthesis (user doesn't have this option, we will always do this one)
if (token.kind === TokenKind.LeftParen && nextNonWhitespaceToken && nextNonWhitespaceToken.kind === TokenKind.RightParen) {
this.removeWhitespaceTokensBackwards(tokens, tokens.indexOf(nextNonWhitespaceToken));
//next loop iteration should be after the closing paren
setIndex(
tokens.indexOf(nextNonWhitespaceToken)
);
}
}
tokens = this.formatSpaceBetweenAssociativeArrayLiteralKeyAndColon(tokens, parser, options);
return tokens;
}
/**
* Remove Whitespace tokens backwards until a non-Whitespace token is encountered
* @param startIndex the index of the non-Whitespace token to start with. This function will start iterating at `startIndex - 1`
*/
private removeWhitespaceTokensBackwards(tokens: Token[], startIndex: number) {
let removeCount = 0;
let i = startIndex - 1;
while (tokens[i--].kind === TokenKind.Whitespace) {
removeCount++;
}
tokens.splice(startIndex - removeCount, removeCount);
}
/**
* Remove Whitespace until the next non-Whitespace character.
* This operates on the array itself
*/
private removeWhitespace(tokens: Token[], index: number) {
while (tokens[index] && tokens[index].kind === TokenKind.Whitespace) {
tokens.splice(index, 1);
index++;
}
}
/**
* Determine if the current token appears to be the negative sign for a numeric leteral
*/
private looksLikeNegativeNumericLiteral(tokens: Token[], index: number) {
let thisToken = tokens[index];
if (thisToken.kind === TokenKind.Minus) {
let nextToken = this.getNextNonWhitespaceToken(tokens, index);
let previousToken = this.getPreviousNonWhitespaceToken(tokens, index);
if (
nextToken &&
//next non-Whitespace token is a numeric literal
NumericLiteralTokenKinds.includes(nextToken.kind) &&
previousToken &&
TokensBeforeNegativeNumericLiteral.includes(previousToken.kind)
) {
return true;
}
}
return false;
}
/**
* Get the first token after the index that is NOT Whitespace. Returns undefined if stopAtNewLine===true and found a newline,
* or if we found the EOF token
*/
private getNextNonWhitespaceToken(tokens: Token[], index: number, stopAtNewLine = false) {
if (index < 0) {
return;
}
for (index += 1; index < tokens.length; index++) {
let token = tokens[index];
if (stopAtNewLine && token && token.kind === TokenKind.Newline) {
return;
}
if (token && token.kind !== TokenKind.Whitespace) {
return token;
}
}
}
/**
* Get the first token before the index that is NOT Whitespace
*/
private getPreviousNonWhitespaceToken(tokens: Token[], startIndex: number, stopAtNewline = false) {
for (let i = startIndex - 1; i > -1; i--) {
let token = tokens[i];
if (stopAtNewline && token.kind === TokenKind.Newline) {
return;
}
if (token && token.kind !== TokenKind.Whitespace) {
return tokens[i];
}
}
}
/**
* Remove all trailing Whitespace
*/
private formatTrailingWhiteSpace(
tokens: Token[],
options: FormattingOptions
) {
let nextLineStartTokenIndex = 0;
//the list of output tokens
let outputTokens: Token[] = [];
//set the loop to run for a max of double the number of tokens we found so we don't end up with an infinite loop
for (let outerLoopCounter = 0; outerLoopCounter <= tokens.length * 2; outerLoopCounter++) {
let lineObj = this.getLineTokens(nextLineStartTokenIndex, tokens);