diff --git a/src/parser/Statement.ts b/src/parser/Statement.ts index 9b9189e53..e45bb10d8 100644 --- a/src/parser/Statement.ts +++ b/src/parser/Statement.ts @@ -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(), @@ -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(), diff --git a/src/parser/tests/statement/For.spec.ts b/src/parser/tests/statement/For.spec.ts new file mode 100644 index 000000000..ba6b3d561 --- /dev/null +++ b/src/parser/tests/statement/For.spec.ts @@ -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 + `); + }); + +}); diff --git a/src/parser/tests/statement/ForEach.spec.ts b/src/parser/tests/statement/ForEach.spec.ts new file mode 100644 index 000000000..fab591aff --- /dev/null +++ b/src/parser/tests/statement/ForEach.spec.ts @@ -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 + `); + }); + +});