Skip to content

Commit 17b5e8d

Browse files
Merge branch 'master' of https://github.com/rokucommunity/brighterscript into ast-to-string
2 parents 168073f + 29628da commit 17b5e8d

File tree

9 files changed

+49
-14
lines changed

9 files changed

+49
-14
lines changed

src/PluginInterface.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('PluginInterface', () => {
77
let pluginInterface: PluginInterface;
88

99
beforeEach(() => {
10-
pluginInterface = new PluginInterface([], new Logger());
10+
pluginInterface = new PluginInterface([], { logger: new Logger() });
1111
});
1212

1313
it('allows adding a plugin', () => {

src/PluginInterface.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,25 @@ export default class PluginInterface<T extends CompilerPlugin = CompilerPlugin>
1111

1212
constructor(
1313
private plugins: CompilerPlugin[],
14-
private logger: Logger
15-
) { }
14+
options: {
15+
logger: Logger;
16+
suppressErrors?: boolean;
17+
}
18+
) {
19+
if (options?.constructor.name === 'Logger') {
20+
this.logger = options as unknown as Logger;
21+
} else {
22+
this.logger = options?.logger;
23+
this.suppressErrors = options?.suppressErrors === false ? false : true;
24+
}
25+
}
26+
27+
private logger: Logger;
28+
29+
/**
30+
* Should plugin errors cause the program to fail, or should they be caught and simply logged
31+
*/
32+
private suppressErrors: boolean;
1633

1734
/**
1835
* Call `event` on plugins
@@ -26,6 +43,9 @@ export default class PluginInterface<T extends CompilerPlugin = CompilerPlugin>
2643
});
2744
} catch (err) {
2845
this.logger.error(`Error when calling plugin ${plugin.name}.${event}:`, err);
46+
if (!this.suppressErrors) {
47+
throw err;
48+
}
2949
}
3050
}
3151
}

src/Program.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe('Program', () => {
187187
beforeFileParse: beforeFileParse,
188188
afterFileParse: afterFileParse,
189189
afterFileValidate: afterFileValidate
190-
}], new Logger());
190+
}], { logger: new Logger() });
191191

192192
//add a new source file
193193
program.setFile('source/main.brs', '');

src/Program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class Program {
6565
) {
6666
this.options = util.normalizeConfig(options);
6767
this.logger = logger || new Logger(options.logLevel as LogLevel);
68-
this.plugins = plugins || new PluginInterface([], this.logger);
68+
this.plugins = plugins || new PluginInterface([], { logger: this.logger });
6969

7070
//inject the bsc plugin as the first plugin in the stack.
7171
this.plugins.addFirst(new BscPlugin());

src/ProgramBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class ProgramBuilder {
3636
private watcher: Watcher;
3737
public program: Program;
3838
public logger = new Logger();
39-
public plugins: PluginInterface = new PluginInterface([], this.logger);
39+
public plugins: PluginInterface = new PluginInterface([], { logger: this.logger });
4040
private fileResolvers = [] as FileResolver[];
4141

4242
public addFileResolver(fileResolver: FileResolver) {

src/Scope.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ describe('Scope', () => {
961961
program.setFile(s`components/comp.brs`, ``);
962962
const sourceScope = program.getScopeByName('source');
963963
const compScope = program.getScopeByName('components/comp.xml');
964-
program.plugins = new PluginInterface([], new Logger());
964+
program.plugins = new PluginInterface([], { logger: new Logger() });
965965
const plugin = program.plugins.add({
966966
name: 'Emits validation events',
967967
beforeScopeValidate: sinon.spy(),

src/bscPlugin/validation/BrsFileValidator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,14 @@ export class BrsFileValidator {
301301
}
302302

303303
private validateContinueStatement(statement: ContinueStatement) {
304-
const validateLoopTypeMatch = (loopType: TokenKind) => {
304+
const validateLoopTypeMatch = (expectedLoopType: TokenKind) => {
305305
//coerce ForEach to For
306-
loopType = loopType === TokenKind.ForEach ? TokenKind.For : loopType;
307-
308-
if (loopType?.toLowerCase() !== statement.tokens.loopType.text?.toLowerCase()) {
306+
expectedLoopType = expectedLoopType === TokenKind.ForEach ? TokenKind.For : expectedLoopType;
307+
const actualLoopType = statement.tokens.loopType;
308+
if (actualLoopType && expectedLoopType?.toLowerCase() !== actualLoopType.text?.toLowerCase()) {
309309
this.event.file.addDiagnostic({
310310
range: statement.tokens.loopType.range,
311-
...DiagnosticMessages.expectedToken(loopType)
311+
...DiagnosticMessages.expectedToken(expectedLoopType)
312312
});
313313
}
314314
};

src/files/BrsFile.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,7 +3385,7 @@ describe('BrsFile', () => {
33853385
util.loadPlugins(tempDir, [
33863386
s`${tempDir}/plugins/${pluginFileName}`
33873387
]),
3388-
new Logger()
3388+
{ logger: new Logger() }
33893389
);
33903390
const file = program.setFile<any>('source/MAIN.brs', '');
33913391
expect(file._customProp).to.exist;
@@ -3396,7 +3396,7 @@ describe('BrsFile', () => {
33963396
util.loadPlugins(tempDir, [
33973397
`./plugins/${pluginFileName}`
33983398
]),
3399-
new Logger()
3399+
{ logger: new Logger() }
34003400
);
34013401
const file = program.setFile<any>('source/MAIN.brs', '');
34023402
expect(file._customProp).to.exist;

src/parser/tests/statement/Continue.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,19 @@ describe('parser continue statements', () => {
108108
end sub
109109
`);
110110
});
111+
112+
it('does not crash when missing loop type', () => {
113+
program.plugins['suppressErrors'] = false;
114+
program.setFile('source/main.brs', `
115+
sub main()
116+
while true
117+
continue
118+
end while
119+
end sub
120+
`);
121+
program.validate();
122+
expectDiagnostics(program, [
123+
DiagnosticMessages.expectedToken(TokenKind.While, TokenKind.For).message
124+
]);
125+
});
111126
});

0 commit comments

Comments
 (0)