azu / babel-preset-jsdoc-to-assert

Babel preset for jsdoc-to-assert.
MIT License
3 stars 0 forks source link

If the function is of the property does not work #1

Closed akabekobeko closed 8 years ago

akabekobeko commented 8 years ago

I tried in the following package.json. npm run build was the execution result, normal function and the constructor it was not a problem.

However, If the function is of the property does not work

{
  "babel": {
    "presets": [
      "es2015"
    ],
    "env": {
      "development": {
        "presets": [
          "jsdoc-to-assert",
          "power-assert",
        ]
      }
    }
  },
  "browserify": {
    "transform": [
      "babelify"
    ]
  },
  "scripts": {
    "start": "npm run watch",
    "build:js-app": "browserify ./src/js/App.js --im -d | exorcist ./src/assets/bundle.js.map > ./src/assets/bundle.js",
    "build": "npm-run-all -p build:js-app",
    "watch:js-app": "watchify ./src/js/App.js -v --im -o \"exorcist ./src/assets/bundle.js.map > ./src/assets/bundle.js\" -d",
    "watch:server": "browser-sync start --server ./ --startPath src/assets/",
    "watch": "npm-run-all -p watch:js-app watch:server"
  },
  "devDependencies": {
    "babel-preset-es2015": "^6.13.2",
    "babel-preset-jsdoc-to-assert": "^3.0.1",
    "babel-preset-power-assert": "^1.0.0",
    "babel-preset-react": "^6.11.1",
    "babel-register": "^6.11.6",
    "babelify": "^7.3.0",
    "browser-sync": "^2.14.0",
    "browserify": "^13.1.0",
    "exorcist": "^0.4.0",
    "npm-run-all": "^2.3.0",
    "watchify": "^3.7.0"
  }
}

Sample 1.

Original code:

/**
 * @param {String} message Message.
 */
function log( text ) {
  console.log( text );
}

const obj = {
  /**
   * @param {String} message Message.
   */
  log: function( message ) {
    console.log( message );
  }
};

Transformed:

/**
 * @param {String} message Message.
 */
function log(text) {
  console.assert(typeof text === "string", 'Invalid JSDoc: typeof text === "string"');

  console.log(text);
}

var obj = {
  /**
   * @param {String} message Message.
   */
  log: function( message ) {
    console.log( message );
  }
};

Sample 2.

Original code:

class Sample {
  /**
   * @param {String} name Name.
   */
  constructor( name ) {
    this._name = name;
  }

  /**
   * @param {String} message Message.
   */
  log( message ) {
    console.log( message );
  }
}

Transformed:

var Sample = function () {
  /**
   * @param {String} name Name.
   */
  function Sample(name) {
    console.assert(typeof name === "string", 'Invalid JSDoc: typeof name === "string"');

    _classCallCheck(this, Sample);

    this._name = name;
  }

  /**
   * @param {String} message Message.
   */

  _createClass(Sample, [{
    key: "log",
    value: function log(text) {
      console.log(text);
    }
  }]);

  return Sample;
}();
azu commented 8 years ago

Thanks for report. Ah, FunctionExpression is not suported yet. https://github.com/azu/babel-plugin-jsdoc-to-assert/blob/master/src/index.js

const obj = {
  /**
   * @param {String} message Message.
   */
  log: function( message ) {
    console.log( message );
  }
};

I feel able to support FunctionExpression with JSDoc.

akabekobeko commented 8 years ago

Thanks for consideration.

ES2015 class method of Is it possible to support? In the output of Babel (Part of _createClass) it has been separated function and comment.

azu commented 8 years ago

I've released https://github.com/azu/babel-plugin-jsdoc-to-assert/releases/tag/2.1.0 It support FunctionExpression transform.

babel-plugin-jsdoc-to-assert@2.1.0 and babel-preset-es2015@6.13.2

transform

  /**
   * @param {String} name Name.
   */
  constructor( name ) {
    this._name = name;
  }

  /**
   * @param {String} message Message.
   */
  log( message ) {
    console.log( message );
  }
}

to

"use strict";

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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

var X = function () {
  function X() {
    _classCallCheck(this, X);
  }

  _createClass(X, [{
    key: "method",

    /**
     * @param {number} x - this is a param.
     * @param {string} y - this is a param.
     */
    value: function method(x, y) {
      console.assert(typeof x === "number");
      console.assert(typeof y === "string");
    }
  }]);

  return X;
}();

Babel's transform order is not specified by default.

When es2015 -> jsdoc-to-assert, transform it as FunctionExpression When jsdoc-to-assert -> es2015, transform it as ClassMethod.

FYI, you may use passPerPreset option for specifing order.( I never try it... )

{
  "presets": [
    "es2015",
    "jsdoc-to-assert"
  ],
  "passPerPreset": true
}
akabekobeko commented 8 years ago

Was confirmed that it is fixed in the babel-plugin-jsdoc-to-assert v2.1.0. Test is the following.

Setting of babel is the following, tried in the babel-plugin-jsdoc-to-assert.

babel-preset-jsdoc-to-assert and babel-plugin-jsdoc-to-assert

{
  "passPerPreset": true,
  "presets": [
    "es2015",
    "jsdoc-to-assert"
  ],
  "env": {
    "development": {
      "presets": [
        "power-assert"
      ]
    }
  }
}

babel-plugin-jsdoc-to-assert only

{
  "passPerPreset": true,
  "presets": [
    "es2015"
  ],
  "env": {
    "development": {
      "presets": [
        "power-assert"
      ],
      "plugins": [
        "jsdoc-to-assert"
      ]
    }
  }
}

Thanks for support!