clasp icon indicating copy to clipboard operation
clasp copied to clipboard

Map objects are not being handled properly during conversion from typescript to google apps script

Open PedroHBrasil opened this issue 4 years ago • 2 comments

(Note: Non-breaking issues are likely not to be prioritized. Please consider a PR in addition to your issue)

Expected Behavior

When pushing a project written in TypeScript, it would be expected that a for...of loop applied on a map iterator (such as the return of map.entries() and map.keys()) would result on an executable loop on google apps script.

Also, the piece of code [...map_object.entries()] should return an array that contains sub-arrays filled with key-value pairs (like [[key1, value1], [key2, value2], ... , [keyN, valueN]]).

Actual Behavior

When pushing a project, for...of loops applied to map iterators have their code replaced to non-executable loops (like for (var _i = 0, _a = map_object.entries(); _i < _a.length; _i++)

Also, the convertion does not handle the piece of code [...map_object.entries()] properly, since it's converted to a piece of code that returns an empty array (__spreadArray([], data_map.entries(), true)).

Steps to Reproduce the Problem

  1. On a GAS project, add a .ts file.
  2. Create a map object, such as const map_object = new Map();
  3. Add an entry to the map object, such as map_object.set("key1", "value1");.
  4. Write a for...of loop such as for (const entry of map_object.entries()).
  5. Inside the loop, write Logger.log(entry);.
  6. Outside the loop, add the line Logger.log([...map_object.entries()]);.
  7. Push the project to GAS.
  8. Check and run the code on GAS and check the log.

Specifications

  • Node version (node -v): v16.14.0
  • Version (clasp -v): 2.4.1
  • OS (Mac/Linux/Windows): Windows

PedroHBrasil avatar Feb 21 '22 18:02 PedroHBrasil

I have also encountered this issue

I don't know why clasp can't just compile to ES6. This issue can easily be mitigated

muddi900 avatar Aug 25 '22 07:08 muddi900

The workaround is to use typescript and add this to the tsconfig.json:

{
    "compilerOptions": {
      "lib": ["esnext"],
      "experimentalDecorators": true,
      "target": "ES2015"
    }
}

muddi900 avatar Aug 26 '22 06:08 muddi900