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

rokucommunity / bsc-plugin-inline-annotation / #13

03 Oct 2024 01:53PM UTC coverage: 85.047% (-5.9%) from 90.909%
#13

push

web-flow
Merge 9407bca3a into 68ad87890

31 of 46 branches covered (67.39%)

Branch coverage included in aggregate %.

59 of 60 new or added lines in 4 files covered. (98.33%)

60 of 61 relevant lines covered (98.36%)

1.77 hits per line

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

76.36
/src/FileTranspilers.ts
1
import type { AstEditor, FunctionStatement, BrsFile, BscFile, Identifier, CallExpression, Expression, ReturnStatement, Statement, AssignmentStatement } from 'brighterscript';
2
import { isBrsFile, Parser, WalkMode, createVisitor, util as bscUtil, isExpressionStatement, GroupingExpression, createToken, TokenKind, isCommentStatement } from 'brighterscript';
1✔
3

4
import { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState';
1✔
5
import { SourceNode } from 'source-map';
1✔
6

7
export class FileTranspiler {
1✔
8
        public preprocess(file: BscFile, editor: AstEditor, annotatedFunctions: Map<string, FunctionStatement>) {
9
                if (isBrsFile(file)) {
1!
10
                        this.walkBscExpressions(file, editor, annotatedFunctions);
1✔
11
                }
12
        }
13
        private walkBscExpressions(file: BscFile, editor: AstEditor, annotatedFunctions: Map<string, FunctionStatement>) {
14
                if (isBrsFile(file)) {
1!
15
                        file.ast.walk(createVisitor({
1✔
16
                                CallExpression: (call) => {
17
                                        const parts = bscUtil.getAllDottedGetParts(call.callee);
1✔
18
                                        return (
1✔
19
                                                this.inlineCall(file, editor, call, parts ?? [], annotatedFunctions)
3!
20
                                        );
21
                                }
22
                        }), {
23
                                walkMode: WalkMode.visitAllRecursive,
24
                                editor: editor
25
                        });
26
                }
27
        }
28

29
        private inlineCall(file: BrsFile, editor: AstEditor, call: CallExpression, parts: Identifier[], annotatedFunctions: Map<string, FunctionStatement>) {
30
                const fullFunctionName = parts?.map(x => x.text).join('.').toLowerCase();
1!
31
                const annotatedFunction = annotatedFunctions.get(fullFunctionName);
1✔
32
                if (annotatedFunction) {
1!
33
                        const parameterMap = new Map<string, Expression>();
1✔
34

35
                        for (let index = 0; index < annotatedFunction.func.parameters.length; index++) {
1✔
36
                                const annotatedParameter = annotatedFunction.func.parameters[index];
2✔
37
                                const callArgument = call.args[index] ?? annotatedParameter?.defaultValue;
2!
38
                                parameterMap.set(annotatedParameter.name.text, callArgument);
2✔
39
                        }
40

41
                        const clonedStatement = this.cloneStatement<ReturnStatement>(annotatedFunction.func.body.statements.filter(x => !isCommentStatement(x))[0], file);
1✔
42
                        clonedStatement.walk(createVisitor({
1✔
43
                                VariableExpression: (variable, parent?, owner?, key?) => {
44
                                        const arg = parameterMap.get(variable.name.text);
2✔
45
                                        if (arg) {
2!
46
                                                return this.cloneExpression(arg, file);
2✔
47
                                        }
48
                                }
49
                        }), {
50
                                walkMode: WalkMode.visitAllRecursive,
51
                                editor: editor
52
                        });
53

54
                        if (isExpressionStatement(call.parent)) {
1!
NEW
55
                                return clonedStatement.value;
×
56
                        } else {
57
                                return new GroupingExpression({
1✔
58
                                        left: createToken(TokenKind.LeftParen),
59
                                        right: createToken(TokenKind.RightParen)
60
                                }, clonedStatement.value!);
61
                        }
62
                }
63
        }
64

65
        private cloneStatement<T>(statement: Statement, file: BrsFile) {
66
                return statement.clone() as T;
1✔
67
        }
68

69
        private cloneExpression<T = Expression>(expression: Expression, file: BrsFile) {
70
                const newCode = new SourceNode(null, null, null, [
2✔
71
                        (expression.transpile(new BrsTranspileState(file)) as any)
72
                ]).toString();
73
                return (Parser.parse(`__thing = ${newCode}`).ast.statements[0] as AssignmentStatement).value as T;
2✔
74
        }
75
}
76

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

© 2026 Coveralls, Inc