Skip to content

Commit

Permalink
Transpile if statements as-written.
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Mar 10, 2022
1 parent 7ba1739 commit a019e41
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/files/BrsFile.Class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ describe('BrsFile BrighterScript classes', () => {
instance.super0_sayHello = instance.sayHello
instance.sayHello = function(text)
text = "The duck says " + text
if text <> invalid then
if text <> invalid
m.super0_sayHello(text)
end if
end function
Expand Down
62 changes: 38 additions & 24 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,44 @@ describe('BrsFile', () => {
});

describe('transpile', () => {
it('transpiles if statement keywords as provided', () => {
const code = `
If True Then
Print True
Else If True Then
print True
Else If False Then
Print False
Else
Print False
End If
`;
testTranspile(code);
testTranspile(code.toLowerCase());
testTranspile(code.toUpperCase());
});

it('does not transpile `then` tokens', () => {
const code = `
if true
print true
else if true
print false
end if
`;
testTranspile(code);
});

it('honors spacing between multi-word tokens', () => {
testTranspile(`
if true
print true
elseif true
print false
endif
`);
});

it('retains casing of parameter types', () => {
function test(type: string) {
testTranspile(`
Expand Down Expand Up @@ -2164,30 +2202,6 @@ describe('BrsFile', () => {
`, null, 'trim');
});

it('adds `then` when missing', () => {
testTranspile(`
sub a()
if true
print "true"
else if true
print "true"
else
print "true"
end if
end sub
`, `
sub a()
if true then
print "true"
else if true then
print "true"
else
print "true"
end if
end sub
`, 'trim');
});

it('does not add leading or trailing newlines', () => {
testTranspile(`function abc()\nend function`, undefined, 'none');
});
Expand Down
7 changes: 3 additions & 4 deletions src/parser/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,12 @@ export class IfStatement extends Statement {
results.push(' ');
//conditions
results.push(...this.condition.transpile(state));
results.push(' ');
//then
if (this.tokens.then) {
results.push(' ');
results.push(
state.transpileToken(this.tokens.then)
);
} else {
results.push('then');
}
state.lineage.unshift(this);

Expand Down Expand Up @@ -467,7 +465,8 @@ export class IfStatement extends Statement {
state.lineage.shift();

if (body.length > 0) {
results.push(' ');
//zero or more spaces between the `else` and the `if`
results.push(this.elseBranch.tokens.if.leadingWhitespace);
results.push(...body);

// stop here because chained if will transpile the rest
Expand Down

0 comments on commit a019e41

Please sign in to comment.