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

rokucommunity / brighterscript / #13286

21 Nov 2024 03:03PM UTC coverage: 88.232% (-0.9%) from 89.088%
#13286

push

TwitchBronBron
0.68.0

6754 of 8102 branches covered (83.36%)

Branch coverage included in aggregate %.

8983 of 9734 relevant lines covered (92.28%)

1868.45 hits per line

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

93.83
/src/Program.ts
1
import * as assert from 'assert';
1✔
2
import * as fsExtra from 'fs-extra';
1✔
3
import * as path from 'path';
1✔
4
import type { CodeAction, CompletionItem, Position, Range, SignatureInformation, Location, DocumentSymbol } from 'vscode-languageserver';
5
import { CompletionItemKind } from 'vscode-languageserver';
1✔
6
import type { BsConfig, FinalizedBsConfig } from './BsConfig';
7
import { Scope } from './Scope';
1✔
8
import { DiagnosticMessages } from './DiagnosticMessages';
1✔
9
import { BrsFile } from './files/BrsFile';
1✔
10
import { XmlFile } from './files/XmlFile';
1✔
11
import type { BsDiagnostic, File, FileReference, FileObj, BscFile, SemanticToken, AfterFileTranspileEvent, FileLink, ProvideHoverEvent, ProvideCompletionsEvent, Hover, ProvideDefinitionEvent, ProvideReferencesEvent, ProvideDocumentSymbolsEvent, ProvideWorkspaceSymbolsEvent } from './interfaces';
12
import { standardizePath as s, util } from './util';
1✔
13
import { XmlScope } from './XmlScope';
1✔
14
import { DiagnosticFilterer } from './DiagnosticFilterer';
1✔
15
import { DependencyGraph } from './DependencyGraph';
1✔
16
import type { Logger } from './logging';
17
import { LogLevel, createLogger } from './logging';
1✔
18
import chalk from 'chalk';
1✔
19
import { globalFile } from './globalCallables';
1✔
20
import { parseManifest, getBsConst } from './preprocessor/Manifest';
1✔
21
import { URI } from 'vscode-uri';
1✔
22
import PluginInterface from './PluginInterface';
1✔
23
import { isBrsFile, isXmlFile, isXmlScope, isNamespaceStatement } from './astUtils/reflection';
1✔
24
import type { FunctionStatement, NamespaceStatement } from './parser/Statement';
25
import { BscPlugin } from './bscPlugin/BscPlugin';
1✔
26
import { AstEditor } from './astUtils/AstEditor';
1✔
27
import type { SourceMapGenerator } from 'source-map';
28
import { rokuDeploy } from 'roku-deploy';
1✔
29
import type { Statement } from './parser/AstNode';
30
import { CallExpressionInfo } from './bscPlugin/CallExpressionInfo';
1✔
31
import { SignatureHelpUtil } from './bscPlugin/SignatureHelpUtil';
1✔
32
import { DiagnosticSeverityAdjuster } from './DiagnosticSeverityAdjuster';
1✔
33

34
const startOfSourcePkgPath = `source${path.sep}`;
1✔
35
const bslibNonAliasedRokuModulesPkgPath = s`source/roku_modules/rokucommunity_bslib/bslib.brs`;
1✔
36
const bslibAliasedRokuModulesPkgPath = s`source/roku_modules/bslib/bslib.brs`;
1✔
37

38
export interface SourceObj {
39
    /**
40
     * @deprecated use `srcPath` instead
41
     */
42
    pathAbsolute: string;
43
    srcPath: string;
44
    source: string;
45
    definitions?: string;
46
}
47

48
export interface TranspileObj {
49
    file: BscFile;
50
    outputPath: string;
51
}
52

53
export interface SignatureInfoObj {
54
    index: number;
55
    key: string;
56
    signature: SignatureInformation;
57
}
58

