UNPKG

@builder.io/mitosis

Version:

Write components once, run everywhere. Compiles to Vue, React, Solid, and Liquid. Import code from Figma and Builder.io

60 lines (59 loc) 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseSetContext = exports.parseHasContext = exports.parseGetContext = void 0; const astring_1 = require("astring"); const string_1 = require("../helpers/string"); function parseGetContext(json, node) { var _a; if (node.declarations.length > 0) { const declaration = node.declarations[0]; const { name } = declaration.id; const arguments_ = (_a = declaration.init) === null || _a === void 0 ? void 0 : _a.arguments; if (arguments === null || arguments === void 0 ? void 0 : arguments.length) { const argument = arguments_[0]; json.context.get[name] = { name: (0, astring_1.generate)(argument), path: '', }; } } } exports.parseGetContext = parseGetContext; function parseHasContext(json, node) { var _a; if (node.declarations.length > 0) { const declaration = node.declarations[0]; const { name } = declaration.id; const arguments_ = (_a = declaration.init) === null || _a === void 0 ? void 0 : _a.arguments; if (arguments === null || arguments === void 0 ? void 0 : arguments.length) { const argument = arguments_[0]; const generatedArgument = (0, astring_1.generate)(argument); json.context.get[(0, string_1.stripQuotes)(generatedArgument)] = { name: generatedArgument, path: '', }; json.state[name] = { code: `get ${name}() { return ${(0, string_1.stripQuotes)(generatedArgument)} !== undefined}`, type: 'getter', }; } } } exports.parseHasContext = parseHasContext; function parseSetContext(json, node) { var _a; if (node.type === 'ExpressionStatement' && node.expression.type === 'CallExpression' && ((_a = node.expression.arguments) === null || _a === void 0 ? void 0 : _a.length)) { const hook = node.expression.callee.name; if (hook === 'setContext') { const key = node.expression.arguments[0]; const value = node.expression.arguments[1]; json.context.set[key.value] = { name: (0, astring_1.generate)(key), ref: (0, astring_1.generate)(value), }; } } } exports.parseSetContext = parseSetContext;