rpetrich / babel-plugin-transform-async-to-promises

Transform async/await to somewhat idiomatic JavaScript promise chains
MIT License
246 stars 17 forks source link

Breaks "this" in class expressions #85

Open wh1t3cAt1k opened 2 years ago

wh1t3cAt1k commented 2 years ago

Excerpt from babel-jest.config.js:

    plugins: [
        '@userlike/babel-plugin-joke',
        [
            'module-resolver',
            {
                root: [repositoryRoot],
                alias: {
                        /**/
                },
                cwd: repositoryRoot,
            },
        ],
        ['@babel/plugin-proposal-decorators', { legacy: true }],
        '@babel/plugin-proposal-class-properties',
        ['babel-plugin-transform-async-to-promises', { inlineHelpers: true }],
        'babel-plugin-macros',
    ],
    presets: [
        '@babel/preset-typescript',
        '@babel/preset-react',
        ['@babel/preset-env', { targets: { node: 16 } }],
    ],

Simplified source file file: test.ts

it('is a simplified test', async () => {
    customFunctionMap[identifier] = coercePartial<
        ClassType<FunctionCallBase> & VelixoFunctionStatic
    >(
        class {
            public constructor(private readonly _group: string) {}

            public static readonly shouldClusterComputations = true;

            public getInvocationClusterKey = (): string => this._group; // here!
        }
    );

    const result = new classVariable("group");

Resulting file:

it('...', _async(function () {
  var _class;

  _customFunctionMap.customFunctionMap[identifier] = (0, _coercePartial.coercePartial)((_class = class {
    constructor(_group) {
      this._group = _group;

      this.getInvocationClusterKey = () => _this._group; // note "_this" is renamed unnecessarily here
    }

  }, _class.shouldClusterComputations = true, _class));

I verified that it does not happen when I remove the plugin from the plugins list.