Skip to content

Commit

Permalink
Prevent a double super call in subclasses (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Nov 8, 2022
1 parent 414568f commit a2e0362
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/files/BrsFile.Class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,45 @@ describe('BrsFile BrighterScript classes', () => {
`, 'trim', 'source/main.bs');
});

it('allows comments as first line of constructor', () => {
testTranspile(`
class Animal
end class
class Duck extends Animal
sub new()
'comment should not cause double super call
super()
end sub
end class
`, `
function __Animal_builder()
instance = {}
instance.new = sub()
end sub
return instance
end function
function Animal()
instance = __Animal_builder()
instance.new()
return instance
end function
function __Duck_builder()
instance = __Animal_builder()
instance.super0_new = instance.new
instance.new = sub()
'comment should not cause double super call
m.super0_new()
end sub
return instance
end function
function Duck()
instance = __Duck_builder()
instance.new()
return instance
end function
`);
});

it('handles class inheritance inferred constructor calls', () => {
testTranspile(`
class Animal
Expand Down
3 changes: 2 additions & 1 deletion src/parser/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2019,8 +2019,9 @@ export class MethodStatement extends FunctionStatement {
return;
}

//find the first non-comment statement
let firstStatement = this.func.body.statements.find(x => !isCommentStatement(x));
//if the first statement is a call to super, quit here
let firstStatement = this.func.body.statements[0];
if (
//is a call statement
isExpressionStatement(firstStatement) && isCallExpression(firstStatement.expression) &&
Expand Down

0 comments on commit a2e0362

Please sign in to comment.