1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.isCustomValue = exports.getBoxValue = exports.createCustomValue = exports.resolveCustomValues = exports.CustomValueStrategy = exports.stTypes = exports.unbox = exports.box = void 0;
|
7 | const lodash_clonedeepwith_1 = __importDefault(require("lodash.clonedeepwith"));
|
8 | const postcss_value_parser_1 = __importDefault(require("postcss-value-parser"));
|
9 | const stylable_value_parsers_1 = require("./stylable-value-parsers");
|
10 | function box(type, value) {
|
11 | return {
|
12 | type,
|
13 | value,
|
14 | };
|
15 | }
|
16 | exports.box = box;
|
17 | const { hasOwnProperty } = Object.prototype;
|
18 | function unbox(boxed) {
|
19 | if (typeof boxed === 'string') {
|
20 | return boxed;
|
21 | }
|
22 | else if (typeof boxed === 'object' && boxed.type && hasOwnProperty.call(boxed, 'value')) {
|
23 | return lodash_clonedeepwith_1.default(boxed.value, unbox);
|
24 | }
|
25 | }
|
26 | exports.unbox = unbox;
|
27 | exports.stTypes = {
|
28 | stArray: createCustomValue({
|
29 | processArgs: (node, customTypes) => {
|
30 | return exports.CustomValueStrategy.args(node, customTypes);
|
31 | },
|
32 | createValue: (args) => {
|
33 | return args;
|
34 | },
|
35 | getValue: (value, index) => value[parseInt(index, 10)],
|
36 | }).register('stArray'),
|
37 | stMap: createCustomValue({
|
38 | processArgs: (node, customTypes) => {
|
39 | return exports.CustomValueStrategy.named(node, customTypes);
|
40 | },
|
41 | createValue: (args) => {
|
42 | return args;
|
43 | },
|
44 | getValue: (value, index) => value[index],
|
45 | }).register('stMap'),
|
46 | };
|
47 | exports.CustomValueStrategy = {
|
48 | args: (fnNode, customTypes) => {
|
49 | const pathArgs = stylable_value_parsers_1.getFormatterArgs(fnNode);
|
50 | const outputArray = [];
|
51 | for (const arg of pathArgs) {
|
52 | const parsedArg = postcss_value_parser_1.default(arg).nodes[0];
|
53 | const ct = parsedArg.type === 'function' && parsedArg.value;
|
54 | const resolvedValue = typeof ct === 'string' && customTypes[ct]
|
55 | ? customTypes[ct].evalVarAst(parsedArg, customTypes)
|
56 | : arg;
|
57 | outputArray.push(resolvedValue);
|
58 | }
|
59 | return outputArray;
|
60 | },
|
61 | named: (fnNode, customTypes) => {
|
62 | const outputMap = {};
|
63 | const s = stylable_value_parsers_1.getNamedArgs(fnNode);
|
64 | for (const [prop, space, ...valueNodes] of s) {
|
65 | if (space.type !== 'space') {
|
66 |
|
67 | throw new Error('Invalid argument');
|
68 | }
|
69 | let resolvedValue;
|
70 | if (valueNodes.length === 0) {
|
71 |
|
72 | }
|
73 | else if (valueNodes.length === 1) {
|
74 | const valueNode = valueNodes[0];
|
75 | resolvedValue = valueNode.resolvedValue;
|
76 | if (!resolvedValue) {
|
77 | const ct = customTypes[valueNode.value];
|
78 | if (valueNode.type === 'function' && ct) {
|
79 | resolvedValue = ct.evalVarAst(valueNode, customTypes);
|
80 | }
|
81 | else {
|
82 | resolvedValue = stylable_value_parsers_1.getStringValue(valueNode);
|
83 | }
|
84 | }
|
85 | }
|
86 | else {
|
87 | resolvedValue = stylable_value_parsers_1.getStringValue(valueNodes);
|
88 | }
|
89 | if (resolvedValue) {
|
90 | outputMap[prop.value] = resolvedValue;
|
91 | }
|
92 | }
|
93 | return outputMap;
|
94 | },
|
95 | };
|
96 | function resolveCustomValues(meta, resolver) {
|
97 | const customValues = { ...exports.stTypes };
|
98 | for (const [symbolName, symbol] of Object.entries(meta.mappedSymbols)) {
|
99 | if (symbol._kind !== 'import') {
|
100 | continue;
|
101 | }
|
102 | const ss = resolver.resolveImport(symbol);
|
103 | if (!ss || ss._kind === 'css') {
|
104 | continue;
|
105 | }
|
106 | if (ss.symbol && isCustomValue(ss.symbol)) {
|
107 | if (customValues[symbolName]) {
|
108 |
|
109 | }
|
110 | else {
|
111 | customValues[symbolName] = ss.symbol.register(symbolName);
|
112 | }
|
113 | }
|
114 | }
|
115 | return customValues;
|
116 | }
|
117 | exports.resolveCustomValues = resolveCustomValues;
|
118 | function createCustomValue({ processArgs, createValue, flattenValue, getValue, }) {
|
119 | return {
|
120 | _kind: 'CustomValue',
|
121 | register(localTypeSymbol) {
|
122 | return {
|
123 | evalVarAst(fnNode, customTypes) {
|
124 | const args = processArgs(fnNode, customTypes);
|
125 | return box(localTypeSymbol, createValue(args));
|
126 | },
|
127 | getValue(path, obj, fallbackNode,
|
128 | customTypes) {
|
129 | if (path.length === 0) {
|
130 | if (flattenValue) {
|
131 | const { delimiter, parts } = flattenValue(obj);
|
132 | return parts
|
133 | .map((v) => getBoxValue([], v, fallbackNode, customTypes))
|
134 | .join(delimiter);
|
135 | }
|
136 | else {
|
137 |
|
138 | return stylable_value_parsers_1.getStringValue([fallbackNode]);
|
139 | }
|
140 | }
|
141 | const value = getValue(obj.value, path[0]);
|
142 | return getBoxValue(path.slice(1), value, fallbackNode, customTypes);
|
143 | },
|
144 | };
|
145 | },
|
146 | };
|
147 | }
|
148 | exports.createCustomValue = createCustomValue;
|
149 | function getBoxValue(path, value, node, customTypes) {
|
150 | if (typeof value === 'string') {
|
151 | return value;
|
152 | }
|
153 | else if (value && customTypes[value.type]) {
|
154 | return customTypes[value.type].getValue(path, value, node, customTypes);
|
155 | }
|
156 | else {
|
157 | throw new Error('Unknown Type ' + JSON.stringify(value));
|
158 |
|
159 | }
|
160 | }
|
161 | exports.getBoxValue = getBoxValue;
|
162 | function isCustomValue(symbol) {
|
163 | return symbol._kind === 'CustomValue';
|
164 | }
|
165 | exports.isCustomValue = isCustomValue;
|
166 |
|
\ | No newline at end of file |