parseDirectives.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. Object.defineProperty(exports, "__esModule", { value: true });
  22. const t = __importStar(require("@babel/types"));
  23. const utils_1 = require("./utils");
  24. /**
  25. * Get JSX element type
  26. *
  27. * @param path Path<JSXOpeningElement>
  28. */
  29. const getType = (path) => {
  30. const typePath = path
  31. .get('attributes')
  32. .find((attribute) => {
  33. if (!t.isJSXAttribute(attribute)) {
  34. return false;
  35. }
  36. return t.isJSXIdentifier(attribute.get('name'))
  37. && attribute.get('name').node.name === 'type';
  38. });
  39. return typePath ? typePath.get('value').node : null;
  40. };
  41. const parseModifiers = (value) => (t.isArrayExpression(value)
  42. ? value.elements
  43. .map((el) => (t.isStringLiteral(el) ? el.value : ''))
  44. .filter(Boolean)
  45. : []);
  46. const parseDirectives = (params) => {
  47. var _a, _b;
  48. const { path, value, state, tag, isComponent, } = params;
  49. const args = [];
  50. const vals = [];
  51. const modifiersSet = [];
  52. let directiveName;
  53. let directiveArgument;
  54. let directiveModifiers;
  55. if ('namespace' in path.node.name) {
  56. [directiveName, directiveArgument] = params.name.split(':');
  57. directiveName = path.node.name.namespace.name;
  58. directiveArgument = path.node.name.name.name;
  59. directiveModifiers = directiveArgument.split('_').slice(1);
  60. }
  61. else {
  62. const underscoreModifiers = params.name.split('_');
  63. directiveName = underscoreModifiers.shift() || '';
  64. directiveModifiers = underscoreModifiers;
  65. }
  66. directiveName = directiveName
  67. .replace(/^v/, '')
  68. .replace(/^-/, '')
  69. .replace(/^\S/, (s) => s.toLowerCase());
  70. if (directiveArgument) {
  71. args.push(t.stringLiteral(directiveArgument));
  72. }
  73. const isVModels = directiveName === 'models';
  74. const isVModel = directiveName === 'model';
  75. if (isVModel && !t.isJSXExpressionContainer(path.get('value'))) {
  76. throw new Error('You have to use JSX Expression inside your v-model');
  77. }
  78. if (isVModels && !isComponent) {
  79. throw new Error('v-models can only use in custom components');
  80. }
  81. const shouldResolve = !['html', 'text', 'model', 'models'].includes(directiveName)
  82. || (isVModel && !isComponent);
  83. let modifiers = directiveModifiers;
  84. if (t.isArrayExpression(value)) {
  85. const elementsList = isVModels ? value.elements : [value];
  86. elementsList.forEach((element) => {
  87. if (isVModels && !t.isArrayExpression(element)) {
  88. throw new Error('You should pass a Two-dimensional Arrays to v-models');
  89. }
  90. const { elements } = element;
  91. const [first, second, third] = elements;
  92. if (second && !t.isArrayExpression(second) && !t.isSpreadElement(second)) {
  93. args.push(second);
  94. modifiers = parseModifiers(third);
  95. }
  96. else if (t.isArrayExpression(second)) {
  97. if (!shouldResolve) {
  98. args.push(t.nullLiteral());
  99. }
  100. modifiers = parseModifiers(second);
  101. }
  102. else if (!shouldResolve) {
  103. // work as v-model={[value]} or v-models={[[value]]}
  104. args.push(t.nullLiteral());
  105. }
  106. modifiersSet.push(new Set(modifiers));
  107. vals.push(first);
  108. });
  109. }
  110. else if (isVModel && !shouldResolve) {
  111. // work as v-model={value}
  112. args.push(t.nullLiteral());
  113. modifiersSet.push(new Set(directiveModifiers));
  114. }
  115. else {
  116. modifiersSet.push(new Set(directiveModifiers));
  117. }
  118. return {
  119. directiveName,
  120. modifiers: modifiersSet,
  121. values: vals.length ? vals : [value],
  122. args,
  123. directive: shouldResolve ? [
  124. resolveDirective(path, state, tag, directiveName),
  125. vals[0] || value,
  126. ((_a = modifiersSet[0]) === null || _a === void 0 ? void 0 : _a.size)
  127. ? args[0] || t.unaryExpression('void', t.numericLiteral(0), true)
  128. : args[0],
  129. !!((_b = modifiersSet[0]) === null || _b === void 0 ? void 0 : _b.size) && t.objectExpression([...modifiersSet[0]].map((modifier) => t.objectProperty(t.identifier(modifier), t.booleanLiteral(true)))),
  130. ].filter(Boolean) : undefined,
  131. };
  132. };
  133. const resolveDirective = (path, state, tag, directiveName) => {
  134. var _a;
  135. if (directiveName === 'show') {
  136. return (0, utils_1.createIdentifier)(state, 'vShow');
  137. }
  138. if (directiveName === 'model') {
  139. let modelToUse;
  140. const type = getType(path.parentPath);
  141. switch (tag.value) {
  142. case 'select':
  143. modelToUse = (0, utils_1.createIdentifier)(state, 'vModelSelect');
  144. break;
  145. case 'textarea':
  146. modelToUse = (0, utils_1.createIdentifier)(state, 'vModelText');
  147. break;
  148. default:
  149. if (t.isStringLiteral(type) || !type) {
  150. switch ((_a = type) === null || _a === void 0 ? void 0 : _a.value) {
  151. case 'checkbox':
  152. modelToUse = (0, utils_1.createIdentifier)(state, 'vModelCheckbox');
  153. break;
  154. case 'radio':
  155. modelToUse = (0, utils_1.createIdentifier)(state, 'vModelRadio');
  156. break;
  157. default:
  158. modelToUse = (0, utils_1.createIdentifier)(state, 'vModelText');
  159. }
  160. }
  161. else {
  162. modelToUse = (0, utils_1.createIdentifier)(state, 'vModelDynamic');
  163. }
  164. }
  165. return modelToUse;
  166. }
  167. return t.callExpression((0, utils_1.createIdentifier)(state, 'resolveDirective'), [
  168. t.stringLiteral(directiveName),
  169. ]);
  170. };
  171. exports.default = parseDirectives;
  172. //# sourceMappingURL=parseDirectives.js.map