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

rokucommunity / brighterscript / #12717

14 Jun 2024 08:20PM UTC coverage: 85.629% (-2.3%) from 87.936%
#12717

push

web-flow
Merge 94311dc0a into 42db50190

10808 of 13500 branches covered (80.06%)

Branch coverage included in aggregate %.

6557 of 7163 new or added lines in 96 files covered. (91.54%)

83 existing lines in 17 files now uncovered.

12270 of 13451 relevant lines covered (91.22%)

26531.66 hits per line

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

85.14
/src/parser/Statement.ts
1
/* eslint-disable no-bitwise */
2
import type { Token, Identifier } from '../lexer/Token';
3
import { TokenKind } from '../lexer/TokenKind';
1✔
4
import type { DottedGetExpression, FunctionExpression, FunctionParameterExpression, LiteralExpression, TypeExpression, TypecastExpression } from './Expression';
5
import { CallExpression, VariableExpression } from './Expression';
1✔
6
import { util } from '../util';
1✔
7
import type { Location } from 'vscode-languageserver';
8
import type { BrsTranspileState } from './BrsTranspileState';
9
import { ParseMode } from './Parser';
1✔
10
import type { WalkVisitor, WalkOptions } from '../astUtils/visitors';
11
import { InternalWalkMode, walk, createVisitor, WalkMode, walkArray } from '../astUtils/visitors';
1✔
12
import { isCallExpression, isCatchStatement, isConditionalCompileStatement, isEnumMemberStatement, isExpression, isExpressionStatement, isFieldStatement, isForEachStatement, isForStatement, isFunctionExpression, isFunctionStatement, isIfStatement, isInterfaceFieldStatement, isInterfaceMethodStatement, isInvalidType, isLiteralExpression, isMethodStatement, isNamespaceStatement, isTryCatchStatement, isTypedefProvider, isUnaryExpression, isVoidType, isWhileStatement } from '../astUtils/reflection';
1✔
13
import { TypeChainEntry, type GetTypeOptions, type TranspileResult, type TypedefProvider } from '../interfaces';
1✔
14
import { createInvalidLiteral, createMethodStatement, createToken } from '../astUtils/creators';
1✔
15
import { DynamicType } from '../types/DynamicType';
1✔
16
import type { BscType } from '../types/BscType';
17
import { SymbolTable } from '../SymbolTable';
1✔
18
import type { Expression } from './AstNode';
19
import { AstNodeKind } from './AstNode';
1✔
20
import { Statement } from './AstNode';
1✔
21
import { ClassType } from '../types/ClassType';
1✔
22
import { EnumMemberType, EnumType } from '../types/EnumType';
1✔
23
import { NamespaceType } from '../types/NamespaceType';
1✔
24
import { InterfaceType } from '../types/InterfaceType';
1✔
25
import { VoidType } from '../types/VoidType';
1✔
26
import { TypedFunctionType } from '../types/TypedFunctionType';
1✔
27
import { ArrayType } from '../types/ArrayType';
1✔
28
import { SymbolTypeFlag } from '../SymbolTypeFlag';
1✔
29

30
export class EmptyStatement extends Statement {
1✔
31
    constructor(options?: { range?: Location }
32
    ) {
33
        super();
4✔
34
        this.location = undefined;
4✔
35
    }
36
    /**
37
     * Create a negative range to indicate this is an interpolated location
38
     */
39
    public readonly location?: Location;
40

41
    public readonly kind = AstNodeKind.EmptyStatement;
4✔
42

43
    transpile(state: BrsTranspileState) {
44
        return [];
2✔
45
    }
46
    walk(visitor: WalkVisitor, options: WalkOptions) {
47
        //nothing to walk
48
    }
49
}
50

51
/**
52
 * This is a top-level statement. Consider this the root of the AST
53
 */
54
export class Body extends Statement implements TypedefProvider {
1✔
55
    constructor(options?: {
56
        statements?: Statement[];
57
    }) {
58
        super();
6,502✔
59
        this.statements = options?.statements ?? [];
6,502!
60
    }
61

62
    public readonly statements: Statement[] = [];
6,502✔
63
    public readonly kind = AstNodeKind.Body;
6,502✔
64

65
    public readonly symbolTable = new SymbolTable('Body', () => this.parent?.getSymbolTable());
6,502✔
66

67
    public get location() {
68
        //this needs to be a getter because the body has its statements pushed to it after being constructed
69
        return util.createBoundingLocation(
675✔
70
            ...(this.statements ?? [])
2,025!
71
        );
72
    }
73

74
    transpile(state: BrsTranspileState) {
75
        let result = [] as TranspileResult;
615✔
76
        for (let i = 0; i < this.statements.length; i++) {
615✔
77
            let statement = this.statements[i];
1,360✔
78
            let previousStatement = this.statements[i - 1];
1,360✔
79
            let nextStatement = this.statements[i + 1];
1,360✔
80

81
            if (!previousStatement) {
1,360✔
82
                //this is the first statement. do nothing related to spacing and newlines
83

84
                //if comment is on same line as prior sibling
85
            } else if (util.hasLeadingComments(statement) && previousStatement && util.getLeadingComments(statement)?.[0]?.location?.range?.start.line === previousStatement.location?.range?.end.line) {
749!
86
                result.push(
8✔
87
                    ' '
88
                );
89
                //add double newline if this is a comment, and next is a function
90
            } else if (util.hasLeadingComments(statement) && nextStatement && isFunctionStatement(nextStatement)) {
741✔
91
                result.push(state.newline, state.newline);
301✔
92

93
                //add double newline if is function not preceeded by a comment
94
            } else if (isFunctionStatement(statement) && previousStatement && !util.hasLeadingComments(statement)) {
440✔
95
                result.push(state.newline, state.newline);
67✔
96
            } else {
97
                //separate statements by a single newline
98
                result.push(state.newline);
373✔
99
            }
100

101
            result.push(...statement.transpile(state));
1,360✔
102
        }
103
        return result;
615✔
104
    }
105

106
    getTypedef(state: BrsTranspileState): TranspileResult {
107
        let result = [] as TranspileResult;
41✔
108
        for (const statement of this.statements) {
41✔
109
            //if the current statement supports generating typedef, call it
110
            if (isTypedefProvider(statement)) {
75!
111
                result.push(
75✔
112
                    state.indent(),
113
                    ...statement.getTypedef(state),
114
                    state.newline
115
                );
116
            }
117
        }
118
        return result;
41✔
119
    }
120

121
    walk(visitor: WalkVisitor, options: WalkOptions) {
122
        if (options.walkMode & InternalWalkMode.walkStatements) {
13,304!
123
            walkArray(this.statements, visitor, options, this);
13,304✔
124
        }
125
    }
126
}
127

128
export class AssignmentStatement extends Statement {
1✔
129
    constructor(options: {
130
        name: Identifier;
131
        equals?: Token;
132
        value: Expression;
133
        as?: Token;
134
        typeExpression?: TypeExpression;
135
    }) {
136
        super();
1,370✔
137
        this.value = options.value;
1,370✔
138
        this.tokens = {
1,370✔
139
            equals: options.equals,
140
            name: options.name,
141
            as: options.as
142
        };
143
        this.typeExpression = options.typeExpression;
1,370✔
144
        this.location = util.createBoundingLocation(util.createBoundingLocationFromTokens(this.tokens), this.value);
1,370✔
145
    }
146

147
    public readonly tokens: {
148
        readonly equals?: Token;
149
        readonly name: Identifier;
150
        readonly as?: Token;
151
    };
152

153
    public readonly value: Expression;
154

155
    public readonly typeExpression?: TypeExpression;
156

157
    public readonly kind = AstNodeKind.AssignmentStatement;
1,370✔
158

159
    public readonly location: Location | undefined;
160

161
    transpile(state: BrsTranspileState) {
162
        return [
525✔
163
            state.transpileToken(this.tokens.name),
164
            ' ',
165
            state.transpileToken(this.tokens.equals ?? createToken(TokenKind.Equal)),
1,575!
166
            ' ',
167
            ...this.value.transpile(state)
168
        ];
169
    }
170

171
    walk(visitor: WalkVisitor, options: WalkOptions) {
172
        if (options.walkMode & InternalWalkMode.walkExpressions) {
5,320✔
173
            walk(this, 'typeExpression', visitor, options);
5,195✔
174
            walk(this, 'value', visitor, options);
5,195✔
175
        }
176
    }
177

178
    getType(options: GetTypeOptions) {
179
        const variableType = this.typeExpression?.getType({ ...options, typeChain: undefined }) ?? this.value.getType({ ...options, typeChain: undefined });
1,181✔
180

181
        // Note: compound assignments (eg. +=) are internally dealt with via the RHS being a BinaryExpression
182
        // so this.value will be a BinaryExpression, and BinaryExpressions can figure out their own types
183
        options.typeChain?.push(new TypeChainEntry({ name: this.tokens.name.text, type: variableType, data: options.data, location: this.tokens.name?.location, astNode: this }));
1,181!
184
        return variableType;
1,181✔
185
    }
186

187
    get leadingTrivia(): Token[] {
188
        return this.tokens.name.leadingTrivia;
1,062✔
189
    }
190
}
191

192
export class AugmentedAssignmentStatement extends Statement {
1✔
193
    constructor(options: {
194
        item: Expression;
195
        operator: Token;
196
        value: Expression;
197
    }) {
198
        super();
78✔
199
        this.value = options.value;
78✔
200
        this.tokens = {
78✔
201
            operator: options.operator
202
        };
203
        this.item = options.item;
78✔
204
        this.value = options.value;
78✔
205
        this.location = util.createBoundingLocation(this.item, util.createBoundingLocationFromTokens(this.tokens), this.value);
78✔
206
    }
207

208
    public readonly tokens: {
209
        readonly operator?: Token;
210
    };
211

212
    public readonly item: Expression;
213

214
    public readonly value: Expression;
215

216
    public readonly kind = AstNodeKind.AugmentedAssignmentStatement;
78✔
217

218
    public readonly location: Location | undefined;
219

220
    transpile(state: BrsTranspileState) {
221
        return [
27✔
222
            this.item.transpile(state),
223
            ' ',
224
            state.transpileToken(this.tokens.operator),
225
            ' ',
226
            this.value.transpile(state)
227
        ];
228
    }
229

230
    walk(visitor: WalkVisitor, options: WalkOptions) {
231
        if (options.walkMode & InternalWalkMode.walkExpressions) {
330✔
232
            walk(this, 'item', visitor, options);
322✔
233
            walk(this, 'value', visitor, options);
322✔
234
        }
235
    }
236

237
    getType(options: GetTypeOptions) {
NEW
238
        const variableType = util.binaryOperatorResultType(this.item.getType(options), this.tokens.operator, this.value.getType(options));
×
239

240
        //const variableType = this.typeExpression?.getType({ ...options, typeChain: undefined }) ?? this.value.getType({ ...options, typeChain: undefined });
241

242
        // Note: compound assignments (eg. +=) are internally dealt with via the RHS being a BinaryExpression
243
        // so this.value will be a BinaryExpression, and BinaryExpressions can figure out their own types
244
        // options.typeChain?.push(new TypeChainEntry({ name: this.tokens.name.text, type: variableType, data: options.data, range: this.tokens.name.range, astNode: this }));
NEW
245
        return variableType;
×
246
    }
247

248
    get leadingTrivia(): Token[] {
249
        return this.item.leadingTrivia;
54✔
250
    }
251
}
252

253
export class Block extends Statement {
1✔
254
    constructor(options: {
255
        statements: Statement[];
256
    }) {
257
        super();
5,847✔
258
        this.statements = options.statements;
5,847✔
259
    }
260

261
    public readonly statements: Statement[];
262

263
    public readonly kind = AstNodeKind.Block;
5,847✔
264

265
    get location(): Location {
266
        if (this.statements.length > 0) {
3,668✔
267
            return util.createBoundingLocation(...this.statements);
3,206✔
268
        }
269
        let lastBitBefore: Location;
270
        let firstBitAfter: Location;
271

272
        if (isFunctionExpression(this.parent)) {
462✔
273
            lastBitBefore = util.createBoundingLocation(
392✔
274
                this.parent.tokens.functionType,
275
                this.parent.tokens.leftParen,
276
                ...(this.parent.parameters ?? []),
1,176!
277
                this.parent.tokens.rightParen,
278
                this.parent.tokens.as,
279
                this.parent.returnTypeExpression
280
            );
281
            firstBitAfter = this.parent.tokens.endFunctionType?.location;
392!
282
        } else if (isIfStatement(this.parent)) {
70!
NEW
283
            if (this.parent.thenBranch === this) {
×
NEW
284
                lastBitBefore = util.createBoundingLocation(
×
285
                    this.parent.tokens.then,
286
                    this.parent.condition
287
                );
NEW
288
                firstBitAfter = util.createBoundingLocation(
×
289
                    this.parent.tokens.else,
290
                    this.parent.elseBranch,
291
                    this.parent.tokens.endIf
292
                );
NEW
293
            } else if (this.parent.elseBranch === this) {
×
NEW
294
                lastBitBefore = this.parent.tokens.else?.location;
×
NEW
295
                firstBitAfter = this.parent.tokens.endIf?.location;
×
296
            }
297
        } else if (isConditionalCompileStatement(this.parent)) {
70!
NEW
298
            if (this.parent.thenBranch === this) {
×
NEW
299
                lastBitBefore = util.createBoundingLocation(
×
300
                    this.parent.tokens.condition,
301
                    this.parent.tokens.not,
302
                    this.parent.tokens.hashIf
303
                );
NEW
304
                firstBitAfter = util.createBoundingLocation(
×
305
                    this.parent.tokens.hashElse,
306
                    this.parent.elseBranch,
307
                    this.parent.tokens.hashEndIf
308
                );
NEW
309
            } else if (this.parent.elseBranch === this) {
×
NEW
310
                lastBitBefore = this.parent.tokens.hashElse?.location;
×
NEW
311
                firstBitAfter = this.parent.tokens.hashEndIf?.location;
×
312
            }
313
        } else if (isForStatement(this.parent)) {
70✔
314
            lastBitBefore = util.createBoundingLocation(
2✔
315
                this.parent.increment,
316
                this.parent.tokens.step,
317
                this.parent.finalValue,
318
                this.parent.tokens.to,
319
                this.parent.counterDeclaration,
320
                this.parent.tokens.for
321
            );
322
            firstBitAfter = this.parent.tokens.endFor?.location;
2!
323
        } else if (isForEachStatement(this.parent)) {
68✔
324
            lastBitBefore = util.createBoundingLocation(
2✔
325
                this.parent.target,
326
                this.parent.tokens.in,
327
                this.parent.tokens.item,
328
                this.parent.tokens.forEach
329
            );
330
            firstBitAfter = this.parent.tokens.endFor?.location;
2!
331
        } else if (isWhileStatement(this.parent)) {
66!
NEW
332
            lastBitBefore = util.createBoundingLocation(
×
333
                this.parent.condition,
334
                this.parent.tokens.while
335
            );
NEW
336
            firstBitAfter = this.parent.tokens.endWhile?.location;
×
337
        } else if (isTryCatchStatement(this.parent)) {
66!
NEW
338
            lastBitBefore = util.createBoundingLocation(
×
339
                this.parent.tokens.try
340
            );
NEW
341
            firstBitAfter = util.createBoundingLocation(
×
342
                this.parent.tokens.endTry,
343
                this.parent.catchStatement
344
            );
345
        } else if (isCatchStatement(this.parent) && isTryCatchStatement(this.parent?.parent)) {
66!
NEW
346
            lastBitBefore = util.createBoundingLocation(
×
347
                this.parent.tokens.catch,
348
                this.parent.tokens.exceptionVariable
349
            );
NEW
350
            firstBitAfter = this.parent.parent.tokens.endTry?.location;
×
351
        }
352
        if (lastBitBefore?.range && firstBitAfter?.range) {
462!
353
            return util.createLocation(
372✔
354
                lastBitBefore.range.end.line,
355
                lastBitBefore.range.end.character,
356
                firstBitAfter.range.start.line,
357
                firstBitAfter.range.start.character,
358
                lastBitBefore.uri ?? firstBitAfter.uri
1,116!
359
            );
360
        }
361
    }
362

363
    transpile(state: BrsTranspileState) {
364
        state.blockDepth++;
3,754✔
365
        let results = [] as TranspileResult;
3,754✔
366
        for (let i = 0; i < this.statements.length; i++) {
3,754✔
367
            let previousStatement = this.statements[i - 1];
4,554✔
368
            let statement = this.statements[i];
4,554✔
369
            //is not a comment
370
            //if comment is on same line as parent
371
            if (util.isLeadingCommentOnSameLine(state.lineage[0]?.location, statement) ||
4,554!
372
                util.isLeadingCommentOnSameLine(previousStatement?.location, statement)
13,614✔
373
            ) {
374
                results.push(' ');
50✔
375

376
                //is not a comment
377
            } else {
378
                //add a newline and indent
379
                results.push(
4,504✔
380
                    state.newline,
381
                    state.indent()
382
                );
383
            }
384

385
            //push block onto parent list
386
            state.lineage.unshift(this);
4,554✔
387
            results.push(
4,554✔
388
                ...statement.transpile(state)
389
            );
390
            state.lineage.shift();
4,554✔
391
        }
392
        state.blockDepth--;
3,754✔
393
        return results;
3,754✔
394
    }
395

396
    public get leadingTrivia(): Token[] {
397
        return this.statements[0]?.leadingTrivia ?? [];
12!
398
    }
399

400
    walk(visitor: WalkVisitor, options: WalkOptions) {
401
        if (options.walkMode & InternalWalkMode.walkStatements) {
22,953✔
402
            walkArray(this.statements, visitor, options, this);
22,947✔
403
        }
404
    }
405

406
}
407

408
export class ExpressionStatement extends Statement {
1✔
409
    constructor(options: {
410
        expression: Expression;
411
    }) {
412
        super();
530✔
413
        this.expression = options.expression;
530✔
414
        this.location = this.expression.location;
530✔
415
    }
416
    public readonly expression: Expression;
417
    public readonly kind = AstNodeKind.ExpressionStatement;
530✔
418

419
    public readonly location: Location | undefined;
420

421
    transpile(state: BrsTranspileState) {
422
        return this.expression.transpile(state);
53✔
423
    }
424

425
    walk(visitor: WalkVisitor, options: WalkOptions) {
426
        if (options.walkMode & InternalWalkMode.walkExpressions) {
2,770✔
427
            walk(this, 'expression', visitor, options);
2,742✔
428
        }
429
    }
430

431
    get leadingTrivia(): Token[] {
432
        return this.expression.leadingTrivia;
110✔
433
    }
434
}
435

