babel-helper-decorate-react
Version:
Babel Helper for custom decorator for React Component
54 lines (53 loc) • 2.43 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const types = require("@babel/types");
const createDecorateVisitor_1 = require("./createDecorateVisitor");
function createDecorateReactTopJSXVisitor(_a) {
var { detectClassComponent = true, detectFunctionComponent = true, condition } = _a, options = __rest(_a, ["detectClassComponent", "detectFunctionComponent", "condition"]);
const vTypes = ['JSXElement'].map((name) => ({
type: name,
condition: (path, a, b, c) => {
if (condition) {
if (false === condition(path, a, b, c)) {
return false;
}
}
if (!path.parent) {
return false;
}
if (detectFunctionComponent && types.isArrowFunctionExpression(path.parent)) {
return true;
}
if (detectFunctionComponent &&
types.isReturnStatement(path.parent) &&
path.findParent((nodePath) => ['FunctionExpression', 'FunctionDeclaration', 'ArrowFunctionExpression'].includes(nodePath.node.type))) {
return true;
}
if (detectClassComponent &&
types.isReturnStatement(path.parent) &&
path.findParent((nodePath) => {
if (['ClassMethod'].includes(nodePath.node.type) && types.isIdentifier(nodePath.get('key'))) {
const key = nodePath.get('key');
return types.isIdentifier(key.node) && String(key) === 'render';
}
return false;
})) {
return true;
}
return false;
}
}));
return createDecorateVisitor_1.default(Object.assign({ deepVisitorTypes: vTypes, visitorTypes: vTypes, detectScopeDepth: 1 }, options));
}
exports.default = createDecorateReactTopJSXVisitor;