|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/core/utils.ts
- var _common = require('@vue-macros/common');
- function filterMacro(stmts) {
- return stmts.map((raw) => {
- let node = raw;
- if (raw.type === "ExpressionStatement")
- node = raw.expression;
- return _common.isCallOf.call(void 0, node, _common.DEFINE_OPTIONS) ? node : void 0;
- }).filter((node) => !!node);
- }
- function hasPropsOrEmits(node) {
- return node.properties.some(
- (prop) => (prop.type === "ObjectProperty" || prop.type === "ObjectMethod") && prop.key.type === "Identifier" && (prop.key.name === "props" || prop.key.name === "emits")
- );
- }
-
- // src/core/transform.ts
-
-
-
-
-
-
-
-
-
-
- var _astwalkerscope = require('ast-walker-scope');
- function transformDefineOptions(code, id) {
- if (!code.includes(_common.DEFINE_OPTIONS))
- return;
- const sfc = _common.parseSFC.call(void 0, code, id);
- if (!sfc.scriptSetup)
- return;
- const { scriptSetup, getSetupAst, getScriptAst } = sfc;
- const setupOffset = scriptSetup.loc.start.offset;
- const setupAst = getSetupAst();
- const nodes = filterMacro(setupAst.body);
- if (nodes.length === 0) {
- return;
- } else if (nodes.length > 1)
- throw new SyntaxError(`duplicate ${_common.DEFINE_OPTIONS}() call`);
- const scriptAst = getScriptAst();
- if (scriptAst)
- checkDefaultExport(scriptAst.body);
- const setupBindings = getIdentifiers(setupAst.body);
- const s = new (0, _common.MagicString)(code);
- const [node] = nodes;
- const [arg] = node.arguments;
- if (arg) {
- const normalScript = _common.addNormalScript.call(void 0, sfc, s);
- const scriptOffset = normalScript.start();
- _common.importHelperFn.call(void 0, s, scriptOffset, "defineComponent", "vue");
- s.appendLeft(
- scriptOffset,
- `
- export default /*#__PURE__*/ ${_common.HELPER_PREFIX}defineComponent(`
- );
- if (arg.type === "ObjectExpression" && hasPropsOrEmits(arg))
- throw new SyntaxError(
- `${_common.DEFINE_OPTIONS}() please use defineProps or defineEmits instead.`
- );
- _common.checkInvalidScopeReference.call(void 0, arg, _common.DEFINE_OPTIONS, setupBindings);
- s.moveNode(arg, scriptOffset, { offset: setupOffset });
- s.remove(setupOffset + node.start, setupOffset + arg.start);
- s.remove(setupOffset + arg.end, setupOffset + node.end);
- s.appendRight(scriptOffset, ");");
- normalScript.end();
- } else {
- s.removeNode(node, { offset: setupOffset });
- }
- return _common.getTransformResult.call(void 0, s, id);
- }
- var checkDefaultExport = (stmts) => {
- const hasDefaultExport = stmts.some(
- (node) => node.type === "ExportDefaultDeclaration"
- );
- if (hasDefaultExport)
- throw new SyntaxError(
- `${_common.DEFINE_OPTIONS} cannot be used with default export within <script>.`
- );
- };
- var getIdentifiers = (stmts) => {
- let ids = [];
- const program = {
- type: "Program",
- body: stmts,
- directives: [],
- sourceType: "module",
- sourceFile: ""
- };
- _astwalkerscope.walkAST.call(void 0, program, {
- enter(node) {
- if (node.type === "BlockStatement") {
- this.skip();
- }
- },
- leave(node) {
- if (node.type !== "Program")
- return;
- ids = Object.keys(this.scope);
- }
- });
- return ids;
- };
-
-
-
-
-
-
-
- exports.filterMacro = filterMacro; exports.hasPropsOrEmits = hasPropsOrEmits; exports.transformDefineOptions = transformDefineOptions; exports.checkDefaultExport = checkDefaultExport; exports.getIdentifiers = getIdentifiers;
|