utils.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. exports.JSX_HELPER_KEY = exports.buildIIFE = exports.walksScope = exports.FRAGMENT = exports.shouldTransformedToSlots = exports.transformJSXExpressionContainer = exports.transformJSXSpreadChild = exports.transformJSXText = exports.getJSXAttributeName = exports.getTag = exports.transformJSXMemberExpression = exports.checkIsComponent = exports.isDirective = exports.createIdentifier = void 0;
  26. const t = __importStar(require("@babel/types"));
  27. const html_tags_1 = __importDefault(require("html-tags"));
  28. const svg_tags_1 = __importDefault(require("svg-tags"));
  29. const JSX_HELPER_KEY = 'JSX_HELPER_KEY';
  30. exports.JSX_HELPER_KEY = JSX_HELPER_KEY;
  31. const FRAGMENT = 'Fragment';
  32. exports.FRAGMENT = FRAGMENT;
  33. const KEEP_ALIVE = 'KeepAlive';
  34. /**
  35. * create Identifier
  36. * @param path NodePath
  37. * @param state
  38. * @param name string
  39. * @returns MemberExpression
  40. */
  41. const createIdentifier = (state, name) => state.get(name)();
  42. exports.createIdentifier = createIdentifier;
  43. /**
  44. * Checks if string is describing a directive
  45. * @param src string
  46. */
  47. const isDirective = (src) => src.startsWith('v-')
  48. || (src.startsWith('v') && src.length >= 2 && src[1] >= 'A' && src[1] <= 'Z');
  49. exports.isDirective = isDirective;
  50. /**
  51. * Should transformed to slots
  52. * @param tag string
  53. * @returns boolean
  54. */
  55. const shouldTransformedToSlots = (tag) => !(tag.endsWith(FRAGMENT) || tag === KEEP_ALIVE);
  56. exports.shouldTransformedToSlots = shouldTransformedToSlots;
  57. /**
  58. * Check if a Node is a component
  59. *
  60. * @param t
  61. * @param path JSXOpeningElement
  62. * @returns boolean
  63. */
  64. const checkIsComponent = (path) => {
  65. const namePath = path.get('name');
  66. if (namePath.isJSXMemberExpression()) {
  67. return shouldTransformedToSlots(namePath.node.property.name); // For withCtx
  68. }
  69. const tag = namePath.node.name;
  70. return shouldTransformedToSlots(tag) && !html_tags_1.default.includes(tag) && !svg_tags_1.default.includes(tag);
  71. };
  72. exports.checkIsComponent = checkIsComponent;
  73. /**
  74. * Transform JSXMemberExpression to MemberExpression
  75. * @param path JSXMemberExpression
  76. * @returns MemberExpression
  77. */
  78. const transformJSXMemberExpression = (path) => {
  79. const objectPath = path.node.object;
  80. const propertyPath = path.node.property;
  81. const transformedObject = t.isJSXMemberExpression(objectPath)
  82. ? transformJSXMemberExpression(path.get('object'))
  83. : t.isJSXIdentifier(objectPath)
  84. ? t.identifier(objectPath.name)
  85. : t.nullLiteral();
  86. const transformedProperty = t.identifier(propertyPath.name);
  87. return t.memberExpression(transformedObject, transformedProperty);
  88. };
  89. exports.transformJSXMemberExpression = transformJSXMemberExpression;
  90. /**
  91. * Get tag (first attribute for h) from JSXOpeningElement
  92. * @param path JSXElement
  93. * @param state State
  94. * @returns Identifier | StringLiteral | MemberExpression | CallExpression
  95. */
  96. const getTag = (path, state) => {
  97. var _a, _b;
  98. const namePath = path.get('openingElement').get('name');
  99. if (namePath.isJSXIdentifier()) {
  100. const { name } = namePath.node;
  101. if (!html_tags_1.default.includes(name) && !svg_tags_1.default.includes(name)) {
  102. return (name === FRAGMENT
  103. ? createIdentifier(state, FRAGMENT)
  104. : path.scope.hasBinding(name)
  105. ? t.identifier(name)
  106. : ((_b = (_a = state.opts).isCustomElement) === null || _b === void 0 ? void 0 : _b.call(_a, name))
  107. ? t.stringLiteral(name)
  108. : t.callExpression(createIdentifier(state, 'resolveComponent'), [t.stringLiteral(name)]));
  109. }
  110. return t.stringLiteral(name);
  111. }
  112. if (namePath.isJSXMemberExpression()) {
  113. return transformJSXMemberExpression(namePath);
  114. }
  115. throw new Error(`getTag: ${namePath.type} is not supported`);
  116. };
  117. exports.getTag = getTag;
  118. const getJSXAttributeName = (path) => {
  119. const nameNode = path.node.name;
  120. if (t.isJSXIdentifier(nameNode)) {
  121. return nameNode.name;
  122. }
  123. return `${nameNode.namespace.name}:${nameNode.name.name}`;
  124. };
  125. exports.getJSXAttributeName = getJSXAttributeName;
  126. /**
  127. * Transform JSXText to StringLiteral
  128. * @param path JSXText
  129. * @returns StringLiteral | null
  130. */
  131. const transformJSXText = (path) => {
  132. const { node } = path;
  133. const lines = node.value.split(/\r\n|\n|\r/);
  134. let lastNonEmptyLine = 0;
  135. for (let i = 0; i < lines.length; i++) {
  136. if (lines[i].match(/[^ \t]/)) {
  137. lastNonEmptyLine = i;
  138. }
  139. }
  140. let str = '';
  141. for (let i = 0; i < lines.length; i++) {
  142. const line = lines[i];
  143. const isFirstLine = i === 0;
  144. const isLastLine = i === lines.length - 1;
  145. const isLastNonEmptyLine = i === lastNonEmptyLine;
  146. // replace rendered whitespace tabs with spaces
  147. let trimmedLine = line.replace(/\t/g, ' ');
  148. // trim whitespace touching a newline
  149. if (!isFirstLine) {
  150. trimmedLine = trimmedLine.replace(/^[ ]+/, '');
  151. }
  152. // trim whitespace touching an endline
  153. if (!isLastLine) {
  154. trimmedLine = trimmedLine.replace(/[ ]+$/, '');
  155. }
  156. if (trimmedLine) {
  157. if (!isLastNonEmptyLine) {
  158. trimmedLine += ' ';
  159. }
  160. str += trimmedLine;
  161. }
  162. }
  163. return str !== '' ? t.stringLiteral(str) : null;
  164. };
  165. exports.transformJSXText = transformJSXText;
  166. /**
  167. * Transform JSXExpressionContainer to Expression
  168. * @param path JSXExpressionContainer
  169. * @returns Expression
  170. */
  171. const transformJSXExpressionContainer = (path) => path.get('expression').node;
  172. exports.transformJSXExpressionContainer = transformJSXExpressionContainer;
  173. /**
  174. * Transform JSXSpreadChild
  175. * @param path JSXSpreadChild
  176. * @returns SpreadElement
  177. */
  178. const transformJSXSpreadChild = (path) => t.spreadElement(path.get('expression').node);
  179. exports.transformJSXSpreadChild = transformJSXSpreadChild;
  180. const walksScope = (path, name, slotFlag) => {
  181. if (path.scope.hasBinding(name) && path.parentPath) {
  182. if (t.isJSXElement(path.parentPath.node)) {
  183. path.parentPath.setData('slotFlag', slotFlag);
  184. }
  185. walksScope(path.parentPath, name, slotFlag);
  186. }
  187. };
  188. exports.walksScope = walksScope;
  189. const buildIIFE = (path, children) => {
  190. const { parentPath } = path;
  191. if (t.isAssignmentExpression(parentPath)) {
  192. const { left } = parentPath.node;
  193. if (t.isIdentifier(left)) {
  194. return children.map((child) => {
  195. if (t.isIdentifier(child) && child.name === left.name) {
  196. const insertName = path.scope.generateUidIdentifier(child.name);
  197. parentPath.insertBefore(t.variableDeclaration('const', [
  198. t.variableDeclarator(insertName, t.callExpression(t.functionExpression(null, [], t.blockStatement([t.returnStatement(child)])), [])),
  199. ]));
  200. return insertName;
  201. }
  202. return child;
  203. });
  204. }
  205. }
  206. return children;
  207. };
  208. exports.buildIIFE = buildIIFE;