jayphelps / core-decorators

Library of stage-0 JavaScript decorators (aka ES2016/ES7 decorators but not accurate) inspired by languages that come with built-ins like @​override, @​deprecate, @​autobind, @​mixin and more. Popular with React/Angular, but is framework agnostic.
MIT License
4.52k stars 263 forks source link

override arrow function error #126

Closed xiong-john closed 7 years ago

xiong-john commented 7 years ago

I am using override decorator with webpack babel (transform-decorators-legacy) just like the way below:

class A {
  constructor() {

  }

  a=()=>{

  }
}

class B extends A {
  constructor() {
    super()
  }

  @override
  a=()=>{

  }
}

but there are some bugs I can't figure out.

Uncaught SyntaxError: No descriptor matching B#a was found on the prototype chain.
    at SyntaxErrorReporter.error (webpack:///./~/core-decorators/es/override.js?:123:13)
    at handleDescriptor (webpack:///./~/core-decorators/es/override.js?:268:14)
    at decorate (webpack:///./~/core-decorators/es/private/utils.js?:88:29)
    at override (webpack:///./~/core-decorators/es/override.js?:281:97)
    at eval (webpack:///./src/modules/caton/view/deviceperformance/catonmonitor/diffversion/index.js?:72:10)
    at Array.reduce (native)
    at _applyDecoratedDescriptor (webpack:///./src/modules/caton/view/deviceperformance/catonmonitor/diffversion/index.js?:71:38)
    at eval (webpack:///./src/modules/caton/view/deviceperformance/catonmonitor/diffversion/index.js?:422:22)
    at Object.<anonymous> (http://localhost/caton/bundle.bedc15f3a60f4010484a.js:586:1)
    at __webpack_require__ (http://localhost/caton/bundle.bedc15f3a60f4010484a.js:20:30)
xiong-john commented 7 years ago

babel-loader

         {
                      "plugins": [
                        "transform-decorators-legacy",
                        "transform-class-properties",
                        "transform-object-rest-spread",
                      ],
                      "presets": ['env', "es2015", "react", "es2017"]
                    }
jayphelps commented 7 years ago

The @override decorator only works on things that are prototype based, so only methods/getters/setters. Property initializers are not attached to the prototype so there is no way (at least currently) to look them up from the parent to see if they might match.

Works with methods and getters/setters. Will ensure name, parameter count, as well as descriptor type (accessor/data). Provides a suggestion if it finds a method with a similar signature, including slight misspellings.

Thinking about this further, even if we could look them up I'm not sure how we could check they override correctly since property initializers are thunked expressions that are only evaluated at class instance creation time so for example, we wouldn't know that the contents of the initializer was an arrow function with zero parameters. They're just fundamentally incompatible concepts.

jayphelps commented 7 years ago

Tried to make the README more clear here: 😄 https://github.com/jayphelps/core-decorators.js/commit/2599b7627424e39b0c026bcfa3516ae85d4100c7

Sorry about the confusion!

xiong-john commented 7 years ago

@jayphelps thanks for your answer