grant / ts2gas

A function that transpiles TypeScript to Google Apps Script.
http://npmjs.com/ts2gas
MIT License
88 stars 11 forks source link

noSubstitutionBeforeBuilder cannot correctly convert try-catch clauses containing asycn / await. #58

Closed Himenon closed 3 years ago

Himenon commented 3 years ago

Input Source Code

async function main() {
  try {
    await wait(1000);
  } catch (error) {
    console.log(error);
  }
}

Build with ts2gas

// ...
function main() {
    return __awaiter(this, void 0, void 0, function () {
        var error_1;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    _a.trys.push([0, 2, , 3]);
                    return [4 /*yield*/, wait(1000)];
                case 1:
                    _a.sent();
                    return [3 /*break*/, 3];
                case 2:
                    error_1 = _a.sent();
                    console.log(error);      // <-------- It will be treated as ReferenceError.
                    return [3 /*break*/, 3];
                case 3: return [2 /*return*/];
            }
        });
    });
}

Conversion with TypeScript Playground

// ...
function main() {
    return __awaiter(this, void 0, void 0, function () {
        var error_1;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    _a.trys.push([0, 2, , 3]);
                    return [4 /*yield*/, wait(1000)];
                case 1:
                    _a.sent();
                    return [3 /*break*/, 3];
                case 2:
                    error_1 = _a.sent();
                    console.log(error_1);      // <-------- here
                    return [3 /*break*/, 3];
                case 3: return [2 /*return*/];
            }
        });
    });
}

The judgment here seems to be wrong.

PopGoesTheWza commented 3 years ago

What is your ts2gas use case? There is likely better solution.

For example: https://github.com/PopGoesTheWza/ts-gas-project-starter

PopGoesTheWza commented 3 years ago

Since you are using async syntax, I assume you are targeting V8 powered GAS.

Have you tried setting the compilerOptions.target property to ES2018,

{
  compilerOptions: {
    target: "ES2018",
  },
}
Himenon commented 3 years ago

https://github.com/grant/ts2gas/issues/58#issuecomment-790405370

That's true. That seems to be the solution to my problem. Thank you, @PopGoesTheWza !

I will leave the handling of this issue to you.

PopGoesTheWza commented 3 years ago

@Himenon do you confirm that changing the target to ES2018 solved the issue?

Himenon commented 3 years ago

Yes, this issue has been resolved by changing the target to ES2018.

PopGoesTheWza commented 3 years ago

Neat. Have a great day.