59
export class Program {
1✔
60
    constructor(
61
        /**
62
         * The root directory for this program
63
         */
64
        options: BsConfig,
65
        logger?: Logger,
66
        plugins?: PluginInterface
67
    ) {
68
        this.options = util.normalizeConfig(options);
1,271✔
69
        this.logger = logger ?? createLogger(options);
1,271✔
70
        this.plugins = plugins || new PluginInterface([], { logger: this.logger });
1,271✔
71

72
        //inject the bsc plugin as the first plugin in the stack.
73
        this.plugins.addFirst(new BscPlugin());
1,271✔
74

75
        //normalize the root dir path
76
        this.options.rootDir = util.getRootDir(this.options);
1,271✔
77

78
        this.createGlobalScope();
1,271✔
79
    }
80

81
    public options: FinalizedBsConfig;
82
    public logger: Logger;
83

84
    private createGlobalScope() {
85
        //create the 'global' scope
86
        this.globalScope = new Scope('global', this, 'scope:global');
1,271✔
87
        this.globalScope.attachDependencyGraph(this.dependencyGraph);
1,271✔
88
        this.scopes.global = this.globalScope;
1,271✔
89
        //hardcode the files list for global scope to only contain the global file
90
        this.globalScope.getAllFiles = () => [globalFile];
18,817✔
91
        this.globalScope.validate();
1,271✔
92
        //for now, disable validation of global scope because the global files have some duplicate method declarations
93
        this.globalScope.getDiagnostics = () => [];
1,271✔
94
        //TODO we might need to fix this because the isValidated clears stuff now
95
        (this.globalScope as any).isValidated = true;
1,271✔
96
    }
97

98
    /**
99
     * A graph of all files and their dependencies.
100
     * For example:
101
     *      File.xml -> [lib1.brs, lib2.brs]
102
     *      lib2.brs -> [lib3.brs] //via an import statement
103
     */
104
    private dependencyGraph = new DependencyGraph();
1,271✔
105

106
    private diagnosticFilterer = new DiagnosticFilterer();
1,271✔
107

108
    private diagnosticAdjuster = new DiagnosticSeverityAdjuster();
1,271✔
109

110
    /**
111
     * A scope that contains all built-in global functions.
112
     * All scopes should directly or indirectly inherit from this scope
113
     */
114
    public globalScope: Scope = undefined as any;
1,271✔
115

116
    /**
117
     * Plugins which can provide extra diagnostics or transform AST
118
     */
119
    public plugins: PluginInterface;
120

121
    /**
122
     * A set of diagnostics. This does not include any of the scope diagnostics.
123
     * Should only be set from `this.validate()`
124
     */
125
    private diagnostics = [] as BsDiagnostic[];
1,271✔
126

127
    /**
128
     * The path to bslib.brs (the BrightScript runtime for certain BrighterScript features)
129
     */
130
    public get bslibPkgPath() {
131
        //if there's an aliased (preferred) version of bslib from roku_modules loaded into the program, use that
132
        if (this.getFile(bslibAliasedRokuModulesPkgPath)) {
413✔
133
            return bslibAliasedRokuModulesPkgPath;
2✔
134

135
            //if there's a non-aliased version of bslib from roku_modules, use that
136
        } else if (this.getFile(bslibNonAliasedRokuModulesPkgPath)) {
411✔
137
            return bslibNonAliasedRokuModulesPkgPath;
3✔
138

139
            //default to the embedded version
140
        } else {
141
            return `${this.options.bslibDestinationDir}${path.sep}bslib.brs`;
408✔
142
        }
143
    }
144

145
    public get bslibPrefix() {
146
        if (this.bslibPkgPath === bslibNonAliasedRokuModulesPkgPath) {
386✔
147
            return 'rokucommunity_bslib';
3✔
148
        } else {
149
            return 'bslib';
383✔
150
        }
151
    }
152

153

154
    /**
155
     * A map of every file loaded into this program, indexed by its original file location
156
     */
157
    public files = {} as Record<string, BscFile>;
1,271✔
158
    private pkgMap = {} as Record<string, BscFile>;
1,271✔
159

160
    private scopes = {} as Record<string, Scope>;
1,271✔
161

162
    protected addScope(scope: Scope) {
163
        this.scopes[scope.name] = scope;
1,161✔
164
    }
165

166
    /**
167
     * A map of every component currently loaded into the program, indexed by the component name.
168
     * It is a compile-time error to have multiple components with the same name. However, we store an array of components
169
     * by name so we can provide a better developer expreience. You shouldn't be directly accessing this array,
170
     * but if you do, only ever use the component at index 0.
171
     */
172
    private components = {} as Record<string, { file: XmlFile; scope: XmlScope }[]>;
1,271✔
173

174
    /**
175
     * Get the component with the specified name
176
     */
177
    public getComponent(componentName: string) {
178
        if (componentName) {
484✔
179
            //return the first compoment in the list with this name
180
            //(components are ordered in this list by pkgPath to ensure consistency)
181
            return this.components[componentName.toLowerCase()]?.[0];
466✔
182
        } else {
183
            return undefined;
18✔
184
        }
185
    }
186

187
    /**
188
     * Register (or replace) the reference to a component in the component map
189
     */
190
    private registerComponent(xmlFile: XmlFile, scope: XmlScope) {
191
        const key = (xmlFile.componentName?.text ?? xmlFile.pkgPath).toLowerCase();
208✔
192
        if (!this.components[key]) {
208✔
193
            this.components[key] = [];
196✔
194
        }
195
        this.components[key].push({
208✔
196
            file: xmlFile,
197
            scope: scope
198
        });
199
        this.components[key].sort((a, b) => {
208✔
200
            const pathA = a.file.pkgPath.toLowerCase();
5✔
201
            const pathB = b.file.pkgPath.toLowerCase();
5✔
202
            if (pathA < pathB) {
5✔
203
                return -1;
1✔
204
            } else if (pathA > pathB) {
4!
205
                return 1;
4✔
206
            }
207
            return 0;
×
208
        });
209
        this.syncComponentDependencyGraph(this.components[key]);
208✔
210
    }
211

212
    /**
213
     * Remove the specified component from the components map
214
     */
215
    private unregisterComponent(xmlFile: XmlFile) {
216
        const key = (xmlFile.componentName?.text ?? xmlFile.pkgPath).toLowerCase();
11✔
217
        const arr = this.components[key] || [];
11!
218
        for (let i = 0; i < arr.length; i++) {
11✔
219
            if (arr[i].file === xmlFile) {
11!
220
                arr.splice(i, 1);
11✔
221
                break;
11✔
222
            }
223
        }
224
        this.syncComponentDependencyGraph(arr);
11✔
225
    }
226

227
    /**
228
     * re-attach the dependency graph with a new key for any component who changed
229
     * their position in their own named array (only matters when there are multiple
230
     * components with the same name)
231
     */
232
    private syncComponentDependencyGraph(components: Array<{ file: XmlFile; scope: XmlScope }>) {
233
        //reattach every dependency graph
234
        for (let i = 0; i < components.length; i++) {
219✔
235
            const { file, scope } = components[i];
214✔
236

237
            //attach (or re-attach) the dependencyGraph for every component whose position changed
238
            if (file.dependencyGraphIndex !== i) {
214✔
239
                file.dependencyGraphIndex = i;
210✔
240
                file.attachDependencyGraph(this.dependencyGraph);
210✔
241
                scope.attachDependencyGraph(this.dependencyGraph);
210✔
242
            }
243
        }
244
    }
245

246
    /**
247
     * Get a list of all files that are included in the project but are not referenced
248
     * by any scope in the program.
249
     */
250
    public getUnreferencedFiles() {
251
        let result = [] as File[];
673✔
252
        for (let filePath in this.files) {
673✔
253
            let file = this.files[filePath];
853✔
254
            //is this file part of a scope
255
            if (!this.getFirstScopeForFile(file)) {
853✔
256
                //no scopes reference this file. add it to the list
257
                result.push(file);
20✔
258
            }
259
        }
260
        return result;
673✔
261
    }
262

263
    /**
264
     * Get the list of errors for the entire program. It's calculated on the fly
265
     * by walking through every file, so call this sparingly.
266
     */
267
    public getDiagnostics() {
268
        return this.logger.time(LogLevel.info, ['Program.getDiagnostics()'], () => {
673✔
269

270
            let diagnostics = [...this.diagnostics];
673✔
271

272
            //get the diagnostics from all scopes
273
            for (let scopeName in this.scopes) {
673✔
274
                let scope = this.scopes[scopeName];
1,396✔
275
                diagnostics.push(
1,396✔
276
                    ...scope.getDiagnostics()
277
                );
278
            }
279

280
            //get the diagnostics from all unreferenced files
281
            let unreferencedFiles = this.getUnreferencedFiles();
673✔
282
            for (let file of unreferencedFiles) {
673✔
283
                diagnostics.push(
20✔
284
                    ...file.getDiagnostics()
285
                );
286
            }
287
            const filteredDiagnostics = this.logger.time(LogLevel.debug, ['filter diagnostics'], () => {
673✔
288
                //filter out diagnostics based on our diagnostic filters
289
                let finalDiagnostics = this.diagnosticFilterer.filter({
673✔
290
                    ...this.options,
291
                    rootDir: this.options.rootDir
292
                }, diagnostics);
293
                return finalDiagnostics;
673✔
294
            });
295

296
            this.logger.time(LogLevel.debug, ['adjust diagnostics severity'], () => {
673✔
297
                this.diagnosticAdjuster.adjust(this.options, diagnostics);
673✔
298
            });
299

300
            this.logger.info(`diagnostic counts: total=${chalk.yellow(diagnostics.length.toString())}, after filter=${chalk.yellow(filteredDiagnostics.length.toString())}`);
673✔
301
            return filteredDiagnostics;
673✔
302
        });
303
    }
304

305
    public addDiagnostics(diagnostics: BsDiagnostic[]) {
306
        this.diagnostics.push(...diagnostics);
13✔
307
    }
308

309
    /**
310
     * Determine if the specified file is loaded in this program right now.
311
     * @param filePath the absolute or relative path to the file
312
     * @param normalizePath should the provided path be normalized before use
313
     */
314
    public hasFile(filePath: string, normalizePath = true) {
1,256✔
315
        return !!this.getFile(filePath, normalizePath);
1,256✔
316
    }
317

318
    public getPkgPath(...args: any[]): any { //eslint-disable-line
319
        throw new Error('Not implemented');
×
320
    }
321

322
    /**
323
     * roku filesystem is case INsensitive, so find the scope by key case insensitive
324
     */
325
    public getScopeByName(scopeName: string): Scope | undefined {
326
        if (!scopeName) {
35!
327
            return undefined;
×
328
        }
329
        //most scopes are xml file pkg paths. however, the ones that are not are single names like "global" and "scope",
330
        //so it's safe to run the standardizePkgPath method
331
        scopeName = s`${scopeName}`;
35✔
332
        let key = Object.keys(this.scopes).find(x => x.toLowerCase() === scopeName.toLowerCase());
86✔
333
        return this.scopes[key!];
35✔
334
    }
335

336
    /**
337
     * Return all scopes
338
     */
339
    public getScopes() {
340
        return Object.values(this.scopes);
9✔
341
    }
342

343
    /**
344
     * Find the scope for the specified component
345
     */
346
    public getComponentScope(componentName: string) {
347
        return this.getComponent(componentName)?.scope;
139✔
348
    }
349

350
    /**
351
     * Update internal maps with this file reference
352
     */
353
    private assignFile<T extends BscFile = BscFile>(file: T) {
354
        this.files[file.srcPath.toLowerCase()] = file;
1,238✔
355
        this.pkgMap[file.pkgPath.toLowerCase()] = file;
1,238✔
356
        return file;
1,238✔
357
    }
358

359
    /**
360
     * Remove this file from internal maps
361
     */
362
    private unassignFile<T extends BscFile = BscFile>(file: T) {
363
        delete this.files[file.srcPath.toLowerCase()];
97✔
364
        delete this.pkgMap[file.pkgPath.toLowerCase()];
97✔
365
        return file;
97✔
366
    }
367

368
    /**
369
     * Load a file into the program. If that file already exists, it is replaced.
370
     * If file contents are provided, those are used, Otherwise, the file is loaded from the file system
371
     * @param srcPath the file path relative to the root dir
372
     * @param fileContents the file contents
373
     * @deprecated use `setFile` instead
374
     */
375
    public addOrReplaceFile<T extends BscFile>(srcPath: string, fileContents: string): T;
376
    /**
377
     * Load a file into the program. If that file already exists, it is replaced.
378
     * @param fileEntry an object that specifies src and dest for the file.
379
     * @param fileContents the file contents. If not provided, the file will be loaded from disk
380
     * @deprecated use `setFile` instead
381
     */
382
    public addOrReplaceFile<T extends BscFile>(fileEntry: FileObj, fileContents: string): T;
383
    public addOrReplaceFile<T extends BscFile>(fileParam: FileObj | string, fileContents: string): T {
384
        return this.setFile<T>(fileParam as any, fileContents);
1✔
385
    }
386

387
    /**
388
     * Load a file into the program. If that file already exists, it is replaced.
389
     * If file contents are provided, those are used, Otherwise, the file is loaded from the file system
390
     * @param srcDestOrPkgPath the absolute path, the pkg path (i.e. `pkg:/path/to/file.brs`), or the destPath (i.e. `path/to/file.brs` relative to `pkg:/`)
391
     * @param fileContents the file contents
392
     */
393
    public setFile<T extends BscFile>(srcDestOrPkgPath: string, fileContents: string): T;
394
    /**
395
     * Load a file into the program. If that file already exists, it is replaced.
396
     * @param fileEntry an object that specifies src and dest for the file.
397
     * @param fileContents the file contents. If not provided, the file will be loaded from disk
398
     */
399
    public setFile<T extends BscFile>(fileEntry: FileObj, fileContents: string): T;
400
    public setFile<T extends BscFile>(fileParam: FileObj | string, fileContents: string): T {
401
        //normalize the file paths
402
        const { srcPath, pkgPath } = this.getPaths(fileParam, this.options.rootDir);
1,241✔
403

404
        let file = this.logger.time(LogLevel.debug, ['Program.setFile()', chalk.green(srcPath)], () => {
1,241✔
405
            //if the file is already loaded, remove it
406
            if (this.hasFile(srcPath)) {
1,241✔
407
                this.removeFile(srcPath);
87✔
408
            }
409
            let fileExtension = path.extname(srcPath).toLowerCase();
1,241✔
410
            let file: BscFile | undefined;
411

412
            if (fileExtension === '.brs' || fileExtension === '.bs') {
1,241✔
413
                //add the file to the program
414
                const brsFile = this.assignFile(
1,030✔
415
                    new BrsFile(srcPath, pkgPath, this)
416
                );
417

418
                //add file to the `source` dependency list
419
                if (brsFile.pkgPath.startsWith(startOfSourcePkgPath)) {
1,030✔
420
                    this.createSourceScope();
892✔
421
                    this.dependencyGraph.addDependency('scope:source', brsFile.dependencyGraphKey);
892✔
422
                }
423

424
                let sourceObj: SourceObj = {
1,030✔
425
                    //TODO remove `pathAbsolute` in v1
426
                    pathAbsolute: srcPath,
427
                    srcPath: srcPath,
428
                    source: fileContents
429
                };
430
                this.plugins.emit('beforeFileParse', sourceObj);
1,030✔
431

432
                this.logger.time(LogLevel.debug, ['parse', chalk.green(srcPath)], () => {
1,030✔
433
                    brsFile.parse(sourceObj.source);
1,030✔
434
                });
435

436
                //notify plugins that this file has finished parsing
437
                this.plugins.emit('afterFileParse', brsFile);
1,030✔
438

439
                file = brsFile;
1,030✔
440

441
                brsFile.attachDependencyGraph(this.dependencyGraph);
1,030✔
442

443
            } else if (
211✔
444
                //is xml file
445
                fileExtension === '.xml' &&
422✔
446
                //resides in the components folder (Roku will only parse xml files in the components folder)
447
                pkgPath.toLowerCase().startsWith(util.pathSepNormalize(`components/`))
448
            ) {
449
                //add the file to the program
450
                const xmlFile = this.assignFile(
208✔
451
                    new XmlFile(srcPath, pkgPath, this)
452
                );
453

454
                let sourceObj: SourceObj = {
208✔
455
                    //TODO remove `pathAbsolute` in v1
456
                    pathAbsolute: srcPath,
457
                    srcPath: srcPath,
458
                    source: fileContents
459
                };
460
                this.plugins.emit('beforeFileParse', sourceObj);
208✔
461

462
                this.logger.time(LogLevel.debug, ['parse', chalk.green(srcPath)], () => {
208✔
463
                    xmlFile.parse(sourceObj.source);
208✔
464
                });
465

466
                //notify plugins that this file has finished parsing
467
                this.plugins.emit('afterFileParse', xmlFile);
208✔
468

469
                file = xmlFile;
208✔
470

471
                //create a new scope for this xml file
472
                let scope = new XmlScope(xmlFile, this);
208✔
473
                this.addScope(scope);
208✔
474

475
                //register this compoent now that we have parsed it and know its component name
476
                this.registerComponent(xmlFile, scope);
208✔
477

478
                //notify plugins that the scope is created and the component is registered
479
                this.plugins.emit('afterScopeCreate', scope);
208✔
480
            } else {
481
                //TODO do we actually need to implement this? Figure out how to handle img paths
482
                // let genericFile = this.files[srcPath] = <any>{
483
                //     srcPath: srcPath,
484
                //     pkgPath: pkgPath,
485
                //     wasProcessed: true
486
                // } as File;
487
                // file = <any>genericFile;
488
            }
489
            return file;
1,241✔
490
        });
491
        return file as T;
1,241✔
492
    }
493

494
    /**
495
     * Given a srcPath, a pkgPath, or both, resolve whichever is missing, relative to rootDir.
496
     * @param fileParam an object representing file paths
497
     * @param rootDir must be a pre-normalized path
498
     */
499
    private getPaths(fileParam: string | FileObj | { srcPath?: string; pkgPath?: string }, rootDir: string) {
500
        let srcPath: string | undefined;
501
        let pkgPath: string | undefined;
502

503
        assert.ok(fileParam, 'fileParam is required');
1,248✔
504

505
        //lift the srcPath and pkgPath vars from the incoming param
506
        if (typeof fileParam === 'string') {
1,248✔
507
            fileParam = this.removePkgPrefix(fileParam);
949✔
508
            srcPath = s`${path.resolve(rootDir, fileParam)}`;
949✔
509
            pkgPath = s`${util.replaceCaseInsensitive(srcPath, rootDir, '')}`;
949✔
510
        } else {
511
            let param: any = fileParam;
299✔
512

513
            if (param.src) {
299✔
514
                srcPath = s`${param.src}`;
296✔
515
            }
516
            if (param.srcPath) {
299✔
517
                srcPath = s`${param.srcPath}`;
2✔
518
            }
519
            if (param.dest) {
299✔
520
                pkgPath = s`${this.removePkgPrefix(param.dest)}`;
296✔
521
            }
522
            if (param.pkgPath) {
299✔
523
                pkgPath = s`${this.removePkgPrefix(param.pkgPath)}`;
2✔
524
            }
525
        }
526

527
        //if there's no srcPath, use the pkgPath to build an absolute srcPath
528
        if (!srcPath) {
1,248✔
529
            srcPath = s`${rootDir}/${pkgPath}`;
1✔
530
        }
531
        //coerce srcPath to an absolute path
532
        if (!path.isAbsolute(srcPath)) {
1,248✔
533
            srcPath = util.standardizePath(srcPath);
1✔
534
        }
535

536
        //if there's no pkgPath, compute relative path from rootDir
537
        if (!pkgPath) {
1,248✔
538
            pkgPath = s`${util.replaceCaseInsensitive(srcPath, rootDir, '')}`;
1✔
539
        }
540

541
        assert.ok(srcPath, 'fileEntry.src is required');
1,248✔
542
        assert.ok(pkgPath, 'fileEntry.dest is required');
1,248✔
543

544
        return {
1,248✔
545
            srcPath: srcPath,
546
            //remove leading slash from pkgPath
547
            pkgPath: pkgPath.replace(/^[\/\\]+/, '')
548
        };
549
    }
550

551
    /**
552
     * Remove any leading `pkg:/` found in the path
553
     */
554
    private removePkgPrefix(path: string) {
555
        return path.replace(/^pkg:\//i, '');
1,247✔
556
    }
557

558
    /**
559
     * Ensure source scope is created.
560
     * Note: automatically called internally, and no-op if it exists already.
561
     */
562
    public createSourceScope() {
563
        if (!this.scopes.source) {
1,280✔
564
            const sourceScope = new Scope('source', this, 'scope:source');
953✔
565
            sourceScope.attachDependencyGraph(this.dependencyGraph);
953✔
566
            this.addScope(sourceScope);
953✔
567
            this.plugins.emit('afterScopeCreate', sourceScope);
953✔
568
        }
569
    }
570

571
    /**
572
     * Find the file by its absolute path. This is case INSENSITIVE, since
573
     * Roku is a case insensitive file system. It is an error to have multiple files
574
     * with the same path with only case being different.
575
     * @param srcPath the absolute path to the file
576
     * @deprecated use `getFile` instead, which auto-detects the path type
577
     */
578
    public getFileByPathAbsolute<T extends BrsFile | XmlFile>(srcPath: string) {
579
        srcPath = s`${srcPath}`;
×
580
        for (let filePath in this.files) {
×
581
            if (filePath.toLowerCase() === srcPath.toLowerCase()) {
×
582
                return this.files[filePath] as T;
×
583
            }
584
        }
585
    }
586

587
    /**
588
     * Get a list of files for the given (platform-normalized) pkgPath array.
589
     * Missing files are just ignored.
590
     * @deprecated use `getFiles` instead, which auto-detects the path types
591
     */
592
    public getFilesByPkgPaths<T extends BscFile[]>(pkgPaths: string[]) {
593
        return pkgPaths
×
594
            .map(pkgPath => this.getFileByPkgPath(pkgPath))
×
595
            .filter(file => file !== undefined) as T;
×
596
    }
597

598
    /**
599
     * Get a file with the specified (platform-normalized) pkg path.
600
     * If not found, return undefined
601
     * @deprecated use `getFile` instead, which auto-detects the path type
602
     */
603
    public getFileByPkgPath<T extends BscFile>(pkgPath: string) {
604
        return this.pkgMap[pkgPath.toLowerCase()] as T;
347✔
605
    }
606

607
    /**
608
     * Remove a set of files from the program
609
     * @param srcPaths can be an array of srcPath or destPath strings
610
     * @param normalizePath should this function repair and standardize the filePaths? Passing false should have a performance boost if you can guarantee your paths are already sanitized
611
     */
612
    public removeFiles(srcPaths: string[], normalizePath = true) {
1✔
613
        for (let srcPath of srcPaths) {
1✔
614
            this.removeFile(srcPath, normalizePath);
1✔
615
        }
616
    }
617

618
    /**
619
     * Remove a file from the program
620
     * @param filePath can be a srcPath, a pkgPath, or a destPath (same as pkgPath but without `pkg:/`)
621
     * @param normalizePath should this function repair and standardize the path? Passing false should have a performance boost if you can guarantee your path is already sanitized
622
     */
623
    public removeFile(filePath: string, normalizePath = true) {
96✔
624
        this.logger.debug('Program.removeFile()', filePath);
97✔
625

626
        let file = this.getFile(filePath, normalizePath);
97✔
627
        if (file) {
97!
628
            this.plugins.emit('beforeFileDispose', file);
97✔
629

630
            //if there is a scope named the same as this file's path, remove it (i.e. xml scopes)
631
            let scope = this.scopes[file.pkgPath];
97✔
632
            if (scope) {
97✔
633
                this.plugins.emit('beforeScopeDispose', scope);
11✔
634
                scope.dispose();
11✔
635
                //notify dependencies of this scope that it has been removed
636
                this.dependencyGraph.remove(scope.dependencyGraphKey!);
11✔
637
                delete this.scopes[file.pkgPath];
11✔
638
                this.plugins.emit('afterScopeDispose', scope);
11✔
639
            }
640
            //remove the file from the program
641
            this.unassignFile(file);
97✔
642

643
            this.dependencyGraph.remove(file.dependencyGraphKey);
97✔
644

645
            //if this is a pkg:/source file, notify the `source` scope that it has changed
646
            if (file.pkgPath.startsWith(startOfSourcePkgPath)) {
97✔
647
                this.dependencyGraph.removeDependency('scope:source', file.dependencyGraphKey);
83✔
648
            }
649

650
            //if this is a component, remove it from our components map
651
            if (isXmlFile(file)) {
97✔
652
                this.unregisterComponent(file);
11✔
653
            }
654
            //dispose file
655
            file?.dispose();
97!
656
            this.plugins.emit('afterFileDispose', file);
97✔
657
        }
658
    }
659

660
    /**
661
     * Traverse the entire project, and validate all scopes
662
     */
663
    public validate() {
664
        this.logger.time(LogLevel.log, ['Validating project'], () => {
687✔
665
            this.diagnostics = [];
687✔
666
            this.plugins.emit('beforeProgramValidate', this);
687✔
667

668
            //validate every file
669
            for (const file of Object.values(this.files)) {
687✔
670
                //for every unvalidated file, validate it
671
                if (!file.isValidated) {
865✔
672
                    this.plugins.emit('beforeFileValidate', {
837✔
673
                        program: this,
674
                        file: file
675
                    });
676

677
                    //emit an event to allow plugins to contribute to the file validation process
678
                    this.plugins.emit('onFileValidate', {
837✔
679
                        program: this,
680
                        file: file
681
                    });
682
                    //call file.validate() IF the file has that function defined
683
                    file.validate?.();
837!
684
                    file.isValidated = true;
837✔
685

686
                    this.plugins.emit('afterFileValidate', file);
837✔
687
                }
688
            }
689

690
            this.logger.time(LogLevel.info, ['Validate all scopes'], () => {
687✔
691
                for (let scopeName in this.scopes) {
687✔
692
                    let scope = this.scopes[scopeName];
1,442✔
693
                    scope.linkSymbolTable();
1,442✔
694
                    scope.validate();
1,442✔
695
                    scope.unlinkSymbolTable();
1,442✔
696
                }
697
            });
698

699
            this.detectDuplicateComponentNames();
687✔
700

701
            this.plugins.emit('afterProgramValidate', this);
687✔
702
        });
703
    }
704

705
    /**
706
     * Flag all duplicate component names
707
     */
708
    private detectDuplicateComponentNames() {
709
        const componentsByName = Object.keys(this.files).reduce<Record<string, XmlFile[]>>((map, filePath) => {
687✔
710
            const file = this.files[filePath];
865✔
711
            //if this is an XmlFile, and it has a valid `componentName` property
712
            if (isXmlFile(file) && file.componentName?.text) {
865✔
713
                let lowerName = file.componentName.text.toLowerCase();
138✔
714
                if (!map[lowerName]) {
138✔
715
                    map[lowerName] = [];
135✔
716
                }
717
                map[lowerName].push(file);
138✔
718
            }
719
            return map;
865✔
720
        }, {});
721

722
        for (let name in componentsByName) {
687✔
723
            const xmlFiles = componentsByName[name];
135✔
724
            //add diagnostics for every duplicate component with this name
725
            if (xmlFiles.length > 1) {
135✔
726
                for (let xmlFile of xmlFiles) {
3✔
727
                    const { componentName } = xmlFile;
6✔
728
                    this.diagnostics.push({
6✔
729
                        ...DiagnosticMessages.duplicateComponentName(componentName.text),
730
                        range: xmlFile.componentName.range,
731
                        file: xmlFile,
732
                        relatedInformation: xmlFiles.filter(x => x !== xmlFile).map(x => {
12✔
733
                            return {
6✔
734
                                location: util.createLocation(
735
                                    URI.file(xmlFile.srcPath ?? xmlFile.srcPath).toString(),
18!
736
                                    x.componentName.range
737
                                ),
738
                                message: 'Also defined here'
739
                            };
740
                        })
741
                    });
742
                }
743
            }
744
        }
745
    }
746

747
    /**
748
     * Get the files for a list of filePaths
749
     * @param filePaths can be an array of srcPath or a destPath strings
750
     * @param normalizePath should this function repair and standardize the paths? Passing false should have a performance boost if you can guarantee your paths are already sanitized
751
     */
752
    public getFiles<T extends BscFile>(filePaths: string[], normalizePath = true) {
24✔
753
        return filePaths
24✔
754
            .map(filePath => this.getFile(filePath, normalizePath))
30✔
755
            .filter(file => file !== undefined) as T[];
30✔
756
    }
757

758
    /**
759
     * Get the file at the given path
760
     * @param filePath can be a srcPath or a destPath
761
     * @param normalizePath should this function repair and standardize the path? Passing false should have a performance boost if you can guarantee your path is already sanitized
762
     */
763
    public getFile<T extends BscFile>(filePath: string, normalizePath = true) {
5,108✔
764
        if (typeof filePath !== 'string') {
6,491✔
765
            return undefined;
1,448✔
766
        } else if (path.isAbsolute(filePath)) {
5,043✔
767
            return this.files[
2,579✔
768
                (normalizePath ? util.standardizePath(filePath) : filePath).toLowerCase()
2,579!
769
            ] as T;
770
        } else {
771
            return this.pkgMap[
2,464✔
772
                (normalizePath ? util.standardizePath(filePath) : filePath).toLowerCase()
2,464!
773
            ] as T;
774
        }
775
    }
776

777
    /**
778
     * Get a list of all scopes the file is loaded into
779
     * @param file the file
780
     */
781
    public getScopesForFile(file: XmlFile | BrsFile | string) {
782

783
        const resolvedFile = typeof file === 'string' ? this.getFile(file) : file;
650✔
784

785
        let result = [] as Scope[];
650✔
786
        if (resolvedFile) {
650✔
787
            for (let key in this.scopes) {
649✔
788
                let scope = this.scopes[key];
1,287✔
789

790
                if (scope.hasFile(resolvedFile)) {
1,287✔
791
                    result.push(scope);
611✔
792
                }
793
            }
794
        }
795
        return result;
650✔
796
    }
797

798
    /**
799
     * Get the first found scope for a file.
800
     */
801
    public getFirstScopeForFile(file: XmlFile | BrsFile): Scope | undefined {
802
        for (let key in this.scopes) {
1,887✔
803
            let scope = this.scopes[key];
4,454✔
804

805
            if (scope.hasFile(file)) {
4,454✔
806
                return scope;
1,852✔
807
            }
808
        }
809
    }
810

811
    public getStatementsByName(name: string, originFile: BrsFile, namespaceName?: string): FileLink<Statement>[] {
812
        let results = new Map<Statement, FileLink<Statement>>();
39✔
813
        const filesSearched = new Set<BrsFile>();
39✔
814
        let lowerNamespaceName = namespaceName?.toLowerCase();
39✔
815
        let lowerName = name?.toLowerCase();
39!
816
        //look through all files in scope for matches
817
        for (const scope of this.getScopesForFile(originFile)) {
39✔
818
            for (const file of scope.getAllFiles()) {
39✔
819
                if (isXmlFile(file) || filesSearched.has(file)) {
45✔
820
                    continue;
3✔
821
                }
822
                filesSearched.add(file);
42✔
823

824
                for (const statement of [...file.parser.references.functionStatements, ...file.parser.references.classStatements.flatMap((cs) => cs.methods)]) {
42✔
825
                    let parentNamespaceName = statement.findAncestor<NamespaceStatement>(isNamespaceStatement)?.getName(originFile.parseMode)?.toLowerCase();
98✔
826
                    if (statement.name.text.toLowerCase() === lowerName && (!lowerNamespaceName || parentNamespaceName === lowerNamespaceName)) {
98✔
827
                        if (!results.has(statement)) {
36!
828
                            results.set(statement, { item: statement, file: file });
36✔
829
                        }
830
                    }
831
                }
832
            }
833
        }
834
        return [...results.values()];
39✔
835
    }
836

837
    public getStatementsForXmlFile(scope: XmlScope, filterName?: string): FileLink<FunctionStatement>[] {
838
        let results = new Map<Statement, FileLink<FunctionStatement>>();
8✔
839
        const filesSearched = new Set<BrsFile>();
8✔
840

841
        //get all function names for the xml file and parents
842
        let funcNames = new Set<string>();
8✔
843
        let currentScope = scope;
8✔
844
        while (isXmlScope(currentScope)) {
8✔
845
            for (let name of currentScope.xmlFile.ast.component.api?.functions.map((f) => f.name) ?? []) {
14✔
846
                if (!filterName || name === filterName) {
14!
847
                    funcNames.add(name);
14✔
848
                }
849
            }
850
            currentScope = currentScope.getParentScope() as XmlScope;
10✔
851
        }
852

853
        //look through all files in scope for matches
854
        for (const file of scope.getOwnFiles()) {
8✔
855
            if (isXmlFile(file) || filesSearched.has(file)) {
16✔
856
                continue;
8✔
857
            }
858
            filesSearched.add(file);
8✔
859

860
            for (const statement of file.parser.references.functionStatements) {
8✔
861
                if (funcNames.has(statement.name.text)) {
13!
862
                    if (!results.has(statement)) {
13!
863
                        results.set(statement, { item: statement, file: file });
13✔
864
                    }
865
                }
866
            }
867
        }
868
        return [...results.values()];
8✔
869
    }
870

871
    /**
872
     * Find all available completion items at the given position
873
     * @param filePath can be a srcPath or a destPath
874
     * @param position the position (line & column) where completions should be found
875
     */
876
    public getCompletions(filePath: string, position: Position) {
877
        let file = this.getFile(filePath);
74✔
878
        if (!file) {
74!
879
            return [];
×
880
        }
881

882
        //find the scopes for this file
883
        let scopes = this.getScopesForFile(file);
74✔
884

885
        //if there are no scopes, include the global scope so we at least get the built-in functions
886
        scopes = scopes.length > 0 ? scopes : [this.globalScope];
74✔
887

888
        const event: ProvideCompletionsEvent = {
74✔
889
            program: this,
890
            file: file,
891
            scopes: scopes,
892
            position: position,
893
            completions: []
894
        };
895

896
        this.plugins.emit('beforeProvideCompletions', event);
74✔
897

898
        this.plugins.emit('provideCompletions', event);
74✔
899

900
        this.plugins.emit('afterProvideCompletions', event);
74✔
901

902
        return event.completions;
74✔
903
    }
904

905
    /**
906
     * Goes through each file and builds a list of workspace symbols for the program. Used by LanguageServer's onWorkspaceSymbol functionality
907
     */
908
    public getWorkspaceSymbols() {
909
        const event: ProvideWorkspaceSymbolsEvent = {
22✔
910
            program: this,
911
            workspaceSymbols: []
912
        };
913
        this.plugins.emit('beforeProvideWorkspaceSymbols', event);
22✔
914
        this.plugins.emit('provideWorkspaceSymbols', event);
22✔
915
        this.plugins.emit('afterProvideWorkspaceSymbols', event);
22✔
916
        return event.workspaceSymbols;
22✔
917
    }
918

919
    /**
920
     * Given a position in a file, if the position is sitting on some type of identifier,
921
     * go to the definition of that identifier (where this thing was first defined)
922
     */
923
    public getDefinition(srcPath: string, position: Position): Location[] {
924
        let file = this.getFile(srcPath);
13✔
925
        if (!file) {
13!
926
            return [];
×
927
        }
928

929
        const event: ProvideDefinitionEvent = {
13✔
930
            program: this,
931
            file: file,
932
            position: position,
933
            definitions: []
934
        };
935

936
        this.plugins.emit('beforeProvideDefinition', event);
13✔
937
        this.plugins.emit('provideDefinition', event);
13✔
938
        this.plugins.emit('afterProvideDefinition', event);
13✔
939
        return event.definitions;
13✔
940
    }
941

942
    /**
943
     * Get hover information for a file and position
944
     */
945
    public getHover(srcPath: string, position: Position): Hover[] {
946
        let file = this.getFile(srcPath);
30✔
947
        let result: Hover[];
948
        if (file) {
30!
949
            const event = {
30✔
950
                program: this,
951
                file: file,
952
                position: position,
953
                scopes: this.getScopesForFile(file),
954
                hovers: []
955
            } as ProvideHoverEvent;
956
            this.plugins.emit('beforeProvideHover', event);
30✔
957
            this.plugins.emit('provideHover', event);
30✔
958
            this.plugins.emit('afterProvideHover', event);
30✔
959
            result = event.hovers;
30✔
960
        }
961

962
        return result ?? [];
30!
963
    }
964

965
    /**
966
     * Get full list of document symbols for a file
967
     * @param srcPath path to the file
968
     */
969
    public getDocumentSymbols(srcPath: string): DocumentSymbol[] | undefined {
970
        let file = this.getFile(srcPath);
18✔
971
        if (file) {
18!
972
            const event: ProvideDocumentSymbolsEvent = {
18✔
973
                program: this,
974
                file: file,
975
                documentSymbols: []
976
            };
977
            this.plugins.emit('beforeProvideDocumentSymbols', event);
18✔
978
            this.plugins.emit('provideDocumentSymbols', event);
18✔
979
            this.plugins.emit('afterProvideDocumentSymbols', event);
18✔
980
            return event.documentSymbols;
18✔
981
        } else {
982
            return undefined;
×
983
        }
984
    }
985

986
    /**
987
     * Compute code actions for the given file and range
988
     */
989
    public getCodeActions(srcPath: string, range: Range) {
990
        const codeActions = [] as CodeAction[];
11✔
991
        const file = this.getFile(srcPath);
11✔
992
        if (file) {
11✔
993
            const diagnostics = this
10✔
994
                //get all current diagnostics (filtered by diagnostic filters)
995
                .getDiagnostics()
996
                //only keep diagnostics related to this file
997
                .filter(x => x.file === file)
26✔
998
                //only keep diagnostics that touch this range
999
                .filter(x => util.rangesIntersectOrTouch(x.range, range));
10✔
1000

1001
            const scopes = this.getScopesForFile(file);
10✔
1002

1003
            this.plugins.emit('onGetCodeActions', {
10✔
1004
                program: this,
1005
                file: file,
1006
                range: range,
1007
                diagnostics: diagnostics,
1008
                scopes: scopes,
1009
                codeActions: codeActions
1010
            });
1011
        }
1012
        return codeActions;
11✔
1013
    }
1014

1015
    /**
1016
     * Get semantic tokens for the specified file
1017
     */
1018
    public getSemanticTokens(srcPath: string): SemanticToken[] | undefined {
1019
        const file = this.getFile(srcPath);
16✔
1020
        if (file) {
16!
1021
            const result = [] as SemanticToken[];
16✔
1022
            this.plugins.emit('onGetSemanticTokens', {
16✔
1023
                program: this,
1024
                file: file,
1025
                scopes: this.getScopesForFile(file),
1026
                semanticTokens: result
1027
            });
1028
            return result;
16✔
1029
        }
1030
    }
1031

1032
    public getSignatureHelp(filepath: string, position: Position): SignatureInfoObj[] {
1033
        let file: BrsFile = this.getFile(filepath);
185✔
1034
        if (!file || !isBrsFile(file)) {
185✔
1035
            return [];
3✔
1036
        }
1037
        let callExpressionInfo = new CallExpressionInfo(file, position);
182✔
1038
        let signatureHelpUtil = new SignatureHelpUtil();
182✔
1039
        return signatureHelpUtil.getSignatureHelpItems(callExpressionInfo);
182✔
1040
    }
1041

1042
    public getReferences(srcPath: string, position: Position): Location[] {
1043
        //find the file
1044
        let file = this.getFile(srcPath);
4✔
1045
        if (!file) {
4!
1046
            return null;
×
1047
        }
1048

1049
        const event: ProvideReferencesEvent = {
4✔
1050
            program: this,
1051
            file: file,
1052
            position: position,
1053
            references: []
1054
        };
1055

1056
        this.plugins.emit('beforeProvideReferences', event);
4✔
1057
        this.plugins.emit('provideReferences', event);
4✔
1058
        this.plugins.emit('afterProvideReferences', event);
4✔
1059

1060
        return event.references;
4✔
1061
    }
1062

1063
    /**
1064
     * Get a list of all script imports, relative to the specified pkgPath
1065
     * @param sourcePkgPath - the pkgPath of the source that wants to resolve script imports.
1066
     */
1067
    public getScriptImportCompletions(sourcePkgPath: string, scriptImport: FileReference) {
1068
        let lowerSourcePkgPath = sourcePkgPath.toLowerCase();
3✔
1069

1070
        let result = [] as CompletionItem[];
3✔
1071
        /**
1072
         * hashtable to prevent duplicate results
1073
         */
1074
        let resultPkgPaths = {} as Record<string, boolean>;
3✔
1075

1076
        //restrict to only .brs files
1077
        for (let key in this.files) {
3✔
1078
            let file = this.files[key];
4✔
1079
            if (
4✔
1080
                //is a BrightScript or BrighterScript file
1081
                (file.extension === '.bs' || file.extension === '.brs') &&
11✔
1082
                //this file is not the current file
1083
                lowerSourcePkgPath !== file.pkgPath.toLowerCase()
1084
            ) {
1085
                //add the relative path
1086
                let relativePath = util.getRelativePath(sourcePkgPath, file.pkgPath).replace(/\\/g, '/');
3✔
1087
                let pkgPathStandardized = file.pkgPath.replace(/\\/g, '/');
3✔
1088
                let filePkgPath = `pkg:/${pkgPathStandardized}`;
3✔
1089
                let lowerFilePkgPath = filePkgPath.toLowerCase();
3✔
1090
                if (!resultPkgPaths[lowerFilePkgPath]) {
3!
1091
                    resultPkgPaths[lowerFilePkgPath] = true;
3✔
1092

1093
                    result.push({
3✔
1094
                        label: relativePath,
1095
                        detail: file.srcPath,
1096
                        kind: CompletionItemKind.File,
1097
                        textEdit: {
1098
                            newText: relativePath,
1099
                            range: scriptImport.filePathRange
1100
                        }
1101
                    });
1102

1103
                    //add the absolute path
1104
                    result.push({
3✔
1105
                        label: filePkgPath,
1106
                        detail: file.srcPath,
1107
                        kind: CompletionItemKind.File,
1108
                        textEdit: {
1109
                            newText: filePkgPath,
1110
                            range: scriptImport.filePathRange
1111
                        }
1112
                    });
1113
                }
1114
            }
1115
        }
1116
        return result;
3✔
1117
    }
1118

1119
    /**
1120
     * Transpile a single file and get the result as a string.
1121
     * This does not write anything to the file system.
1122
     *
1123
     * This should only be called by `LanguageServer`.
1124
     * Internal usage should call `_getTranspiledFileContents` instead.
1125
     * @param filePath can be a srcPath or a destPath
1126
     */
1127
    public async getTranspiledFileContents(filePath: string) {
1128
        let fileMap = await rokuDeploy.getFilePaths(this.options.files, this.options.rootDir);
4✔
1129
        //remove files currently loaded in the program, we will transpile those instead (even if just for source maps)
1130
        let filteredFileMap = [] as FileObj[];
4✔
1131
        for (let fileEntry of fileMap) {
4✔
1132
            if (this.hasFile(fileEntry.src) === false) {
2!
1133
                filteredFileMap.push(fileEntry);
×
1134
            }
1135
        }
1136
        const { entries, astEditor } = this.beforeProgramTranspile(fileMap, this.options.stagingDir);
4✔
1137
        const result = this._getTranspiledFileContents(
4✔
1138
            this.getFile(filePath)
1139
        );
1140
        this.afterProgramTranspile(entries, astEditor);
4✔
1141
        return result;
4✔
1142
    }
1143

1144
    /**
1145
     * Internal function used to transpile files.
1146
     * This does not write anything to the file system
1147
     */
1148
    private _getTranspiledFileContents(file: BscFile, outputPath?: string): FileTranspileResult {
1149
        const editor = new AstEditor();
286✔
1150

1151
        this.plugins.emit('beforeFileTranspile', {
286✔
1152
            program: this,
1153
            file: file,
1154
            outputPath: outputPath,
1155
            editor: editor
1156
        });
1157

1158
        //if we have any edits, assume the file needs to be transpiled
1159
        if (editor.hasChanges) {
286✔
1160
            //use the `editor` because it'll track the previous value for us and revert later on
1161
            editor.setProperty(file, 'needsTranspiled', true);
61✔
1162
        }
1163

1164
        //transpile the file
1165
        const result = file.transpile();
286✔
1166

1167
        //generate the typedef if enabled
1168
        let typedef: string;
1169
        if (isBrsFile(file) && this.options.emitDefinitions) {
286✔
1170
            typedef = file.getTypedef();
2✔
1171
        }
1172

1173
        const event: AfterFileTranspileEvent = {
286✔
1174
            program: this,
1175
            file: file,
1176
            outputPath: outputPath,
1177
            editor: editor,
1178
            code: result.code,
1179
            map: result.map,
1180
            typedef: typedef
1181
        };
1182
        this.plugins.emit('afterFileTranspile', event);
286✔
1183

1184
        //undo all `editor` edits that may have been applied to this file.
1185
        editor.undoAll();
286✔
1186

1187
        return {
286✔
1188
            srcPath: file.srcPath,
1189
            pkgPath: file.pkgPath,
1190
            code: event.code,
1191
            map: event.map,
1192
            typedef: event.typedef
1193
        };
1194
    }
1195

1196
    private beforeProgramTranspile(fileEntries: FileObj[], stagingDir: string) {
1197
        // map fileEntries using their path as key, to avoid excessive "find()" operations
1198
        const mappedFileEntries = fileEntries.reduce<Record<string, FileObj>>((collection, entry) => {
37✔
1199
            collection[s`${entry.src}`] = entry;
18✔
1200
            return collection;
18✔
1201
        }, {});
1202

1203
        const getOutputPath = (file: BscFile) => {
37✔
1204
            let filePathObj = mappedFileEntries[s`${file.srcPath}`];
77✔
1205
            if (!filePathObj) {
77✔
1206
                //this file has been added in-memory, from a plugin, for example
1207
                filePathObj = {
50✔
1208
                    //add an interpolated src path (since it doesn't actually exist in memory)
1209
                    src: `bsc:/${file.pkgPath}`,
1210
                    dest: file.pkgPath
1211
                };
1212
            }
1213
            //replace the file extension
1214
            let outputPath = filePathObj.dest.replace(/\.bs$/gi, '.brs');
77✔
1215
            //prepend the staging folder path
1216
            outputPath = s`${stagingDir}/${outputPath}`;
77✔
1217
            return outputPath;
77✔
1218
        };
1219

1220
        const entries = Object.values(this.files).map(file => {
37✔
1221
            return {
39✔
1222
                file: file,
1223
                outputPath: getOutputPath(file)
1224
            };
1225
            //sort the entries to make transpiling more deterministic
1226
        }).sort((a, b) => {
1227
            return a.file.srcPath < b.file.srcPath ? -1 : 1;
8✔
1228
        });
1229

1230
        const astEditor = new AstEditor();
37✔
1231

1232
        this.plugins.emit('beforeProgramTranspile', this, entries, astEditor);
37✔
1233
        return {
37✔
1234
            entries: entries,
1235
            getOutputPath: getOutputPath,
1236
            astEditor: astEditor
1237
        };
1238
    }
1239

1240
    public async transpile(fileEntries: FileObj[], stagingDir: string) {
1241
        const { entries, getOutputPath, astEditor } = this.beforeProgramTranspile(fileEntries, stagingDir);
32✔
1242

1243
        const processedFiles = new Set<string>();
32✔
1244

1245
        const transpileFile = async (srcPath: string, outputPath?: string) => {
32✔
1246
            //find the file in the program
1247
            const file = this.getFile(srcPath);
35✔
1248
            //mark this file as processed so we don't process it more than once
1249
            processedFiles.add(outputPath?.toLowerCase());
35!
1250

1251
            if (!this.options.pruneEmptyCodeFiles || !file.canBePruned) {
35✔
1252
                //skip transpiling typedef files
1253
                if (isBrsFile(file) && file.isTypedef) {
34✔
1254
                    return;
1✔
1255
                }
1256

1257
                const fileTranspileResult = this._getTranspiledFileContents(file, outputPath);
33✔
1258

1259
                //make sure the full dir path exists
1260
                await fsExtra.ensureDir(path.dirname(outputPath));
33✔
1261

1262
                if (await fsExtra.pathExists(outputPath)) {
33!
1263
                    throw new Error(`Error while transpiling "${file.srcPath}". A file already exists at "${outputPath}" and will not be overwritten.`);
×
1264
                }
1265
                const writeMapPromise = fileTranspileResult.map ? fsExtra.writeFile(`${outputPath}.map`, fileTranspileResult.map.toString()) : null;
33✔
1266
                await Promise.all([
33✔
1267
                    fsExtra.writeFile(outputPath, fileTranspileResult.code),
1268
                    writeMapPromise
1269
                ]);
1270

1271
                if (fileTranspileResult.typedef) {
33✔
1272
                    const typedefPath = outputPath.replace(/\.brs$/i, '.d.bs');
2✔
1273
                    await fsExtra.writeFile(typedefPath, fileTranspileResult.typedef);
2✔
1274
                }
1275
            }
1276
        };
1277

1278
        let promises = entries.map(async (entry) => {
32✔
1279
            return transpileFile(entry?.file?.srcPath, entry.outputPath);
33!
1280
        });
1281

1282
        //if there's no bslib file already loaded into the program, copy it to the staging directory
1283
        if (!this.getFile(bslibAliasedRokuModulesPkgPath) && !this.getFile(s`source/bslib.brs`)) {
32✔
1284
            promises.push(util.copyBslibToStaging(stagingDir, this.options.bslibDestinationDir));
31✔
1285
        }
1286
        await Promise.all(promises);
32✔
1287

1288
        //transpile any new files that plugins added since the start of this transpile process
1289
        do {
32✔
1290
            promises = [];
33✔
1291
            for (const key in this.files) {
33✔
1292
                const file = this.files[key];
38✔
1293
                //this is a new file
1294
                const outputPath = getOutputPath(file);
38✔
1295
                if (!processedFiles.has(outputPath?.toLowerCase())) {
38!
1296
                    promises.push(
2✔
1297
                        transpileFile(file?.srcPath, outputPath)
6!
1298
                    );
1299
                }
1300
            }
1301
            if (promises.length > 0) {
33✔
1302
                this.logger.info(`Transpiling ${promises.length} new files`);
1✔
1303
                await Promise.all(promises);
1✔
1304
            }
1305
        }
1306
        while (promises.length > 0);
1307
        this.afterProgramTranspile(entries, astEditor);
32✔
1308
    }
1309

1310
    private afterProgramTranspile(entries: TranspileObj[], astEditor: AstEditor) {
1311
        this.plugins.emit('afterProgramTranspile', this, entries, astEditor);
36✔
1312
        astEditor.undoAll();
36✔
1313
    }
1314

1315
    /**
1316
     * Find a list of files in the program that have a function with the given name (case INsensitive)
1317
     */
1318
    public findFilesForFunction(functionName: string) {
1319
        const files = [] as BscFile[];
7✔
1320
        const lowerFunctionName = functionName.toLowerCase();
7✔
1321
        //find every file with this function defined
1322
        for (const file of Object.values(this.files)) {
7✔
1323
            if (isBrsFile(file)) {
25✔
1324
                //TODO handle namespace-relative function calls
1325
                //if the file has a function with this name
1326
                if (file.parser.references.functionStatementLookup.get(lowerFunctionName) !== undefined) {
17✔
1327
                    files.push(file);
2✔
1328
                }
1329
            }
1330
        }
1331
        return files;
7✔
1332
    }
1333

1334
    /**
1335
     * Find a list of files in the program that have a class with the given name (case INsensitive)
1336
     */
1337
    public findFilesForClass(className: string) {
1338
        const files = [] as BscFile[];
7✔
1339
        const lowerClassName = className.toLowerCase();
7✔
1340
        //find every file with this class defined
1341
        for (const file of Object.values(this.files)) {
7✔
1342
            if (isBrsFile(file)) {
25✔
1343
                //TODO handle namespace-relative classes
1344
                //if the file has a function with this name
1345
                if (file.parser.references.classStatementLookup.get(lowerClassName) !== undefined) {
17✔
1346
                    files.push(file);
1✔
1347
                }
1348
            }
1349
        }
1350
        return files;
7✔
1351
    }
1352

1353
    public findFilesForNamespace(name: string) {
1354
        const files = [] as BscFile[];
7✔
1355
        const lowerName = name.toLowerCase();
7✔
1356
        //find every file with this class defined
1357
        for (const file of Object.values(this.files)) {
7✔
1358
            if (isBrsFile(file)) {
25✔
1359
                if (file.parser.references.namespaceStatements.find((x) => {
17✔
1360
                    const namespaceName = x.name.toLowerCase();
7✔
1361
                    return (
7✔
1362
                        //the namespace name matches exactly
1363
                        namespaceName === lowerName ||
9✔
1364
                        //the full namespace starts with the name (honoring the part boundary)
1365
                        namespaceName.startsWith(lowerName + '.')
1366
                    );
1367
                })) {
1368
                    files.push(file);
6✔
1369
                }
1370
            }
1371
        }
1372
        return files;
7✔
1373
    }
1374

1375
    public findFilesForEnum(name: string) {
1376
        const files = [] as BscFile[];
8✔
1377
        const lowerName = name.toLowerCase();
8✔
1378
        //find every file with this class defined
1379
        for (const file of Object.values(this.files)) {
8✔
1380
            if (isBrsFile(file)) {
26✔
1381
                if (file.parser.references.enumStatementLookup.get(lowerName)) {
18✔
1382
                    files.push(file);
1✔
1383
                }
1384
            }
1385
        }
1386
        return files;
8✔
1387
    }
1388

1389
    private _manifest: Map<string, string>;
1390

1391
    /**
1392
     * Modify a parsed manifest map by reading `bs_const` and injecting values from `options.manifest.bs_const`
1393
     * @param parsedManifest The manifest map to read from and modify
1394
     */
1395
    private buildBsConstsIntoParsedManifest(parsedManifest: Map<string, string>) {
1396
        // Lift the bs_consts defined in the manifest
1397
        let bsConsts = getBsConst(parsedManifest, false);
11✔
1398

1399
        // Override or delete any bs_consts defined in the bs config
1400
        for (const key in this.options?.manifest?.bs_const) {
11!
1401
            const value = this.options.manifest.bs_const[key];
3✔
1402
            if (value === null) {
3✔
1403
                bsConsts.delete(key);
1✔
1404
            } else {
1405
                bsConsts.set(key, value);
2✔
1406
            }
1407
        }
1408

1409
        // convert the new list of bs consts back into a string for the rest of the down stream systems to use
1410
        let constString = '';
11✔
1411
        for (const [key, value] of bsConsts) {
11✔
1412
            constString += `${constString !== '' ? ';' : ''}${key}=${value.toString()}`;
6✔
1413
        }
1414

1415
        // Set the updated bs_const value
1416
        parsedManifest.set('bs_const', constString);
11✔
1417
    }
1418

1419
    /**
1420
     * Try to find and load the manifest into memory
1421
     * @param manifestFileObj A pointer to a potential manifest file object found during loading
1422
     * @param replaceIfAlreadyLoaded should we overwrite the internal `_manifest` if it already exists
1423
     */
1424
    public loadManifest(manifestFileObj?: FileObj, replaceIfAlreadyLoaded = true) {
870✔
1425
        //if we already have a manifest instance, and should not replace...then don't replace
1426
        if (!replaceIfAlreadyLoaded && this._manifest) {
874!
1427
            return;
×
1428
        }
1429
        let manifestPath = manifestFileObj
874✔
1430
            ? manifestFileObj.src
874✔
1431
            : path.join(this.options.rootDir, 'manifest');
1432

1433
        try {
874✔
1434
            // we only load this manifest once, so do it sync to improve speed downstream
1435
            const contents = fsExtra.readFileSync(manifestPath, 'utf-8');
874✔
1436
            const parsedManifest = parseManifest(contents);
11✔
1437
            this.buildBsConstsIntoParsedManifest(parsedManifest);
11✔
1438
            this._manifest = parsedManifest;
11✔
1439
        } catch (e) {
1440
            this._manifest = new Map();
863✔
1441
        }
1442
    }
1443

1444
    /**
1445
     * Get a map of the manifest information
1446
     */
1447
    public getManifest() {
1448
        if (!this._manifest) {
1,084✔
1449
            this.loadManifest();
869✔
1450
        }
1451
        return this._manifest;
1,084✔
1452
    }
1453

1454
    public dispose() {
1455
        this.plugins.emit('beforeProgramDispose', { program: this });
1,084✔
1456

1457
        for (let filePath in this.files) {
1,084✔
1458
            this.files[filePath].dispose();
1,032✔
1459
        }
1460
        for (let name in this.scopes) {
1,084✔
1461
            this.scopes[name].dispose();
2,166✔
1462
        }
1463
        this.globalScope.dispose();
1,084✔
1464
        this.dependencyGraph.dispose();
1,084✔
1465
    }
1466
}
1467

1468
export interface FileTranspileResult {
1469
    srcPath: string;
1470
    pkgPath: string;
1471
    code: string;
1472
    map: SourceMapGenerator;
1473
    typedef: string;
1474
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc