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

Issue with transpiling + destructuring #15

Open
tonygentilcore opened this issue Jan 2, 2017 · 3 comments
Open

Issue with transpiling + destructuring #15

tonygentilcore opened this issue Jan 2, 2017 · 3 comments

Comments

@tonygentilcore
Copy link

tonygentilcore commented Jan 2, 2017

I have a class w/ destructured params in its constructor like this:

class Foo {
  /**
   * @param {?string} x - param.
   */
  constructor({x}) {
    this.x = x

My .babelrc looks like this:

{
  "presets": [["es2015", { "modules": false }], "stage-2"],
  "plugins": [
    "jsdoc-to-assert",
  ],
}

And the code is incorrectly generated like this:

var Foo = function () {
  /**
   * @param {?string} x - param.
   */
  function Foo(_ref) {
    if (!(x == null || typeof x === "string")) {
      console.assert(x == null || typeof x === "string", 'Expected type: @param {?string} x\nActual value:', x, '\nFailure assertion: (x == null || typeof x === "string")');
    }

    var x = _ref.x

NOTE The assert reads x before it's defined.

I'd expect it to be:

var Foo = function () {
  /**
   * @param {?string} x - param.
   */
  function Foo(_ref) {
    var x = _ref.x

    if (!(x == null || typeof x === "string")) {
      console.assert(x == null || typeof x === "string", 'Expected type: @param {?string} x\nActual value:', x, '\nFailure assertion: (x == null || typeof x === "string")');
    }

NOTE that the destructuring happens before the assertion. The current order would also work if the assertion were based on _ref.x instead of x.

tonygentilcore pushed a commit to WildcatIP/other.js that referenced this issue Jan 2, 2017
There are some issues w/ it in prod. Notably:
azu/jsdoc-to-assert#15
azu added a commit to azu/babel-plugin-jsdoc-to-assert that referenced this issue Jan 3, 2017
@azu
Copy link
Owner

azu commented Jan 3, 2017

@tonygentilcore Thanks for report.
I've created test case, but it will be difference result

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Foo =
/**
 * @param {?string} x - param.
 */
function Foo(_ref) {
  var x = _ref.x;

  _classCallCheck(this, Foo);

  this.x = x;
};

Maybe enabling "passPerPreset": true and it will be better.

{
  "passPerPreset": true,
  "presets": [["es2015", { "modules": false }], "stage-2"],
  "plugins": [
    "jsdoc-to-assert",
  ],
}

Result:

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Foo =
/**
 * @param {string} x
 */
function Foo(_ref) {
  var x = _ref.x;

  _classCallCheck(this, Foo);

  console.assert(typeof x === "string");

  this.x = x;
};

@tonygentilcore
Copy link
Author

Unfortunately, passPerPreset didn't solve it for me. Also, the result you pasted above is different from the PR you linked to.

@azu
Copy link
Owner

azu commented Jan 4, 2017

Another snippet to use passPerPreset.

{
  "passPerPreset": true,
  "presets": [
    [
      "es2015",
      {
        "modules": false
      }
    ],
    "stage-2",
    [
      {
        "plugins": [
          "jsdoc-to-assert"
        ]
      }
    ]
  ]
}

Maybe, We should start from creating minimum reproduce repository...

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

2 participants