wix-storybook-utils
Version:
Utilities for automated component documentation within Storybook
101 lines • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformCode = exports.formatCode = void 0;
var tslib_1 = require("tslib");
var standalone_1 = tslib_1.__importDefault(require("prettier/standalone"));
var parser_1 = require("@babel/parser");
var standalone_2 = require("@babel/standalone");
var types_1 = require("@babel/types");
var traverse_1 = tslib_1.__importDefault(require("@babel/traverse"));
var babelParse = function (source) {
return (0, parser_1.parse)(source, {
plugins: [
['decorators', { decoratorsBeforeExport: true }],
'jsx',
'typescript',
'classProperties',
'objectRestSpread',
'dynamicImport',
require('@babel/plugin-proposal-class-properties'),
require('@babel/plugin-transform-async-to-generator'),
],
sourceType: 'module',
});
};
var locStart = function (node, opts) {
var ignoreDecorators = (opts || {}).ignoreDecorators;
if (!ignoreDecorators) {
var decorators = (node.declaration && node.declaration.decorators) || node.decorators;
if (decorators && decorators.length > 0) {
return locStart(decorators[0]);
}
}
return node.range ? node.range[0] : node.start;
};
var locEnd = function (node) {
var end = node.range ? node.range[1] : node.end;
return node.typeAnnotation ? Math.max(end, locEnd(node.typeAnnotation)) : end;
};
var formatCode = function (code) {
try {
var filteredCode = code
.split('\n')
.filter(function (line) {
return !/\/(\*|\/)+.*((t|e)slint[-|:](dis|en)able|prettier-ignore)/.test(line);
})
.join('\n');
return standalone_1.default.format(filteredCode, {
parser: 'babel',
plugins: [
{
parsers: {
babel: {
astFormat: 'estree',
parse: babelParse,
locStart: locStart,
locEnd: locEnd,
},
},
},
],
singleQuote: true,
trailingComma: 'all',
});
}
catch (e) {
console.error('Unable to format code', e);
return code;
}
};
exports.formatCode = formatCode;
var transformCode = function (code) {
var ast = babelParse(code);
(0, traverse_1.default)(ast, {
ImportDeclaration: function (path) {
path.remove();
},
ExportDefaultDeclaration: function (path) {
path.remove();
},
ExportNamedDeclaration: function (path) {
path.remove();
},
CallExpression: function (path) {
if ((0, types_1.isIdentifier)(path.node.callee, { name: 'require' })) {
var parent_1 = path.getStatementParent();
if (parent_1.type === 'VariableDeclaration') {
path.getStatementParent().remove();
}
}
},
});
var transformed = (0, standalone_2.transformFromAst)(ast, code, {
plugins: [
require('@babel/plugin-syntax-jsx'),
[require('@babel/plugin-proposal-class-properties'), { loose: true }],
],
});
return transformed.code;
};
exports.transformCode = transformCode;
//# sourceMappingURL=doctor-code.js.map