Skip to content

Commit

Permalink
Compute ownScriptImports after invalidateReferences() (#529)
Browse files Browse the repository at this point in the history
* Compute ownScriptImports after invalidateReferences()

* fix lint issues.
  • Loading branch information
TwitchBronBron authored Feb 25, 2022
1 parent 4fb6987 commit 61a0361
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
13 changes: 13 additions & 0 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import PluginInterface from '../PluginInterface';
import { expectCompletionsIncludes, expectDiagnostics, expectHasDiagnostics, expectZeroDiagnostics, getTestTranspile, trim } from '../testHelpers.spec';
import { ParseMode } from '../parser/Parser';
import { Logger } from '../Logger';
import { ImportStatement } from '../parser/Statement';
import { createToken } from '../astUtils/creators';

let sinon = sinonImport.createSandbox();

Expand Down Expand Up @@ -67,6 +69,17 @@ describe('BrsFile', () => {
expect(new BrsFile(`${rootDir}/source/main.bs`, 'source/main.bs', program).needsTranspiled).to.be.true;
});

it('computes new import statements after clearing parser references', () => {
const file = program.setFile<BrsFile>('source/main.bs', ``);
expect(file.ownScriptImports).to.be.empty;
file.parser.ast.statements.push(
new ImportStatement(createToken(TokenKind.Import), createToken(TokenKind.StringLiteral, 'pkg:/source/lib.brs'))
);
expect(file.ownScriptImports).to.be.empty;
file.parser.invalidateReferences();
expect(file.ownScriptImports.map(x => x.text)).to.eql(['pkg:/source/lib.brs']);
});

it('allows adding diagnostics', () => {
const expected = [{
message: 'message',
Expand Down
37 changes: 19 additions & 18 deletions src/files/BrsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,25 @@ export class BrsFile {
/**
* files referenced by import statements
*/
public ownScriptImports = [] as FileReference[];
public get ownScriptImports() {
// eslint-disable-next-line @typescript-eslint/dot-notation
const result = this._parser?.references['cache'].getOrAdd('BrsFile_ownScriptImports', () => {
const result = [] as FileReference[];
for (const statement of this.parser?.references?.importStatements ?? []) {
//register import statements
if (isImportStatement(statement) && statement.filePathToken) {
result.push({
filePathRange: statement.filePathToken.range,
pkgPath: util.getPkgPathFromTarget(this.pkgPath, statement.filePath),
sourceFile: this,
text: statement.filePathToken?.text
});
}
}
return result;
}) ?? [];
return result as FileReference[];
}

/**
* Does this file need to be transpiled?
Expand Down Expand Up @@ -281,9 +299,6 @@ export class BrsFile {
//find all places where a sub/function is being called
this.findFunctionCalls();

//register all import statements for use in the rest of the program
this.registerImports();

//attach this file to every diagnostic
for (let diagnostic of this.diagnostics) {
diagnostic.file = this;
Expand All @@ -298,20 +313,6 @@ export class BrsFile {
}
}

private registerImports() {
for (const statement of this.parser?.references?.importStatements ?? []) {
//register import statements
if (isImportStatement(statement) && statement.filePathToken) {
this.ownScriptImports.push({
filePathRange: statement.filePathToken.range,
pkgPath: util.getPkgPathFromTarget(this.pkgPath, statement.filePath),
sourceFile: this,
text: statement.filePathToken?.text
});
}
}
}

public validate() {
//only validate the file if it was actually parsed (skip files containing typedefs)
if (!this.hasTypedef) {
Expand Down

0 comments on commit 61a0361

Please sign in to comment.