babel-plugin-jsdoc-to-assert
Version:
Babel plugin convert jsdoc to assertion.
255 lines (190 loc) • 6.69 kB
JavaScript
// LICENSE : MIT
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var _jsdocToAssert = require("jsdoc-to-assert");
var _astUtil = require("./ast-util");
var _util = require("./util");
var _generators = require("./generators");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* `comment` node contain @type, return true
* @param {Object} comment
* @returns {boolean}
*/
function containTypeComment(comment) {
if (comment == null) {
return false;
}
return / /.test(comment.value);
}
/**
* if the `path` have not comments, return true
* @param {Object} path
* @returns {boolean}
*/
function maybeSkip(path) {
if (path.__jsdoc_to_assert_checked__) {
return true;
}
var node = path.node;
if (node.leadingComments != null && node.leadingComments.length > 0) {
return false;
}
return true;
}
/**
* @param {Object} [options]
* @returns {{Generator: *}}
*/
function useGenerator() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
// more simple console.assert
if (options.simple) {
return {
Generator: _generators.SimpleGenerator
};
} // throw new Error
if (options.throw) {
return {
Generator: _generators.ThrowGenerator
};
} // use `assert` module
// It depended on https://github.com/azu/babel-plugin-auto-import-assert
if (options.useNodeAssert) {
return {
Generator: _generators.NodeAssertGenerator
};
} // Default: SpecGenerator
// Readable output for human
return {
Generator: _generators.SpecGenerator
};
}
function _default(_ref) {
var _visitor;
var t = _ref.types,
template = _ref.template;
// work only { checkAtType: true }
var injectTypeAssert = function injectTypeAssert(declarationsPath, identifierName, leadingComments, options) {
if (!options.checkAtType) {
return;
}
var converterOptions = useGenerator(options);
var comment = leadingComments[leadingComments.length - 1];
if (comment.type !== 'CommentBlock') {
return;
}
var asserts = _jsdocToAssert.CommentConverter.toTypeAsserts(identifierName, comment, converterOptions); // no have assert, ignore this
if (asserts.length === 0) {
return;
}
var functionDeclarationString = (0, _util.trimSpaceEachLine)(asserts).join("\n");
var builtAssert = template(functionDeclarationString)();
if (builtAssert) {
declarationsPath.insertAfter(builtAssert);
}
}; // work only { checkAtParam: true|undefined }
var injectParameterAssert = function injectParameterAssert(path, leadingComments, options) {
// default: enable
if (options.checkAtParam === false) {
return;
}
var converterOptions = useGenerator(options);
var comment = leadingComments[leadingComments.length - 1];
if (comment.type !== 'CommentBlock') {
return;
}
var asserts = _jsdocToAssert.CommentConverter.toAsserts(comment, converterOptions); // no have assert, ignore this
if (asserts.length === 0) {
return;
} // add check mark and it is not enumerable
Object.defineProperty(path, "__jsdoc_to_assert_checked__", {
enumerable: false,
value: true
});
var functionDeclarationString = (0, _util.trimSpaceEachLine)(asserts).join("\n");
var builtAssert = template(functionDeclarationString)();
var bodyPath = path.get("body");
if (bodyPath && bodyPath.node && bodyPath.node["body"]) {
bodyPath.unshiftContainer("body", builtAssert);
}
};
return {
visitor: (_visitor = {}, _defineProperty(_visitor, "AssignmentExpression", function AssignmentExpression(path) {
var parentPath = path.parentPath;
if (maybeSkip(parentPath)) {
return;
}
var node = path.node;
var leadingComments = parentPath.node.leadingComments;
if (leadingComments == null) {
return;
}
var identifierName = (0, _astUtil.AssignmentExpressionLeftToString)(node.left);
var isTypeComments = leadingComments.some(containTypeComment);
if (identifierName && isTypeComments) {
injectTypeAssert(path, identifierName, leadingComments, this.opts);
}
}), _defineProperty(_visitor, "ArrowFunctionExpression|VariableDeclaration", function ArrowFunctionExpressionVariableDeclaration(path) {
if (maybeSkip(path)) {
return;
}
var node = path.node;
if (node.declarations) {
var firstDeclaration = path.get('declarations')[0];
if (firstDeclaration.isVariableDeclaration()) {
return;
}
var init = firstDeclaration.get("init");
if (!init) {
return;
}
var leadingComments = node.leadingComments;
var isTypeComments = leadingComments.some(containTypeComment);
if (isTypeComments) {
if (firstDeclaration.node.id == null) {
return;
}
if (firstDeclaration.node.id.type !== "Identifier") {
return;
}
var identifierName = firstDeclaration.node.id.name;
injectTypeAssert(path, identifierName, leadingComments, this.opts);
} else {
injectParameterAssert(init, leadingComments, this.opts);
}
}
}), _defineProperty(_visitor, "FunctionExpression", function FunctionExpression(path) {
if (maybeSkip(path.parentPath)) {
return;
}
var leadingComments = path.parentPath.node.leadingComments;
if (leadingComments == null) {
return;
}
injectParameterAssert(path, leadingComments, this.opts);
}), _defineProperty(_visitor, "ExportNamedDeclaration|ExportDefaultDeclaration", function ExportNamedDeclarationExportDefaultDeclaration(path) {
if (maybeSkip(path)) {
return;
}
var node = path.node;
if (node.declaration) {
var declaration = path.get("declaration");
if (declaration.isVariableDeclaration()) {
return;
}
injectParameterAssert(declaration, node.leadingComments, this.opts);
}
}), _defineProperty(_visitor, "ObjectMethod|ClassMethod|FunctionDeclaration", function ObjectMethodClassMethodFunctionDeclaration(path) {
if (maybeSkip(path)) {
return;
}
var node = path.node;
injectParameterAssert(path, node.leadingComments, this.opts);
}), _visitor)
};
}
//# sourceMappingURL=index.js.map