Swatinem / rollup-plugin-dts

A rollup plugin to generate .d.ts rollup files for your typescript project
GNU Lesser General Public License v3.0
816 stars 71 forks source link

typeof this; are being replaced by typeof this$1; #279

Closed AndreiLucas123 closed 1 year ago

AndreiLucas123 commented 1 year ago

Checklist

Code Before

export declare class NumberSchema<T = number> extends Schema<T> {
    min: (value: number) => typeof this;
}

Code After

There are no error messages, what is happening is typeof this; is becaming typeof this$1; for some weird reason

declare class NumberSchema<T = number> extends Schema<T> {
    min: (value: number) => typeof this$1;
}

My rollup config

{
  input: './dist/src/index.d.ts',
  output: [{ file: 'dist/index.d.ts', format: 'es' }],
  plugins: [dts()],
},

My temporary sollution

Created by ChatGPT

// rollup-plugin-replace-this.js
function replaceThis() {
  return {
    name: 'replace-this-plugin',
    renderChunk(code) {
      // Substitui todas as ocorrências de `this$1` por `this`
      const modifiedCode = code.replace(/this\$1;/g, 'this;');
      return {
        code: modifiedCode,
        map: null,
      };
    },
  };
}
crutchcorn commented 1 year ago

I'm investigating a fix for this. I can confirm that by the time the code reaches renderChunk the code is failing: https://github.com/Swatinem/rollup-plugin-dts/blob/master/src/transform/index.ts#L96

However, preprocessed code is fine (no transformation of this) at this point: https://github.com/Swatinem/rollup-plugin-dts/blob/master/src/transform/index.ts#L81

And fine at the parse step: https://github.com/Swatinem/rollup-plugin-dts/blob/master/src/transform/index.ts#L83

Using this process of elimination, I'm gonna start investigating into the convert function: https://github.com/Swatinem/rollup-plugin-dts/blob/master/src/transform/index.ts#L84

crutchcorn commented 1 year ago

I've opened a pull request that seems to solve this issue here:

https://github.com/Swatinem/rollup-plugin-dts/pull/287