Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destructuring and prototype with ESM builds #183

Open
M3kH opened this issue Oct 26, 2022 · 0 comments
Open

Destructuring and prototype with ESM builds #183

M3kH opened this issue Oct 26, 2022 · 0 comments

Comments

@M3kH
Copy link

M3kH commented Oct 26, 2022

When consuming orga you can stubble in such error.

    TypeError: Cannot read properties of undefined (reading 'level')

      160 |       snapshot = {
      161 |         stack: [...stack],
    > 162 |         level: this.level,
          |                     ^
      163 |         attributes: { ...this.attributes },
      164 |         savePoint: lexer.save(),
      165 |       }

      at save (src/parse/context.ts:162:21)
      at drawer (src/parse/drawer.ts:7:3)
      at Object.parse (src/parse/index.ts:132:23)
      at Object.parse (src/index.ts:21:10)
      at src/__tests__/index.test.ts:87:18

The culprit of this issue is when we use this in an object that gets destructured during the consume phase.

An example is the usage of this in packages/orga/src/parse/context.ts which gets destructured here.

The issue is not currently visible in this repository because seems to mainly happen when ESM is enable.

How to reproduce it
An easy way to reproduce it is change the following line:

 {
-  "extends": "../../tsconfig.base.json",
+  "extends": "../../tsconfig.esm.json",
   "compilerOptions": {
     "rootDir": "./src",
     "outDir": "./dist"

And run the test.

Additional thoughts
I believe the right way to solve it is by making sure of not destructuring when relay on scope.
Since this plain JavaScript also fails:

const obj = {
  foo: function () {
    return this.bar;
  },

  get bar() {
    return "hello";
  },
};

const a = (p) => p.foo();
const b = ({ foo }) => foo();

console.log(a(obj));
console.log(b(obj));
M3kH pushed a commit to M3kH/orgajs that referenced this issue Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant