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

Fixes bug with transpiling empty for loop #459 #519

Merged
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
14 changes: 8 additions & 6 deletions src/parser/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,9 +836,10 @@ export class ForStatement extends Statement {
state.lineage.unshift(this);
result.push(...this.body.transpile(state));
state.lineage.shift();
if (this.body.statements.length > 0) {
result.push('\n');
}

// add new line before "end for"
result.push('\n');

//end for
result.push(
state.indent(),
Expand Down Expand Up @@ -903,9 +904,10 @@ export class ForEachStatement extends Statement {
state.lineage.unshift(this);
result.push(...this.body.transpile(state));
state.lineage.shift();
if (this.body.statements.length > 0) {
result.push('\n');
}

// add new line before "end for"
result.push('\n');

//end for
result.push(
state.indent(),
Expand Down
51 changes: 51 additions & 0 deletions src/parser/tests/statement/For.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

import { standardizePath as s } from '../../../util';
import { Program } from '../../../Program';
import { createSandbox } from 'sinon';
import { getTestTranspile } from '../../../testHelpers.spec';

const sinon = createSandbox();

describe('ForStatement', () => {
let rootDir = s`${process.cwd()}/.tmp/rootDir`;
let program: Program;
let testTranspile = getTestTranspile(() => [program, rootDir]);

beforeEach(() => {
program = new Program({ rootDir: rootDir, sourceMap: true });
});
afterEach(() => {
sinon.restore();
program.dispose();
});

it('transpiles a simple loop', () => {
testTranspile(`
sub main()
for i = 0 to 10
print i
end for
end sub
`);
});

it('transpiles with a for loop with a step value', () => {
testTranspile(`
sub main()
for i = 0 to 10 step 2
print i
end for
end sub
`);
});

it('adds newline to end of empty loop declaration', () => {
testTranspile(`
sub main()
for i = 0 to 10
end for
end sub
`);
});

});
41 changes: 41 additions & 0 deletions src/parser/tests/statement/ForEach.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import { standardizePath as s } from '../../../util';
import { Program } from '../../../Program';
import { createSandbox } from 'sinon';
import { getTestTranspile } from '../../../testHelpers.spec';

const sinon = createSandbox();

describe('ForEachStatement', () => {
let rootDir = s`${process.cwd()}/.tmp/rootDir`;
let program: Program;
let testTranspile = getTestTranspile(() => [program, rootDir]);

beforeEach(() => {
program = new Program({ rootDir: rootDir, sourceMap: true });
});
afterEach(() => {
sinon.restore();
program.dispose();
});

it('transpiles a simple loop', () => {
testTranspile(`
sub doLoop(data)
for each i in data
print i
end for
end sub
`);
});

it('adds newline to end of empty loop declaration', () => {
testTranspile(`
sub doLoop(data)
for each i in data
end for
end sub
`);
});

});