Repository: rokucommunity/brighterscript (ts)

Issue:
empty loop transpiles incorrectly
This code:
```brightscript
for i = 0 to 10
end for
```

incorrectly transpiles to:
```brightscript
for i = 0 to 10    end for
```

Here's a good unit test for this issue:
```brightscript
it.only('adds newline to end of loop', () => {
    testTranspile(`
        sub main()
            for i = 0 to 10
            end for
        end sub
    `);
});
```

Pull request:
Fixes bug with transpiling empty for loop #459
Fixes #459 

Patch:
diff --git a/src/parser/Statement.ts b/src/parser/Statement.ts
index 9b9189e5..e45bb10d 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(),


Test patch:
diff --git a/src/parser/tests/statement/For.spec.ts b/src/parser/tests/statement/For.spec.ts
new file mode 100644
index 00000000..ba6b3d56
--- /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 00000000..fab591af
--- /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
+        `);
+    });
+
+});