endel/js2php

there is a bug when converting "for" loop

supersky07 opened this issue · 4 comments

when convert"for(var i = 0; i < test.length; i++)"

Hi @supersky07 ,

There is a test case for for and for in loops here: https://github.com/endel/js2php/blob/master/test/fixtures/loops.js

Could you write a test case to demonstrate the problem you've found?

Thank you.

hi @endel ,
Thank you for your reply. I saw that case.

Here are the problems:
1."for(var i = 0; i < 10; i++)"
After converting, there is a "\n" after $i=0;

2."for(j=0;j<10;j++)"
When there is only one variable before the first ";" and at the same time it has no "var".After converting, there is no ";" between "j=0" and "j<10".

PS: I try to read your code, but it is a little difficult for me to understand the main process. Do you have some flow chart for reference?

Thank you.

@supersky07, actually after compilation there will be ";\n". It isn't a pretty output but it's a valid PHP syntax, as you can see in the test case:

To be sure that it outputs a valid syntax you may pipe the output string to php interpreter from the commandline:

./js2php test/fixtures/loops.js | php
int(1)
int(2)
int(3)
int(4)
...

I see, thank you.
After compilation, I will format that code, it makes me a little uncomfortable, LOL.

Thanks again, it really helps me a lot!