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

rokucommunity / bslint / #1039

03 Oct 2024 07:42PM CUT coverage: 91.231% (-0.5%) from 91.746%
#1039

Pull #96

TwitchBronBron
1.0.0-alpha.39
Pull Request #96: v1

927 of 1061 branches covered (87.37%)

Branch coverage included in aggregate %.

231 of 240 new or added lines in 12 files covered. (96.25%)

11 existing lines in 3 files now uncovered.

1008 of 1060 relevant lines covered (95.09%)

68.84 hits per line

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

91.91
/src/plugins/codeStyle/index.ts
1
import {
1✔
2
    BsDiagnostic,
3
    createVisitor,
4
    FunctionExpression,
5
    isBrsFile,
6
    isXmlFile,
7
    isGroupingExpression,
8
    TokenKind,
9
    WalkMode,
10
    CancellationTokenSource,
11
    DiagnosticSeverity,
12
    OnGetCodeActionsEvent,
13
    AALiteralExpression,
14
    BrsFile,
15
    CompilerPlugin,
16
    AfterFileValidateEvent,
17
    Expression,
18
    isVoidType,
19
    Statement,
20
    SymbolTypeFlag,
21
    XmlFile,
22
    AstNode,
23
    Token,
24
    isNamespaceStatement,
25
    util,
26
    isAnyReferenceType,
27
    ExtraSymbolData,
28
    OnScopeValidateEvent,
29
    InternalWalkMode,
30
    isCallableType
31
} from 'brighterscript';
32
import { RuleAAComma } from '../..';
33
import { addFixesToEvent } from '../../textEdit';
1✔
34
import { PluginContext } from '../../util';
35
import { createColorValidator } from '../../createColorValidator';
1✔
36
import { messages } from './diagnosticMessages';
1✔
37
import { extractFixes } from './styleFixes';
1✔
38
import { BsLintDiagnosticContext } from '../../Linter';
1✔
39
import { Location } from 'vscode-languageserver-types';
40

