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

rokucommunity / bslint / #769

07 Dec 2023 06:32PM CUT coverage: 91.908%. Remained the same
#769

push

TwitchBronBron
0.8.13

775 of 875 branches covered (0.0%)

Branch coverage included in aggregate %.

906 of 954 relevant lines covered (94.97%)

63.17 hits per line

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

90.63
/src/plugins/codeStyle/styleFixes.ts
1
import { BscFile, BsDiagnostic, FunctionExpression, GroupingExpression, IfStatement, isIfStatement, Position, Range, TokenKind, WhileStatement } from 'brighterscript';
1✔
2
import { ChangeEntry, comparePos, insertText, replaceText } from '../../textEdit';
1✔
3
import { CodeStyleError } from './diagnosticMessages';
1✔
4
import { platform } from 'process';
1✔
5

6
export function extractFixes(
1✔
7
    addFixes: (file: BscFile, changes: ChangeEntry) => void,
8
    diagnostics: BsDiagnostic[]
9
): BsDiagnostic[] {
10
    return diagnostics.filter(diagnostic => {
12✔
11
        const fix = getFixes(diagnostic);
41✔
12
        if (fix) {
41!
13
            addFixes(diagnostic.file, fix);
41✔
14
            return false;
41✔
15
        }
16
        return true;
×
17
    });
18
}
19

20
export function getFixes(diagnostic: BsDiagnostic): ChangeEntry {
1✔
21
    switch (diagnostic.code) {
41✔
22
        case CodeStyleError.FunctionKeywordExpected:
45!
23
            return replaceFunctionTokens(diagnostic, 'function');
3✔
24
        case CodeStyleError.SubKeywordExpected:
25
            return replaceFunctionTokens(diagnostic, 'sub');
3✔
26
        case CodeStyleError.InlineIfThenFound:
27
        case CodeStyleError.BlockIfThenFound:
28
            return removeThenToken(diagnostic);
4✔
29
        case CodeStyleError.InlineIfThenMissing:
30
        case CodeStyleError.BlockIfThenMissing:
31
            return addThenToken(diagnostic);
3✔
32
        case CodeStyleError.ConditionGroupFound:
33
            return removeConditionGroup(diagnostic);
5✔
34
        case CodeStyleError.ConditionGroupMissing:
35
            return addConditionGroup(diagnostic);
5✔
36
        case CodeStyleError.AACommaFound:
37
            return removeAAComma(diagnostic);
9✔
38
        case CodeStyleError.AACommaMissing:
39
            return addAAComma(diagnostic);
6✔
40
        case CodeStyleError.EolLastMissing:
41
            return addEolLast(diagnostic);
2✔
42
        case CodeStyleError.EolLastFound:
43
            return removeEolLast(diagnostic);
1✔
44
        default:
45
            return null;
×
46
    }
47
}
48

49
function addAAComma(diagnostic: BsDiagnostic) {
50
    const { range } = diagnostic;
6✔
51
    return {
6✔
52
        diagnostic,
53
        changes: [
54
            insertText(range.end, ',')
55
        ]
56
    };
57
}
58

59
function removeAAComma(diagnostic: BsDiagnostic) {
60
    const { range } = diagnostic;
9✔
61
    return {
9✔
62
        diagnostic,
63
        changes: [
64
            replaceText(range, '')
65
        ]
66
    };
67
}
68

69
function addConditionGroup(diagnostic: BsDiagnostic) {
70
    const stat: IfStatement | WhileStatement = diagnostic.data;
5✔
71
    const { start, end } = stat.condition.range;
5✔
72
    return {
5✔
73
        diagnostic,
74
        changes: [
75
            insertText(Position.create(start.line, start.character), '('),
76
            insertText(Position.create(end.line, end.character), ')')
77
        ]
78
    };
79
}
80

81
function removeConditionGroup(diagnostic: BsDiagnostic) {
82
    const stat: (IfStatement | WhileStatement) & { condition: GroupingExpression } = diagnostic.data;
5✔
83
    const { left, right } = stat.condition.tokens;
5✔
84
    const spaceBefore = left.leadingWhitespace?.length > 0 ? '' : ' ';
5!
85
    let spaceAfter = '';
5✔
86
    if (isIfStatement(stat)) {
5✔
87
        spaceAfter = stat.isInline ? ' ' : '';
3✔
88
        if (stat.tokens.then) {
3✔
89
            spaceAfter = stat.tokens.then.leadingWhitespace?.length > 0 ? '' : ' ';
2!
90
        }
91
    }
92
    return {
5✔
93
        diagnostic,
94
        changes: [
95
            replaceText(left.range, spaceBefore),
96
            replaceText(right.range, spaceAfter)
97
        ]
98
    };
99
}
100

101
function addThenToken(diagnostic: BsDiagnostic) {
102
    const stat: IfStatement = diagnostic.data;
3✔
103
    const { end } = stat.condition.range;
3✔
104
    // const { start } = stat.thenBranch.range; // TODO: use when Block range bug is fixed
105
    const start = stat.thenBranch.statements[0]?.range.start;
3!
106
    const space = stat.isInline && comparePos(end, start) === 0 ? ' ' : '';
3✔
107
    return {
3✔
108
        diagnostic,
109
        changes: [
110
            insertText(end, ` then${space}`)
111
        ]
112
    };
113
}
114

115
function removeThenToken(diagnostic: BsDiagnostic) {
116
    const stat: IfStatement = diagnostic.data;
4✔
117
    const { then } = stat.tokens;
4✔
118
    const { line, character } = then.range.start;
4✔
119
    const range = Range.create(
4✔
120
        line, character - (then.leadingWhitespace?.length || 0), line, character + then.text.length
17!
121
    );
122
    return {
4✔
123
        diagnostic,
124
        changes: [
125
            replaceText(range, '')
126
        ]
127
    };
128
}
129

130
function replaceFunctionTokens(diagnostic: BsDiagnostic, token: string) {
131
    const fun: FunctionExpression = diagnostic.data;
6✔
132
    const space = fun.end?.text.indexOf(' ') > 0 ? ' ' : '';
6!
133
    // sub/function keyword
134
    const keywordChanges = [
6✔
135
        replaceText(fun.functionType.range, token),
136
        replaceText(fun.end?.range, `end${space}${token}`)
18!
137
    ];
138
    // remove `as void` in case of `sub`
139
    const returnChanges = token === 'sub' && fun.returnTypeToken?.kind === TokenKind.Void ? [
6✔
140
        replaceText(Range.create(fun.rightParen.range.end, fun.returnTypeToken.range.end), '')
141
    ] : [];
142
    return {
6✔
143
        diagnostic,
144
        changes: [
145
            ...keywordChanges,
146
            ...returnChanges
147
        ]
148
    };
149
}
150

151
function addEolLast(diagnostic: BsDiagnostic): ChangeEntry {
152
    return {
2✔
153
        diagnostic,
154
        changes: [
155
            insertText(
156
                diagnostic.range.end,
157
                // In single line files, the `preferredEol` cannot be determined
158
                // e.g: `sub foo() end sub\EOF`
159
                diagnostic.data.preferredEol ?? (platform.toString() === 'win32' ? '\r\n' : '\n')
7!
160
            )
161
        ]
162
    };
163
}
164

165
function removeEolLast(diagnostic: BsDiagnostic): ChangeEntry {
166
    return {
1✔
167
        diagnostic,
168
        changes: [
169
            replaceText(diagnostic.range, '')
170
        ]
171
    };
172
}
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