436

437
export class ExitForStatement extends Statement {
1✔
438
    constructor(options?: {
439
        exitFor?: Token;
440
    }) {
441
        super();
5✔
442
        this.tokens = {
5✔
443
            exitFor: options?.exitFor
15!
444
        };
445
        this.location = this.tokens.exitFor?.location;
5!
446
    }
447

448
    public readonly tokens: {
449
        readonly exitFor?: Token;
450
    };
451

452
    public readonly kind = AstNodeKind.ExitForStatement;
5✔
453

454
    public readonly location?: Location;
455

456
    transpile(state: BrsTranspileState) {
457
        return this.tokens.exitFor ? state.transpileToken(this.tokens.exitFor) : ['exit for'];
2!
458
    }
459

460
    walk(visitor: WalkVisitor, options: WalkOptions) {
461
        //nothing to walk
462
    }
463

464
    get leadingTrivia(): Token[] {
465
        return this.tokens.exitFor?.leadingTrivia;
8!
466
    }
467

468
}
469

470
export class ExitWhileStatement extends Statement {
1✔
471
    constructor(options?: {
472
        exitWhile?: Token;
473
    }) {
474
        super();
8✔
475
        this.tokens = {
8✔
476
            exitWhile: options?.exitWhile
24!
477
        };
478
        this.location = this.tokens.exitWhile?.location;
8!
479
    }
480

481
    public readonly tokens: {
482
        readonly exitWhile?: Token;
483
    };
484

485
    public readonly kind = AstNodeKind.ExitWhileStatement;
8✔
486

487
    public readonly location?: Location;
488

489
    transpile(state: BrsTranspileState) {
490
        return this.tokens.exitWhile ? state.transpileToken(this.tokens.exitWhile) : ['exit while'];
3!
491
    }
492

493
    walk(visitor: WalkVisitor, options: WalkOptions) {
494
        //nothing to walk
495
    }
496

497
    get leadingTrivia(): Token[] {
498
        return this.tokens.exitWhile?.leadingTrivia;
10!
499
    }
500
}
501

502
export class FunctionStatement extends Statement implements TypedefProvider {
1✔
503
    constructor(options: {
504
        name: Identifier;
505
        func: FunctionExpression;
506
    }) {
507
        super();
3,510✔
508
        this.tokens = {
3,510✔
509
            name: options.name
510
        };
511
        this.func = options.func;
3,510✔
512
        this.func.symbolTable.name += `: '${this.tokens.name?.text}'`;
3,510!
513
        this.func.functionStatement = this;
3,510✔
514

515
        this.location = this.func.location;
3,510✔
516
    }
517

518
    public readonly tokens: {
519
        readonly name: Identifier;
520
    };
521
    public readonly func: FunctionExpression;
522

523
    public readonly kind = AstNodeKind.FunctionStatement as AstNodeKind;
3,510✔
524

525
    public readonly location: Location | undefined;
526

527
    /**
528
     * Get the name of this expression based on the parse mode
529
     */
530
    public getName(parseMode: ParseMode) {
531
        const namespace = this.findAncestor<NamespaceStatement>(isNamespaceStatement);
11,073✔
532
        if (namespace) {
11,073✔
533
            let delimiter = parseMode === ParseMode.BrighterScript ? '.' : '_';
2,659✔
534
            let namespaceName = namespace.getName(parseMode);
2,659✔
535
            return namespaceName + delimiter + this.tokens.name?.text;
2,659!
536
        } else {
537
            return this.tokens.name.text;
8,414✔
538
        }
539
    }
540

541
    public get leadingTrivia(): Token[] {
542
        return this.func.leadingTrivia;
2,507✔
543
    }
544

545
    transpile(state: BrsTranspileState) {
546
        //create a fake token using the full transpiled name
547
        let nameToken = {
1,188✔
548
            ...this.tokens.name,
549
            text: this.getName(ParseMode.BrightScript)
550
        };
551

552
        return this.func.transpile(state, nameToken);
1,188✔
553
    }
554

555
    getTypedef(state: BrsTranspileState) {
556
        let result: TranspileResult = [];
39✔
557
        for (let comment of util.getLeadingComments(this) ?? []) {
39!
558
            result.push(
154✔
559
                comment.text,
560
                state.newline,
561
                state.indent()
562
            );
563
        }
564
        for (let annotation of this.annotations ?? []) {
39✔
565
            result.push(
2✔
566
                ...annotation.getTypedef(state),
567
                state.newline,
568
                state.indent()
569
            );
570
        }
571

572
        result.push(
39✔
573
            ...this.func.getTypedef(state)
574
        );
575
        return result;
39✔
576
    }
577

578
    walk(visitor: WalkVisitor, options: WalkOptions) {
579
        if (options.walkMode & InternalWalkMode.walkExpressions) {
14,427✔
580
            walk(this, 'func', visitor, options);
12,784✔
581
        }
582
    }
583

584
    getType(options: GetTypeOptions) {
585
        const funcExprType = this.func.getType(options);
3,035✔
586
        funcExprType.setName(this.getName(ParseMode.BrighterScript));
3,035✔
587
        return funcExprType;
3,035✔
588
    }
589
}
590

591
export class IfStatement extends Statement {
1✔
592
    constructor(options: {
593
        if?: Token;
594
        then?: Token;
595
        else?: Token;
596
        endIf?: Token;
597

598
        condition: Expression;
599
        thenBranch: Block;
600
        elseBranch?: IfStatement | Block;
601
    }) {
602
        super();
1,800✔
603
        this.condition = options.condition;
1,800✔
604
        this.thenBranch = options.thenBranch;
1,800✔
605
        this.elseBranch = options.elseBranch;
1,800✔
606

607
        this.tokens = {
1,800✔
608
            if: options.if,
609
            then: options.then,
610
            else: options.else,
611
            endIf: options.endIf
612
        };
613

614
        this.location = util.createBoundingLocation(
1,800✔
615
            util.createBoundingLocationFromTokens(this.tokens),
616
            this.condition,
617
            this.thenBranch,
618
            this.elseBranch
619
        );
620
    }
621

622
    readonly tokens: {
623
        readonly if?: Token;
624
        readonly then?: Token;
625
        readonly else?: Token;
626
        readonly endIf?: Token;
627
    };
628
    public readonly condition: Expression;
629
    public readonly thenBranch: Block;
630
    public readonly elseBranch?: IfStatement | Block;
631

632
    public readonly kind = AstNodeKind.IfStatement;
1,800✔
633

634
    public readonly location: Location | undefined;
635

636
    get isInline() {
637
        const allLeadingTrivia = [
12✔
638
            ...this.thenBranch.leadingTrivia,
639
            ...this.thenBranch.statements.map(s => s.leadingTrivia).flat(),
16✔
640
            ...(this.tokens.else?.leadingTrivia ?? []),
72✔
641
            ...(this.tokens.endIf?.leadingTrivia ?? [])
72✔
642
        ];
643

644
        const hasNewline = allLeadingTrivia.find(t => t.kind === TokenKind.Newline);
29✔
645
        return !hasNewline;
12✔
646
    }
647

648
    transpile(state: BrsTranspileState) {
649
        let results = [] as TranspileResult;
1,854✔
650
        //if   (already indented by block)
651
        results.push(state.transpileToken(this.tokens.if ?? createToken(TokenKind.If)));
1,854!
652
        results.push(' ');
1,854✔
653
        //conditions
654
        results.push(...this.condition.transpile(state));
1,854✔
655
        //then
656
        if (this.tokens.then) {
1,854✔
657
            results.push(' ');
1,542✔
658
            results.push(
1,542✔
659
                state.transpileToken(this.tokens.then)
660
            );
661
        }
662
        state.lineage.unshift(this);
1,854✔
663

664
        //if statement body
665
        let thenNodes = this.thenBranch.transpile(state);
1,854✔
666
        state.lineage.shift();
1,854✔
667
        if (thenNodes.length > 0) {
1,854✔
668
            results.push(thenNodes);
1,843✔
669
        }
670
        //else branch
671
        if (this.elseBranch) {
1,854✔
672
            //else
673
            results.push(...state.transpileEndBlockToken(this.thenBranch, this.tokens.else, 'else'));
1,534✔
674

675
            if (isIfStatement(this.elseBranch)) {
1,534✔
676
                //chained elseif
677
                state.lineage.unshift(this.elseBranch);
922✔
678
                let body = this.elseBranch.transpile(state);
922✔
679
                state.lineage.shift();
922✔
680

681
                if (body.length > 0) {
922!
682
                    //zero or more spaces between the `else` and the `if`
683
                    results.push(this.elseBranch.tokens.if.leadingWhitespace!);
922✔
684
                    results.push(...body);
922✔
685

686
                    // stop here because chained if will transpile the rest
687
                    return results;
922✔
688
                } else {
689
                    results.push('\n');
×
690
                }
691

692
            } else {
693
                //else body
694
                state.lineage.unshift(this.tokens.else!);
612✔
695
                let body = this.elseBranch.transpile(state);
612✔
696
                state.lineage.shift();
612✔
697

698
                if (body.length > 0) {
612✔
699
                    results.push(...body);
610✔
700
                }
701
            }
702
        }
703

704
        //end if
705
        results.push(...state.transpileEndBlockToken(this.elseBranch ?? this.thenBranch, this.tokens.endIf, 'end if'));
932✔
706

707
        return results;
932✔
708
    }
709

710
    walk(visitor: WalkVisitor, options: WalkOptions) {
711
        if (options.walkMode & InternalWalkMode.walkExpressions) {
5,728✔
712
            walk(this, 'condition', visitor, options);
5,712✔
713
        }
714
        if (options.walkMode & InternalWalkMode.walkStatements) {
5,728✔
715
            walk(this, 'thenBranch', visitor, options);
5,726✔
716
        }
717
        if (this.elseBranch && options.walkMode & InternalWalkMode.walkStatements) {
5,728✔
718
            walk(this, 'elseBranch', visitor, options);
4,505✔
719
        }
720
    }
721

722
    get leadingTrivia(): Token[] {
723
        return this.tokens.if?.leadingTrivia ?? [];
1,869!
724
    }
725

726
    get endTrivia(): Token[] {
727
        return this.tokens.endIf?.leadingTrivia ?? [];
1!
728
    }
729

730
}
731

732
export class IncrementStatement extends Statement {
1✔
733
    constructor(options: {
734
        value: Expression;
735
        operator: Token;
736
    }) {
737
        super();
21✔
738
        this.value = options.value;
21✔
739
        this.tokens = {
21✔
740
            operator: options.operator
741
        };
742
        this.location = util.createBoundingLocation(
21✔
743
            this.value,
744
            this.tokens.operator
745
        );
746
    }
747

748
    public readonly value: Expression;
749
    public readonly tokens: {
750
        readonly operator: Token;
751
    };
752

753
    public readonly kind = AstNodeKind.IncrementStatement;
21✔
754

755
    public readonly location: Location | undefined;
756

757
    transpile(state: BrsTranspileState) {
758
        return [
6✔
759
            ...this.value.transpile(state),
760
            state.transpileToken(this.tokens.operator)
761
        ];
762
    }
763

764
    walk(visitor: WalkVisitor, options: WalkOptions) {
765
        if (options.walkMode & InternalWalkMode.walkExpressions) {
73✔
766
            walk(this, 'value', visitor, options);
72✔
767
        }
768
    }
769

770
    get leadingTrivia(): Token[] {
771
        return this.value?.leadingTrivia ?? [];
15!
772
    }
773
}
774

775
/** Used to indent the current `print` position to the next 16-character-width output zone. */
776
export interface PrintSeparatorTab extends Token {
777
    kind: TokenKind.Comma;
778
}
779

780
/** Used to insert a single whitespace character at the current `print` position. */
781
export interface PrintSeparatorSpace extends Token {
782
    kind: TokenKind.Semicolon;
783
}
784

785
/**
786
 * Represents a `print` statement within BrightScript.
787
 */
788
export class PrintStatement extends Statement {
1✔
789
    /**
790
     * Creates a new internal representation of a BrightScript `print` statement.
791
     * @param options the options for this statement
792
     * @param options.print a print token
793
     * @param options.expressions an array of expressions or `PrintSeparator`s to be evaluated and printed.
794
     */
795
    constructor(options: {
796
        print: Token;
797
        expressions: Array<Expression | PrintSeparatorTab | PrintSeparatorSpace>;
798
    }) {
799
        super();
1,058✔
800
        this.tokens = {
1,058✔
801
            print: options.print
802
        };
803
        this.expressions = options.expressions;
1,058✔
804
        this.location = util.createBoundingLocation(
1,058✔
805
            this.tokens.print,
806
            ...(this.expressions ?? [])
3,174!
807
        );
808
    }
809
    public readonly tokens: {
810
        readonly print: Token;
811
    };
812
    public readonly expressions: Array<Expression | PrintSeparatorTab | PrintSeparatorSpace>;
813
    public readonly kind = AstNodeKind.PrintStatement;
1,058✔
814

815
    public readonly location: Location | undefined;
816

817
    transpile(state: BrsTranspileState) {
818
        let result = [
214✔
819
            state.transpileToken(this.tokens.print),
820
            ' '
821
        ] as TranspileResult;
822
        for (let i = 0; i < this.expressions.length; i++) {
214✔
823
            const expressionOrSeparator: any = this.expressions[i];
278✔
824
            if (expressionOrSeparator.transpile) {
278✔
825
                result.push(...(expressionOrSeparator as ExpressionStatement).transpile(state));
252✔
826
            } else {
827
                result.push(
26✔
828
                    state.tokenToSourceNode(expressionOrSeparator)
829
                );
830
            }
831
            //if there's an expression after us, add a space
832
            if ((this.expressions[i + 1] as any)?.transpile) {
278✔
833
                result.push(' ');
38✔
834
            }
835
        }
836
        return result;
214✔
837
    }
838

839
    walk(visitor: WalkVisitor, options: WalkOptions) {
840
        if (options.walkMode & InternalWalkMode.walkExpressions) {
4,913✔
841
            //sometimes we have semicolon Tokens in the expressions list (should probably fix that...), so only walk the actual expressions
842
            walkArray(this.expressions, visitor, options, this, (item) => isExpression(item as any));
5,382✔
843
        }
844
    }
845

846
    get leadingTrivia(): Token[] {
847
        return this.tokens.print?.leadingTrivia ?? [];
444!
848
    }
849
}
850

851
export class DimStatement extends Statement {
1✔
852
    constructor(options: {
853
        dim?: Token;
854
        name: Identifier;
855
        openingSquare?: Token;
856
        dimensions: Expression[];
857
        closingSquare?: Token;
858
    }) {
859
        super();
40✔
860
        this.tokens = {
40✔
861
            dim: options?.dim,
120!
862
            name: options.name,
863
            openingSquare: options.openingSquare,
864
            closingSquare: options.closingSquare
865
        };
866
        this.dimensions = options.dimensions;
40✔
867
        this.location = util.createBoundingLocation(
40✔
868
            options.dim,
869
            options.name,
870
            options.openingSquare,
871
            ...(this.dimensions ?? []),
120!
872
            options.closingSquare
873
        );
874
    }
875

876
    public readonly tokens: {
877
        readonly dim?: Token;
878
        readonly name: Identifier;
879
        readonly openingSquare?: Token;
880
        readonly closingSquare?: Token;
881
    };
882
    public readonly dimensions: Expression[];
883

884
    public readonly kind = AstNodeKind.DimStatement;
40✔
885

886
    public readonly location: Location | undefined;
887

888
    public transpile(state: BrsTranspileState) {
889
        let result: TranspileResult = [
15✔
890
            state.transpileToken(this.tokens.dim, 'dim'),
891
            ' ',
892
            state.transpileToken(this.tokens.name),
893
            state.transpileToken(this.tokens.openingSquare, '[')
894
        ];
895
        for (let i = 0; i < this.dimensions.length; i++) {
15✔
896
            if (i > 0) {
32✔
897
                result.push(', ');
17✔
898
            }
899
            result.push(
32✔
900
                ...this.dimensions![i].transpile(state)
901
            );
902
        }
903
        result.push(state.transpileToken(this.tokens.closingSquare, ']'));
15✔
904
        return result;
15✔
905
    }
906

907
    public walk(visitor: WalkVisitor, options: WalkOptions) {
908
        if (this.dimensions?.length !== undefined && this.dimensions?.length > 0 && options.walkMode & InternalWalkMode.walkExpressions) {
131!
909
            walkArray(this.dimensions, visitor, options, this);
116✔
910

911
        }
912
    }
913

914
    public getType(options: GetTypeOptions): BscType {
915
        const numDimensions = this.dimensions?.length ?? 1;
18!
916
        let type = new ArrayType();
18✔
917
        for (let i = 0; i < numDimensions - 1; i++) {
18✔
918
            type = new ArrayType(type);
17✔
919
        }
920
        return type;
18✔
921
    }
922

923
    get leadingTrivia(): Token[] {
924
        return this.tokens.dim?.leadingTrivia ?? [];
30!
925
    }
926
}
927

928
export class GotoStatement extends Statement {
1✔
929
    constructor(options: {
930
        goto?: Token;
931
        label: Token;
932
    }) {
933
        super();
11✔
934
        this.tokens = {
11✔
935
            goto: options.goto,
936
            label: options.label
937
        };
938
        this.location = util.createBoundingLocation(
11✔
939
            this.tokens.goto,
940
            this.tokens.label
941
        );
942
    }
943

944
    public readonly tokens: {
945
        readonly goto?: Token;
946
        readonly label: Token;
947
    };
948

949
    public readonly kind = AstNodeKind.GotoStatement;
11✔
950

951
    public readonly location: Location | undefined;
952

953
    transpile(state: BrsTranspileState) {
954
        return [
2✔
955
            state.transpileToken(this.tokens.goto, 'goto'),
956
            ' ',
957
            state.transpileToken(this.tokens.label)
958
        ];
959
    }
960

961
    walk(visitor: WalkVisitor, options: WalkOptions) {
962
        //nothing to walk
963
    }
964

965
    get leadingTrivia(): Token[] {
966
        return this.tokens.goto?.leadingTrivia ?? [];
8!
967
    }
968
}
969