41
export default class CodeStyle implements CompilerPlugin {
1✔
42

43
    name: 'codeStyle';
44

45
    constructor(private lintContext: PluginContext) {
147✔
46
    }
47

48
    onGetCodeActions(event: OnGetCodeActionsEvent) {
UNCOV
49
        const addFixes = addFixesToEvent(event);
×
NEW
UNCOV
50
        extractFixes(event.file, addFixes, event.diagnostics);
×
51
    }
52

53
    validateXMLFile(file: XmlFile) {
54
        const diagnostics: BsDiagnostic[] = [];
2✔
55
        const { noArrayComponentFieldType, noAssocarrayComponentFieldType } = this.lintContext.severity;
2✔
56

57
        const validateArrayComponentFieldType = noArrayComponentFieldType !== DiagnosticSeverity.Hint;
2✔
58
        const validateAssocarrayComponentFieldType = noAssocarrayComponentFieldType !== DiagnosticSeverity.Hint;
2✔
59

60
        for (const field of file.parser?.ast?.componentElement?.interfaceElement?.fields ?? []) {
2!
61
            if (field.tokens.startTagName?.text?.toLowerCase() === 'field') {
6!
62
                const typeAttribute = field.getAttribute('type');
6✔
63

64
                const typeValue = typeAttribute?.tokens?.value?.text?.toLowerCase();
6!
65
                if (typeValue === 'array' && validateArrayComponentFieldType) {
6✔
66
                    diagnostics.push(
1✔
67
                        messages.noArrayFieldType(
68
                            typeAttribute?.tokens?.value?.location,
9!
69
                            noArrayComponentFieldType
70
                        )
71
                    );
72
                } else if (typeValue === 'assocarray' && validateAssocarrayComponentFieldType) {
5✔
73
                    diagnostics.push(
1✔
74
                        messages.noAssocarrayFieldType(
75
                            typeAttribute?.tokens?.value?.location,
9!
76
                            noAssocarrayComponentFieldType
77
                        )
78
                    );
79
                }
80
            }
81
        }
82

83
        return diagnostics;
2✔
84
    }
85

86
    validateBrsFile(file: BrsFile) {
87
        const diagnostics: (BsDiagnostic)[] = [];
64✔
88
        const { severity } = this.lintContext;
64✔
89
        const { inlineIfStyle, blockIfStyle, conditionStyle, noPrint, noTodo, noStop, aaCommaStyle, eolLast, colorFormat } = severity;
64✔
90
        const validatePrint = noPrint !== DiagnosticSeverity.Hint;
64✔
91
        const validateTodo = noTodo !== DiagnosticSeverity.Hint;
64✔
92
        const validateNoStop = noStop !== DiagnosticSeverity.Hint;
64✔
93
        const validateInlineIf = inlineIfStyle !== 'off';
64✔
94
        const validateColorFormat = (colorFormat === 'hash-hex' || colorFormat === 'quoted-numeric-hex' || colorFormat === 'never');
64✔
95
        const disallowInlineIf = inlineIfStyle === 'never';
64✔
96
        const requireInlineIfThen = inlineIfStyle === 'then';
64✔
97
        const validateBlockIf = blockIfStyle !== 'off';
64✔
98
        const requireBlockIfThen = blockIfStyle === 'then';
64✔
99
        const validateCondition = conditionStyle !== 'off';
64✔
100
        const requireConditionGroup = conditionStyle === 'group';
64✔
101
        const validateAAStyle = aaCommaStyle !== 'off';
64✔
102
        const validateEolLast = eolLast !== 'off';
64✔
103
        const disallowEolLast = eolLast === 'never';
64✔
104
        const validateColorStyle = validateColorFormat ? createColorValidator(severity) : undefined;
64✔
105

106
        // Check if the file is empty by going backwards from the last token,
107
        // meaning there are tokens other than `Eof` and `Newline`.
108
        const { tokens } = file.parser;
64✔
109
        let isFileEmpty = true;
64✔
110
        for (let i = tokens.length - 1; i >= 0; i--) {
64✔
111
            if (tokens[i].kind !== TokenKind.Eof &&
180✔
112
                tokens[i].kind !== TokenKind.Newline) {
113
                isFileEmpty = false;
63✔
114
                break;
63✔
115
            }
116
        }
117

118
        // Validate `eol-last` on non-empty files
119
        if (validateEolLast && !isFileEmpty) {
64✔
120
            const penultimateToken = tokens[tokens.length - 2];
43✔
121
            if (disallowEolLast) {
43✔
122
                if (penultimateToken?.kind === TokenKind.Newline) {
2!
123
                    diagnostics.push(messages.removeEolLast(penultimateToken.location));
2✔
124
                }
125
            } else if (penultimateToken?.kind !== TokenKind.Newline) {
41!
126
                // Set the preferredEol as the last newline.
127
                // The fix function will handle the case where preferredEol is undefined.
128
                // This could happen in valid single line files, like:
129
                // `sub foo() end sub\EOF`
130
                let preferredEol;
131
                for (let i = tokens.length - 1; i >= 0; i--) {
3✔
132
                    if (tokens[i].kind === TokenKind.Newline) {
38✔
133
                        preferredEol = tokens[i].text;
4✔
134
                    }
135
                }
136

137
                diagnostics.push(
3✔
138
                    messages.addEolLast(
139
                        penultimateToken.location,
140
                        preferredEol
141
                    )
142
                );
143
            }
144
        }
145

146
        file.ast.walk(createVisitor({
64✔
147
            // validate function style (`function` or `sub`)
148
            FunctionExpression: (func) => {
149
                this.validateFunctionStyle(func, diagnostics);
142✔
150
            },
151
            IfStatement: s => {
152
                const hasThenToken = !!s.tokens.then;
74✔
153
                if (!s.isInline && validateBlockIf) {
74✔
154
                    if (hasThenToken !== requireBlockIfThen) {
14✔
155
                        diagnostics.push(requireBlockIfThen
7✔
156
                            ? messages.addBlockIfThenKeyword(s)
157
                            : messages.removeBlockIfThenKeyword(s)
158
                        );
159
                    }
160
                } else if (s.isInline && validateInlineIf) {
60✔
161
                    if (disallowInlineIf) {
14✔
162
                        diagnostics.push(messages.inlineIfNotAllowed(s.location));
2✔
163
                    } else if (hasThenToken !== requireInlineIfThen) {
12✔
164
                        diagnostics.push(requireInlineIfThen
6✔
165
                            ? messages.addInlineIfThenKeyword(s)
166
                            : messages.removeInlineIfThenKeyword(s)
167
                        );
168
                    }
169
                }
170

171
                if (validateCondition) {
74✔
172
                    if (isGroupingExpression(s.condition) !== requireConditionGroup) {
22✔
173
                        diagnostics.push(requireConditionGroup
11✔
174
                            ? messages.addParenthesisAroundCondition(s)
175
                            : messages.removeParenthesisAroundCondition(s)
176
                        );
177
                    }
178
                }
179
            },
180
            WhileStatement: s => {
181
                if (validateCondition) {
12✔
182
                    if (isGroupingExpression(s.condition) !== requireConditionGroup) {
6✔
183
                        diagnostics.push(requireConditionGroup
3✔
184
                            ? messages.addParenthesisAroundCondition(s)
185
                            : messages.removeParenthesisAroundCondition(s)
186
                        );
187
                    }
188
                }
189
            },
190
            PrintStatement: s => {
191
                if (validatePrint) {
118✔
192
                    diagnostics.push(messages.noPrint(s.tokens.print.location, noPrint));
2✔
193
                }
194
            },
195
            LiteralExpression: e => {
196
                if (validateColorStyle && e.tokens.value.kind === TokenKind.StringLiteral) {
416✔
197
                    validateColorStyle(e.tokens.value.text, e.tokens.value.location, diagnostics);
63✔
198
                }
199
            },
200
            TemplateStringExpression: e => {
201
                // only validate template strings that look like regular strings (i.e. `0xAABBCC`)
202
                if (validateColorStyle && e.quasis.length === 1 && e.quasis[0].expressions.length === 1) {
11✔
203
                    validateColorStyle(e.quasis[0].expressions[0].tokens.value.text, e.quasis[0].expressions[0].tokens.value.location, diagnostics);
6✔
204
                }
205
            },
206
            AALiteralExpression: e => {
207
                if (validateAAStyle) {
33✔
208
                    this.validateAAStyle(e, aaCommaStyle, diagnostics);
30✔
209
                }
210
            },
211
            StopStatement: s => {
212
                if (validateNoStop) {
1!
213
                    diagnostics.push(messages.noStop(s.tokens.stop.location, noStop));
1✔
214
                }
215
            },
216
            AstNode: (node: Statement | Expression) => {
217
                const comments = [...node.leadingTrivia ?? [], ...node.endTrivia ?? []].filter(t => t.kind === TokenKind.Comment);
2,840!
218
                if (validateTodo && comments.length > 0) {
1,757✔
219
                    for (const e of comments) {
6✔
220
                        if (this.lintContext.todoPattern.test(e.text)) {
26✔
221
                            diagnostics.push(messages.noTodo(e.location, noTodo));
10✔
222
                        }
223
                    }
224
                }
225
            }
226
        }), { walkMode: WalkMode.visitAllRecursive });
227

228
        return diagnostics;
64✔
229
    }
230

231
    validateBrsFileInScope(file: BrsFile) {
232
        const diagnostics: (BsDiagnostic)[] = [];
63✔
233
        const { severity } = this.lintContext;
63✔
234
        const { nameShadowing } = severity;
63✔
235

236
        file.ast.walk(createVisitor({
63✔
237
            NamespaceStatement: (nsStmt) => {
238
                this.validateNameShadowing(file, nsStmt, nsStmt.getNameParts()?.[0], nameShadowing, diagnostics);
4!
239
            },
240
            ClassStatement: (classStmt) => {
241
                this.validateNameShadowing(file, classStmt, classStmt.tokens.name, nameShadowing, diagnostics);
7✔
242
            },
243
            InterfaceStatement: (ifaceStmt) => {
244
                this.validateNameShadowing(file, ifaceStmt, ifaceStmt.tokens.name, nameShadowing, diagnostics);
7✔
245
            },
246
            ConstStatement: (constStmt) => {
247
                this.validateNameShadowing(file, constStmt, constStmt.tokens.name, nameShadowing, diagnostics);
11✔
248
            },
249
            EnumStatement: (enumStmt) => {
250
                this.validateNameShadowing(file, enumStmt, enumStmt.tokens.name, nameShadowing, diagnostics);
7✔
251
            }
252
            // eslint-disable-next-line no-bitwise
253
        }), { walkMode: WalkMode.visitStatementsRecursive | InternalWalkMode.visitFalseConditionalCompilationBlocks });
254

255
        return diagnostics;
63✔
256
    }
257

258
    afterFileValidate(event: AfterFileValidateEvent) {
259
        const { file } = event;
65✔
260
        if (this.lintContext.ignores(file)) {
65!
UNCOV
261
            return;
×
262
        }
263

264
        const diagnostics: (BsDiagnostic)[] = [];
65✔
265
        if (isXmlFile(file)) {
65✔
266
            diagnostics.push(...this.validateXMLFile(file));
2✔
267
        } else if (isBrsFile(file)) {
63!
268
            diagnostics.push(...this.validateBrsFile(file));
63✔
269
        }
270

271
        // add file reference
272
        let bsDiagnostics: BsDiagnostic[] = diagnostics.map(diagnostic => ({
126✔
273
            ...diagnostic,
274
            file
275
        }));
276

277
        const { fix } = this.lintContext;
65✔
278

279
        // apply fix
280
        if (fix) {
65✔
281
            bsDiagnostics = extractFixes(event.file, this.lintContext.addFixes, bsDiagnostics);
12✔
282
        }
283

284
        // append diagnostics
285
        event.program.diagnostics.register(bsDiagnostics, BsLintDiagnosticContext);
65✔
286
    }
287

288
    onScopeValidate(event: OnScopeValidateEvent) {
289
        for (const file of event.scope.getOwnFiles()) {
64✔
290
            if (this.lintContext.ignores(file)) {
65!
NEW
291
                return;
×
292
            }
293

294
            const diagnostics: (BsDiagnostic)[] = [];
65✔
295
            if (isBrsFile(file)) {
65✔
296
                diagnostics.push(...this.validateBrsFileInScope(file));
63✔
297
            }
298

299
            // add file reference
300
            let bsDiagnostics: BsDiagnostic[] = diagnostics.map(diagnostic => ({
65✔
301
                ...diagnostic,
302
                file
303
            }));
304

305
            const { fix } = this.lintContext;
65✔
306

307
            // apply fix
308
            if (fix) {
65✔
309
                bsDiagnostics = extractFixes(file, this.lintContext.addFixes, bsDiagnostics);
12✔
310
            }
311

312
            // append diagnostics
313
            event.program.diagnostics.register(bsDiagnostics, BsLintDiagnosticContext);
65✔
314
        }
315
    }
316

317
    validateAAStyle(aa: AALiteralExpression, aaCommaStyle: RuleAAComma, diagnostics: (BsDiagnostic)[]) {
318
        const indexes = collectWrappingAAMembersIndexes(aa);
30✔
319
        const last = indexes.length - 1;
30✔
320
        const isSingleLine = (aa: AALiteralExpression): boolean => {
30✔
321
            return aa.tokens.open.location.range.start.line === aa.tokens.close.location.range.end.line;
10✔
322
        };
323

324
        indexes.forEach((index, i) => {
30✔
325
            const member = aa.elements[index];
66✔
326
            const hasComma = !!member.tokens.comma;
66✔
327
            if (aaCommaStyle === 'never' || (i === last && ((aaCommaStyle === 'no-dangling') || isSingleLine(aa)))) {
66✔
328
                if (hasComma) {
36✔
329
                    diagnostics.push(messages.removeAAComma(member.tokens.comma.location));
18✔
330
                }
331
            } else if (!hasComma) {
30✔
332
                diagnostics.push(messages.addAAComma(member.value.location));
12✔
333
            }
334
        });
335
    }
336

337
    validateFunctionStyle(fun: FunctionExpression, diagnostics: (BsDiagnostic)[]) {
338
        const { severity } = this.lintContext;
142✔
339
        const { namedFunctionStyle, anonFunctionStyle, typeAnnotations } = severity;
142✔
340
        const style = fun.parent ? namedFunctionStyle : anonFunctionStyle;
142!
341
        const kind = fun.tokens.functionType.kind;
142✔
342
        const hasReturnedValue = style === 'auto' || typeAnnotations !== 'off' ? this.getFunctionReturns(fun) : false;
142✔
343

344
        // type annotations
345
        if (typeAnnotations !== 'off') {
142✔
346
            if (typeAnnotations !== 'args') {
9✔
347
                if (hasReturnedValue && !fun.returnTypeExpression) {
6✔
348
                    diagnostics.push(messages.expectedReturnTypeAnnotation(
2✔
349
                        // add the error to the function keyword (or just highlight the whole function if that's somehow missing)
350
                        fun.tokens.functionType?.location ?? fun.location
12!
351
                    ));
352
                }
353
            }
354
            if (typeAnnotations !== 'return') {
9✔
355
                const missingAnnotation = fun.parameters.find(arg => !arg.typeExpression);
8✔
356
                if (missingAnnotation) {
6✔
357
                    // only report 1st missing arg annotation to avoid error overload
358
                    diagnostics.push(messages.expectedTypeAnnotation(missingAnnotation.location));
4✔
359
                }
360
            }
361
        }
362

363
        // keyword style
364
        if (style === 'off') {
142✔
365
            return;
51✔
366
        }
367
        if (style === 'no-function') {
91✔
368
            if (kind === TokenKind.Function) {
12✔
369
                diagnostics.push(messages.expectedSubKeyword(fun, `(always use 'sub')`));
6✔
370
            }
371
            return;
12✔
372
        }
373

374
        if (style === 'no-sub') {
79✔
375
            if (kind === TokenKind.Sub) {
11✔
376
                diagnostics.push(messages.expectedFunctionKeyword(fun, `(always use 'function')`));
5✔
377
            }
378
            return;
11✔
379
        }
380

381
        // auto
382
        if (hasReturnedValue) {
68✔
383
            if (kind !== TokenKind.Function) {
7✔
384
                diagnostics.push(messages.expectedFunctionKeyword(fun, `(use 'function' when a value is returned)`));
2✔
385
            }
386
        } else if (kind !== TokenKind.Sub) {
61✔
387
            diagnostics.push(messages.expectedSubKeyword(fun, `(use 'sub' when no value is returned)`));
4✔
388
        }
389
    }
390

391
    getFunctionReturns(fun: FunctionExpression) {
392
        let hasReturnedValue = false;
77✔
393
        if (fun.returnTypeExpression) {
77✔
394
            hasReturnedValue = !isVoidType(fun.returnTypeExpression.getType({ flags: SymbolTypeFlag.typetime }));
7✔
395
        } else {
396
            const cancel = new CancellationTokenSource();
70✔
397
            fun.body.walk(createVisitor({
70✔
398
                ReturnStatement: s => {
399
                    hasReturnedValue = !!s.value;
8✔
400
                    cancel.cancel();
8✔
401
                }
402
            }), { walkMode: WalkMode.visitStatements, cancel: cancel.token });
403
        }
404
        return hasReturnedValue;
77✔
405
    }
406

407
    validateNameShadowing(file: BrsFile, node: AstNode, nameIdentifier: Token, severity: DiagnosticSeverity, diagnostics: (BsDiagnostic)[]) {
408
        const name = nameIdentifier?.text;
36!
409
        if (!name || !node) {
36!
NEW
UNCOV
410
            return;
×
411
        }
412
        const nameLocation = nameIdentifier.location;
36✔
413

414
        const astTable = file.ast.getSymbolTable();
36✔
415
        const data = {} as ExtraSymbolData;
36✔
416
        const typeChain = [];
36✔
417
        // eslint-disable-next-line no-bitwise
418
        const existingType = astTable.getSymbolType(name, { flags: SymbolTypeFlag.runtime | SymbolTypeFlag.typetime, data: data, typeChain: typeChain });
36✔
419

420
        if (!existingType || isAnyReferenceType(existingType)) {
36!
NEW
UNCOV
421
            return;
×
422
        }
423
        if ((data.definingNode === node) || (isNamespaceStatement(data.definingNode) && isNamespaceStatement(node))) {
36✔
424
            return;
20✔
425
        }
426
        const otherNode = data.definingNode as unknown as { tokens: { name: Token }; location: Location };
16✔
427
        const thisNodeKindName = util.getAstNodeFriendlyName(node);
16✔
428
        let thatNodeKindName = util.getAstNodeFriendlyName(data.definingNode) ?? '';
16✔
429
        if (!thatNodeKindName && isCallableType(existingType)) {
16✔
430
            thatNodeKindName = 'Global Function';
1✔
431
        }
432

433
        let thatNameLocation = otherNode?.tokens?.name?.location ?? otherNode?.location;
16✔
434

435
        if (isNamespaceStatement(data.definingNode)) {
16✔
436
            thatNameLocation = data.definingNode.getNameParts()?.[0]?.location;
2!
437
        }
438

439
        const relatedInformation = thatNameLocation ? [{
16✔
440
            message: `${thatNodeKindName} declared here`,
441
            location: thatNameLocation
442
        }] : undefined;
443

444
        diagnostics.push({
16✔
445
            ...messages.nameShadowing(thisNodeKindName, thatNodeKindName, name, severity),
446
            location: nameLocation,
447
            relatedInformation: relatedInformation
448
        });
449
    }
450
}
451

452
/**
453
 * Collect indexes of non-inline AA members
454
 */
455
export function collectWrappingAAMembersIndexes(aa: AALiteralExpression): number[] {
1✔
456
    const indexes: number[] = [];
38✔
457
    const { elements } = aa;
38✔
458
    const lastIndex = elements.length - 1;
38✔
459
    for (let i = 0; i < lastIndex; i++) {
38✔
460
        const e = elements[i];
65✔
461

462
        const ne = elements[i + 1];
65✔
463
        const hasNL = ne.location.range.start.line > e.location.range.end.line;
65✔
464
        if (hasNL) {
65✔
465
            indexes.push(i);
39✔
466
        }
467
    }
468
    const last = elements[lastIndex];
38✔
469
    if (last) {
38✔
470
        indexes.push(lastIndex);
37✔
471
    }
472
    return indexes;
38✔
473
}
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