From 61a036108741c74338983bb8f95d1436a6f5e3ef Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Fri, 25 Feb 2022 16:16:26 -0500 Subject: [PATCH] Compute ownScriptImports after invalidateReferences() (#529) * Compute ownScriptImports after invalidateReferences() * fix lint issues. --- src/files/BrsFile.spec.ts | 13 +++++++++++++ src/files/BrsFile.ts | 37 +++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/files/BrsFile.spec.ts b/src/files/BrsFile.spec.ts index 82149cb40..f8a28e12e 100644 --- a/src/files/BrsFile.spec.ts +++ b/src/files/BrsFile.spec.ts @@ -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(); @@ -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('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', diff --git a/src/files/BrsFile.ts b/src/files/BrsFile.ts index a57466738..b510c3fd1 100644 --- a/src/files/BrsFile.ts +++ b/src/files/BrsFile.ts @@ -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? @@ -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; @@ -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) {