970
export class LabelStatement extends Statement {
1✔
971
    constructor(options: {
972
        name: Token;
973
        colon?: Token;
974
    }) {
975
        super();
11✔
976
        this.tokens = {
11✔
977
            name: options.name,
978
            colon: options.colon
979
        };
980
        this.location = util.createBoundingLocation(
11✔
981
            this.tokens.name,
982
            this.tokens.colon
983
        );
984
    }
985
    public readonly tokens: {
986
        readonly name: Token;
987
        readonly colon: Token;
988
    };
989
    public readonly kind = AstNodeKind.LabelStatement;
11✔
990

991
    public readonly location: Location | undefined;
992

993
    public get leadingTrivia(): Token[] {
994
        return this.tokens.name.leadingTrivia;
8✔
995
    }
996

997
    transpile(state: BrsTranspileState) {
998
        return [
2✔
999
            state.transpileToken(this.tokens.name),
1000
            state.transpileToken(this.tokens.colon, ':')
1001

1002
        ];
1003
    }
1004

1005
    walk(visitor: WalkVisitor, options: WalkOptions) {
1006
        //nothing to walk
1007
    }
1008
}
1009

1010
export class ReturnStatement extends Statement {
1✔
1011
    constructor(options?: {
1012
        return?: Token;
1013
        value?: Expression;
1014
    }) {
1015
        super();
2,753✔
1016
        this.tokens = {
2,753✔
1017
            return: options?.return
8,259!
1018
        };
1019
        this.value = options?.value;
2,753!
1020
        this.location = util.createBoundingLocation(
2,753✔
1021
            this.tokens.return,
1022
            this.value
1023
        );
1024
    }
1025

1026
    public readonly tokens: {
1027
        readonly return?: Token;
1028
    };
1029
    public readonly value?: Expression;
1030
    public readonly kind = AstNodeKind.ReturnStatement;
2,753✔
1031

1032
    public readonly location: Location | undefined;
1033

1034
    transpile(state: BrsTranspileState) {
1035
        let result = [] as TranspileResult;
2,726✔
1036
        result.push(
2,726✔
1037
            state.transpileToken(this.tokens.return, 'return')
1038
        );
1039
        if (this.value) {
2,726✔
1040
            result.push(' ');
2,725✔
1041
            result.push(...this.value.transpile(state));
2,725✔
1042
        }
1043
        return result;
2,726✔
1044
    }
1045

1046
    walk(visitor: WalkVisitor, options: WalkOptions) {
1047
        if (options.walkMode & InternalWalkMode.walkExpressions) {
9,311✔
1048
            walk(this, 'value', visitor, options);
9,294✔
1049
        }
1050
    }
1051

1052
    get leadingTrivia(): Token[] {
1053
        return this.tokens.return?.leadingTrivia ?? [];
5,464!
1054
    }
1055
}
1056

1057
export class EndStatement extends Statement {
1✔
1058
    constructor(options?: {
1059
        end?: Token;
1060
    }) {
1061
        super();
9✔
1062
        this.tokens = {
9✔
1063
            end: options?.end
27!
1064
        };
1065
        this.location = this.tokens.end?.location;
9!
1066
    }
1067
    public readonly tokens: {
1068
        readonly end?: Token;
1069
    };
1070
    public readonly kind = AstNodeKind.EndStatement;
9✔
1071

1072
    public readonly location: Location;
1073

1074
    transpile(state: BrsTranspileState) {
1075
        return [
2✔
1076
            state.transpileToken(this.tokens.end, 'end')
1077
        ];
1078
    }
1079

1080
    walk(visitor: WalkVisitor, options: WalkOptions) {
1081
        //nothing to walk
1082
    }
1083

1084
    get leadingTrivia(): Token[] {
1085
        return this.tokens.end?.leadingTrivia ?? [];
8!
1086
    }
1087
}
1088

1089
export class StopStatement extends Statement {
1✔
1090
    constructor(options?: {
1091
        stop?: Token;
1092
    }) {
1093
        super();
17✔
1094
        this.tokens = { stop: options?.stop };
17!
1095
        this.location = this.tokens?.stop?.location;
17!
1096
    }
1097
    public readonly tokens: {
1098
        readonly stop?: Token;
1099
    };
1100

1101
    public readonly kind = AstNodeKind.StopStatement;
17✔
1102

1103
    public readonly location: Location;
1104

1105
    transpile(state: BrsTranspileState) {
1106
        return [
2✔
1107
            state.transpileToken(this.tokens.stop, 'stop')
1108
        ];
1109
    }
1110

1111
    walk(visitor: WalkVisitor, options: WalkOptions) {
1112
        //nothing to walk
1113
    }
1114

1115
    get leadingTrivia(): Token[] {
1116
        return this.tokens.stop?.leadingTrivia ?? [];
8!
1117
    }
1118
}
1119

1120
export class ForStatement extends Statement {
1✔
1121
    constructor(options: {
1122
        for?: Token;
1123
        counterDeclaration: AssignmentStatement;
1124
        to?: Token;
1125
        finalValue: Expression;
1126
        body: Block;
1127
        endFor?: Token;
1128
        step?: Token;
1129
        increment?: Expression;
1130
    }) {
1131
        super();
31✔
1132
        this.tokens = {
31✔
1133
            for: options.for,
1134
            to: options.to,
1135
            endFor: options.endFor,
1136
            step: options.step
1137
        };
1138
        this.counterDeclaration = options.counterDeclaration;
31✔
1139
        this.finalValue = options.finalValue;
31✔
1140
        this.body = options.body;
31✔
1141
        this.increment = options.increment;
31✔
1142

1143
        this.location = util.createBoundingLocation(
31✔
1144
            this.tokens.for,
1145
            this.counterDeclaration,
1146
            this.tokens.to,
1147
            this.finalValue,
1148
            this.tokens.step,
1149
            this.increment,
1150
            this.body,
1151
            this.tokens.endFor
1152
        );
1153
    }
1154

1155
    public readonly tokens: {
1156
        readonly for?: Token;
1157
        readonly to?: Token;
1158
        readonly endFor?: Token;
1159
        readonly step?: Token;
1160
    };
1161

1162
    public readonly counterDeclaration: AssignmentStatement;
1163
    public readonly finalValue: Expression;
1164
    public readonly body: Block;
1165
    public readonly increment?: Expression;
1166

1167
    public readonly kind = AstNodeKind.ForStatement;
31✔
1168

1169
    public readonly location: Location | undefined;
1170

1171
    transpile(state: BrsTranspileState) {
1172
        let result = [] as TranspileResult;
9✔
1173
        //for
1174
        result.push(
9✔
1175
            state.transpileToken(this.tokens.for, 'for'),
1176
            ' '
1177
        );
1178
        //i=1
1179
        result.push(
9✔
1180
            ...this.counterDeclaration.transpile(state),
1181
            ' '
1182
        );
1183
        //to
1184
        result.push(
9✔
1185
            state.transpileToken(this.tokens.to, 'to'),
1186
            ' '
1187
        );
1188
        //final value
1189
        result.push(this.finalValue.transpile(state));
9✔
1190
        //step
1191
        if (this.increment) {
9✔
1192
            result.push(
4✔
1193
                ' ',
1194
                state.transpileToken(this.tokens.step, 'step'),
1195
                ' ',
1196
                this.increment!.transpile(state)
1197
            );
1198
        }
1199
        //loop body
1200
        state.lineage.unshift(this);
9✔
1201
        result.push(...this.body.transpile(state));
9✔
1202
        state.lineage.shift();
9✔
1203

1204
        //end for
1205
        result.push(...state.transpileEndBlockToken(this.body, this.tokens.endFor, 'end for'));
9✔
1206

1207
        return result;
9✔
1208
    }
1209

1210
    walk(visitor: WalkVisitor, options: WalkOptions) {
1211
        if (options.walkMode & InternalWalkMode.walkStatements) {
122✔
1212
            walk(this, 'counterDeclaration', visitor, options);
121✔
1213
        }
1214
        if (options.walkMode & InternalWalkMode.walkExpressions) {
122✔
1215
            walk(this, 'finalValue', visitor, options);
118✔
1216
            walk(this, 'increment', visitor, options);
118✔
1217
        }
1218
        if (options.walkMode & InternalWalkMode.walkStatements) {
122✔
1219
            walk(this, 'body', visitor, options);
121✔
1220
        }
1221
    }
1222

1223
    get leadingTrivia(): Token[] {
1224
        return this.tokens.for?.leadingTrivia ?? [];
22!
1225
    }
1226

1227
    public get endTrivia(): Token[] {
NEW
1228
        return this.tokens.endFor?.leadingTrivia ?? [];
×
1229
    }
1230
}
1231

1232
export class ForEachStatement extends Statement {
1✔
1233
    constructor(options: {
1234
        forEach?: Token;
1235
        item: Token;
1236
        in?: Token;
1237
        target: Expression;
1238
        body: Block;
1239
        endFor?: Token;
1240
    }) {
1241
        super();
34✔
1242
        this.tokens = {
34✔
1243
            forEach: options.forEach,
1244
            item: options.item,
1245
            in: options.in,
1246
            endFor: options.endFor
1247
        };
1248
        this.body = options.body;
34✔
1249
        this.target = options.target;
34✔
1250

1251
        this.location = util.createBoundingLocation(
34✔
1252
            this.tokens.forEach,
1253
            this.tokens.item,
1254
            this.tokens.in,
1255
            this.target,
1256
            this.body,
1257
            this.tokens.endFor
1258
        );
1259
    }
1260

1261
    public readonly tokens: {
1262
        readonly forEach?: Token;
1263
        readonly item: Token;
1264
        readonly in?: Token;
1265
        readonly endFor?: Token;
1266
    };
1267
    public readonly body: Block;
1268
    public readonly target: Expression;
1269

1270
    public readonly kind = AstNodeKind.ForEachStatement;
34✔
1271

1272
    public readonly location: Location | undefined;
1273

1274
    transpile(state: BrsTranspileState) {
1275
        let result = [] as TranspileResult;
5✔
1276
        //for each
1277
        result.push(
5✔
1278
            state.transpileToken(this.tokens.forEach, 'for each'),
1279
            ' '
1280
        );
1281
        //item
1282
        result.push(
5✔
1283
            state.transpileToken(this.tokens.item),
1284
            ' '
1285
        );
1286
        //in
1287
        result.push(
5✔
1288
            state.transpileToken(this.tokens.in, 'in'),
1289
            ' '
1290
        );
1291
        //target
1292
        result.push(...this.target.transpile(state));
5✔
1293
        //body
1294
        state.lineage.unshift(this);
5✔
1295
        result.push(...this.body.transpile(state));
5✔
1296
        state.lineage.shift();
5✔
1297

1298
        //end for
1299
        result.push(...state.transpileEndBlockToken(this.body, this.tokens.endFor, 'end for'));
5✔
1300

1301
        return result;
5✔
1302
    }
1303

1304
    walk(visitor: WalkVisitor, options: WalkOptions) {
1305
        if (options.walkMode & InternalWalkMode.walkExpressions) {
161✔
1306
            walk(this, 'target', visitor, options);
154✔
1307
        }
1308
        if (options.walkMode & InternalWalkMode.walkStatements) {
161✔
1309
            walk(this, 'body', visitor, options);
160✔
1310
        }
1311
    }
1312

1313
    getType(options: GetTypeOptions): BscType {
1314
        return this.getSymbolTable().getSymbolType(this.tokens.item.text, options);
24✔
1315
    }
1316

1317
    get leadingTrivia(): Token[] {
1318
        return this.tokens.forEach?.leadingTrivia ?? [];
16!
1319
    }
1320

1321
    public get endTrivia(): Token[] {
1322
        return this.tokens.endFor?.leadingTrivia ?? [];
1!
1323
    }
1324
}
1325

1326
export class WhileStatement extends Statement {
1✔
1327
    constructor(options: {
1328
        while?: Token;
1329
        endWhile?: Token;
1330
        condition: Expression;
1331
        body: Block;
1332
    }) {
1333
        super();
23✔
1334
        this.tokens = {
23✔
1335
            while: options.while,
1336
            endWhile: options.endWhile
1337
        };
1338
        this.body = options.body;
23✔
1339
        this.condition = options.condition;
23✔
1340
        this.location = util.createBoundingLocation(
23✔
1341
            this.tokens.while,
1342
            this.condition,
1343
            this.body,
1344
            this.tokens.endWhile
1345
        );
1346
    }
1347

1348
    public readonly tokens: {
1349
        readonly while?: Token;
1350
        readonly endWhile?: Token;
1351
    };
1352
    public readonly condition: Expression;
1353
    public readonly body: Block;
1354

1355
    public readonly kind = AstNodeKind.WhileStatement;
23✔
1356

1357
    public readonly location: Location | undefined;
1358

1359
    transpile(state: BrsTranspileState) {
1360
        let result = [] as TranspileResult;
4✔
1361
        //while
1362
        result.push(
4✔
1363
            state.transpileToken(this.tokens.while, 'while'),
1364
            ' '
1365
        );
1366
        //condition
1367
        result.push(
4✔
1368
            ...this.condition.transpile(state)
1369
        );
1370
        state.lineage.unshift(this);
4✔
1371
        //body
1372
        result.push(...this.body.transpile(state));
4✔
1373
        state.lineage.shift();
4✔
1374

1375
        //end while
1376
        result.push(...state.transpileEndBlockToken(this.body, this.tokens.endWhile, 'end while'));
4✔
1377

1378
        return result;
4✔
1379
    }
1380

1381
    walk(visitor: WalkVisitor, options: WalkOptions) {
1382
        if (options.walkMode & InternalWalkMode.walkExpressions) {
84✔
1383
            walk(this, 'condition', visitor, options);
81✔
1384
        }
1385
        if (options.walkMode & InternalWalkMode.walkStatements) {
84✔
1386
            walk(this, 'body', visitor, options);
83✔
1387
        }
1388
    }
1389

1390
    get leadingTrivia(): Token[] {
1391
        return this.tokens.while?.leadingTrivia ?? [];
12!
1392
    }
1393

1394
    public get endTrivia(): Token[] {
1395
        return this.tokens.endWhile?.leadingTrivia ?? [];
1!
1396
    }
1397
}
1398

1399
export class DottedSetStatement extends Statement {
1✔
1400
    constructor(options: {
1401
        obj: Expression;
1402
        name: Identifier;
1403
        value: Expression;
1404
        dot?: Token;
1405
        equals?: Token;
1406
    }) {
1407
        super();
285✔
1408
        this.tokens = {
285✔
1409
            name: options.name,
1410
            dot: options.dot,
1411
            equals: options.equals
1412
        };
1413
        this.obj = options.obj;
285✔
1414
        this.value = options.value;
285✔
1415
        this.location = util.createBoundingLocation(
285✔
1416
            this.obj,
1417
            this.tokens.dot,
1418
            this.tokens.name,
1419
            this.value
1420
        );
1421
    }
1422
    public readonly tokens: {
1423
        readonly name: Identifier;
1424
        readonly equals?: Token;
1425
        readonly dot?: Token;
1426
    };
1427

1428
    public readonly obj: Expression;
1429
    public readonly value: Expression;
1430

1431
    public readonly kind = AstNodeKind.DottedSetStatement;
285✔
1432

1433
    public readonly location: Location | undefined;
1434

1435
    transpile(state: BrsTranspileState) {
1436
        //if the value is a compound assignment, don't add the obj, dot, name, or operator...the expression will handle that
1437
        return [
5✔
1438
            //object
1439
            ...this.obj.transpile(state),
1440
            this.tokens.dot ? state.tokenToSourceNode(this.tokens.dot) : '.',
5!
1441
            //name
1442
            state.transpileToken(this.tokens.name),
1443
            ' ',
1444
            state.transpileToken(this.tokens.equals, '='),
1445
            ' ',
1446
            //right-hand-side of assignment
1447
            ...this.value.transpile(state)
1448
        ];
1449

1450
    }
1451

1452
    walk(visitor: WalkVisitor, options: WalkOptions) {
1453
        if (options.walkMode & InternalWalkMode.walkExpressions) {
681✔
1454
            walk(this, 'obj', visitor, options);
680✔
1455
            walk(this, 'value', visitor, options);
680✔
1456
        }
1457
    }
1458

1459
    getType(options: GetTypeOptions) {
1460
        const objType = this.obj?.getType(options);
73!
1461
        const result = objType?.getMemberType(this.tokens.name?.text, options);
73!
1462
        options.typeChain?.push(new TypeChainEntry({
73!
1463
            name: this.tokens.name?.text,
219!
1464
            type: result, data: options.data,
1465
            location: this.tokens.name?.location,
219!
1466
            astNode: this
1467
        }));
1468
        return result;
73✔
1469
    }
1470

1471
    get leadingTrivia(): Token[] {
1472
        return this.obj.leadingTrivia;
14✔
1473
    }
1474
}
1475

