Skip to content

Commit

Permalink
Add onScopeValidate plugin event. (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron authored Feb 4, 2022
1 parent 0190562 commit edc72b0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
6 changes: 5 additions & 1 deletion docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ Full compiler lifecycle:
- `beforeFileParse`
- `afterFileParse`
- `afterScopeCreate` (component scope)
- `afterFileValidate`
- `beforeProgramValidate`
- For each file:
- `beforeFileValidate`
- `onFileValidate`
- `afterFileValidate`
- For each scope:
- `beforeScopeValidate`
- `onScopeValidate`
- `afterScopeValidate`
- `afterProgramValidate`
- `beforePrepublish`
Expand Down
33 changes: 21 additions & 12 deletions src/Scope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { expectDiagnostics, expectZeroDiagnostics, trim } from './testHelpers.sp
import { Logger } from './Logger';
import type { BrsFile } from './files/BrsFile';
import type { FunctionStatement, NamespaceStatement } from './parser/Statement';
import type { OnScopeValidateEvent } from './interfaces';

describe('Scope', () => {
let sinon = sinonImport.createSandbox();
Expand Down Expand Up @@ -503,8 +504,6 @@ describe('Scope', () => {
});

it('Emits validation events', () => {
const validateStartScope = sinon.spy();
const validateEndScope = sinon.spy();
program.addOrReplaceFile('source/file.brs', ``);
program.addOrReplaceFile('components/comp.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
Expand All @@ -515,18 +514,28 @@ describe('Scope', () => {
program.addOrReplaceFile(s`components/comp.brs`, ``);
const sourceScope = program.getScopeByName('source');
const compScope = program.getScopeByName('components/comp.xml');
program.plugins = new PluginInterface([{
program.plugins = new PluginInterface([], new Logger());
const plugin = program.plugins.add({
name: 'Emits validation events',
beforeScopeValidate: validateStartScope,
afterScopeValidate: validateEndScope
}], new Logger());
beforeScopeValidate: sinon.spy(),
onScopeValidate: sinon.spy(),
afterScopeValidate: sinon.spy()
});
program.validate();
expect(validateStartScope.callCount).to.equal(2);
expect(validateStartScope.calledWith(sourceScope)).to.be.true;
expect(validateStartScope.calledWith(compScope)).to.be.true;
expect(validateEndScope.callCount).to.equal(2);
expect(validateEndScope.calledWith(sourceScope)).to.be.true;
expect(validateEndScope.calledWith(compScope)).to.be.true;
const scopeNames = program.getScopes().map(x => x.name).filter(x => x !== 'global').sort();

expect(plugin.beforeScopeValidate.callCount).to.equal(2);
expect(plugin.beforeScopeValidate.calledWith(sourceScope)).to.be.true;
expect(plugin.beforeScopeValidate.calledWith(compScope)).to.be.true;

expect(plugin.onScopeValidate.callCount).to.equal(2);
expect(plugin.onScopeValidate.getCalls().map(
x => (x.args[0] as OnScopeValidateEvent).scope.name
).sort()).to.eql(scopeNames);

expect(plugin.afterScopeValidate.callCount).to.equal(2);
expect(plugin.afterScopeValidate.calledWith(sourceScope)).to.be.true;
expect(plugin.afterScopeValidate.calledWith(compScope)).to.be.true;
});

describe('custom types', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/Scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ export class Scope {

this.program.plugins.emit('beforeScopeValidate', this, files, callableContainerMap);

this.program.plugins.emit('onScopeValidate', {
program: this.program,
scope: this
});

this._validate(callableContainerMap);

this.program.plugins.emit('afterScopeValidate', this, files, callableContainerMap);
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export interface CompilerPlugin {
beforeScopeDispose?: (scope: Scope) => void;
afterScopeDispose?: (scope: Scope) => void;
beforeScopeValidate?: ValidateHandler;
onScopeValidate?: PluginHandler<OnScopeValidateEvent>;
afterScopeValidate?: ValidateHandler;
//file events
beforeFileParse?: (source: SourceObj) => void;
Expand Down Expand Up @@ -253,6 +254,11 @@ export interface OnFileValidateEvent<T extends BscFile = BscFile> {
file: T;
}

export interface OnScopeValidateEvent {
program: Program;
scope: Scope;
}

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

export interface BeforeFileTranspileEvent {
Expand Down

0 comments on commit edc72b0

Please sign in to comment.