sitevision / sitevision-apps

Create SiteVision WebApps and RESTApps
15 stars 17 forks source link

Variables are not redeclared in looping scopes when creating an app with TypeScript #81

Closed Wiktry closed 1 year ago

Wiktry commented 1 year ago

Description

When declaring a const variable inside a looped scope that variable will not update during loops beyond the first. This issue does not occur when using normal JavaScript.

Example code

for (let i = 0; i < 10; i++) {
    const test = i;
    logUtil.info(JSON.stringify(test));
}

This code should print 0 to 9, but currently prints 10 zeros.

Using

Sitevision 2022.11.1.1 sitevision api: 2023.2.1 sitevision-scripts: 3.1.5

Additional

The same issue occurs when creating a script module directly on a Sitevision page using the following code:

for (let i = 0; i < 10; i++) {
   const test = i;
   out.println(test);
}
albinohrn commented 1 year ago

Good catch!

This is due to incompatibilities in the Rhino scripting engine. You can fix this by updating your tsconfig.json to

{
  "compilerOptions": {
    "target": "ES5"
    ...
  }
}

I will update the provided tsconfig to address this...

Wiktry commented 1 year ago

Thanks for a quick answer. I'm guessing Issue 83 will have the same solution, then. But I'm leaving it up for posterity.

albinohrn commented 1 year ago

Yes! #83 will be solved by the same fix!

Thanks you for your reports!