• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rokucommunity / brighterscript-formatter / #856

07 Mar 2024 08:04PM UTC coverage: 100.0%. Remained the same
#856

push

TwitchBronBron
1.7.0

500 of 500 branches covered (100.0%)

Branch coverage included in aggregate %.

597 of 597 relevant lines covered (100.0%)

994.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/formatters/MultiLineItemFormatter.ts
1
import type { Token } from 'brighterscript';
2
import { TokenKind } from 'brighterscript';
1✔
3
import type { TokenWithStartIndex } from '../constants';
4
import { util } from '../util';
1✔
5

6
export class MultiLineItemFormatter {
1✔
7
    /**
8
     * Handle indentation for an array of tokens
9
     */
10
    public format(tokens: Token[]) {
11
        for (let i = 0; i < tokens.length; i++) {
264✔
12
            let token = tokens[i];
5,315✔
13
            let openKind: TokenKind | undefined;
14
            let closeKind: TokenKind | undefined;
15

16
            if (token.kind === TokenKind.LeftCurlyBrace) {
5,315✔
17
                openKind = TokenKind.LeftCurlyBrace;
40✔
18
                closeKind = TokenKind.RightCurlyBrace;
40✔
19
            } else if (token.kind === TokenKind.LeftSquareBracket) {
5,275✔
20
                openKind = TokenKind.LeftSquareBracket;
47✔
21
                closeKind = TokenKind.RightSquareBracket;
47✔
22
            }
23

24
            let nextNonWhitespaceToken = util.getNextNonWhitespaceToken(tokens, i, true);
5,315✔
25
            //move contents to new line if this is a multi-line array or AA
26
            if (
5,315✔
27
                //is open curly or open square
28
                openKind && closeKind &&
5,568✔
29
                //is a multi-line array or AA
30
                !this.isStartofSingleLineArrayOrAA(tokens, i, openKind, closeKind) &&
31
                //there is extra stuff on this line that is not the end of the file
32
                nextNonWhitespaceToken && nextNonWhitespaceToken.kind !== TokenKind.Eof &&
33
                //is NOT array like `[[ ...\n ]]`, or `[{ ...\n }]`)
34
                !this.isMatchingDoubleArrayOrArrayCurly(tokens, i)
35
            ) {
36
                tokens.splice(i + 1, 0, {
16✔
37
                    kind: TokenKind.Newline,
38
                    text: '\n'
39
                } as TokenWithStartIndex);
40
                let closingToken = util.getClosingToken(tokens, i, openKind, closeKind);
16✔
41
                /* istanbul ignore next */
42
                let closingTokenKindex = closingToken ? tokens.indexOf(closingToken) : -1;
43

44
                i++;
16✔
45

46
                //if there's stuff before the closer, move it to a newline
47
                if (util.getPreviousNonWhitespaceToken(tokens, closingTokenKindex, true)) {
16✔
48
                    tokens.splice(closingTokenKindex, 0, {
10✔
49
                        kind: TokenKind.Newline,
50
                        text: '\n'
51
                    } as TokenWithStartIndex);
52
                }
53
            }
54
        }
55
        return tokens;
264✔
56
    }
57

58
    /**
59
     * Determines if the current index is the start of a single-line array or AA.
60
     * Walks forward until we find the equal number of open and close curlies/squares, or a newline
61
     */
62
    public isStartofSingleLineArrayOrAA(tokens: Token[], currentIndex: number, openKind: TokenKind, closeKind: TokenKind) {
63
        let openCount = 0;
87✔
64
        for (let i = currentIndex; i < tokens.length; i++) {
87✔
65
            let token = tokens[i];
613✔
66
            if (token.kind === openKind) {
613✔
67
                openCount++;
104✔
68
            } else if (token.kind === closeKind) {
509✔
69
                openCount--;
63✔
70
            }
71
            if (openCount === 0) {
613✔
72
                return true;
49✔
73
            } else if (token.kind === TokenKind.Newline) {
564✔
74
                return false;
37✔
75
            }
76
        }
77
        return false;
1✔
78
    }
79

80
    public isMatchingDoubleArrayOrArrayCurly(tokens: Token[], currentIndex: number) {
81
        let token = tokens[currentIndex];
20✔
82
        let nextNonWhitespaceToken = util.getNextNonWhitespaceToken(tokens, currentIndex, true);
20✔
83
        //don't separate multiple open/close pairs
84
        if (
20✔
85
            //is open array
86
            token.kind === TokenKind.LeftSquareBracket &&
52✔
87
            //there is another token on this line
88
            nextNonWhitespaceToken &&
89
            //is next token an open array or open object
90
            (nextNonWhitespaceToken.kind === TokenKind.LeftSquareBracket || nextNonWhitespaceToken.kind === TokenKind.LeftCurlyBrace)
91
        ) {
92
            let closingToken = util.getClosingToken(tokens, currentIndex, TokenKind.LeftSquareBracket, TokenKind.RightSquareBracket);
4✔
93
            //look at the previous token
94
            let previous = closingToken && util.getPreviousNonWhitespaceToken(tokens, tokens.indexOf(closingToken), true);
4✔
95
            /* istanbul ignore else (because I can't figure out how to make this happen but I think it's still necessary) */
96
            if (previous && (previous.kind === TokenKind.RightSquareBracket || previous.kind === TokenKind.RightCurlyBrace)) {
4✔
97
                return true;
4✔
98
            }
99
        }
100
    }
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc