|
- import {
- isTs
- } from "./chunk-X26ALPRY.mjs";
- import {
- REGEX_LANG_JSX
- } from "./chunk-TDSRBKSQ.mjs";
-
- // src/ast.ts
- import { babelParse as _babelParse, walkIdentifiers } from "@vue/compiler-sfc";
-
- // ../../node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/walker.js
- var WalkerBase = class {
- constructor() {
- this.should_skip = false;
- this.should_remove = false;
- this.replacement = null;
- this.context = {
- skip: () => this.should_skip = true,
- remove: () => this.should_remove = true,
- replace: (node) => this.replacement = node
- };
- }
- /**
- * @template {Node} Parent
- * @param {Parent | null | undefined} parent
- * @param {keyof Parent | null | undefined} prop
- * @param {number | null | undefined} index
- * @param {Node} node
- */
- replace(parent, prop, index, node) {
- if (parent && prop) {
- if (index != null) {
- parent[prop][index] = node;
- } else {
- parent[prop] = node;
- }
- }
- }
- /**
- * @template {Node} Parent
- * @param {Parent | null | undefined} parent
- * @param {keyof Parent | null | undefined} prop
- * @param {number | null | undefined} index
- */
- remove(parent, prop, index) {
- if (parent && prop) {
- if (index !== null && index !== void 0) {
- parent[prop].splice(index, 1);
- } else {
- delete parent[prop];
- }
- }
- }
- };
-
- // ../../node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/sync.js
- var SyncWalker = class extends WalkerBase {
- /**
- *
- * @param {SyncHandler} [enter]
- * @param {SyncHandler} [leave]
- */
- constructor(enter, leave) {
- super();
- this.should_skip = false;
- this.should_remove = false;
- this.replacement = null;
- this.context = {
- skip: () => this.should_skip = true,
- remove: () => this.should_remove = true,
- replace: (node) => this.replacement = node
- };
- this.enter = enter;
- this.leave = leave;
- }
- /**
- * @template {Node} Parent
- * @param {Node} node
- * @param {Parent | null} parent
- * @param {keyof Parent} [prop]
- * @param {number | null} [index]
- * @returns {Node | null}
- */
- visit(node, parent, prop, index) {
- if (node) {
- if (this.enter) {
- const _should_skip = this.should_skip;
- const _should_remove = this.should_remove;
- const _replacement = this.replacement;
- this.should_skip = false;
- this.should_remove = false;
- this.replacement = null;
- this.enter.call(this.context, node, parent, prop, index);
- if (this.replacement) {
- node = this.replacement;
- this.replace(parent, prop, index, node);
- }
- if (this.should_remove) {
- this.remove(parent, prop, index);
- }
- const skipped = this.should_skip;
- const removed = this.should_remove;
- this.should_skip = _should_skip;
- this.should_remove = _should_remove;
- this.replacement = _replacement;
- if (skipped)
- return node;
- if (removed)
- return null;
- }
- let key;
- for (key in node) {
- const value = node[key];
- if (value && typeof value === "object") {
- if (Array.isArray(value)) {
- const nodes = (
- /** @type {Array<unknown>} */
- value
- );
- for (let i = 0; i < nodes.length; i += 1) {
- const item = nodes[i];
- if (isNode(item)) {
- if (!this.visit(item, node, key, i)) {
- i--;
- }
- }
- }
- } else if (isNode(value)) {
- this.visit(value, node, key, null);
- }
- }
- }
- if (this.leave) {
- const _replacement = this.replacement;
- const _should_remove = this.should_remove;
- this.replacement = null;
- this.should_remove = false;
- this.leave.call(this.context, node, parent, prop, index);
- if (this.replacement) {
- node = this.replacement;
- this.replace(parent, prop, index, node);
- }
- if (this.should_remove) {
- this.remove(parent, prop, index);
- }
- const removed = this.should_remove;
- this.replacement = _replacement;
- this.should_remove = _should_remove;
- if (removed)
- return null;
- }
- }
- return node;
- }
- };
- function isNode(value) {
- return value !== null && typeof value === "object" && "type" in value && typeof value.type === "string";
- }
-
- // ../../node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/index.js
- function walk(ast, { enter, leave }) {
- const instance = new SyncWalker(enter, leave);
- return instance.visit(ast, null);
- }
-
- // src/ast.ts
- function babelParse(code, lang, options = {}) {
- const plugins = [...options.plugins || []];
- if (isTs(lang)) {
- plugins.push(["typescript", { dts: lang === "dts" }]);
- if (REGEX_LANG_JSX.test(lang))
- plugins.push("jsx");
- if (!plugins.includes("decorators"))
- plugins.push("decorators-legacy");
- } else {
- plugins.push("jsx");
- }
- const { program } = _babelParse(code, {
- sourceType: "module",
- plugins,
- ...options
- });
- return program;
- }
- function isCallOf(node, test) {
- return !!(node && node.type === "CallExpression" && node.callee.type === "Identifier" && (typeof test === "string" ? node.callee.name === test : Array.isArray(test) ? test.includes(node.callee.name) : test(node.callee.name)));
- }
- function checkInvalidScopeReference(node, method, setupBindings) {
- if (!node)
- return;
- walkIdentifiers(node, (id) => {
- if (setupBindings.includes(id.name))
- throw new SyntaxError(
- `\`${method}()\` in <script setup> cannot reference locally declared variables (${id.name}) because it will be hoisted outside of the setup() function.`
- );
- });
- }
- function isStaticExpression(node, options = {}) {
- var _a;
- const { magicComment, fn, object, objectMethod, array, unary } = options;
- if (magicComment && ((_a = node.leadingComments) == null ? void 0 : _a.some(
- (comment) => comment.value.trim() === magicComment
- )))
- return true;
- else if (fn && isFunctionType(node))
- return true;
- switch (node.type) {
- case "UnaryExpression":
- return !!unary && isStaticExpression(node.argument, options);
- case "LogicalExpression":
- case "BinaryExpression":
- return isStaticExpression(node.left, options) && isStaticExpression(node.right, options);
- case "ConditionalExpression":
- return isStaticExpression(node.test, options) && isStaticExpression(node.consequent, options) && isStaticExpression(node.alternate, options);
- case "SequenceExpression":
- case "TemplateLiteral":
- return node.expressions.every((expr) => isStaticExpression(expr, options));
- case "ArrayExpression":
- return !!array && node.elements.every(
- (element) => element && isStaticExpression(element, options)
- );
- case "ObjectExpression":
- return !!object && node.properties.every((prop) => {
- if (prop.type === "SpreadElement") {
- return prop.argument.type === "ObjectExpression" && isStaticExpression(prop.argument, options);
- } else if (!isLiteralType(prop.key) && prop.computed) {
- return false;
- } else if (prop.type === "ObjectProperty" && !isStaticExpression(prop.value, options)) {
- return false;
- }
- if (prop.type === "ObjectMethod" && !objectMethod) {
- return false;
- }
- return true;
- });
- case "ParenthesizedExpression":
- case "TSNonNullExpression":
- case "TSAsExpression":
- case "TSTypeAssertion":
- return isStaticExpression(node.expression, options);
- }
- if (isLiteralType(node))
- return true;
- return false;
- }
- function isLiteralType(node) {
- return node.type.endsWith("Literal");
- }
- function resolveTemplateLiteral(node) {
- return node.quasis.reduce((prev, curr, idx) => {
- if (node.expressions[idx]) {
- return prev + curr.value.cooked + resolveLiteral(node.expressions[idx]);
- }
- return prev + curr.value.cooked;
- }, "");
- }
- function resolveLiteral(node) {
- switch (node.type) {
- case "TemplateLiteral":
- return resolveTemplateLiteral(node);
- case "NullLiteral":
- return null;
- case "BigIntLiteral":
- return BigInt(node.value);
- case "RegExpLiteral":
- return new RegExp(node.pattern, node.flags);
- case "BooleanLiteral":
- case "NumericLiteral":
- case "StringLiteral":
- return node.value;
- }
- return void 0;
- }
- function isStaticObjectKey(node) {
- return node.properties.every((prop) => {
- if (prop.type === "SpreadElement") {
- return prop.argument.type === "ObjectExpression" && isStaticObjectKey(prop.argument);
- }
- return !prop.computed || isLiteralType(prop.key);
- });
- }
- function resolveObjectExpression(node) {
- const maps = {};
- for (const property of node.properties) {
- if (property.type === "SpreadElement") {
- if (property.argument.type !== "ObjectExpression")
- return void 0;
- Object.assign(maps, resolveObjectExpression(property.argument));
- } else {
- const key = resolveObjectKey(property.key, property.computed, false);
- maps[key] = property;
- }
- }
- return maps;
- }
- function resolveObjectKey(node, computed = false, raw = true) {
- switch (node.type) {
- case "StringLiteral":
- case "NumericLiteral":
- return raw ? node.extra.raw : node.value;
- case "Identifier":
- if (!computed)
- return raw ? `'${node.name}'` : node.name;
- default:
- throw new SyntaxError(`Unexpected node type: ${node.type}`);
- }
- }
- function walkAST(node, options) {
- return walk(node, options);
- }
- function isFunctionType(node) {
- return /Function(?:Expression|Declaration)$|Method$/.test(node.type);
- }
- var TS_NODE_TYPES = [
- "TSAsExpression",
- // foo as number
- "TSTypeAssertion",
- // (<number>foo)
- "TSNonNullExpression",
- // foo!
- "TSInstantiationExpression",
- // foo<string>
- "TSSatisfiesExpression"
- // foo satisfies T
- ];
- function unwrapTSNode(node) {
- if (TS_NODE_TYPES.includes(node.type)) {
- return unwrapTSNode(node.expression);
- } else {
- return node;
- }
- }
-
- export {
- babelParse,
- isCallOf,
- checkInvalidScopeReference,
- isStaticExpression,
- isLiteralType,
- resolveTemplateLiteral,
- resolveLiteral,
- isStaticObjectKey,
- resolveObjectExpression,
- resolveObjectKey,
- walkAST,
- isFunctionType,
- TS_NODE_TYPES,
- unwrapTSNode
- };
|