thingworx-field-work / ThingworxVSCodeProject

Develop thingworx models using a real IDE
MIT License
33 stars 17 forks source link

Enum used by functions called by functions are not always translated in their values #68

Closed kklorenzotesta closed 6 months ago

kklorenzotesta commented 6 months ago

(But also exists after updating to version 2.0.1) Similar to issue #66 , when an enum is used in a function called by a function, and the enum is declared in a different file (doesn't matter if the file is before or after alphabetically in this case) the enum value is not copied, leading to runtime errors. A full ready to use example that reproduces the issue is on the same fork: github.com/kklorenzotesta/ThingworxVSCodeProjectFunctionsOrderBug

In this example I have this files:

const enum MyEnum {
  M1 = "m1",
  M2 = "m2",
}

@ThingDefinition
class ThingA extends GenericThing {
  test() {
    f1();
  }
}

and

function f2() {
  logger.error(MyEnum.M2);
}

function f1() {
  MyEnum.M1;
  f2();
}

@ThingDefinition
class ThingB extends GenericThing {
  test() {
    f1();
  }
}

In both ThingA and ThingB the service test is transpiled as:

var result = (function () {const METHOD_NAME = "test";
function f2() {
    logger.error(MyEnum.M2);
}

function f1() {
    "m1";
    f2();
}

    f1();
}).apply(me, [])

Which fails at runtime since MyEnum doesn't exist for Thingworx.

If I move the enum definition in the same file as ThingB then all services are correctly compiled as:

var result = (function () {const METHOD_NAME = "test";
function f2() {
    logger.error("m2");
}

function f1() {
    "m1";
    f2();
}

    f1();
}).apply(me, [])
BogdanMihaiciuc commented 6 months ago

Should be fixed by https://github.com/BogdanMihaiciuc/ThingTransformer/commit/b4d988aa36912d9693dd39e0d6d5281d0b215d8f

This issue occurred for functions that were defined on the first line in any source file and it didn't matter which file the enum or function were defined in or whether they were dependencies.

When you have time, can you repeat your test with the transformer version in the development branch? If that solves this for you I will publish a release with the fix and close this issue.

Thanks

kklorenzotesta commented 6 months ago

I tested it from development and now its working correctly in my code, thank you for the fix!

BogdanMihaiciuc commented 6 months ago

Fixed in transformer v2.0.2, please reopen if you still have this issue