1476
export class IndexedSetStatement extends Statement {
1✔
1477
    constructor(options: {
1478
        obj: Expression;
1479
        indexes: Expression[];
1480
        value: Expression;
1481
        openingSquare?: Token;
1482
        closingSquare?: Token;
1483
        equals?: Token;
1484
    }) {
1485
        super();
25✔
1486
        this.tokens = {
25✔
1487
            openingSquare: options.openingSquare,
1488
            closingSquare: options.closingSquare,
1489
            equals: options.equals
1490
        };
1491
        this.obj = options.obj;
25✔
1492
        this.indexes = options.indexes;
25✔
1493
        this.value = options.value;
25✔
1494
        this.location = util.createBoundingLocation(
25✔
1495
            this.obj,
1496
            this.tokens.openingSquare,
1497
            ...this.indexes,
1498
            this.tokens.closingSquare,
1499
            this.value
1500
        );
1501
    }
1502

1503
    public readonly tokens: {
1504
        readonly openingSquare?: Token;
1505
        readonly closingSquare?: Token;
1506
        readonly equals?: Token;
1507
    };
1508
    public readonly obj: Expression;
1509
    public readonly indexes: Expression[];
1510
    public readonly value: Expression;
1511

1512
    public readonly kind = AstNodeKind.IndexedSetStatement;
25✔
1513

1514
    public readonly location: Location | undefined;
1515

1516
    transpile(state: BrsTranspileState) {
1517
        const result = [];
10✔
1518
        result.push(
10✔
1519
            //obj
1520
            ...this.obj.transpile(state),
1521
            //   [
1522
            state.transpileToken(this.tokens.openingSquare, '[')
1523
        );
1524
        for (let i = 0; i < this.indexes.length; i++) {
10✔
1525
            //add comma between indexes
1526
            if (i > 0) {
11✔
1527
                result.push(', ');
1✔
1528
            }
1529
            let index = this.indexes[i];
11✔
1530
            result.push(
11✔
1531
                ...(index?.transpile(state) ?? [])
66!
1532
            );
1533
        }
1534
        result.push(
10✔
1535
            state.transpileToken(this.tokens.closingSquare, ']'),
1536
            ' ',
1537
            state.transpileToken(this.tokens.equals, '='),
1538
            ' ',
1539
            ...this.value.transpile(state)
1540
        );
1541
        return result;
10✔
1542

1543
    }
1544

1545
    walk(visitor: WalkVisitor, options: WalkOptions) {
1546
        if (options.walkMode & InternalWalkMode.walkExpressions) {
111✔
1547
            walk(this, 'obj', visitor, options);
110✔
1548
            walkArray(this.indexes, visitor, options, this);
110✔
1549
            walk(this, 'value', visitor, options);
110✔
1550
        }
1551
    }
1552

1553
    get leadingTrivia(): Token[] {
1554
        return this.obj.leadingTrivia;
23✔
1555
    }
1556
}
1557

1558
export class LibraryStatement extends Statement implements TypedefProvider {
1✔
1559
    constructor(options: {
1560
        library: Token;
1561
        filePath?: Token;
1562
    }) {
1563
        super();
14✔
1564
        this.tokens = {
14✔
1565
            library: options.library,
1566
            filePath: options.filePath
1567
        };
1568
        this.location = util.createBoundingLocation(
14✔
1569
            this.tokens.library,
1570
            this.tokens.filePath
1571
        );
1572
    }
1573
    public readonly tokens: {
1574
        readonly library: Token;
1575
        readonly filePath?: Token;
1576
    };
1577

1578
    public readonly kind = AstNodeKind.LibraryStatement;
14✔
1579

1580
    public readonly location: Location | undefined;
1581

1582
    transpile(state: BrsTranspileState) {
1583
        let result = [] as TranspileResult;
2✔
1584
        result.push(
2✔
1585
            state.transpileToken(this.tokens.library)
1586
        );
1587
        //there will be a parse error if file path is missing, but let's prevent a runtime error just in case
1588
        if (this.tokens.filePath) {
2!
1589
            result.push(
2✔
1590
                ' ',
1591
                state.transpileToken(this.tokens.filePath)
1592
            );
1593
        }
1594
        return result;
2✔
1595
    }
1596

1597
    getTypedef(state: BrsTranspileState) {
1598
        return this.transpile(state);
×
1599
    }
1600

1601
    walk(visitor: WalkVisitor, options: WalkOptions) {
1602
        //nothing to walk
1603
    }
1604

1605
    get leadingTrivia(): Token[] {
1606
        return this.tokens.library?.leadingTrivia ?? [];
4!
1607
    }
1608
}
1609

1610
export class NamespaceStatement extends Statement implements TypedefProvider {
1✔
1611
    constructor(options: {
1612
        namespace?: Token;
1613
        nameExpression: VariableExpression | DottedGetExpression;
1614
        body: Body;
1615
        endNamespace?: Token;
1616
    }) {
1617
        super();
577✔
1618
        this.tokens = {
577✔
1619
            namespace: options.namespace,
1620
            endNamespace: options.endNamespace
1621
        };
1622
        this.nameExpression = options.nameExpression;
577✔
1623
        this.body = options.body;
577✔
1624
        this.name = this.getName(ParseMode.BrighterScript);
577✔
1625
        this.symbolTable = new SymbolTable(`NamespaceStatement: '${this.name}'`, () => this.parent?.getSymbolTable());
3,841!
1626
    }
1627

1628
    public readonly tokens: {
1629
        readonly namespace?: Token;
1630
        readonly endNamespace?: Token;
1631
    };
1632

1633
    public readonly nameExpression: VariableExpression | DottedGetExpression;
1634
    public readonly body: Body;
1635

1636
    public readonly kind = AstNodeKind.NamespaceStatement;
577✔
1637

1638
    /**
1639
     * The string name for this namespace
1640
     */
1641
    public name: string;
1642

1643
    public get location() {
1644
        return this.cacheLocation();
309✔
1645
    }
1646
    private _location: Location | undefined;
1647

1648
    public cacheLocation() {
1649
        if (!this._location) {
884✔
1650
            this._location = util.createBoundingLocation(
577✔
1651
                this.tokens.namespace,
1652
                this.nameExpression,
1653
                this.body,
1654
                this.tokens.endNamespace
1655
            );
1656
        }
1657
        return this._location;
884✔
1658
    }
1659

1660
    public getName(parseMode: ParseMode) {
1661
        const sep = parseMode === ParseMode.BrighterScript ? '.' : '_';
6,199✔
1662
        let name = util.getAllDottedGetPartsAsString(this.nameExpression, parseMode);
6,199✔
1663
        if ((this.parent as Body)?.parent?.kind === AstNodeKind.NamespaceStatement) {
6,199✔
1664
            name = (this.parent.parent as NamespaceStatement).getName(parseMode) + sep + name;
236✔
1665
        }
1666
        return name;
6,199✔
1667
    }
1668

1669
    public get leadingTrivia(): Token[] {
1670
        return this.tokens.namespace?.leadingTrivia;
61!
1671
    }
1672

1673
    public get endTrivia(): Token[] {
NEW
1674
        return this.tokens.endNamespace?.leadingTrivia;
×
1675
    }
1676

1677
    public getNameParts() {
1678
        let parts = util.getAllDottedGetParts(this.nameExpression);
974✔
1679

1680
        if ((this.parent as Body)?.parent?.kind === AstNodeKind.NamespaceStatement) {
974!
1681
            parts = (this.parent.parent as NamespaceStatement).getNameParts().concat(parts);
55✔
1682
        }
1683
        return parts;
974✔
1684
    }
1685

1686
    transpile(state: BrsTranspileState) {
1687
        //namespaces don't actually have any real content, so just transpile their bodies
1688
        return [
38✔
1689
            state.transpileLeadingComments(this.tokens.namespace),
1690
            this.body.transpile(state),
1691
            state.transpileLeadingComments(this.tokens.endNamespace)
1692
        ];
1693
    }
1694

1695
    getTypedef(state: BrsTranspileState) {
1696
        let result: TranspileResult = [];
7✔
1697
        for (let comment of util.getLeadingComments(this) ?? []) {
7!
NEW
1698
            result.push(
×
1699
                comment.text,
1700
                state.newline,
1701
                state.indent()
1702
            );
1703
        }
1704

1705
        result.push('namespace ',
7✔
1706
            ...this.getName(ParseMode.BrighterScript),
1707
            state.newline
1708
        );
1709
        state.blockDepth++;
7✔
1710
        result.push(
7✔
1711
            ...this.body.getTypedef(state)
1712
        );
1713
        state.blockDepth--;
7✔
1714

1715
        result.push(
7✔
1716
            state.indent(),
1717
            'end namespace'
1718
        );
1719
        return result;
7✔
1720
    }
1721

1722
    walk(visitor: WalkVisitor, options: WalkOptions) {
1723
        if (options.walkMode & InternalWalkMode.walkExpressions) {
2,900✔
1724
            walk(this, 'nameExpression', visitor, options);
2,367✔
1725
        }
1726

1727
        if (this.body.statements.length > 0 && options.walkMode & InternalWalkMode.walkStatements) {
2,900✔
1728
            walk(this, 'body', visitor, options);
2,708✔
1729
        }
1730
    }
1731

1732
    getType(options: GetTypeOptions) {
1733
        const resultType = new NamespaceType(this.name);
1,010✔
1734
        return resultType;
1,010✔
1735
    }
1736

1737
}
1738

