Skip to content

Commit

Permalink
Fixes bug with transpilinig empty for loop (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
markwpearce authored Feb 17, 2022
1 parent 2be26dc commit bd49753
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
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
`);
});

});

0 comments on commit bd49753

Please sign in to comment.