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

Transpile if statements as written #537

Merged
merged 6 commits into from
Mar 10, 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
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
75 changes: 51 additions & 24 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,57 @@ 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('handles when only some of the statements have `then`', () => {
testTranspile(`
if true
else if true then
else if true
else if true then
if true then
return true
end if
end if
`);
});

it('retains casing of parameter types', () => {
function test(type: string) {
testTranspile(`
Expand Down Expand Up @@ -2164,30 +2215,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