1739
export class ImportStatement extends Statement implements TypedefProvider {
1✔
1740
    constructor(options: {
1741
        import?: Token;
1742
        path?: Token;
1743
    }) {
1744
        super();
203✔
1745
        this.tokens = {
203✔
1746
            import: options.import,
1747
            path: options.path
1748
        };
1749
        this.location = util.createBoundingLocation(
203✔
1750
            this.tokens.import,
1751
            this.tokens.path
1752
        );
1753
        if (this.tokens.path) {
203✔
1754
            //remove quotes
1755
            this.filePath = this.tokens.path.text.replace(/"/g, '');
201✔
1756
            if (this.tokens.path?.location?.range) {
201!
1757
                //adjust the range to exclude the quotes
1758
                this.tokens.path.location = util.createLocation(
197✔
1759
                    this.tokens.path.location.range.start.line,
1760
                    this.tokens.path.location.range.start.character + 1,
1761
                    this.tokens.path.location.range.end.line,
1762
                    this.tokens.path.location.range.end.character - 1,
1763
                    this.tokens.path.location.uri
1764
                );
1765
            }
1766
        }
1767
    }
1768

1769
    public readonly tokens: {
1770
        readonly import?: Token;
1771
        readonly path: Token;
1772
    };
1773

1774
    public readonly kind = AstNodeKind.ImportStatement;
203✔
1775

1776
    public readonly location: Location;
1777

1778
    public readonly filePath: string;
1779

1780
    transpile(state: BrsTranspileState) {
1781
        //The xml files are responsible for adding the additional script imports, but
1782
        //add the import statement as a comment just for debugging purposes
1783
        return [
13✔
1784
            state.transpileToken(this.tokens.import, 'import', true),
1785
            ' ',
1786
            state.transpileToken(this.tokens.path)
1787
        ];
1788
    }
1789

1790
    /**
1791
     * Get the typedef for this statement
1792
     */
1793
    public getTypedef(state: BrsTranspileState) {
1794
        return [
3✔
1795
            this.tokens.import?.text ?? 'import',
18!
1796
            ' ',
1797
            //replace any `.bs` extension with `.brs`
1798
            this.tokens.path.text.replace(/\.bs"?$/i, '.brs"')
1799
        ];
1800
    }
1801

1802
    walk(visitor: WalkVisitor, options: WalkOptions) {
1803
        //nothing to walk
1804
    }
1805

1806
    get leadingTrivia(): Token[] {
1807
        return this.tokens.import?.leadingTrivia ?? [];
9!
1808
    }
1809
}
1810

1811
export class InterfaceStatement extends Statement implements TypedefProvider {
1✔
1812
    constructor(options: {
1813
        interface: Token;
1814
        name: Identifier;
1815
        extends?: Token;
1816
        parentInterfaceName?: TypeExpression;
1817
        body: Statement[];
1818
        endInterface?: Token;
1819
    }) {
1820
        super();
145✔
1821
        this.tokens = {
145✔
1822
            interface: options.interface,
1823
            name: options.name,
1824
            extends: options.extends,
1825
            endInterface: options.endInterface
1826
        };
1827
        this.parentInterfaceName = options.parentInterfaceName;
145✔
1828
        this.body = options.body;
145✔
1829
        this.location = util.createBoundingLocation(
145✔
1830
            this.tokens.interface,
1831
            this.tokens.name,
1832
            this.tokens.extends,
1833
            this.parentInterfaceName,
1834
            ...this.body,
1835
            this.tokens.endInterface
1836
        );
1837
    }
1838
    public readonly parentInterfaceName?: TypeExpression;
1839
    public readonly body: Statement[];
1840

1841
    public readonly kind = AstNodeKind.InterfaceStatement;
145✔
1842

1843
    public readonly tokens = {} as {
145✔
1844
        readonly interface?: Token;
1845
        readonly name: Identifier;
1846
        readonly extends?: Token;
1847
        readonly endInterface?: Token;
1848
    };
1849

1850
    public readonly location: Location | undefined;
1851

1852
    public get fields(): InterfaceFieldStatement[] {
1853
        return this.body.filter(x => isInterfaceFieldStatement(x)) as InterfaceFieldStatement[];
205✔
1854
    }
1855

1856
    public get methods(): InterfaceMethodStatement[] {
1857
        return this.body.filter(x => isInterfaceMethodStatement(x)) as InterfaceMethodStatement[];
200✔
1858
    }
1859

1860

1861
    public hasParentInterface() {
NEW
1862
        return !!this.parentInterfaceName;
×
1863
    }
1864

1865
    public get leadingTrivia(): Token[] {
1866
        return this.tokens.interface?.leadingTrivia;
21!
1867
    }
1868

1869
    public get endTrivia(): Token[] {
NEW
1870
        return this.tokens.endInterface?.leadingTrivia;
×
1871
    }
1872

1873

1874
    /**
1875
     * The name of the interface WITH its leading namespace (if applicable)
1876
     */
1877
    public get fullName() {
1878
        const name = this.tokens.name?.text;
2!
1879
        if (name) {
2!
1880
            const namespace = this.findAncestor<NamespaceStatement>(isNamespaceStatement);
2✔
1881
            if (namespace) {
2✔
1882
                let namespaceName = namespace.getName(ParseMode.BrighterScript);
1✔
1883
                return `${namespaceName}.${name}`;
1✔
1884
            } else {
1885
                return name;
1✔
1886
            }
1887
        } else {
1888
            //return undefined which will allow outside callers to know that this interface doesn't have a name
1889
            return undefined;
×
1890
        }
1891
    }
1892

1893
    /**
1894
     * The name of the interface (without the namespace prefix)
1895
     */
1896
    public get name() {
1897
        return this.tokens.name?.text;
136!
1898
    }
1899

1900
    /**
1901
     * Get the name of this expression based on the parse mode
1902
     */
1903
    public getName(parseMode: ParseMode) {
1904
        const namespace = this.findAncestor<NamespaceStatement>(isNamespaceStatement);
136✔
1905
        if (namespace) {
136✔
1906
            let delimiter = parseMode === ParseMode.BrighterScript ? '.' : '_';
22!
1907
            let namespaceName = namespace.getName(parseMode);
22✔
1908
            return namespaceName + delimiter + this.name;
22✔
1909
        } else {
1910
            return this.name;
114✔
1911
        }
1912
    }
1913

1914
    public transpile(state: BrsTranspileState): TranspileResult {
1915
        //interfaces should completely disappear at runtime
1916
        return [
13✔
1917
            state.transpileLeadingComments(this.tokens.interface)
1918
        ];
1919
    }
1920

1921
    getTypedef(state: BrsTranspileState) {
1922
        const result = [] as TranspileResult;
7✔
1923
        for (let comment of util.getLeadingComments(this) ?? []) {
7!
NEW
1924
            result.push(
×
1925
                comment.text,
1926
                state.newline,
1927
                state.indent()
1928
            );
1929
        }
1930
        for (let annotation of this.annotations ?? []) {
7✔
1931
            result.push(
1✔
1932
                ...annotation.getTypedef(state),
1933
                state.newline,
1934
                state.indent()
1935
            );
1936
        }
1937
        result.push(
7✔
1938
            this.tokens.interface.text,
1939
            ' ',
1940
            this.tokens.name.text
1941
        );
1942
        const parentInterfaceName = this.parentInterfaceName?.getName();
7!
1943
        if (parentInterfaceName) {
7!
1944
            result.push(
×
1945
                ' extends ',
1946
                parentInterfaceName
1947
            );
1948
        }
1949
        const body = this.body ?? [];
7!
1950
        if (body.length > 0) {
7!
1951
            state.blockDepth++;
7✔
1952
        }
1953
        for (const statement of body) {
7✔
1954
            if (isInterfaceMethodStatement(statement) || isInterfaceFieldStatement(statement)) {
22!
1955
                result.push(
22✔
1956
                    state.newline,
1957
                    state.indent(),
1958
                    ...statement.getTypedef(state)
1959
                );
1960
            } else {
UNCOV
1961
                result.push(
×
1962
                    state.newline,
1963
                    state.indent(),
1964
                    ...statement.transpile(state)
1965
                );
1966
            }
1967
        }
1968
        if (body.length > 0) {
7!
1969
            state.blockDepth--;
7✔
1970
        }
1971
        result.push(
7✔
1972
            state.newline,
1973
            state.indent(),
1974
            'end interface',
1975
            state.newline
1976
        );
1977
        return result;
7✔
1978
    }
1979

1980
    walk(visitor: WalkVisitor, options: WalkOptions) {
1981
        //visitor-less walk function to do parent linking
1982
        walk(this, 'parentInterfaceName', null, options);
670✔
1983

1984
        if (options.walkMode & InternalWalkMode.walkStatements) {
670!
1985
            walkArray(this.body, visitor, options, this);
670✔
1986
        }
1987
    }
1988

1989
    getType(options: GetTypeOptions) {
1990
        const superIface = this.parentInterfaceName?.getType(options) as InterfaceType;
130✔
1991

1992
        const resultType = new InterfaceType(this.getName(ParseMode.BrighterScript), superIface);
130✔
1993
        for (const statement of this.methods) {
130✔
1994
            const memberType = statement?.getType({ ...options, typeChain: undefined }); // no typechain info needed
28!
1995
            const flag = statement.isOptional ? SymbolTypeFlag.runtime | SymbolTypeFlag.optional : SymbolTypeFlag.runtime;
28✔
1996
            resultType.addMember(statement?.tokens.name?.text, { definingNode: statement }, memberType, flag);
28!
1997
        }
1998
        for (const statement of this.fields) {
130✔
1999
            const memberType = statement?.getType({ ...options, typeChain: undefined }); // no typechain info needed
167!
2000
            const flag = statement.isOptional ? SymbolTypeFlag.runtime | SymbolTypeFlag.optional : SymbolTypeFlag.runtime;
167✔
2001
            resultType.addMember(statement?.tokens.name?.text, { definingNode: statement, isInstance: true }, memberType, flag);
167!
2002
        }
2003
        options.typeChain?.push(new TypeChainEntry({
130✔
2004
            name: this.getName(ParseMode.BrighterScript),
2005
            type: resultType,
2006
            data: options.data,
2007
            astNode: this
2008
        }));
2009
        return resultType;
130✔
2010
    }
2011
}
2012

2013
export class InterfaceFieldStatement extends Statement implements TypedefProvider {
1✔
2014
    public transpile(state: BrsTranspileState): TranspileResult {
2015
        throw new Error('Method not implemented.');
×
2016
    }
2017
    constructor(options: {
2018
        name: Identifier;
2019
        as?: Token;
2020
        typeExpression?: TypeExpression;
2021
        optional?: Token;
2022
    }) {
2023
        super();
179✔
2024
        this.tokens = {
179✔
2025
            optional: options.optional,
2026
            name: options.name,
2027
            as: options.as
2028
        };
2029
        this.typeExpression = options.typeExpression;
179✔
2030
        this.location = util.createBoundingLocation(
179✔
2031
            this.tokens.optional,
2032
            this.tokens.name,
2033
            this.tokens.as,
2034
            this.typeExpression
2035
        );
2036
    }
2037

2038
    public readonly kind = AstNodeKind.InterfaceFieldStatement;
179✔
2039

2040
    public readonly typeExpression?: TypeExpression;
2041

2042
    public readonly location: Location | undefined;
2043

2044
    public readonly tokens: {
2045
        readonly name: Identifier;
2046
        readonly as: Token;
2047
        readonly optional?: Token;
2048
    };
2049

2050
    public get leadingTrivia(): Token[] {
2051
        return this.tokens.optional?.leadingTrivia ?? this.tokens.name.leadingTrivia;
21✔
2052
    }
2053

2054
    public get name() {
2055
        return this.tokens.name.text;
×
2056
    }
2057

2058
    public get isOptional() {
2059
        return !!this.tokens.optional;
187✔
2060
    }
2061

2062
    walk(visitor: WalkVisitor, options: WalkOptions) {
2063
        if (options.walkMode & InternalWalkMode.walkExpressions) {
1,156✔
2064
            walk(this, 'typeExpression', visitor, options);
1,001✔
2065
        }
2066
    }
2067

2068
    getTypedef(state: BrsTranspileState): TranspileResult {
2069
        const result = [] as TranspileResult;
12✔
2070
        for (let comment of util.getLeadingComments(this) ?? []) {
12!
NEW
2071
            result.push(
×
2072
                comment.text,
2073
                state.newline,
2074
                state.indent()
2075
            );
2076
        }
2077
        for (let annotation of this.annotations ?? []) {
12✔
2078
            result.push(
1✔
2079
                ...annotation.getTypedef(state),
2080
                state.newline,
2081
                state.indent()
2082
            );
2083
        }
2084
        if (this.isOptional) {
12✔
2085
            result.push(
1✔
2086
                this.tokens.optional!.text,
2087
                ' '
2088
            );
2089
        }
2090
        result.push(
12✔
2091
            this.tokens.name.text
2092
        );
2093

2094
        if (this.typeExpression) {
12!
2095
            result.push(
12✔
2096
                ' as ',
2097
                ...this.typeExpression.getTypedef(state)
2098
            );
2099
        }
2100
        return result;
12✔
2101
    }
2102

2103
    public getType(options: GetTypeOptions): BscType {
2104
        return this.typeExpression?.getType(options) ?? DynamicType.instance;
171✔
2105
    }
2106

2107
}
2108

2109
//TODO: there is much that is similar with this and FunctionExpression.
2110
//It would be nice to refactor this so there is less duplicated code
2111
export class InterfaceMethodStatement extends Statement implements TypedefProvider {
1✔
2112
    public transpile(state: BrsTranspileState): TranspileResult {
2113
        throw new Error('Method not implemented.');
×
2114
    }
2115
    constructor(options: {
2116
        functionType?: Token;
2117
        name: Identifier;
2118
        leftParen?: Token;
2119
        params?: FunctionParameterExpression[];
2120
        rightParen?: Token;
2121
        as?: Token;
2122
        returnTypeExpression?: TypeExpression;
2123
        optional?: Token;
2124
    }) {
2125
        super();
38✔
2126
        this.tokens = {
38✔
2127
            optional: options.optional,
2128
            functionType: options.functionType,
2129
            name: options.name,
2130
            leftParen: options.leftParen,
2131
            rightParen: options.rightParen,
2132
            as: options.as
2133
        };
2134
        this.params = options.params ?? [];
38!
2135
        this.returnTypeExpression = options.returnTypeExpression;
38✔
2136
    }
2137

2138
    public readonly kind = AstNodeKind.InterfaceMethodStatement;
38✔
2139

2140
    public get location() {
2141
        return util.createBoundingLocation(
52✔
2142
            this.tokens.optional,
2143
            this.tokens.functionType,
2144
            this.tokens.name,
2145
            this.tokens.leftParen,
2146
            ...(this.params ?? []),
156!
2147
            this.tokens.rightParen,
2148
            this.tokens.as,
2149
            this.returnTypeExpression
2150
        );
2151
    }
2152
    /**
2153
     * Get the name of this method.
2154
     */
2155
    public getName(parseMode: ParseMode) {
2156
        return this.tokens.name.text;
28✔
2157
    }
2158

2159
    public readonly tokens: {
2160
        readonly optional?: Token;
2161
        readonly functionType: Token;
2162
        readonly name: Identifier;
2163
        readonly leftParen?: Token;
2164
        readonly rightParen?: Token;
2165
        readonly as?: Token;
2166
    };
2167

2168
    public readonly params: FunctionParameterExpression[];
2169
    public readonly returnTypeExpression?: TypeExpression;
2170

2171
    public get isOptional() {
2172
        return !!this.tokens.optional;
41✔
2173
    }
2174

2175
    public get leadingTrivia(): Token[] {
2176
        return this.tokens.optional?.leadingTrivia ?? this.tokens.functionType.leadingTrivia;
13✔
2177
    }
2178

2179
    walk(visitor: WalkVisitor, options: WalkOptions) {
2180
        if (options.walkMode & InternalWalkMode.walkExpressions) {
200✔
2181
            walk(this, 'returnTypeExpression', visitor, options);
175✔
2182
        }
2183
    }
2184

2185
    getTypedef(state: BrsTranspileState) {
2186
        const result = [] as TranspileResult;
10✔
2187
        for (let comment of util.getLeadingComments(this) ?? []) {
10!
2188
            result.push(
1✔
2189
                comment.text,
2190
                state.newline,
2191
                state.indent()
2192
            );
2193
        }
2194
        for (let annotation of this.annotations ?? []) {
10✔
2195
            result.push(
1✔
2196
                ...annotation.getTypedef(state),
2197
                state.newline,
2198
                state.indent()
2199
            );
2200
        }
2201
        if (this.isOptional) {
10!
UNCOV
2202
            result.push(
×
2203
                this.tokens.optional!.text,
2204
                ' '
2205
            );
2206
        }
2207
        result.push(
10✔
2208
            this.tokens.functionType?.text ?? 'function',
60!
2209
            ' ',
2210
            this.tokens.name.text,
2211
            '('
2212
        );
2213
        const params = this.params ?? [];
10!
2214
        for (let i = 0; i < params.length; i++) {
10✔
2215
            if (i > 0) {
2✔
2216
                result.push(', ');
1✔
2217
            }
2218
            const param = params[i];
2✔
2219
            result.push(param.tokens.name.text);
2✔
2220
            if (param.typeExpression) {
2!
2221
                result.push(
2✔
2222
                    ' as ',
2223
                    ...param.typeExpression.getTypedef(state)
2224
                );
2225
            }
2226
        }
2227
        result.push(
10✔
2228
            ')'
2229
        );
2230
        if (this.returnTypeExpression) {
10!
2231
            result.push(
10✔
2232
                ' as ',
2233
                ...this.returnTypeExpression.getTypedef(state)
2234
            );
2235
        }
2236
        return result;
10✔
2237
    }
2238

2239
    public getType(options: GetTypeOptions): TypedFunctionType {
2240
        //if there's a defined return type, use that
2241
        let returnType = this.returnTypeExpression?.getType(options);
28✔
2242
        const isSub = this.tokens.functionType?.kind === TokenKind.Sub || !returnType;
28!
2243
        //if we don't have a return type and this is a sub, set the return type to `void`. else use `dynamic`
2244
        if (!returnType) {
28✔
2245
            returnType = isSub ? VoidType.instance : DynamicType.instance;
10!
2246
        }
2247

2248
        const resultType = new TypedFunctionType(returnType);
28✔
2249
        resultType.isSub = isSub;
28✔
2250
        for (let param of this.params) {
28✔
2251
            resultType.addParameter(param.tokens.name.text, param.getType(options), !!param.defaultValue);
7✔
2252
        }
2253
        if (options.typeChain) {
28!
2254
            // need Interface type for type chain
NEW
2255
            this.parent?.getType(options);
×
2256
        }
2257
        let funcName = this.getName(ParseMode.BrighterScript);
28✔
2258
        resultType.setName(funcName);
28✔
2259
        options.typeChain?.push(new TypeChainEntry({ name: resultType.name, type: resultType, data: options.data, astNode: this }));
28!
2260
        return resultType;
28✔
2261
    }
2262
}
2263

2264
export class ClassStatement extends Statement implements TypedefProvider {
1✔
2265
    constructor(options: {
2266
        class?: Token;
2267
        /**
2268
         * The name of the class (without namespace prefix)
2269
         */
2270
        name: Identifier;
2271
        body: Statement[];
2272
        endClass?: Token;
2273
        extends?: Token;
2274
        parentClassName?: TypeExpression;
2275
    }) {
2276
        super();
654✔
2277
        this.body = options.body ?? [];
654!
2278
        this.tokens = {
654✔
2279
            name: options.name,
2280
            class: options.class,
2281
            endClass: options.endClass,
2282
            extends: options.extends
2283
        };
2284
        this.parentClassName = options.parentClassName;
654✔
2285
        this.symbolTable = new SymbolTable(`ClassStatement: '${this.tokens.name?.text}'`, () => this.parent?.getSymbolTable());
1,152!
2286

2287
        for (let statement of this.body) {
654✔
2288
            if (isMethodStatement(statement)) {
653✔
2289
                this.methods.push(statement);
344✔
2290
                this.memberMap[statement?.tokens.name?.text.toLowerCase()] = statement;
344!
2291
            } else if (isFieldStatement(statement)) {
309!
2292
                this.fields.push(statement);
309✔
2293
                this.memberMap[statement?.tokens.name?.text.toLowerCase()] = statement;
309!
2294
            }
2295
        }
2296

2297
        this.location = util.createBoundingLocation(
654✔
2298
            this.parentClassName,
2299
            ...(this.body ?? []),
1,962!
2300
            util.createBoundingLocationFromTokens(this.tokens)
2301
        );
2302
    }
2303

2304
    public readonly kind = AstNodeKind.ClassStatement;
654✔
2305

2306

2307
    public readonly tokens: {
2308
        readonly class?: Token;
2309
        /**
2310
         * The name of the class (without namespace prefix)
2311
         */
2312
        readonly name: Identifier;
2313
        readonly endClass?: Token;
2314
        readonly extends?: Token;
2315
    };
2316
    public readonly body: Statement[];
2317
    public readonly parentClassName: TypeExpression;
2318

2319

2320
    public getName(parseMode: ParseMode) {
2321
        const name = this.tokens.name?.text;
1,985✔
2322
        if (name) {
1,985✔
2323
            const namespace = this.findAncestor<NamespaceStatement>(isNamespaceStatement);
1,983✔
2324
            if (namespace) {
1,983✔
2325
                let namespaceName = namespace.getName(parseMode);
580✔
2326
                let separator = parseMode === ParseMode.BrighterScript ? '.' : '_';
580✔
2327
                return namespaceName + separator + name;
580✔
2328
            } else {
2329
                return name;
1,403✔
2330
            }
2331
        } else {
2332
            //return undefined which will allow outside callers to know that this class doesn't have a name
2333
            return undefined;
2✔
2334
        }
2335
    }
2336

2337
    public get leadingTrivia(): Token[] {
2338
        return this.tokens.class?.leadingTrivia;
81!
2339
    }
2340

2341
    public get endTrivia(): Token[] {
NEW
2342
        return this.tokens.endClass?.leadingTrivia ?? [];
×
2343
    }
2344

2345
    public readonly memberMap = {} as Record<string, MemberStatement>;
654✔
2346
    public readonly methods = [] as MethodStatement[];
654✔
2347
    public readonly fields = [] as FieldStatement[];
654✔
2348

2349
    public readonly location: Location | undefined;
2350

2351
    transpile(state: BrsTranspileState) {
2352
        let result = [] as TranspileResult;
46✔
2353
        //make the builder
2354
        result.push(...this.getTranspiledBuilder(state));
46✔
2355
        result.push(
46✔
2356
            '\n',
2357
            state.indent()
2358
        );
2359
        //make the class assembler (i.e. the public-facing class creator method)
2360
        result.push(...this.getTranspiledClassFunction(state));
46✔
2361
        return result;
46✔
2362
    }
2363

2364
    getTypedef(state: BrsTranspileState) {
2365
        const result = [] as TranspileResult;
15✔
2366
        for (let comment of util.getLeadingComments(this) ?? []) {
15!
NEW
2367
            result.push(
×
2368
                comment.text,
2369
                state.newline,
2370
                state.indent()
2371
            );
2372
        }
2373
        for (let annotation of this.annotations ?? []) {
15!
UNCOV
2374
            result.push(
×
2375
                ...annotation.getTypedef(state),
2376
                state.newline,
2377
                state.indent()
2378
            );
2379
        }
2380
        result.push(
15✔
2381
            'class ',
2382
            this.tokens.name.text
2383
        );
2384
        if (this.parentClassName) {
15✔
2385
            const namespace = this.findAncestor<NamespaceStatement>(isNamespaceStatement);
4✔
2386
            const fqName = util.getFullyQualifiedClassName(
4✔
2387
                this.parentClassName.getName(),
2388
                namespace?.getName(ParseMode.BrighterScript)
12✔
2389
            );
2390
            result.push(
4✔
2391
                ` extends ${fqName}`
2392
            );
2393
        }
2394
        result.push(state.newline);
15✔
2395
        state.blockDepth++;
15✔
2396

2397
        let body = this.body;
15✔
2398
        //inject an empty "new" method if missing
2399
        if (!this.getConstructorFunction()) {
15✔
2400
            const constructor = createMethodStatement('new', TokenKind.Sub);
11✔
2401
            constructor.parent = this;
11✔
2402
            //walk the constructor to set up parent links
2403
            constructor.link();
11✔
2404
            body = [
11✔
2405
                constructor,
2406
                ...this.body
2407
            ];
2408
        }
2409

2410
        for (const member of body) {
15✔
2411
            if (isTypedefProvider(member)) {
33!
2412
                result.push(
33✔
2413
                    state.indent(),
2414
                    ...member.getTypedef(state),
2415
                    state.newline
2416
                );
2417
            }
2418
        }
2419
        state.blockDepth--;
15✔
2420
        result.push(
15✔
2421
            state.indent(),
2422
            'end class'
2423
        );
2424
        return result;
15✔
2425
    }
2426

2427
    /**
2428
     * Find the parent index for this class's parent.
2429
     * For class inheritance, every class is given an index.
2430
     * The base class is index 0, its child is index 1, and so on.
2431
     */
2432
    public getParentClassIndex(state: BrsTranspileState) {
2433
        let myIndex = 0;
107✔
2434
        let stmt = this as ClassStatement;
107✔
2435
        while (stmt) {
107✔
2436
            if (stmt.parentClassName) {
156✔
2437
                const namespace = stmt.findAncestor<NamespaceStatement>(isNamespaceStatement);
50✔
2438
                //find the parent class
2439
                stmt = state.file.getClassFileLink(
50✔
2440
                    stmt.parentClassName.getName(),
2441
                    namespace?.getName(ParseMode.BrighterScript)
150✔
2442
                )?.item;
50✔
2443
                myIndex++;
50✔
2444
            } else {
2445
                break;
106✔
2446
            }
2447
        }
2448
        const result = myIndex - 1;
107✔
2449
        if (result >= 0) {
107✔
2450
            return result;
43✔
2451
        } else {
2452
            return null;
64✔
2453
        }
2454
    }
2455

2456
    public hasParentClass() {
2457
        return !!this.parentClassName;
270✔
2458
    }
2459

2460
    /**
2461
     * Get all ancestor classes, in closest-to-furthest order (i.e. 0 is parent, 1 is grandparent, etc...).
2462
     * This will return an empty array if no ancestors were found
2463
     */
2464
    public getAncestors(state: BrsTranspileState) {
2465
        let ancestors = [] as ClassStatement[];
92✔
2466
        let stmt = this as ClassStatement;
92✔
2467
        while (stmt) {
92✔
2468
            if (stmt.parentClassName) {
134✔
2469
                const namespace = this.findAncestor<NamespaceStatement>(isNamespaceStatement);
42✔
2470
                stmt = state.file.getClassFileLink(
42✔
2471
                    stmt.parentClassName.getName(),
2472
                    namespace?.getName(ParseMode.BrighterScript)
126✔
2473
                )?.item;
42!
2474
                ancestors.push(stmt);
42✔
2475
            } else {
2476
                break;
92✔
2477
            }
2478
        }
2479
        return ancestors;
92✔
2480
    }
2481

2482
    private getBuilderName(name: string) {
2483
        if (name.includes('.')) {
110✔
2484
            name = name.replace(/\./gi, '_');
3✔
2485
        }
2486
        return `__${name}_builder`;
110✔
2487
    }
2488

2489
    public getConstructorType() {
2490
        const constructorType = this.getConstructorFunction()?.getType({ flags: SymbolTypeFlag.runtime }) ?? new TypedFunctionType(null);
2!
2491
        constructorType.returnType = this.getType({ flags: SymbolTypeFlag.runtime });
2✔
2492
        return constructorType;
2✔
2493
    }
2494

2495
    /**
2496
     * Get the constructor function for this class (if exists), or undefined if not exist
2497
     */
2498
    private getConstructorFunction() {
2499
        return this.body.find((stmt) => {
109✔
2500
            return (stmt as MethodStatement)?.tokens.name?.text?.toLowerCase() === 'new';
102!
2501
        }) as MethodStatement;
2502
    }
2503

2504
    /**
2505
     * Determine if the specified field was declared in one of the ancestor classes
2506
     */
2507
    public isFieldDeclaredByAncestor(fieldName: string, ancestors: ClassStatement[]) {
2508
        let lowerFieldName = fieldName.toLowerCase();
×
2509
        for (let ancestor of ancestors) {
×
2510
            if (ancestor.memberMap[lowerFieldName]) {
×
2511
                return true;
×
2512
            }
2513
        }
2514
        return false;
×
2515
    }
2516

2517
    /**
2518
     * The builder is a function that assigns all of the methods and property names to a class instance.
2519
     * This needs to be a separate function so that child classes can call the builder from their parent
2520
     * without instantiating the parent constructor at that point in time.
2521
     */
2522
    private getTranspiledBuilder(state: BrsTranspileState) {
2523
        let result = [] as TranspileResult;
46✔
2524
        result.push(`function ${this.getBuilderName(this.getName(ParseMode.BrightScript)!)}()\n`);
46✔
2525
        state.blockDepth++;
46✔
2526
        //indent
2527
        result.push(state.indent());
46✔
2528

2529
        /**
2530
         * The lineage of this class. index 0 is a direct parent, index 1 is index 0's parent, etc...
2531
         */
2532
        let ancestors = this.getAncestors(state);
46✔
2533

2534
        //construct parent class or empty object
2535
        if (ancestors[0]) {
46✔
2536
            const ancestorNamespace = ancestors[0].findAncestor<NamespaceStatement>(isNamespaceStatement);
18✔
2537
            let fullyQualifiedClassName = util.getFullyQualifiedClassName(
18✔
2538
                ancestors[0].getName(ParseMode.BrighterScript)!,
2539
                ancestorNamespace?.getName(ParseMode.BrighterScript)
54✔
2540
            );
2541
            result.push(
18✔
2542
                'instance = ',
2543
                this.getBuilderName(fullyQualifiedClassName), '()');
2544
        } else {
2545
            //use an empty object.
2546
            result.push('instance = {}');
28✔
2547
        }
2548
        result.push(
46✔
2549
            state.newline,
2550
            state.indent()
2551
        );
2552
        let parentClassIndex = this.getParentClassIndex(state);
46✔
2553

2554
        let body = this.body;
46✔
2555
        //inject an empty "new" method if missing
2556
        if (!this.getConstructorFunction()) {
46✔
2557
            body = [
26✔
2558
                createMethodStatement('new', TokenKind.Sub),
2559
                ...this.body
2560
            ];
2561
        }
2562

2563
        for (let statement of body) {
46✔
2564
            //is field statement
2565
            if (isFieldStatement(statement)) {
73✔
2566
                //do nothing with class fields in this situation, they are handled elsewhere
2567
                continue;
13✔
2568

2569
                //methods
2570
            } else if (isMethodStatement(statement)) {
60!
2571

2572
                //store overridden parent methods as super{parentIndex}_{methodName}
2573
                if (
60✔
2574
                    //is override method
2575
                    statement.tokens.override ||
162✔
2576
                    //is constructor function in child class
2577
                    (statement.tokens.name.text.toLowerCase() === 'new' && ancestors[0])
2578
                ) {
2579
                    result.push(
22✔
2580
                        `instance.super${parentClassIndex}_${statement.tokens.name.text} = instance.${statement.tokens.name.text}`,
2581
                        state.newline,
2582
                        state.indent()
2583
                    );
2584
                }
2585

2586
                state.classStatement = this;
60✔
2587
                result.push(
60✔
2588
                    'instance.',
2589
                    state.transpileToken(statement.tokens.name),
2590
                    ' = ',
2591
                    ...statement.transpile(state),
2592
                    state.newline,
2593
                    state.indent()
2594
                );
2595
                delete state.classStatement;
60✔
2596
            } else {
2597
                //other random statements (probably just comments)
2598
                result.push(
×
2599
                    ...statement.transpile(state),
2600
                    state.newline,
2601
                    state.indent()
2602
                );
2603
            }
2604
        }
2605
        //return the instance
2606
        result.push('return instance\n');
46✔
2607
        state.blockDepth--;
46✔
2608
        result.push(state.indent());
46✔
2609
        result.push(`end function`);
46✔
2610
        return result;
46✔
2611
    }
2612

2613
    /**
2614
     * The class function is the function with the same name as the class. This is the function that
2615
     * consumers should call to create a new instance of that class.
2616
     * This invokes the builder, gets an instance of the class, then invokes the "new" function on that class.
2617
     */
2618
    private getTranspiledClassFunction(state: BrsTranspileState) {
2619
        let result = [] as TranspileResult;
46✔
2620
        const constructorFunction = this.getConstructorFunction();
46✔
2621
        const constructorParams = constructorFunction ? constructorFunction.func.parameters : [];
46✔
2622

2623
        result.push(
46✔
2624
            state.sourceNode(this.tokens.class, 'function'),
2625
            state.sourceNode(this.tokens.class, ' '),
2626
            state.sourceNode(this.tokens.name, this.getName(ParseMode.BrightScript)),
2627
            `(`
2628
        );
2629
        let i = 0;
46✔
2630
        for (let param of constructorParams) {
46✔
2631
            if (i > 0) {
8✔
2632
                result.push(', ');
2✔
2633
            }
2634
            result.push(
8✔
2635
                param.transpile(state)
2636
            );
2637
            i++;
8✔
2638
        }
2639
        result.push(
46✔
2640
            ')',
2641
            '\n'
2642
        );
2643

2644
        state.blockDepth++;
46✔
2645
        result.push(state.indent());
46✔
2646
        result.push(`instance = ${this.getBuilderName(this.getName(ParseMode.BrightScript)!)}()\n`);
46✔
2647

2648
        result.push(state.indent());
46✔
2649
        result.push(`instance.new(`);
46✔
2650

2651
        //append constructor arguments
2652
        i = 0;
46✔
2653
        for (let param of constructorParams) {
46✔
2654
            if (i > 0) {
8✔
2655
                result.push(', ');
2✔
2656
            }
2657
            result.push(
8✔
2658
                state.transpileToken(param.tokens.name)
2659
            );
2660
            i++;
8✔
2661
        }
2662
        result.push(
46✔
2663
            ')',
2664
            '\n'
2665
        );
2666

2667
        result.push(state.indent());
46✔
2668
        result.push(`return instance\n`);
46✔
2669

2670
        state.blockDepth--;
46✔
2671
        result.push(state.indent());
46✔
2672
        result.push(`end function`);
46✔
2673
        return result;
46✔
2674
    }
2675

2676
    walk(visitor: WalkVisitor, options: WalkOptions) {
2677
        //visitor-less walk function to do parent linking
2678
        walk(this, 'parentClassName', null, options);
2,552✔
2679

2680
        if (options.walkMode & InternalWalkMode.walkStatements) {
2,552!
2681
            walkArray(this.body, visitor, options, this);
2,552✔
2682
        }
2683
    }
2684

2685
    getType(options: GetTypeOptions) {
2686
        const superClass = this.parentClassName?.getType(options) as ClassType;
402✔
2687

2688
        const resultType = new ClassType(this.getName(ParseMode.BrighterScript), superClass);
402✔
2689

2690
        for (const statement of this.methods) {
402✔
2691
            const funcType = statement?.func.getType({ ...options, typeChain: undefined }); //no typechain needed
227!
2692
            let flag = SymbolTypeFlag.runtime;
227✔
2693
            if (statement.accessModifier?.kind === TokenKind.Private) {
227✔
2694
                flag |= SymbolTypeFlag.private;
8✔
2695
            }
2696
            if (statement.accessModifier?.kind === TokenKind.Protected) {
227✔
2697
                flag |= SymbolTypeFlag.protected;
7✔
2698
            }
2699
            resultType.addMember(statement?.tokens.name?.text, { definingNode: statement }, funcType, flag);
227!
2700
        }
2701
        for (const statement of this.fields) {
402✔
2702
            const fieldType = statement.getType({ ...options, typeChain: undefined }); //no typechain needed
202✔
2703
            let flag = SymbolTypeFlag.runtime;
202✔
2704
            if (statement.isOptional) {
202✔
2705
                flag |= SymbolTypeFlag.optional;
7✔
2706
            }
2707
            if (statement.tokens.accessModifier?.kind === TokenKind.Private) {
202✔
2708
                flag |= SymbolTypeFlag.private;
14✔
2709
            }
2710
            if (statement.tokens.accessModifier?.kind === TokenKind.Protected) {
202✔
2711
                flag |= SymbolTypeFlag.protected;
9✔
2712
            }
2713
            resultType.addMember(statement?.tokens.name?.text, { definingNode: statement, isInstance: true }, fieldType, flag);
202!
2714
        }
2715
        options.typeChain?.push(new TypeChainEntry({ name: resultType.name, type: resultType, data: options.data, astNode: this }));
402✔
2716
        return resultType;
402✔
2717
    }
2718
}
2719

2720
const accessModifiers = [
1✔
2721
    TokenKind.Public,
2722
    TokenKind.Protected,
2723
    TokenKind.Private
2724
];
2725
export class MethodStatement extends FunctionStatement {
1✔
2726
    constructor(
2727
        options: {
2728
            modifiers?: Token | Token[];
2729
            name: Identifier;
2730
            func: FunctionExpression;
2731
            override?: Token;
2732
        }
2733
    ) {
2734
        super(options);
381✔
2735
        if (options.modifiers) {
381✔
2736
            if (Array.isArray(options.modifiers)) {
35!
NEW
2737
                this.modifiers.push(...options.modifiers);
×
2738
            } else {
2739
                this.modifiers.push(options.modifiers);
35✔
2740
            }
2741
        }
2742
        this.tokens = {
381✔
2743
            ...this.tokens,
2744
            override: options.override
2745
        };
2746
        this.location = util.createBoundingLocation(
381✔
2747
            ...(this.modifiers),
2748
            util.createBoundingLocationFromTokens(this.tokens),
2749
            this.func
2750
        );
2751
    }
2752

2753
    public readonly kind = AstNodeKind.MethodStatement as AstNodeKind;
381✔
2754

2755
    public readonly modifiers: Token[] = [];
381✔
2756

2757
    public readonly tokens: {
2758
        readonly name: Identifier;
2759
        readonly override?: Token;
2760
    };
2761

2762
    public get accessModifier() {
2763
        return this.modifiers.find(x => accessModifiers.includes(x.kind));
560✔
2764
    }
2765

2766
    public readonly location: Location | undefined;
2767

2768
    /**
2769
     * Get the name of this method.
2770
     */
2771
    public getName(parseMode: ParseMode) {
2772
        return this.tokens.name.text;
269✔
2773
    }
2774

2775
    public get leadingTrivia(): Token[] {
2776
        return this.func.leadingTrivia;
37✔
2777
    }
2778

2779
    transpile(state: BrsTranspileState) {
2780
        if (this.tokens.name.text.toLowerCase() === 'new') {
60✔
2781
            this.ensureSuperConstructorCall(state);
46✔
2782
            //TODO we need to undo this at the bottom of this method
2783
            this.injectFieldInitializersForConstructor(state);
46✔
2784
        }
2785
        //TODO - remove type information from these methods because that doesn't work
2786
        //convert the `super` calls into the proper methods
2787
        const parentClassIndex = state.classStatement.getParentClassIndex(state);
60✔
2788
        const visitor = createVisitor({
60✔
2789
            VariableExpression: e => {
2790
                if (e.tokens.name.text.toLocaleLowerCase() === 'super') {
61✔
2791
                    state.editor.setProperty(e.tokens.name, 'text', `m.super${parentClassIndex}_new`);
18✔
2792
                }
2793
            },
2794
            DottedGetExpression: e => {
2795
                const beginningVariable = util.findBeginningVariableExpression(e);
30✔
2796
                const lowerName = beginningVariable?.getName(ParseMode.BrighterScript).toLowerCase();
30!
2797
                if (lowerName === 'super') {
30✔
2798
                    state.editor.setProperty(beginningVariable.tokens.name, 'text', 'm');
7✔
2799
                    state.editor.setProperty(e.tokens.name, 'text', `super${parentClassIndex}_${e.tokens.name.text}`);
7✔
2800
                }
2801
            }
2802
        });
2803
        const walkOptions: WalkOptions = { walkMode: WalkMode.visitExpressions };
60✔
2804
        for (const statement of this.func.body.statements) {
60✔
2805
            visitor(statement, undefined);
60✔
2806
            statement.walk(visitor, walkOptions);
60✔
2807
        }
2808
        return this.func.transpile(state);
60✔
2809
    }
2810

2811
    getTypedef(state: BrsTranspileState) {
2812
        const result: TranspileResult = [];
23✔
2813
        for (let comment of util.getLeadingComments(this) ?? []) {
23!
NEW
2814
            result.push(
×
2815
                comment.text,
2816
                state.newline,
2817
                state.indent()
2818
            );
2819
        }
2820
        for (let annotation of this.annotations ?? []) {
23✔
2821
            result.push(
2✔
2822
                ...annotation.getTypedef(state),
2823
                state.newline,
2824
                state.indent()
2825
            );
2826
        }
2827
        if (this.accessModifier) {
23✔
2828
            result.push(
8✔
2829
                this.accessModifier.text,
2830
                ' '
2831
            );
2832
        }
2833
        if (this.tokens.override) {
23✔
2834
            result.push('override ');
1✔
2835
        }
2836
        result.push(
23✔
2837
            ...this.func.getTypedef(state)
2838
        );
2839
        return result;
23✔
2840
    }
2841

2842
    /**
2843
     * All child classes must call the parent constructor. The type checker will warn users when they don't call it in their own class,
2844
     * but we still need to call it even if they have omitted it. This injects the super call if it's missing
2845
     */
2846
    private ensureSuperConstructorCall(state: BrsTranspileState) {
2847
        //if this class doesn't extend another class, quit here
2848
        if (state.classStatement!.getAncestors(state).length === 0) {
46✔
2849
            return;
28✔
2850
        }
2851

2852
        //check whether any calls to super exist
2853
        let containsSuperCall =
2854
            this.func.body.statements.findIndex((x) => {
18✔
2855
                //is a call statement
2856
                return isExpressionStatement(x) && isCallExpression(x.expression) &&
8✔
2857
                    //is a call to super
2858
                    util.findBeginningVariableExpression(x.expression.callee as any).tokens.name?.text.toLowerCase() === 'super';
21!
2859
            }) !== -1;
2860

2861
        //if a call to super exists, quit here
2862
        if (containsSuperCall) {
18✔
2863
            return;
7✔
2864
        }
2865

2866
        //this is a child class, and the constructor doesn't contain a call to super. Inject one
2867
        const superCall = new ExpressionStatement({
11✔
2868
            expression: new CallExpression({
2869
                callee: new VariableExpression({
2870
                    name: {
2871
                        kind: TokenKind.Identifier,
2872
                        text: 'super',
2873
                        isReserved: false,
2874
                        location: state.classStatement.tokens.name.location,
2875
                        leadingWhitespace: '',
2876
                        leadingTrivia: []
2877
                    }
2878
                }),
2879
                openingParen: {
2880
                    kind: TokenKind.LeftParen,
2881
                    text: '(',
2882
                    isReserved: false,
2883
                    location: state.classStatement.tokens.name.location,
2884
                    leadingWhitespace: '',
2885
                    leadingTrivia: []
2886
                },
2887
                closingParen: {
2888
                    kind: TokenKind.RightParen,
2889
                    text: ')',
2890
                    isReserved: false,
2891
                    location: state.classStatement.tokens.name.location,
2892
                    leadingWhitespace: '',
2893
                    leadingTrivia: []
2894
                },
2895
                args: []
2896
            })
2897
        });
2898
        state.editor.arrayUnshift(this.func.body.statements, superCall);
11✔
2899
    }
2900

2901
    /**
2902
     * Inject field initializers at the top of the `new` function (after any present `super()` call)
2903
     */
2904
    private injectFieldInitializersForConstructor(state: BrsTranspileState) {
2905
        let startingIndex = state.classStatement!.hasParentClass() ? 1 : 0;
46✔
2906

2907
        let newStatements = [] as Statement[];
46✔
2908
        //insert the field initializers in order
2909
        for (let field of state.classStatement!.fields) {
46✔
2910
            let thisQualifiedName = { ...field.tokens.name };
13✔
2911
            thisQualifiedName.text = 'm.' + field.tokens.name?.text;
13!
2912
            const fieldAssignment = field.initialValue
13✔
2913
                ? new AssignmentStatement({
13✔
2914
                    equals: field.tokens.equals,
2915
                    name: thisQualifiedName,
2916
                    value: field.initialValue
2917
                })
2918
                : new AssignmentStatement({
2919
                    equals: createToken(TokenKind.Equal, '=', field.tokens.name.location),
2920
                    name: thisQualifiedName,
2921
                    //if there is no initial value, set the initial value to `invalid`
2922
                    value: createInvalidLiteral('invalid', field.tokens.name.location)
2923
                });
2924
            // Add parent so namespace lookups work
2925
            fieldAssignment.parent = state.classStatement;
13✔
2926
            newStatements.push(fieldAssignment);
13✔
2927
        }
2928
        state.editor.arraySplice(this.func.body.statements, startingIndex, 0, ...newStatements);
46✔
2929
    }
2930

2931
    walk(visitor: WalkVisitor, options: WalkOptions) {
2932
        if (options.walkMode & InternalWalkMode.walkExpressions) {
2,108✔
2933
            walk(this, 'func', visitor, options);
1,675✔
2934
        }
2935
    }
2936
}
2937

2938
export class FieldStatement extends Statement implements TypedefProvider {
1✔
2939
    constructor(options: {
2940
        accessModifier?: Token;
2941
        name: Identifier;
2942
        as?: Token;
2943
        typeExpression?: TypeExpression;
2944
        equals?: Token;
2945
        initialValue?: Expression;
2946
        optional?: Token;
2947
    }) {
2948
        super();
309✔
2949
        this.tokens = {
309✔
2950
            accessModifier: options.accessModifier,
2951
            name: options.name,
2952
            as: options.as,
2953
            equals: options.equals,
2954
            optional: options.optional
2955
        };
2956
        this.typeExpression = options.typeExpression;
309✔
2957
        this.initialValue = options.initialValue;
309✔
2958

2959
        this.location = util.createBoundingLocation(
309✔
2960
            util.createBoundingLocationFromTokens(this.tokens),
2961
            this.typeExpression,
2962
            this.initialValue
2963
        );
2964
    }
2965

2966
    public readonly tokens: {
2967
        readonly accessModifier?: Token;
2968
        readonly name: Identifier;
2969
        readonly as?: Token;
2970
        readonly equals?: Token;
2971
        readonly optional?: Token;
2972
    };
2973

2974
    public readonly typeExpression?: TypeExpression;
2975
    public readonly initialValue?: Expression;
2976

2977
    public readonly kind = AstNodeKind.FieldStatement;
309✔
2978

2979
    /**
2980
     * Derive a ValueKind from the type token, or the initial value.
2981
     * Defaults to `DynamicType`
2982
     */
2983
    getType(options: GetTypeOptions) {
2984
        return this.typeExpression?.getType({ ...options, flags: SymbolTypeFlag.typetime }) ??
243✔
2985
            this.initialValue?.getType({ ...options, flags: SymbolTypeFlag.runtime }) ?? DynamicType.instance;
579✔
2986
    }
2987

2988
    public readonly location: Location | undefined;
2989

2990
    public get leadingTrivia(): Token[] {
2991
        return this.tokens.accessModifier?.leadingTrivia ?? this.tokens.optional?.leadingTrivia ?? this.tokens.name.leadingTrivia;
30!
2992
    }
2993

2994
    public get isOptional() {
2995
        return !!this.tokens.optional;
219✔
2996
    }
2997

2998
    transpile(state: BrsTranspileState): TranspileResult {
2999
        throw new Error('transpile not implemented for ' + Object.getPrototypeOf(this).constructor.name);
×
3000
    }
3001

3002
    getTypedef(state: BrsTranspileState) {
3003
        const result = [];
10✔
3004
        if (this.tokens.name) {
10!
3005
            for (let comment of util.getLeadingComments(this) ?? []) {
10!
NEW
3006
                result.push(
×
3007
                    comment.text,
3008
                    state.newline,
3009
                    state.indent()
3010
                );
3011
            }
3012
            for (let annotation of this.annotations ?? []) {
10✔
3013
                result.push(
2✔
3014
                    ...annotation.getTypedef(state),
3015
                    state.newline,
3016
                    state.indent()
3017
                );
3018
            }
3019

3020
            let type = this.getType({ flags: SymbolTypeFlag.typetime });
10✔
3021
            if (isInvalidType(type) || isVoidType(type)) {
10!
UNCOV
3022
                type = new DynamicType();
×
3023
            }
3024

3025
            result.push(
10✔
3026
                this.tokens.accessModifier?.text ?? 'public',
60!
3027
                ' '
3028
            );
3029
            if (this.isOptional) {
10!
NEW
3030
                result.push(this.tokens.optional.text, ' ');
×
3031
            }
3032
            result.push(this.tokens.name?.text,
10!
3033
                ' as ',
3034
                type.toTypeString()
3035
            );
3036
        }
3037
        return result;
10✔
3038
    }
3039

3040
    walk(visitor: WalkVisitor, options: WalkOptions) {
3041
        if (options.walkMode & InternalWalkMode.walkExpressions) {
1,528✔
3042
            walk(this, 'typeExpression', visitor, options);
1,333✔
3043
            walk(this, 'initialValue', visitor, options);
1,333✔
3044
        }
3045
    }
3046
}
3047

3048
export type MemberStatement = FieldStatement | MethodStatement;
3049

3050
export class TryCatchStatement extends Statement {
1✔
3051
    constructor(options?: {
3052
        try?: Token;
3053
        endTry?: Token;
3054
        tryBranch?: Block;
3055
        catchStatement?: CatchStatement;
3056
    }) {
3057
        super();
28✔
3058
        this.tokens = {
28✔
3059
            try: options.try,
3060
            endTry: options.endTry
3061
        };
3062
        this.tryBranch = options.tryBranch;
28✔
3063
        this.catchStatement = options.catchStatement;
28✔
3064
        this.location = util.createBoundingLocation(
28✔
3065
            this.tokens.try,
3066
            this.tryBranch,
3067
            this.catchStatement,
3068
            this.tokens.endTry
3069
        );
3070
    }
3071

3072
    public readonly tokens: {
3073
        readonly try?: Token;
3074
        readonly endTry?: Token;
3075
    };
3076

3077
    public readonly tryBranch: Block;
3078
    public readonly catchStatement: CatchStatement;
3079

3080
    public readonly kind = AstNodeKind.TryCatchStatement;
28✔
3081

3082
    public readonly location: Location | undefined;
3083

3084
    public transpile(state: BrsTranspileState): TranspileResult {
3085
        return [
4✔
3086
            state.transpileToken(this.tokens.try, 'try'),
3087
            ...this.tryBranch.transpile(state),
3088
            state.newline,
3089
            state.indent(),
3090
            ...(this.catchStatement?.transpile(state) ?? ['catch']),
24!
3091
            state.newline,
3092
            state.indent(),
3093
            state.transpileToken(this.tokens.endTry!, 'end try')
3094
        ];
3095
    }
3096

3097
    public walk(visitor: WalkVisitor, options: WalkOptions) {
3098
        if (this.tryBranch && options.walkMode & InternalWalkMode.walkStatements) {
59!
3099
            walk(this, 'tryBranch', visitor, options);
59✔
3100
            walk(this, 'catchStatement', visitor, options);
59✔
3101
        }
3102
    }
3103

3104
    public get leadingTrivia(): Token[] {
3105
        return this.tokens.try?.leadingTrivia ?? [];
12!
3106
    }
3107

3108
    public get endTrivia(): Token[] {
3109
        return this.tokens.endTry?.leadingTrivia ?? [];
1!
3110
    }
3111
}
3112

3113
export class CatchStatement extends Statement {
1✔
3114
    constructor(options?: {
3115
        catch?: Token;
3116
        exceptionVariable?: Identifier;
3117
        catchBranch?: Block;
3118
    }) {
3119
        super();
26✔
3120
        this.tokens = {
26✔
3121
            catch: options?.catch,
78!
3122
            exceptionVariable: options?.exceptionVariable
78!
3123
        };
3124
        this.catchBranch = options?.catchBranch;
26!
3125
        this.location = util.createBoundingLocation(
26✔
3126
            this.tokens.catch,
3127
            this.tokens.exceptionVariable,
3128
            this.catchBranch
3129
        );
3130
    }
3131

3132
    public readonly tokens: {
3133
        readonly catch?: Token;
3134
        readonly exceptionVariable?: Identifier;
3135
    };
3136

3137
    public readonly catchBranch?: Block;
3138

3139
    public readonly kind = AstNodeKind.CatchStatement;
26✔
3140

3141
    public readonly location: Location | undefined;
3142

3143
    public transpile(state: BrsTranspileState): TranspileResult {
3144
        return [
4✔
3145
            state.transpileToken(this.tokens.catch, 'catch'),
3146
            ' ',
3147
            this.tokens.exceptionVariable?.text ?? 'e',
24!
3148
            ...(this.catchBranch?.transpile(state) ?? [])
24!
3149
        ];
3150
    }
3151

3152
    public walk(visitor: WalkVisitor, options: WalkOptions) {
3153
        if (this.catchBranch && options.walkMode & InternalWalkMode.walkStatements) {
57!
3154
            walk(this, 'catchBranch', visitor, options);
57✔
3155
        }
3156
    }
3157

3158
    public get leadingTrivia(): Token[] {
3159
        return this.tokens.catch?.leadingTrivia ?? [];
4!
3160
    }
3161
}
3162

3163
export class ThrowStatement extends Statement {
1✔
3164
    constructor(options?: {
3165
        throw?: Token;
3166
        expression?: Expression;
3167
    }) {
3168
        super();
10✔
3169
        this.tokens = {
10✔
3170
            throw: options.throw
3171
        };
3172
        this.expression = options.expression;
10✔
3173
        this.location = util.createBoundingLocation(
10✔
3174
            this.tokens.throw,
3175
            this.expression
3176
        );
3177
    }
3178

3179
    public readonly tokens: {
3180
        readonly throw?: Token;
3181
    };
3182
    public readonly expression?: Expression;
3183

3184
    public readonly kind = AstNodeKind.ThrowStatement;
10✔
3185

3186
    public readonly location: Location | undefined;
3187

3188
    public transpile(state: BrsTranspileState) {
3189
        const result = [
5✔
3190
            state.transpileToken(this.tokens.throw, 'throw'),
3191
            ' '
3192
        ] as TranspileResult;
3193

3194
        //if we have an expression, transpile it
3195
        if (this.expression) {
5✔
3196
            result.push(
4✔
3197
                ...this.expression.transpile(state)
3198
            );
3199

3200
            //no expression found. Rather than emit syntax errors, provide a generic error message
3201
        } else {
3202
            result.push('"User-specified exception"');
1✔
3203
        }
3204
        return result;
5✔
3205
    }
3206

3207
    public walk(visitor: WalkVisitor, options: WalkOptions) {
3208
        if (this.expression && options.walkMode & InternalWalkMode.walkExpressions) {
33✔
3209
            walk(this, 'expression', visitor, options);
26✔
3210
        }
3211
    }
3212

3213
    public get leadingTrivia(): Token[] {
3214
        return this.tokens.throw?.leadingTrivia ?? [];
14!
3215
    }
3216
}
3217

3218

3219
export class EnumStatement extends Statement implements TypedefProvider {
1✔
3220
    constructor(options: {
3221
        enum?: Token;
3222
        name: Identifier;
3223
        endEnum?: Token;
3224
        body: Array<EnumMemberStatement>;
3225
    }) {
3226
        super();
156✔
3227
        this.tokens = {
156✔
3228
            enum: options.enum,
3229
            name: options.name,
3230
            endEnum: options.endEnum
3231
        };
3232
        this.symbolTable = new SymbolTable('Enum');
156✔
3233
        this.body = options.body ?? [];
156!
3234
    }
3235

3236
    public readonly tokens: {
3237
        readonly enum?: Token;
3238
        readonly name: Identifier;
3239
        readonly endEnum?: Token;
3240
    };
3241
    public readonly body: Array<EnumMemberStatement>;
3242

3243
    public readonly kind = AstNodeKind.EnumStatement;
156✔
3244

3245
    public get location(): Location | undefined {
3246
        return util.createBoundingLocation(
136✔
3247
            this.tokens.enum,
3248
            this.tokens.name,
3249
            ...this.body,
3250
            this.tokens.endEnum
3251
        );
3252
    }
3253

3254
    public getMembers() {
3255
        const result = [] as EnumMemberStatement[];
327✔
3256
        for (const statement of this.body) {
327✔
3257
            if (isEnumMemberStatement(statement)) {
679!
3258
                result.push(statement);
679✔
3259
            }
3260
        }
3261
        return result;
327✔
3262
    }
3263

3264
    public get leadingTrivia(): Token[] {
3265
        return this.tokens.enum?.leadingTrivia;
57!
3266
    }
3267

3268
    public get endTrivia(): Token[] {
NEW
3269
        return this.tokens.endEnum?.leadingTrivia ?? [];
×
3270
    }
3271

3272
    /**
3273
     * Get a map of member names and their values.
3274
     * All values are stored as their AST LiteralExpression representation (i.e. string enum values include the wrapping quotes)
3275
     */
3276
    public getMemberValueMap() {
3277
        const result = new Map<string, string>();
59✔
3278
        const members = this.getMembers();
59✔
3279
        let currentIntValue = 0;
59✔
3280
        for (const member of members) {
59✔
3281
            //if there is no value, assume an integer and increment the int counter
3282
            if (!member.value) {
148✔
3283
                result.set(member.name?.toLowerCase(), currentIntValue.toString());
33!
3284
                currentIntValue++;
33✔
3285

3286
                //if explicit integer value, use it and increment the int counter
3287
            } else if (isLiteralExpression(member.value) && member.value.tokens.value.kind === TokenKind.IntegerLiteral) {
115✔
3288
                //try parsing as integer literal, then as hex integer literal.
3289
                let tokenIntValue = util.parseInt(member.value.tokens.value.text) ?? util.parseInt(member.value.tokens.value.text.replace(/&h/i, '0x'));
29✔
3290
                if (tokenIntValue !== undefined) {
29!
3291
                    currentIntValue = tokenIntValue;
29✔
3292
                    currentIntValue++;
29✔
3293
                }
3294
                result.set(member.name?.toLowerCase(), member.value.tokens.value.text);
29!
3295

3296
                //simple unary expressions (like `-1`)
3297
            } else if (isUnaryExpression(member.value) && isLiteralExpression(member.value.right)) {
86✔
3298
                result.set(member.name?.toLowerCase(), member.value.tokens.operator.text + member.value.right.tokens.value.text);
1!
3299

3300
                //all other values
3301
            } else {
3302
                result.set(member.name?.toLowerCase(), (member.value as LiteralExpression)?.tokens?.value?.text ?? 'invalid');
85!
3303
            }
3304
        }
3305
        return result;
59✔
3306
    }
3307

3308
    public getMemberValue(name: string) {
3309
        return this.getMemberValueMap().get(name.toLowerCase());
56✔
3310
    }
3311

3312
    /**
3313
     * The name of the enum (without the namespace prefix)
3314
     */
3315
    public get name() {
3316
        return this.tokens.name?.text;
1!
3317
    }
3318

3319
    /**
3320
     * The name of the enum WITH its leading namespace (if applicable)
3321
     */
3322
    public get fullName() {
3323
        const name = this.tokens.name?.text;
674!
3324
        if (name) {
674!
3325
            const namespace = this.findAncestor<NamespaceStatement>(isNamespaceStatement);
674✔
3326

3327
            if (namespace) {
674✔
3328
                let namespaceName = namespace.getName(ParseMode.BrighterScript);
251✔
3329
                return `${namespaceName}.${name}`;
251✔
3330
            } else {
3331
                return name;
423✔
3332
            }
3333
        } else {
3334
            //return undefined which will allow outside callers to know that this doesn't have a name
3335
            return undefined;
×
3336
        }
3337
    }
3338

3339
    transpile(state: BrsTranspileState) {
3340
        //enum declarations do not exist at runtime, so don't transpile anything...
3341
        return [
23✔
3342
            state.transpileLeadingComments(this.tokens.enum)
3343
        ];
3344
    }
3345

3346
    getTypedef(state: BrsTranspileState) {
3347
        const result = [] as TranspileResult;
1✔
3348
        for (let comment of util.getLeadingComments(this) ?? []) {
1!
NEW
3349
            result.push(
×
3350
                comment.text,
3351
                state.newline,
3352
                state.indent()
3353
            );
3354
        }
3355
        for (let annotation of this.annotations ?? []) {
1!
3356
            result.push(
×
3357
                ...annotation.getTypedef(state),
3358
                state.newline,
3359
                state.indent()
3360
            );
3361
        }
3362
        result.push(
1✔
3363
            this.tokens.enum?.text ?? 'enum',
6!
3364
            ' ',
3365
            this.tokens.name.text
3366
        );
3367
        result.push(state.newline);
1✔
3368
        state.blockDepth++;
1✔
3369
        for (const member of this.body) {
1✔
3370
            if (isTypedefProvider(member)) {
1!
3371
                result.push(
1✔
3372
                    state.indent(),
3373
                    ...member.getTypedef(state),
3374
                    state.newline
3375
                );
3376
            }
3377
        }
3378
        state.blockDepth--;
1✔
3379
        result.push(
1✔
3380
            state.indent(),
3381
            this.tokens.endEnum?.text ?? 'end enum'
6!
3382
        );
3383
        return result;
1✔
3384
    }
3385

3386
    walk(visitor: WalkVisitor, options: WalkOptions) {
3387
        if (options.walkMode & InternalWalkMode.walkStatements) {
996!
3388
            walkArray(this.body, visitor, options, this);
996✔
3389

3390
        }
3391
    }
3392

3393
    getType(options: GetTypeOptions) {
3394
        const members = this.getMembers();
134✔
3395

3396
        const resultType = new EnumType(
134✔
3397
            this.fullName,
3398
            members[0]?.getType(options).underlyingType
402✔
3399
        );
3400
        resultType.pushMemberProvider(() => this.getSymbolTable());
134✔
3401
        for (const statement of members) {
134✔
3402
            resultType.addMember(statement?.tokens?.name?.text, { definingNode: statement }, statement.getType(options), SymbolTypeFlag.runtime);
264!
3403
        }
3404
        return resultType;
134✔
3405
    }
3406
}
3407

3408
export class EnumMemberStatement extends Statement implements TypedefProvider {
1✔
3409
    public constructor(options: {
3410
        name: Identifier;
3411
        equals?: Token;
3412
        value?: Expression;
3413
    }) {
3414
        super();
311✔
3415
        this.tokens = {
311✔
3416
            name: options.name,
3417
            equals: options.equals
3418
        };
3419
        this.value = options.value;
311✔
3420
    }
3421

3422
    public readonly tokens: {
3423
        readonly name: Identifier;
3424
        readonly equals?: Token;
3425
    };
3426
    public readonly value?: Expression;
3427

3428
    public readonly kind = AstNodeKind.EnumMemberStatement;
311✔
3429

3430
    public get location() {
3431
        return util.createBoundingLocation(
443✔
3432
            this.tokens.name,
3433
            this.tokens.equals,
3434
            this.value
3435
        );
3436
    }
3437

3438
    /**
3439
     * The name of the member
3440
     */
3441
    public get name() {
3442
        return this.tokens.name.text;
405✔
3443
    }
3444

3445
    public get leadingTrivia(): Token[] {
3446
        return this.tokens.name.leadingTrivia;
20✔
3447
    }
3448

3449
    public transpile(state: BrsTranspileState): TranspileResult {
3450
        return [];
×
3451
    }
3452

3453
    getTypedef(state: BrsTranspileState): TranspileResult {
3454
        const result: TranspileResult = [];
1✔
3455
        for (let comment of util.getLeadingComments(this) ?? []) {
1!
NEW
3456
            result.push(
×
3457
                comment.text,
3458
                state.newline,
3459
                state.indent()
3460
            );
3461
        }
3462
        result.push(this.tokens.name.text);
1✔
3463
        if (this.tokens.equals) {
1!
NEW
3464
            result.push(' ', this.tokens.equals.text, ' ');
×
3465
            if (this.value) {
×
3466
                result.push(
×
3467
                    ...this.value.transpile(state)
3468
                );
3469
            }
3470
        }
3471
        return result;
1✔
3472
    }
3473

3474
    walk(visitor: WalkVisitor, options: WalkOptions) {
3475
        if (this.value && options.walkMode & InternalWalkMode.walkExpressions) {
2,384✔
3476
            walk(this, 'value', visitor, options);
1,131✔
3477
        }
3478
    }
3479

3480
    getType(options: GetTypeOptions) {
3481
        return new EnumMemberType(
398✔
3482
            (this.parent as EnumStatement)?.fullName,
1,194!
3483
            this.tokens?.name?.text,
2,388!
3484
            this.value?.getType(options)
1,194✔
3485
        );
3486
    }
3487
}
3488

3489
export class ConstStatement extends Statement implements TypedefProvider {
1✔
3490
    public constructor(options: {
3491
        const?: Token;
3492
        name: Identifier;
3493
        equals?: Token;
3494
        value: Expression;
3495
    }) {
3496
        super();
146✔
3497
        this.tokens = {
146✔
3498
            const: options.const,
3499
            name: options.name,
3500
            equals: options.equals
3501
        };
3502
        this.value = options.value;
146✔
3503
        this.location = util.createBoundingLocation(this.tokens.const, this.tokens.name, this.tokens.equals, this.value);
146✔
3504
    }
3505

3506
    public readonly tokens: {
3507
        readonly const: Token;
3508
        readonly name: Identifier;
3509
        readonly equals: Token;
3510
    };
3511
    public readonly value: Expression;
3512

3513
    public readonly kind = AstNodeKind.ConstStatement;
146✔
3514

3515
    public readonly location: Location | undefined;
3516

3517
    public get name() {
UNCOV
3518
        return this.tokens.name.text;
×
3519
    }
3520

3521
    public get leadingTrivia(): Token[] {
3522
        return this.tokens.const?.leadingTrivia;
37!
3523
    }
3524

3525
    /**
3526
     * The name of the statement WITH its leading namespace (if applicable)
3527
     */
3528
    public get fullName() {
3529
        const name = this.tokens.name?.text;
218!
3530
        if (name) {
218!
3531
            const namespace = this.findAncestor<NamespaceStatement>(isNamespaceStatement);
218✔
3532
            if (namespace) {
218✔
3533
                let namespaceName = namespace.getName(ParseMode.BrighterScript);
195✔
3534
                return `${namespaceName}.${name}`;
195✔
3535
            } else {
3536
                return name;
23✔
3537
            }
3538
        } else {
3539
            //return undefined which will allow outside callers to know that this doesn't have a name
3540
            return undefined;
×
3541
        }
3542
    }
3543

3544
    public transpile(state: BrsTranspileState): TranspileResult {
3545
        //const declarations don't exist at runtime, so just transpile empty
3546
        return [state.transpileLeadingComments(this.tokens.const)];
21✔
3547
    }
3548

3549
    getTypedef(state: BrsTranspileState): TranspileResult {
3550
        const result: TranspileResult = [];
3✔
3551
        for (let comment of util.getLeadingComments(this) ?? []) {
3!
NEW
3552
            result.push(
×
3553
                comment.text,
3554
                state.newline,
3555
                state.indent()
3556
            );
3557
        }
3558
        result.push(
3✔
3559
            this.tokens.const ? state.tokenToSourceNode(this.tokens.const) : 'const',
3!
3560
            ' ',
3561
            state.tokenToSourceNode(this.tokens.name),
3562
            ' ',
3563
            this.tokens.equals ? state.tokenToSourceNode(this.tokens.equals) : '=',
3!
3564
            ' ',
3565
            ...this.value.transpile(state)
3566
        );
3567
        return result;
3✔
3568
    }
3569

3570
    walk(visitor: WalkVisitor, options: WalkOptions) {
3571
        if (this.value && options.walkMode & InternalWalkMode.walkExpressions) {
970✔
3572
            walk(this, 'value', visitor, options);
838✔
3573
        }
3574
    }
3575

3576
    getType(options: GetTypeOptions) {
3577
        return this.value.getType(options);
144✔
3578
    }
3579
}
3580

3581
export class ContinueStatement extends Statement {
1✔
3582
    constructor(options: {
3583
        continue?: Token;
3584
        loopType: Token;
3585
    }
3586
    ) {
3587
        super();
11✔
3588
        this.tokens = {
11✔
3589
            continue: options.continue,
3590
            loopType: options.loopType
3591
        };
3592
        this.location = util.createBoundingLocation(
11✔
3593
            this.tokens.continue,
3594
            this.tokens.loopType
3595
        );
3596
    }
3597

3598
    public readonly tokens: {
3599
        readonly continue?: Token;
3600
        readonly loopType: Token;
3601
    };
3602

3603
    public readonly kind = AstNodeKind.ContinueStatement;
11✔
3604

3605
    public readonly location: Location | undefined;
3606

3607
    transpile(state: BrsTranspileState) {
3608
        return [
3✔
3609
            state.sourceNode(this.tokens.continue, this.tokens.continue?.text ?? 'continue'),
18!
3610
            this.tokens.loopType?.leadingWhitespace ?? ' ',
18✔
3611
            state.sourceNode(this.tokens.continue, this.tokens.loopType?.text)
9✔
3612
        ];
3613
    }
3614
    walk(visitor: WalkVisitor, options: WalkOptions) {
3615
        //nothing to walk
3616
    }
3617

3618
    public get leadingTrivia(): Token[] {
3619
        return this.tokens.continue?.leadingTrivia ?? [];
6!
3620
    }
3621
}
3622

3623

3624
export class TypecastStatement extends Statement {
1✔
3625
    constructor(options: {
3626
        typecast?: Token;
3627
        typecastExpression: TypecastExpression;
3628
    }
3629
    ) {
3630
        super();
23✔
3631
        this.tokens = {
23✔
3632
            typecast: options.typecast
3633
        };
3634
        this.typecastExpression = options.typecastExpression;
23✔
3635
        this.location = util.createBoundingLocation(
23✔
3636
            this.tokens.typecast,
3637
            this.typecastExpression
3638
        );
3639
    }
3640

3641
    public readonly tokens: {
3642
        readonly typecast?: Token;
3643
    };
3644

3645
    public readonly typecastExpression: TypecastExpression;
3646

3647
    public readonly kind = AstNodeKind.TypecastStatement;
23✔
3648

3649
    public readonly location: Location;
3650

3651
    transpile(state: BrsTranspileState) {
3652
        //the typecast statement is a comment just for debugging purposes
3653
        return [
1✔
3654
            state.transpileToken(this.tokens.typecast, 'typecast', true),
3655
            ' ',
3656
            this.typecastExpression.obj.transpile(state),
3657
            ' ',
3658
            state.transpileToken(this.typecastExpression.tokens.as, 'as'),
3659
            ' ',
3660
            this.typecastExpression.typeExpression.transpile(state)
3661
        ];
3662
    }
3663

3664
    walk(visitor: WalkVisitor, options: WalkOptions) {
3665
        if (options.walkMode & InternalWalkMode.walkExpressions) {
135✔
3666
            walk(this, 'typecastExpression', visitor, options);
125✔
3667
        }
3668
    }
3669

3670
    get leadingTrivia(): Token[] {
NEW
3671
        return this.tokens.typecast?.leadingTrivia ?? [];
×
3672
    }
3673

3674
    getType(options: GetTypeOptions): BscType {
3675
        return this.typecastExpression.getType(options);
19✔
3676
    }
3677
}
3678

3679
export class ConditionalCompileErrorStatement extends Statement {
1✔
3680
    constructor(options: {
3681
        hashError?: Token;
3682
        message: Token;
3683
    }) {
3684
        super();
9✔
3685
        this.tokens = {
9✔
3686
            hashError: options.hashError,
3687
            message: options.message
3688
        };
3689
        this.location = util.createBoundingLocation(util.createBoundingLocationFromTokens(this.tokens));
9✔
3690
    }
3691

3692
    public readonly tokens: {
3693
        readonly hashError?: Token;
3694
        readonly message: Token;
3695
    };
3696

3697

3698
    public readonly kind = AstNodeKind.ConditionalCompileErrorStatement;
9✔
3699

3700
    public readonly location: Location | undefined;
3701

3702
    transpile(state: BrsTranspileState) {
3703
        return [
3✔
3704
            state.transpileToken(this.tokens.hashError, '#error'),
3705
            ' ',
3706
            state.transpileToken(this.tokens.message)
3707

3708
        ];
3709
    }
3710

3711
    walk(visitor: WalkVisitor, options: WalkOptions) {
3712
        // nothing to walk
3713
    }
3714

3715
    get leadingTrivia(): Token[] {
3716
        return this.tokens.hashError.leadingTrivia ?? [];
6!
3717
    }
3718
}
3719

3720
export class AliasStatement extends Statement {
1✔
3721
    constructor(options: {
3722
        alias?: Token;
3723
        name: Token;
3724
        equals?: Token;
3725
        value: VariableExpression | DottedGetExpression;
3726
    }
3727
    ) {
3728
        super();
32✔
3729
        this.tokens = {
32✔
3730
            alias: options.alias,
3731
            name: options.name,
3732
            equals: options.equals
3733
        };
3734
        this.value = options.value;
32✔
3735
        this.location = util.createBoundingLocation(
32✔
3736
            this.tokens.alias,
3737
            this.tokens.name,
3738
            this.tokens.equals,
3739
            this.value
3740
        );
3741
    }
3742

3743
    public readonly tokens: {
3744
        readonly alias?: Token;
3745
        readonly name: Token;
3746
        readonly equals?: Token;
3747
    };
3748

3749
    public readonly value: Expression;
3750

3751
    public readonly kind = AstNodeKind.AliasStatement;
32✔
3752

3753
    public readonly location: Location;
3754

3755
    transpile(state: BrsTranspileState) {
3756
        //the alias statement is a comment just for debugging purposes
3757
        return [
12✔
3758
            state.transpileToken(this.tokens.alias, 'alias', true),
3759
            ' ',
3760
            state.transpileToken(this.tokens.name),
3761
            ' ',
3762
            state.transpileToken(this.tokens.equals, '='),
3763
            ' ',
3764
            this.value.transpile(state)
3765
        ];
3766
    }
3767

3768
    walk(visitor: WalkVisitor, options: WalkOptions) {
3769
        if (options.walkMode & InternalWalkMode.walkExpressions) {
221✔
3770
            walk(this, 'value', visitor, options);
192✔
3771
        }
3772
    }
3773

3774
    get leadingTrivia(): Token[] {
3775
        return this.tokens.alias?.leadingTrivia ?? [];
24!
3776
    }
3777

3778
    getType(options: GetTypeOptions): BscType {
3779
        return this.value.getType(options);
1✔
3780
    }
3781
}
3782

3783
export class ConditionalCompileStatement extends Statement {
1✔
3784
    constructor(options: {
3785
        hashIf?: Token;
3786
        not?: Token;
3787
        condition: Token;
3788
        hashElse?: Token;
3789
        hashEndIf?: Token;
3790
        thenBranch: Block;
3791
        elseBranch?: ConditionalCompileStatement | Block;
3792
    }) {
3793
        super();
49✔
3794
        this.thenBranch = options.thenBranch;
49✔
3795
        this.elseBranch = options.elseBranch;
49✔
3796

3797
        this.tokens = {
49✔
3798
            hashIf: options.hashIf,
3799
            not: options.not,
3800
            condition: options.condition,
3801
            hashElse: options.hashElse,
3802
            hashEndIf: options.hashEndIf
3803
        };
3804

3805
        this.location = util.createBoundingLocation(
49✔
3806
            util.createBoundingLocationFromTokens(this.tokens),
3807
            this.thenBranch,
3808
            this.elseBranch
3809
        );
3810
    }
3811

3812
    readonly tokens: {
3813
        readonly hashIf?: Token;
3814
        readonly not?: Token;
3815
        readonly condition: Token;
3816
        readonly hashElse?: Token;
3817
        readonly hashEndIf?: Token;
3818
    };
3819
    public readonly thenBranch: Block;
3820
    public readonly elseBranch?: ConditionalCompileStatement | Block;
3821

3822
    public readonly kind = AstNodeKind.ConditionalCompileStatement;
49✔
3823

3824
    public readonly location: Location | undefined;
3825

3826
    transpile(state: BrsTranspileState) {
3827
        let results = [] as TranspileResult;
6✔
3828
        //if   (already indented by block)
3829
        if (!state.conditionalCompileStatement) {
6✔
3830
            // only transpile the #if in the case when we're not in a conditionalCompileStatement already
3831
            results.push(state.transpileToken(this.tokens.hashIf ?? createToken(TokenKind.HashIf)));
3!
3832
        }
3833

3834
        results.push(' ');
6✔
3835
        //conditions
3836
        if (this.tokens.not) {
6✔
3837
            results.push('not');
2✔
3838
            results.push(' ');
2✔
3839
        }
3840
        results.push(state.transpileToken(this.tokens.condition));
6✔
3841
        state.lineage.unshift(this);
6✔
3842

3843
        //if statement body
3844
        let thenNodes = this.thenBranch.transpile(state);
6✔
3845
        state.lineage.shift();
6✔
3846
        if (thenNodes.length > 0) {
6!
3847
            results.push(thenNodes);
6✔
3848
        }
3849
        //else branch
3850
        if (this.elseBranch) {
6!
3851
            const elseIsCC = isConditionalCompileStatement(this.elseBranch);
6✔
3852
            const endBlockToken = elseIsCC ? (this.elseBranch as ConditionalCompileStatement).tokens.hashIf ?? createToken(TokenKind.HashElseIf) : this.tokens.hashElse;
6!
3853
            //else
3854

3855
            results.push(...state.transpileEndBlockToken(this.thenBranch, endBlockToken, createToken(TokenKind.HashElse).text));
6✔
3856

3857
            if (elseIsCC) {
6✔
3858
                //chained else if
3859
                state.lineage.unshift(this.elseBranch);
3✔
3860

3861
                // transpile following #if with knowledge of current
3862
                const existingCCStmt = state.conditionalCompileStatement;
3✔
3863
                state.conditionalCompileStatement = this;
3✔
3864
                let body = this.elseBranch.transpile(state);
3✔
3865
                state.conditionalCompileStatement = existingCCStmt;
3✔
3866

3867
                state.lineage.shift();
3✔
3868

3869
                if (body.length > 0) {
3!
3870
                    //zero or more spaces between the `else` and the `if`
3871
                    results.push(...body);
3✔
3872

3873
                    // stop here because chained if will transpile the rest
3874
                    return results;
3✔
3875
                } else {
NEW
3876
                    results.push('\n');
×
3877
                }
3878

3879
            } else {
3880
                //else body
3881
                state.lineage.unshift(this.tokens.hashElse!);
3✔
3882
                let body = this.elseBranch.transpile(state);
3✔
3883
                state.lineage.shift();
3✔
3884

3885
                if (body.length > 0) {
3!
3886
                    results.push(...body);
3✔
3887
                }
3888
            }
3889
        }
3890

3891
        //end if
3892
        results.push(...state.transpileEndBlockToken(this.elseBranch ?? this.thenBranch, this.tokens.hashEndIf, '#end if'));
3!
3893

3894
        return results;
3✔
3895
    }
3896

3897
    walk(visitor: WalkVisitor, options: WalkOptions) {
3898
        if (options.walkMode & InternalWalkMode.walkStatements) {
160!
3899
            const bsConsts = options.bsConsts ?? this.getBsConsts();
160✔
3900
            let conditionTrue = bsConsts?.get(this.tokens.condition.text.toLowerCase());
160!
3901
            if (this.tokens.not) {
160✔
3902
                // flips the boolean value
3903
                conditionTrue = !conditionTrue;
23✔
3904
            }
3905
            const walkFalseBlocks = options.walkMode & InternalWalkMode.visitFalseConditionalCompilationBlocks;
160✔
3906
            if (conditionTrue || walkFalseBlocks) {
160✔
3907
                walk(this, 'thenBranch', visitor, options);
96✔
3908
            }
3909
            if (this.elseBranch && (!conditionTrue || walkFalseBlocks)) {
160✔
3910
                walk(this, 'elseBranch', visitor, options);
52✔
3911
            }
3912
        }
3913
    }
3914

3915
    get leadingTrivia(): Token[] {
3916
        return this.tokens.hashIf?.leadingTrivia ?? [];
6!
3917
    }
3918
}
3919

3920

3921
export class ConditionalCompileConstStatement extends Statement {
1✔
3922
    constructor(options: {
3923
        hashConst?: Token;
3924
        assignment: AssignmentStatement;
3925
    }) {
3926
        super();
17✔
3927
        this.tokens = {
17✔
3928
            hashConst: options.hashConst
3929
        };
3930
        this.assignment = options.assignment;
17✔
3931
        this.location = util.createBoundingLocation(util.createBoundingLocationFromTokens(this.tokens), this.assignment);
17✔
3932
    }
3933

3934
    public readonly tokens: {
3935
        readonly hashConst?: Token;
3936
    };
3937

3938
    public readonly assignment: AssignmentStatement;
3939

3940
    public readonly kind = AstNodeKind.ConditionalCompileConstStatement;
17✔
3941

3942
    public readonly location: Location | undefined;
3943

3944
    transpile(state: BrsTranspileState) {
3945
        return [
3✔
3946
            state.transpileToken(this.tokens.hashConst, '#const'),
3947
            ' ',
3948
            state.transpileToken(this.assignment.tokens.name),
3949
            ' ',
3950
            state.transpileToken(this.assignment.tokens.equals, '='),
3951
            ' ',
3952
            ...this.assignment.value.transpile(state)
3953
        ];
3954

3955
    }
3956

3957
    walk(visitor: WalkVisitor, options: WalkOptions) {
3958
        // nothing to walk
3959
    }
3960

3961

3962
    get leadingTrivia(): Token[] {
3963
        return this.tokens.hashConst.leadingTrivia ?? [];
6!
3964
    }
3965
}
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