1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/@vue/babel-sugar-v-model/dist/plugin.js

2 lines
8.7 KiB
JavaScript
Raw Normal View History

2024-01-16 21:26:16 +08:00
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}function index(e){const i=e.types;return{inherits:syntaxJsx,visitor:{Program(e){e.traverse({JSXAttribute(e){const n=parseVModel(i,e);if(n){const{modifiers:modifiers,valuePath:valuePath}=n,t=e.parentPath;transformModel(i,t,valuePath,modifiers),e.remove()}}})}}}}var camelCase=_interopDefault(require("camelcase")),syntaxJsx=_interopDefault(require("@babel/plugin-syntax-jsx")),htmlTags=_interopDefault(require("html-tags")),svgTags=_interopDefault(require("svg-tags"));const RANGE_TOKEN="__r",cachedCamelCase=(()=>{const e=Object.create(null);return i=>{e[i]||(e[i]=camelCase(i));return e[i]}})(),equalCamel=(e,i)=>e===i||e===cachedCamelCase(i),startsWithCamel=(e,i)=>e.startsWith(i)||e.startsWith(cachedCamelCase(i)),parseVModel=(e,i)=>{if(e.isJSXNamespacedName(i.get("name"))||!startsWithCamel(i.get("name.name").node,"v-model"))return null;if(!e.isJSXExpressionContainer(i.get("value")))throw new Error("You have to use JSX Expression inside your v-model");const n=i.get("name.name").node.split("_");n.shift();return{modifiers:new Set(n),valuePath:i.get("value.expression")}},transformModel=(e,i,n,t)=>{if(isComponent(e,i))return genComponentModel(e,i,n,t);const r=getTagName(e,i);const s=getType(e,i);if("select"===r)genSelect(e,i,n,t);else if("input"===r&&"checkbox"===s)genCheckboxModel(e,i,n,t);else if("input"===r&&"radio"===s)genRadioModel(e,i,n,t);else{if("input"!==r&&"textarea"!==r)throw new Error(`vModel: ${r}[type=${s}] is not supported`);genDefaultModel(e,i,n,t,s)}addModel(e,i,n,t)},isComponent=(e,i)=>{const n=i.get("name");if(e.isJSXMemberExpression(n))return!0;const t=n.get("name").node;return!htmlTags.includes(t)&&!svgTags.includes(t)},getTagName=(e,i)=>i.get("name.name").node,getType=(e,i)=>{const n=i.get("attributes").find(i=>e.isJSXAttribute(i)&&e.isJSXIdentifier(i.get("name"))&&"type"===i.get("name.name").node&&e.isStringLiteral(i.get("value")));return n?n.get("value.value").node:""},addHandler=(e,i,n,t)=>{addProp(e,i,`on-${n}`,e.arrowFunctionExpression([e.identifier("$event")],e.blockStatement(t)),!0)},addProp=(e,i,n,t,r=!1)=>{i.node.attributes[r?"unshift":"push"](e.jSXAttribute(e.jSXIdentifier(n),e.jSXExpressionContainer(t)))},genAssignmentCode=(e,i,n)=>{let t;return e.isMemberExpression(i)&&!e.isThisExpression(t=i.get("object").node)?e.callExpression(e.memberExpression(e.thisExpression(),e.identifier("$set")),[t,i.node.computed?i.get("property").node:e.stringLiteral(i.get("property.name").node),n]):e.assignmentExpression("=",i.node,n)},getBindingAttr=(e,i,n)=>{const t=i.get("attributes").find(i=>e.isJSXAttribute(i)&&e.isJSXIdentifier(i.get("name"))&&equalCamel(i.get("name.name").node,n));if(t){const i=e.isJSXExpressionContainer(t.get("value"))?t.get("value.expression").node:t.get("value").node;return t.remove(),i}return null},genComponentModel=(e,i,n,t)=>{const r=e.identifier("$$v");let s=r;t.has("trim")&&(s=e.conditionalExpression(e.binaryExpression("===",e.unaryExpression("typeof",r),e.stringLiteral("string")),e.callExpression(e.memberExpression(r,e.identifier("trim")),[]),r));t.has("number")&&(s=e.callExpression(e.memberExpression(e.thisExpression(),e.identifier("_n")),[s]));const o=genAssignmentCode(e,n,s);i.node.attributes.push(e.jSXAttribute(e.jSXIdentifier("model"),e.jSXExpressionContainer(e.objectExpression([e.objectProperty(e.identifier("value"),n.node),e.objectProperty(e.identifier("callback"),e.arrowFunctionExpression([e.identifier("$$v")],e.blockStatement([e.expressionStatement(o)])))]))))},genSelect=(e,i,n,t)=>{const r=t.has("number");const s=e.conditionalExpression(e.binaryExpression("in",e.stringLiteral("_value"),e.identifier("o")),e.memberExpression(e.identifier("o"),e.identifier("_value")),e.memberExpression(e.identifier("o"),e.identifier("value")));const o=e.callExpression(e.memberExpression(e.callExpression(e.memberExpression(e.memberExpression(e.memberExpression(e.identifier("Array"),e.identifier("prototype")),e.identifier("filter")),e.identifier("call")),[e.memberExpression(e.memberExpression(e.identifi