Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move not-referenced check into ProgramValidator #773

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Program.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ describe('Program', () => {
DiagnosticMessages.fileNotReferencedByAnyOtherFile()
]);
});

it('does not throw errors on shadowed init functions in components', () => {
program.setFile('lib.brs', `
function DoSomething()
Expand Down
28 changes: 2 additions & 26 deletions src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ export class Program {
let result = [] as File[];
for (let filePath in this.files) {
let file = this.files[filePath];
if (!this.fileIsIncludedInAnyScope(file)) {
//is this file part of a scope
if (!this.getFirstScopeForFile(file)) {
//no scopes reference this file. add it to the list
result.push(file);
}
Expand Down Expand Up @@ -655,18 +656,6 @@ export class Program {

//validate every file
for (const file of Object.values(this.files)) {

//find any files NOT loaded into a scope
if (!this.fileIsIncludedInAnyScope(file)) {
this.logger.debug('Program.validate(): fileNotReferenced by any scope', () => chalk.green(file?.pkgPath));
//the file is not loaded in any scope
this.diagnostics.push({
...DiagnosticMessages.fileNotReferencedByAnyOtherFile(),
file: file,
range: util.createRange(0, 0, 0, Number.MAX_VALUE)
});
}

//for every unvalidated file, validate it
if (!file.isValidated) {
this.plugins.emit('beforeFileValidate', {
Expand All @@ -687,7 +676,6 @@ export class Program {
}
}


this.logger.time(LogLevel.info, ['Validate all scopes'], () => {
for (let scopeName in this.scopes) {
let scope = this.scopes[scopeName];
Expand Down Expand Up @@ -745,18 +733,6 @@ export class Program {
}
}

/**
* Determine if the given file is included in at least one scope in this program
*/
private fileIsIncludedInAnyScope(file: BscFile) {
for (let scopeName in this.scopes) {
if (this.scopes[scopeName].hasFile(file)) {
return true;
}
}
return false;
}

/**
* Get the files for a list of filePaths
* @param filePaths can be an array of srcPath or a destPath strings
Expand Down
2 changes: 2 additions & 0 deletions src/bscPlugin/BscPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HoverProcessor } from './hover/HoverProcessor';
import { BrsFileSemanticTokensProcessor } from './semanticTokens/BrsFileSemanticTokensProcessor';
import { BrsFilePreTranspileProcessor } from './transpile/BrsFilePreTranspileProcessor';
import { BrsFileValidator } from './validation/BrsFileValidator';
import { ProgramValidator } from './validation/ProgramValidator';
import { ScopeValidator } from './validation/ScopeValidator';
import { XmlFileValidator } from './validation/XmlFileValidator';

Expand Down Expand Up @@ -46,6 +47,7 @@ export class BscPlugin implements CompilerPlugin {
}

public afterProgramValidate(program: Program) {
new ProgramValidator(program).process();
//release memory once the validation cycle has finished
this.scopeValidator.reset();
}
Expand Down
36 changes: 36 additions & 0 deletions src/bscPlugin/validation/ProgramValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { isBrsFile } from '../../astUtils/reflection';
import { DiagnosticMessages } from '../../DiagnosticMessages';
import type { Program } from '../../Program';
import util from '../../util';

export class ProgramValidator {
constructor(private program: Program) { }

public process() {
this.flagScopelessBrsFiles();
}

/**
* Flag any files that are included in 0 scopes.
*/
private flagScopelessBrsFiles() {
for (const key in this.program.files) {
const file = this.program.files[key];

if (
//if this isn't a brs file, skip
!isBrsFile(file) ||
//if the file is included in at least one scope, skip
this.program.getFirstScopeForFile(file)
) {
continue;
}

this.program.addDiagnostics([{
...DiagnosticMessages.fileNotReferencedByAnyOtherFile(),
file: file,
range: util.createRange(0, 0, 0, Number.MAX_VALUE)
}]);
}
}
}