UNPKG

babel-plugin-transform-reblend-function-to-class

Version:

164 lines 7.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.isTypescriptNode = exports.set = exports.get = exports.isHookName = exports.isComponentName = exports.TRANSFORMED_COMMENT = exports.REBLEND_IMPORT_NAME_ID = void 0; exports.hasReblendComment = hasReblendComment; exports.getProps = getProps; exports.findReblendImportName = findReblendImportName; const t = __importStar(require("@babel/types")); exports.REBLEND_IMPORT_NAME_ID = 'id/REBLEND_IMPORT_NAME_ID'; exports.TRANSFORMED_COMMENT = ' @Reblend: Transformed from function to class '; function hasReblendComment(commentSuffix, path) { var _a, _b, _c, _d; if (!commentSuffix || !path) { return false; } commentSuffix = commentSuffix.toLowerCase(); const hasIt = (node) => { var _a; if (node === null || node === void 0 ? void 0 : node.leadingComments) { return (_a = node === null || node === void 0 ? void 0 : node.leadingComments) === null || _a === void 0 ? void 0 : _a.some(comment => comment.value .trim() .toLowerCase() .startsWith(`@reblend${commentSuffix}`)); } return false; }; const { node } = path; return (hasIt(node) || hasIt((_b = (_a = path === null || path === void 0 ? void 0 : path.parentPath) === null || _a === void 0 ? void 0 : _a.parentPath) === null || _b === void 0 ? void 0 : _b.node) || hasIt(path === null || path === void 0 ? void 0 : path.container) || hasIt((_d = (_c = path === null || path === void 0 ? void 0 : path.parentPath) === null || _c === void 0 ? void 0 : _c.parentPath) === null || _d === void 0 ? void 0 : _d.container)); } function getProps(node) { return node.params || []; /* .length < 1 ? [] : t.isObjectPattern(node.params[0]) ? (node.params[0] as t.ObjectPattern).properties : [node.params[0]]; */ } // Helper to find the local name for reblendjs import/require/__importStar function findReblendImportName(path) { const program = path.isProgram() ? path : path.findParent(p => p.isProgram()); if (!program) return t.identifier('Reblend'); for (const node of program.node.body) { if (t.isImportDeclaration(node) && node.source.value === 'reblendjs' && node.specifiers.length > 0) { const defaultImport = node.specifiers.find(s => t.isImportDefaultSpecifier(s)); if (defaultImport && t.isIdentifier(defaultImport.local)) { return defaultImport.local; } const namedImport = node.specifiers.find(s => t.isImportSpecifier(s) && t.isIdentifier(s.imported, { name: 'Reblend' })); if (namedImport && t.isImportSpecifier(namedImport)) { return namedImport.local; } // Handle import * as Reblend from 'reblendjs' const namespaceImport = node.specifiers.find(s => t.isImportNamespaceSpecifier(s)); if (namespaceImport && t.isImportNamespaceSpecifier(namespaceImport)) { // Return Reblend.Reblend return t.memberExpression(namespaceImport.local, t.identifier('Reblend')); } } // CJS: const Reblend = require('reblendjs') if (t.isVariableDeclaration(node)) { for (const decl of node.declarations) { if (t.isVariableDeclarator(decl) && t.isCallExpression(decl.init) && t.isIdentifier(decl.init.callee, { name: 'require' }) && decl.init.arguments.length === 1 && t.isStringLiteral(decl.init.arguments[0], { value: 'reblendjs' }) && t.isIdentifier(decl.id)) { // return MemberExpression: Reblend.construct return t.memberExpression(decl.id, t.identifier('Reblend')); } // const reblendjs_1 = __importStar(require("reblendjs")) if (t.isVariableDeclarator(decl) && t.isCallExpression(decl.init) && t.isIdentifier(decl.init.callee, { name: '__importStar' }) && decl.init.arguments.length === 1 && t.isCallExpression(decl.init.arguments[0]) && t.isIdentifier(decl.init.arguments[0].callee, { name: 'require' }) && decl.init.arguments[0].arguments.length === 1 && t.isStringLiteral(decl.init.arguments[0].arguments[0], { value: 'reblendjs', }) && t.isIdentifier(decl.id)) { // return MemberExpression: reblendjs_1.Reblend return t.memberExpression(decl.id, t.identifier('Reblend')); } } } } // fallback: Identifier('Reblend') return t.identifier('Reblend'); } const isComponentName = (functionName) => !!functionName && /^[A-Z][A-Za-z0-9_]*$/.test(functionName); exports.isComponentName = isComponentName; const isHookName = (functionName) => { var _a; return functionName && functionName.startsWith('use') && functionName[3] && functionName[3] === ((_a = functionName[3]) === null || _a === void 0 ? void 0 : _a.toUpperCase()); }; exports.isHookName = isHookName; // Plugin state helpers const get = (pass, name) => pass.get(`babel-plugin-transform-reblend-function-to-class/${name}`); exports.get = get; const set = (pass, name, v) => pass.set(`babel-plugin-transform-reblend-function-to-class/${name}`, v); exports.set = set; const isTypescriptNode = (node) => t.isTSType(node) || t.isTSTypeAnnotation(node) || t.isTSTypeParameterDeclaration(node) || t.isTSTypeParameterInstantiation(node) || t.isTSParameterProperty(node) || t.isTSDeclareFunction(node) || t.isTSInterfaceDeclaration(node) || t.isTSTypeAliasDeclaration(node) || t.isTSModuleDeclaration(node) || t.isTSTypeReference(node) || t.isTSTypeQuery(node) || t.isTSImportType(node) || t.isTSEnumDeclaration(node) || t.isTSAsExpression(node) || //t.isTSNonNullExpression(node) || t.isTSDeclareMethod(node); exports.isTypescriptNode = isTypescriptNode; //# sourceMappingURL=utils.js.map