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

Adds plugin hooks for file validation #490

Merged
merged 2 commits into from
Jan 25, 2022
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
30 changes: 30 additions & 0 deletions src/Program.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2335,4 +2335,34 @@ describe('Program', () => {
}
});
});

describe('plugins', () => {
it('emits file validation events', () => {
const plugin = {
name: 'test',
beforeFileValidate: sinon.spy(),
onFileValidate: sinon.spy(),
afterFileValidate: sinon.spy()
};
program.plugins.add(plugin);
program.addOrReplaceFile('source/main.brs', '');
expect(plugin.beforeFileValidate.callCount).to.equal(1);
expect(plugin.onFileValidate.callCount).to.equal(1);
expect(plugin.afterFileValidate.callCount).to.equal(1);
});

it('emits file validation events', () => {
const plugin = {
name: 'test',
beforeFileValidate: sinon.spy(),
onFileValidate: sinon.spy(),
afterFileValidate: sinon.spy()
};
program.plugins.add(plugin);
program.addOrReplaceFile('components/main.xml', '');
expect(plugin.beforeFileValidate.callCount).to.equal(1);
expect(plugin.onFileValidate.callCount).to.equal(1);
expect(plugin.afterFileValidate.callCount).to.equal(1);
});
});
});
12 changes: 12 additions & 0 deletions src/files/BrsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ export class BrsFile {
//find all places where a sub/function is being called
this.findFunctionCalls();

//emit an event before starting to validate this file
this.program.plugins.emit('beforeFileValidate', {
file: this,
program: this.program
});

//emit an event to allow plugins to contribute to the file validation process
this.program.plugins.emit('onFileValidate', {
file: this,
program: this.program
});

this.findAndValidateImportAndImportStatements();

//attach this file to every diagnostic
Expand Down
26 changes: 19 additions & 7 deletions src/files/XmlFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,28 @@ export class XmlFile {

this.getCommentFlags(this.parser.tokens as any[]);

if (!this.parser.ast.root) {
//skip empty XML
return;
}

//notify AST ready
this.program.plugins.emit('afterFileParse', this);

//initial validation
this.validateComponent(this.parser.ast);
//emit an event before starting to validate this file
this.program.plugins.emit('beforeFileValidate', {
file: this,
program: this.program
});

//emit an event to allow plugins to contribute to the file validation process
this.program.plugins.emit('onFileValidate', {
file: this,
program: this.program
});

if (this.parser.ast.root) {

//initial validation
this.validateComponent(this.parser.ast);
} else {
//skip empty XML
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ export interface CompilerPlugin {
//file events
beforeFileParse?: (source: SourceObj) => void;
afterFileParse?: (file: BscFile) => void;
/**
* Called before each file is validated
*/
beforeFileValidate?: PluginHandler<BeforeFileValidateEvent>;
/**
* Called during the file validation process. If your plugin contributes file validations, this is a good place to contribute them.
*/
onFileValidate?: PluginHandler<OnFileValidateEvent>;
/**
* Called after each file is validated
*/
afterFileValidate?: (file: BscFile) => void;
beforeFileTranspile?: PluginHandler<BeforeFileTranspileEvent>;
afterFileTranspile?: PluginHandler<AfterFileTranspileEvent>;
Expand All @@ -231,6 +242,16 @@ export interface OnGetSemanticTokensEvent {
semanticTokens: SemanticToken[];
}

export interface BeforeFileValidateEvent<T extends BscFile = BscFile> {
program: Program;
file: T;
}

export interface OnFileValidateEvent<T extends BscFile = BscFile> {
program: Program;
file: T;
}

export type Editor = Pick<AstEditor, 'addToArray' | 'hasChanges' | 'removeFromArray' | 'setArrayValue' | 'setProperty'>;

export interface BeforeFileTranspileEvent {
Expand Down