Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

11.0.0-beta.0 - Private class methods no-undef error #746

Closed
nrei0 opened this issue Jan 27, 2019 · 7 comments
Closed

11.0.0-beta.0 - Private class methods no-undef error #746

nrei0 opened this issue Jan 27, 2019 · 7 comments

Comments

@nrei0
Copy link

nrei0 commented Jan 27, 2019

I tried using of private class methods, but stuck with no-undef error.

@babel/core@^7.2.0, also added as peer dependency.
babel-eslint@^11.0.0-beta.0.

image

Anybody have any suggestions?

@nrei0
Copy link
Author

nrei0 commented Jan 27, 2019

I also tried to compile, not works.

class WebGLRenderer

  #getBaseTexture(name) {
    const t = this.#textures[name];
    return t && t.textureAtlas || t;
  }

  #isTextureLoaded(name) {
    const t = this.#getBaseTexture(name);
    return !!(t && t.glTexture)
  }

Code above produces Babel compilation error:

ERROR in ./src/engine/renderer/webgl_renderer.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: /home/dev/projects/your-own-game-engine/src/engine/renderer/webgl_renderer.js: Duplicate declaration "t"
  350 | 
  351 |   #isTextureLoaded(name) {
> 352 |     const t = this.#getBaseTexture(name);
      |           ^
  353 |     return !!(t && t.glTexture)
  354 |   }
  355 | 
    at File.buildCodeFrameError (/home/dev/projects/your-own-game-engine/node_modules/@babel/core/lib/transformation/file/file.js:261:12)
    at Scope.checkBlockScopedCollisions (/home/dev/projects/your-own-game-engine/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:347:22)
    at Scope.registerBinding (/home/dev/projects/your-own-game-engine/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:504:16)
    at Scope.registerDeclaration (/home/dev/projects/your-own-game-engine/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:444:14)
    at Object.BlockScoped (/home/dev/projects/your-own-game-engine/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:189:28)
    at Object.newFn (/home/dev/projects/your-own-game-engine/node_modules/@babel/core/node_modules/@babel/traverse/lib/visitors.js:230:17)
    at NodePath._call (/home/dev/projects/your-own-game-engine/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/home/dev/projects/your-own-game-engine/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:36:14)
    at NodePath.visit (/home/dev/projects/your-own-game-engine/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/home/dev/projects/your-own-game-engine/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:118:16)
 @ ./src/engine/renderer/index.js 2:0-49 5:0-101 8:17-30
 @ ./src/example/example1.js
 @ ./src/example/index.js
 @ multi (webpack)-dev-server/client?http://0.0.0.0:8081 ./src/example/index.js

Seems when I have defined next private method, previous was not blocked in method's context.
Looks like babel error, so not related to babel-eslint.

Fun part, if you will rename t of second method to something else (for example a), then compilations works without problems.

There is an example PR: https://github.com/Ateiri/your-own-game-engine/pull/9
Babel issue with naming collision: babel/babel#9267

@kaicataldo
Copy link
Member

The core ESLint rule won’t be able to handle this syntax correctly. If eslint-plugin-babel doesn’t have a corresponding rule that handles this, it’ll have to be added there for this to work correctly.

@gverni
Copy link

gverni commented Feb 3, 2019

Hi @Ateiri I had a similar problem with a class defined like that:

class foo {
  ...
  #myPrivateMethod(arg) {
    ...
  }
  ...
  publicMethod() {
    ...
    this.#myPrivateMethod(arg)
    ...
  }
}

This would trigger the following eslint error:

error 'myPrivateMethod' is not defined no-undef

Not sure this is your same problem. If it is, I solved replacing the definition of the private method with:

  #myPrivateMethod = function (arg) {
    ...
  }

Hope it helps

@nrei0
Copy link
Author

nrei0 commented Feb 3, 2019

@gverni Thank you for your message, yeah, but it will create a property, not a method, so this property will be directly written in instance instead of prototype I guess.

@gverni
Copy link

gverni commented Feb 3, 2019

Good point @Ateiri. To help investigate this issue, I created a repo here with the sample private instance method from Babel 7.2.0 release note. The file transpile and execute correctly but the lint causes the no-undef error.

@nrei0
Copy link
Author

nrei0 commented Feb 3, 2019

@gverni as @kaicataldo mentioned, we need to take a look at eslint-plugin-babel first.
For now I'm going to take a look to babel issue first, I had some comments about missed aliases for babel/types.

@kaicataldo
Copy link
Member

On second look, given that this error is coming from @babel/core and the original author confirmed that this isn't compiled by Babel either, this doesn't look like an issue with babel-eslint. If you're still experiencing this, feel free to make an issue in https://github.com/babel/babel.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants