@jesscss/plugin-less-compat 2.0.0-alpha.6 → 2.0.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/lib/index.cjs +844 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +800 -10
- package/lib/less-adapter.cjs +216 -0
- package/lib/less-adapter.js +181 -0
- package/lib/less-compat-structures.cjs +378 -0
- package/lib/less-compat-structures.d.ts +2 -2
- package/lib/less-compat-structures.js +367 -512
- package/lib/nodes/at-rule.d.ts +2 -2
- package/lib/nodes/attribute-selector.d.ts +2 -2
- package/lib/nodes/call.d.ts +2 -2
- package/lib/nodes/color.d.ts +2 -1
- package/lib/nodes/combinator.d.ts +2 -1
- package/lib/nodes/comment.d.ts +2 -1
- package/lib/nodes/condition.d.ts +2 -2
- package/lib/nodes/declaration.d.ts +2 -2
- package/lib/nodes/dimension.d.ts +2 -1
- package/lib/nodes/expression.d.ts +2 -2
- package/lib/nodes/extend.d.ts +2 -1
- package/lib/nodes/import.d.ts +2 -2
- package/lib/nodes/keyword.d.ts +2 -1
- package/lib/nodes/list.d.ts +3 -2
- package/lib/nodes/mixin.d.ts +2 -2
- package/lib/nodes/negative.d.ts +2 -2
- package/lib/nodes/operation.d.ts +2 -2
- package/lib/nodes/paren.d.ts +2 -2
- package/lib/nodes/quoted.d.ts +2 -1
- package/lib/nodes/reference.d.ts +2 -1
- package/lib/nodes/ruleset.d.ts +2 -2
- package/lib/nodes/selector.d.ts +2 -2
- package/lib/nodes/sequence.d.ts +2 -2
- package/lib/nodes/url.d.ts +2 -1
- package/lib/nodes/var-declaration.d.ts +2 -2
- package/lib/plugin.d.ts +44 -16
- package/lib/transform/adapter.d.ts +9 -11
- package/lib/transform/index.cjs +13 -0
- package/lib/transform/index.d.ts +1 -1
- package/lib/transform/index.js +3 -8
- package/lib/transform/less-adapter.d.ts +29 -0
- package/lib/transform.cjs +1045 -0
- package/lib/transform.js +1015 -0
- package/lib/types.d.ts +153 -28
- package/package.json +6 -9
- package/lib/index.js.map +0 -1
- package/lib/less-compat-structures.js.map +0 -1
- package/lib/nodes/at-rule.js +0 -21
- package/lib/nodes/at-rule.js.map +0 -1
- package/lib/nodes/attribute-selector.js +0 -18
- package/lib/nodes/attribute-selector.js.map +0 -1
- package/lib/nodes/call.js +0 -27
- package/lib/nodes/call.js.map +0 -1
- package/lib/nodes/color.js +0 -23
- package/lib/nodes/color.js.map +0 -1
- package/lib/nodes/combinator.js +0 -7
- package/lib/nodes/combinator.js.map +0 -1
- package/lib/nodes/comment.js +0 -9
- package/lib/nodes/comment.js.map +0 -1
- package/lib/nodes/condition.js +0 -19
- package/lib/nodes/condition.js.map +0 -1
- package/lib/nodes/declaration.js +0 -38
- package/lib/nodes/declaration.js.map +0 -1
- package/lib/nodes/dimension.js +0 -9
- package/lib/nodes/dimension.js.map +0 -1
- package/lib/nodes/expression.js +0 -16
- package/lib/nodes/expression.js.map +0 -1
- package/lib/nodes/extend.js +0 -18
- package/lib/nodes/extend.js.map +0 -1
- package/lib/nodes/import.js +0 -22
- package/lib/nodes/import.js.map +0 -1
- package/lib/nodes/index.js +0 -308
- package/lib/nodes/index.js.map +0 -1
- package/lib/nodes/keyword.js +0 -8
- package/lib/nodes/keyword.js.map +0 -1
- package/lib/nodes/list.js +0 -61
- package/lib/nodes/list.js.map +0 -1
- package/lib/nodes/mixin.js +0 -22
- package/lib/nodes/mixin.js.map +0 -1
- package/lib/nodes/negative.js +0 -13
- package/lib/nodes/negative.js.map +0 -1
- package/lib/nodes/operation.js +0 -22
- package/lib/nodes/operation.js.map +0 -1
- package/lib/nodes/paren.js +0 -13
- package/lib/nodes/paren.js.map +0 -1
- package/lib/nodes/quoted.js +0 -23
- package/lib/nodes/quoted.js.map +0 -1
- package/lib/nodes/reference.js +0 -44
- package/lib/nodes/reference.js.map +0 -1
- package/lib/nodes/ruleset.js +0 -67
- package/lib/nodes/ruleset.js.map +0 -1
- package/lib/nodes/selector.js +0 -158
- package/lib/nodes/selector.js.map +0 -1
- package/lib/nodes/sequence.js +0 -56
- package/lib/nodes/sequence.js.map +0 -1
- package/lib/nodes/url.js +0 -13
- package/lib/nodes/url.js.map +0 -1
- package/lib/nodes/var-declaration.js +0 -21
- package/lib/nodes/var-declaration.js.map +0 -1
- package/lib/plugin-utils.js +0 -100
- package/lib/plugin-utils.js.map +0 -1
- package/lib/plugin.js +0 -1026
- package/lib/plugin.js.map +0 -1
- package/lib/transform/adapter.js +0 -111
- package/lib/transform/adapter.js.map +0 -1
- package/lib/transform/from-less.js +0 -170
- package/lib/transform/from-less.js.map +0 -1
- package/lib/transform/index.js.map +0 -1
- package/lib/transform/proxy.d.ts +0 -32
- package/lib/transform/proxy.js +0 -138
- package/lib/transform/proxy.js.map +0 -1
- package/lib/transform/to-less.js +0 -130
- package/lib/transform/to-less.js.map +0 -1
- package/lib/transform/type-map.js +0 -105
- package/lib/transform/type-map.js.map +0 -1
- package/lib/types.js +0 -5
- package/lib/types.js.map +0 -1
|
@@ -0,0 +1,1045 @@
|
|
|
1
|
+
const require_less_adapter = require("./less-adapter.cjs");
|
|
2
|
+
require("./index.cjs");
|
|
3
|
+
let _jesscss_core = require("@jesscss/core");
|
|
4
|
+
//#region src/transform/adapter.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create a NodeTransformer from a declarative adapter definition.
|
|
7
|
+
*
|
|
8
|
+
* - `type` and `typeIndex` are auto-mapped.
|
|
9
|
+
* - If no `accept` is provided, leaf behavior (return self) is used.
|
|
10
|
+
* - Field lookups are dispatched from the `fields` record.
|
|
11
|
+
*/
|
|
12
|
+
function createFromAdapter(adapter) {
|
|
13
|
+
return (jessNode, cache) => {
|
|
14
|
+
return require_less_adapter.createLessAdapter(jessNode, {
|
|
15
|
+
lessType: adapter.lessType ?? ((node) => require_less_adapter.mapJessTypeToLessType(node.type)),
|
|
16
|
+
fields: adapter.fields,
|
|
17
|
+
accept: adapter.accept
|
|
18
|
+
}, cache);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function selfVisitAccept() {
|
|
22
|
+
return (node) => {
|
|
23
|
+
return node;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Accept helper: traverse child nodes via visitor.visitArray.
|
|
28
|
+
*/
|
|
29
|
+
function childrenAccept(getChildren) {
|
|
30
|
+
return (node, visitor, cache) => {
|
|
31
|
+
const children = getChildren(node, cache);
|
|
32
|
+
if (children.length > 0) {
|
|
33
|
+
const lessChildren = children.map((child) => toLessNode(child, { cache })).filter((child) => child != null);
|
|
34
|
+
if (lessChildren.length > 0) {
|
|
35
|
+
if (visitor.visitArray) visitor.visitArray(lessChildren);
|
|
36
|
+
else for (const child of lessChildren) if (child?.accept) child.accept(visitor);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return node;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Accept helper: traverse a single child node.
|
|
44
|
+
*/
|
|
45
|
+
function singleChildAccept(getChild) {
|
|
46
|
+
return (node, visitor, cache) => {
|
|
47
|
+
const child = getChild(node);
|
|
48
|
+
if (child instanceof _jesscss_core.Node) {
|
|
49
|
+
const lessChild = toLessNode(child, { cache });
|
|
50
|
+
if (lessChild?.accept) lessChild.accept(visitor);
|
|
51
|
+
else if (lessChild && visitor.visitArray) visitor.visitArray([lessChild]);
|
|
52
|
+
else if (lessChild && visitor.visit) visitor.visit(lessChild);
|
|
53
|
+
}
|
|
54
|
+
return node;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/nodes/selector.ts
|
|
59
|
+
/**
|
|
60
|
+
* Flatten a hierarchical Jess selector into Less's flat Element array.
|
|
61
|
+
* Less expects: Element[] where each Element has { combinator, value }
|
|
62
|
+
* Jess now models complex selectors as an interleaved stream of selector
|
|
63
|
+
* components and explicit combinators. Less still expects flat Elements with
|
|
64
|
+
* the combinator attached to the following selector component.
|
|
65
|
+
*/
|
|
66
|
+
function flattenSelectorToElements(selector, cache) {
|
|
67
|
+
const elements = [];
|
|
68
|
+
if (typeof selector === "string") {
|
|
69
|
+
elements.push(createElementAdapter(selector, createLessCombinator(""), cache));
|
|
70
|
+
return elements;
|
|
71
|
+
}
|
|
72
|
+
if (Array.isArray(selector)) {
|
|
73
|
+
const first = selector[0];
|
|
74
|
+
return first !== void 0 ? flattenSelectorToElements(first, cache) : [];
|
|
75
|
+
}
|
|
76
|
+
if (selector instanceof _jesscss_core.SelectorList) {
|
|
77
|
+
const selectorListValue = selector.value;
|
|
78
|
+
if (selectorListValue.length > 0 && selectorListValue[0]) return flattenSelectorToElements(selectorListValue[0], cache);
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
if (selector instanceof _jesscss_core.ComplexSelector) {
|
|
82
|
+
const components = selector.value;
|
|
83
|
+
let nextCombinatorValue = "";
|
|
84
|
+
for (let i = 0; i < components.length; i++) {
|
|
85
|
+
const component = components[i];
|
|
86
|
+
if (!component) continue;
|
|
87
|
+
if (component instanceof _jesscss_core.Combinator) {
|
|
88
|
+
nextCombinatorValue = component.value;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (typeof component === "string" && (0, _jesscss_core.isStringCombinator)(component)) {
|
|
92
|
+
nextCombinatorValue = component;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (component instanceof _jesscss_core.CompoundSelector) {
|
|
96
|
+
const compoundValue = component.value;
|
|
97
|
+
for (let j = 0; j < compoundValue.length; j++) {
|
|
98
|
+
const simple = compoundValue[j];
|
|
99
|
+
if (!simple) continue;
|
|
100
|
+
const elementCombinator = createLessCombinator(j === 0 ? nextCombinatorValue : "");
|
|
101
|
+
elements.push(createElementAdapter(simple, elementCombinator, cache));
|
|
102
|
+
}
|
|
103
|
+
} else if (typeof component === "string") {
|
|
104
|
+
const elementCombinator = createLessCombinator(nextCombinatorValue);
|
|
105
|
+
elements.push(createElementAdapter(component, elementCombinator, cache));
|
|
106
|
+
} else if (component instanceof _jesscss_core.BasicSelector || component instanceof _jesscss_core.Ampersand) {
|
|
107
|
+
const elementCombinator = createLessCombinator(nextCombinatorValue);
|
|
108
|
+
elements.push(createElementAdapter(component, elementCombinator, cache));
|
|
109
|
+
} else if (component instanceof _jesscss_core.Node) {
|
|
110
|
+
const elementCombinator = createLessCombinator(nextCombinatorValue);
|
|
111
|
+
elements.push(createElementAdapter(component, elementCombinator, cache));
|
|
112
|
+
}
|
|
113
|
+
nextCombinatorValue = "";
|
|
114
|
+
}
|
|
115
|
+
} else if (selector instanceof _jesscss_core.CompoundSelector) {
|
|
116
|
+
const compoundSelValue = selector.value;
|
|
117
|
+
for (let i = 0; i < compoundSelValue.length; i++) {
|
|
118
|
+
const basic = compoundSelValue[i];
|
|
119
|
+
if (!basic) continue;
|
|
120
|
+
const combinator = createLessCombinator(i === 0 ? "" : "");
|
|
121
|
+
elements.push(createElementAdapter(basic, combinator, cache));
|
|
122
|
+
}
|
|
123
|
+
} else if (selector instanceof _jesscss_core.BasicSelector || selector instanceof _jesscss_core.Ampersand) elements.push(createElementAdapter(selector, createLessCombinator(""), cache));
|
|
124
|
+
return elements;
|
|
125
|
+
}
|
|
126
|
+
function createLessCombinator(value) {
|
|
127
|
+
return {
|
|
128
|
+
type: "Combinator",
|
|
129
|
+
typeIndex: void 0,
|
|
130
|
+
value,
|
|
131
|
+
emptyOrWhitespace: value === "" || value === " ",
|
|
132
|
+
accept() {
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function createElementAdapter(basic, combinator, cache) {
|
|
138
|
+
if (typeof basic === "string") return {
|
|
139
|
+
type: "Element",
|
|
140
|
+
typeIndex: void 0,
|
|
141
|
+
combinator,
|
|
142
|
+
value: basic,
|
|
143
|
+
isVariable: false,
|
|
144
|
+
accept(visitor) {
|
|
145
|
+
if (combinator && visitor.visit) visitor.visit(combinator);
|
|
146
|
+
return basic;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const adapter = require_less_adapter.createLessAdapter(basic, {
|
|
150
|
+
lessType: "Element",
|
|
151
|
+
fields: {
|
|
152
|
+
combinator: () => combinator,
|
|
153
|
+
value: (target) => "value" in target ? target.value : void 0,
|
|
154
|
+
isVariable: () => false
|
|
155
|
+
},
|
|
156
|
+
accept: (target, visitor) => {
|
|
157
|
+
if (combinator && visitor.visit) visitor.visit(combinator);
|
|
158
|
+
const value = "value" in target ? target.value : void 0;
|
|
159
|
+
if (value && typeof value === "object" && value instanceof _jesscss_core.Node) {
|
|
160
|
+
const lessValue = toLessNode(value, { cache });
|
|
161
|
+
if (lessValue && visitor.visit) visitor.visit(lessValue);
|
|
162
|
+
}
|
|
163
|
+
return target;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
if (cache) cache.set(basic, adapter);
|
|
167
|
+
return adapter;
|
|
168
|
+
}
|
|
169
|
+
function transformSelectorToLess(sel, cache) {
|
|
170
|
+
const elements = flattenSelectorToElements(sel, cache);
|
|
171
|
+
if (typeof sel === "string" || Array.isArray(sel)) return {
|
|
172
|
+
type: "Selector",
|
|
173
|
+
typeIndex: void 0,
|
|
174
|
+
elements,
|
|
175
|
+
length: elements.length,
|
|
176
|
+
accept(visitor) {
|
|
177
|
+
if (visitor.visitArray) visitor.visitArray(elements);
|
|
178
|
+
return sel;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
const adapter = require_less_adapter.createLessAdapter(sel, {
|
|
182
|
+
lessType: "Selector",
|
|
183
|
+
fields: {
|
|
184
|
+
elements: () => elements,
|
|
185
|
+
length: () => elements.length
|
|
186
|
+
},
|
|
187
|
+
accept: (node, visitor) => {
|
|
188
|
+
if (visitor.visitArray) visitor.visitArray(elements);
|
|
189
|
+
else if (visitor.visit) for (const element of elements) visitor.visit(element);
|
|
190
|
+
else for (const element of elements) if (element?.accept) element.accept(visitor);
|
|
191
|
+
return node;
|
|
192
|
+
}
|
|
193
|
+
}, cache);
|
|
194
|
+
for (let i = 0; i < elements.length; i++) Object.defineProperty(adapter, String(i), {
|
|
195
|
+
enumerable: true,
|
|
196
|
+
configurable: true,
|
|
197
|
+
get: () => elements[i]
|
|
198
|
+
});
|
|
199
|
+
return adapter;
|
|
200
|
+
}
|
|
201
|
+
//#endregion
|
|
202
|
+
//#region src/nodes/ruleset.ts
|
|
203
|
+
/**
|
|
204
|
+
* Return a Ruleset's child rules as a flat array.
|
|
205
|
+
*
|
|
206
|
+
* Core's `Ruleset.rules` is normally a plain `Node[]` (the parser delivers an
|
|
207
|
+
* array), but a caller may also construct a Ruleset with a `Rules` wrapper node
|
|
208
|
+
* as its body (`new Ruleset({ rules: new Rules([...]) })`). The wrapper is stored
|
|
209
|
+
* as-is now that the old factory-side unwrap was removed, so read its `.rules`.
|
|
210
|
+
*/
|
|
211
|
+
function ruleList(rs) {
|
|
212
|
+
const rules = rs.rules;
|
|
213
|
+
if (rules instanceof _jesscss_core.Rules) return rules.rules;
|
|
214
|
+
if (Array.isArray(rules)) return rules;
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
const transformRulesetToLess = createFromAdapter({
|
|
218
|
+
fields: {
|
|
219
|
+
selectors: (rs, cache) => {
|
|
220
|
+
const selector = rs.selector;
|
|
221
|
+
if (selector instanceof _jesscss_core.Nil) return [];
|
|
222
|
+
if (!selector) return [];
|
|
223
|
+
if (selector instanceof _jesscss_core.SelectorList) return selector.value.filter((s) => s instanceof _jesscss_core.Selector).map((s) => transformSelectorToLess(s, cache));
|
|
224
|
+
return [transformSelectorToLess(selector, cache)];
|
|
225
|
+
},
|
|
226
|
+
rules: (rs, cache) => {
|
|
227
|
+
return ruleList(rs).map((r) => toLessNode(r, { cache }));
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
accept: (ruleset, visitor, cache) => {
|
|
231
|
+
const selector = ruleset.selector;
|
|
232
|
+
const rules = ruleList(ruleset);
|
|
233
|
+
if (selector && !(selector instanceof _jesscss_core.Nil)) if (selector instanceof _jesscss_core.SelectorList) {
|
|
234
|
+
const lessSelectors = selector.value.filter((s) => s instanceof _jesscss_core.Selector).map((s) => toLessNode(s, { cache }));
|
|
235
|
+
if (visitor.visitArray) visitor.visitArray(lessSelectors);
|
|
236
|
+
else for (const ls of lessSelectors) if (ls?.accept) ls.accept(visitor);
|
|
237
|
+
} else {
|
|
238
|
+
const lessSelector = transformSelectorToLess(selector, cache);
|
|
239
|
+
if (lessSelector) {
|
|
240
|
+
if (visitor.visitArray) visitor.visitArray([lessSelector]);
|
|
241
|
+
else if (lessSelector.accept) lessSelector.accept(visitor);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (rules.length > 0) {
|
|
245
|
+
const lessRules = rules.map((r) => toLessNode(r, { cache }));
|
|
246
|
+
if (visitor.visitArray) visitor.visitArray(lessRules);
|
|
247
|
+
else for (const lr of lessRules) if (lr?.accept) lr.accept(visitor);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
//#endregion
|
|
252
|
+
//#region src/transform/from-less.ts
|
|
253
|
+
/**
|
|
254
|
+
* Convert a Less node back to a Jess node
|
|
255
|
+
*
|
|
256
|
+
* @param lessNode - The Less node to convert
|
|
257
|
+
* @param options - Conversion options
|
|
258
|
+
* @returns A Jess node
|
|
259
|
+
*/
|
|
260
|
+
function fromLessNode(lessNode, options) {
|
|
261
|
+
if (!lessNode) return lessNode;
|
|
262
|
+
const cache = options?.cache || /* @__PURE__ */ new WeakMap();
|
|
263
|
+
if (cache.has(lessNode)) {
|
|
264
|
+
const cached = cache.get(lessNode);
|
|
265
|
+
if (cached) return cached;
|
|
266
|
+
}
|
|
267
|
+
if (lessNode instanceof require_less_adapter.LessAdapterBase) {
|
|
268
|
+
cache.set(lessNode, lessNode.jessNode);
|
|
269
|
+
return lessNode.jessNode;
|
|
270
|
+
}
|
|
271
|
+
if (lessNode && typeof lessNode === "object" && "__jessNode" in lessNode) return lessNode.__jessNode;
|
|
272
|
+
if (lessNode && typeof lessNode === "object" && typeof lessNode.type === "string") {
|
|
273
|
+
if (lessNode.type === "Quoted") {
|
|
274
|
+
const quote = lessNode.quote === "'" || lessNode.quote === "\"" ? lessNode.quote : "\"";
|
|
275
|
+
const out = new _jesscss_core.Quoted(typeof lessNode.value === "string" ? lessNode.value : String(lessNode.value), {
|
|
276
|
+
quote,
|
|
277
|
+
escaped: !!lessNode.escaped
|
|
278
|
+
});
|
|
279
|
+
cache.set(lessNode, out);
|
|
280
|
+
return out;
|
|
281
|
+
}
|
|
282
|
+
if (lessNode.type === "Anonymous") {
|
|
283
|
+
const out = new _jesscss_core.Any(typeof lessNode.value === "string" ? lessNode.value : String(lessNode.value));
|
|
284
|
+
cache.set(lessNode, out);
|
|
285
|
+
return out;
|
|
286
|
+
}
|
|
287
|
+
if (lessNode.type === "Dimension" || lessNode.type === "Num") {
|
|
288
|
+
const out = new _jesscss_core.Dimension({
|
|
289
|
+
number: typeof lessNode.value === "number" ? lessNode.value : Number(lessNode.value),
|
|
290
|
+
unit: (typeof lessNode.unit === "string" ? lessNode.unit : "") || void 0
|
|
291
|
+
});
|
|
292
|
+
cache.set(lessNode, out);
|
|
293
|
+
return out;
|
|
294
|
+
}
|
|
295
|
+
if (lessNode.type === "Color") {
|
|
296
|
+
const [r = 0, g = 0, b = 0] = Array.isArray(lessNode.rgb) ? lessNode.rgb : [
|
|
297
|
+
0,
|
|
298
|
+
0,
|
|
299
|
+
0
|
|
300
|
+
];
|
|
301
|
+
const alpha = typeof lessNode.alpha === "number" ? lessNode.alpha : 1;
|
|
302
|
+
const out = new _jesscss_core.Color({
|
|
303
|
+
rgb: [
|
|
304
|
+
r,
|
|
305
|
+
g,
|
|
306
|
+
b
|
|
307
|
+
],
|
|
308
|
+
alpha
|
|
309
|
+
}, { format: _jesscss_core.ColorFormat.HEX });
|
|
310
|
+
cache.set(lessNode, out);
|
|
311
|
+
return out;
|
|
312
|
+
}
|
|
313
|
+
if (lessNode.type === "Declaration" || lessNode.type === "Rule") {
|
|
314
|
+
const prop = String(lessNode.name ?? "");
|
|
315
|
+
const val = lessNode.value;
|
|
316
|
+
const decl = new _jesscss_core.Declaration({
|
|
317
|
+
name: prop,
|
|
318
|
+
value: new _jesscss_core.Any(val && typeof val === "object" && typeof val.value === "string" ? val.value : String(val ?? ""))
|
|
319
|
+
});
|
|
320
|
+
cache.set(lessNode, decl);
|
|
321
|
+
return decl;
|
|
322
|
+
}
|
|
323
|
+
if (lessNode.type === "DetachedRuleset") {
|
|
324
|
+
const ruleset = lessNode.ruleset;
|
|
325
|
+
const rules = ruleset && Array.isArray(ruleset.rules) ? ruleset.rules : [];
|
|
326
|
+
const nodes = [];
|
|
327
|
+
for (const rr of rules) if (rr && typeof rr === "object" && (rr.type === "Declaration" || rr.type === "Rule")) {
|
|
328
|
+
const prop = String(rr.name ?? "");
|
|
329
|
+
const val = rr.value;
|
|
330
|
+
const decl = new _jesscss_core.Declaration({
|
|
331
|
+
name: prop,
|
|
332
|
+
value: new _jesscss_core.Any(val && typeof val === "object" && typeof val.value === "string" ? val.value : String(val ?? ""))
|
|
333
|
+
});
|
|
334
|
+
nodes.push(decl);
|
|
335
|
+
}
|
|
336
|
+
const out = new _jesscss_core.Collection(nodes);
|
|
337
|
+
cache.set(lessNode, out);
|
|
338
|
+
return out;
|
|
339
|
+
}
|
|
340
|
+
if (lessNode.type === "AtRule") {
|
|
341
|
+
const n = String(lessNode.name ?? "");
|
|
342
|
+
const line = `${n} ${String(lessNode.value ?? "")};`;
|
|
343
|
+
const out = n === "@charset" ? new _jesscss_core.Any(line, { role: "charset" }) : new _jesscss_core.Any(line);
|
|
344
|
+
cache.set(lessNode, out);
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
throw new Error(`Cannot convert Less node back to Jess: ${lessNode?.type || "unknown type"}. Less visitors should not create new nodes, only modify existing ones.`);
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Convert a Less plugin function return value to a Jess node.
|
|
352
|
+
* Handles primitives (number, boolean), Less nodes (via fromLessNode), and toCSS() objects.
|
|
353
|
+
* Use this for @plugin-loaded function results so conversion is centralized here.
|
|
354
|
+
*/
|
|
355
|
+
function fromLessPluginReturnValue(value, options) {
|
|
356
|
+
if (value === null || value === void 0) return;
|
|
357
|
+
if (typeof value === "number") return new _jesscss_core.Any(String(value));
|
|
358
|
+
if (value === true || value === false) {
|
|
359
|
+
if (options?.statementContext) return;
|
|
360
|
+
return new _jesscss_core.Any(String(value));
|
|
361
|
+
}
|
|
362
|
+
if (typeof value === "object" && value !== null) {
|
|
363
|
+
if ("type" in value && typeof value.type === "string") return fromLessNode(value, options);
|
|
364
|
+
if ("toCSS" in value && typeof value.toCSS === "function") try {
|
|
365
|
+
const css = value.toCSS();
|
|
366
|
+
return new _jesscss_core.Any(typeof css === "string" ? css : String(css));
|
|
367
|
+
} catch {}
|
|
368
|
+
}
|
|
369
|
+
if (value instanceof _jesscss_core.Node) return value;
|
|
370
|
+
return new _jesscss_core.Any(String(value));
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Convert an entire Less tree back to Jess Rules
|
|
374
|
+
*
|
|
375
|
+
* @param lessTree - The Less tree to convert
|
|
376
|
+
* @param options - Conversion options
|
|
377
|
+
* @returns A Jess Rules tree
|
|
378
|
+
*/
|
|
379
|
+
function fromLessTree(lessTree, _options) {
|
|
380
|
+
if (lessTree instanceof require_less_adapter.LessAdapterBase && lessTree.jessNode instanceof _jesscss_core.Rules) return lessTree.jessNode;
|
|
381
|
+
throw new Error("Cannot convert Less tree back to Jess Rules");
|
|
382
|
+
}
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region src/nodes/declaration.ts
|
|
385
|
+
const transformDeclarationToLess = createFromAdapter({
|
|
386
|
+
fields: {
|
|
387
|
+
name: {
|
|
388
|
+
get: (d) => d.name,
|
|
389
|
+
set: (d, value) => {
|
|
390
|
+
d.name = value instanceof _jesscss_core.Interpolated ? value : String(value);
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
value: {
|
|
394
|
+
get: (d, cache) => {
|
|
395
|
+
const value = d.value;
|
|
396
|
+
return value instanceof _jesscss_core.Node ? toLessNode(value, { cache }) : value;
|
|
397
|
+
},
|
|
398
|
+
set: (d, value) => {
|
|
399
|
+
const node = value instanceof _jesscss_core.Node ? value : fromLessNode(value);
|
|
400
|
+
d.adopt(node);
|
|
401
|
+
d.value = node;
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
important: (d) => d.important || false,
|
|
405
|
+
variable: (d) => d.options?.assign !== void 0,
|
|
406
|
+
merge: () => false
|
|
407
|
+
},
|
|
408
|
+
accept: (decl, visitor, cache) => {
|
|
409
|
+
const value = decl.value;
|
|
410
|
+
if (value instanceof _jesscss_core.Node) {
|
|
411
|
+
const lessValue = toLessNode(value, { cache });
|
|
412
|
+
if (lessValue?.accept) lessValue.accept(visitor);
|
|
413
|
+
else if (lessValue && visitor.visitArray) visitor.visitArray([lessValue]);
|
|
414
|
+
else if (lessValue && visitor.visit) visitor.visit(lessValue);
|
|
415
|
+
}
|
|
416
|
+
return decl;
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
//#endregion
|
|
420
|
+
//#region src/nodes/mixin.ts
|
|
421
|
+
const transformMixinToLess = createFromAdapter({
|
|
422
|
+
fields: {
|
|
423
|
+
name: (m) => m.name,
|
|
424
|
+
params: (m, cache) => {
|
|
425
|
+
const params = m.params;
|
|
426
|
+
return params instanceof _jesscss_core.Node ? toLessNode(params, { cache }) : params;
|
|
427
|
+
},
|
|
428
|
+
rules: (m, cache) => {
|
|
429
|
+
return m.rules.map((r) => toLessNode(r, { cache }));
|
|
430
|
+
},
|
|
431
|
+
condition: (m, cache) => {
|
|
432
|
+
const guard = m.guard;
|
|
433
|
+
return guard instanceof _jesscss_core.Node ? toLessNode(guard, { cache }) : guard;
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
accept: selfVisitAccept()
|
|
437
|
+
});
|
|
438
|
+
//#endregion
|
|
439
|
+
//#region src/nodes/dimension.ts
|
|
440
|
+
const transformDimensionToLess = createFromAdapter({ fields: {
|
|
441
|
+
value: (d) => d.number,
|
|
442
|
+
unit: (d) => d instanceof _jesscss_core.Num ? "" : d.unit || ""
|
|
443
|
+
} });
|
|
444
|
+
//#endregion
|
|
445
|
+
//#region src/nodes/color.ts
|
|
446
|
+
const transformColorToLess = createFromAdapter({ fields: {
|
|
447
|
+
rgb: (c) => c.rgb || [
|
|
448
|
+
0,
|
|
449
|
+
0,
|
|
450
|
+
0
|
|
451
|
+
],
|
|
452
|
+
alpha: (c) => c._alpha,
|
|
453
|
+
value: (c) => {
|
|
454
|
+
const rgb = c.rgb;
|
|
455
|
+
const alpha = c._alpha;
|
|
456
|
+
if (rgb && alpha !== void 0 && alpha < 1) return `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${alpha})`;
|
|
457
|
+
if (rgb) return `#${rgb.map((v) => {
|
|
458
|
+
const numericValue = Array.isArray(v) ? v[0] : v;
|
|
459
|
+
return Math.round(numericValue).toString(16).padStart(2, "0");
|
|
460
|
+
}).join("")}`;
|
|
461
|
+
return "";
|
|
462
|
+
}
|
|
463
|
+
} });
|
|
464
|
+
//#endregion
|
|
465
|
+
//#region src/nodes/operation.ts
|
|
466
|
+
const transformOperationToLess = createFromAdapter({
|
|
467
|
+
fields: {
|
|
468
|
+
op: (o) => o.operator || "",
|
|
469
|
+
operands: (o, cache) => {
|
|
470
|
+
const operands = [];
|
|
471
|
+
const { left, right } = o;
|
|
472
|
+
if (left instanceof _jesscss_core.Node) operands.push(left);
|
|
473
|
+
if (right instanceof _jesscss_core.Node) operands.push(right);
|
|
474
|
+
return operands.map((n) => toLessNode(n, { cache }));
|
|
475
|
+
}
|
|
476
|
+
},
|
|
477
|
+
accept: selfVisitAccept()
|
|
478
|
+
});
|
|
479
|
+
//#endregion
|
|
480
|
+
//#region src/nodes/expression.ts
|
|
481
|
+
const transformExpressionToLess = createFromAdapter({
|
|
482
|
+
fields: { value: (e, cache) => {
|
|
483
|
+
const value = e.value;
|
|
484
|
+
if (value instanceof _jesscss_core.Node) return [toLessNode(value, { cache })];
|
|
485
|
+
return [value];
|
|
486
|
+
} },
|
|
487
|
+
accept: selfVisitAccept()
|
|
488
|
+
});
|
|
489
|
+
//#endregion
|
|
490
|
+
//#region src/nodes/sequence.ts
|
|
491
|
+
const transformSequenceToLess = createFromAdapter({
|
|
492
|
+
fields: {
|
|
493
|
+
value: (seq, cache) => (seq.value ?? []).map((item) => item instanceof _jesscss_core.Node ? toLessNode(item, { cache }) : item).filter((item) => item != null),
|
|
494
|
+
length: (seq) => (seq.value ?? []).filter((v) => v != null).length
|
|
495
|
+
},
|
|
496
|
+
accept: (seq, visitor, cache) => {
|
|
497
|
+
const raw = seq.value ?? [];
|
|
498
|
+
if (!Array.isArray(raw) || raw.length === 0) return seq;
|
|
499
|
+
for (let i = 0; i < raw.length; i++) {
|
|
500
|
+
const item = raw[i];
|
|
501
|
+
if (item == null) continue;
|
|
502
|
+
const lessItem = item instanceof _jesscss_core.Node ? toLessNode(item, { cache }) : item;
|
|
503
|
+
if (!lessItem || typeof visitor?.visit !== "function") continue;
|
|
504
|
+
const visited = visitor.visit(lessItem);
|
|
505
|
+
if (visited && visited !== lessItem && typeof visited === "object" && "type" in visited) try {
|
|
506
|
+
const jessReplacement = fromLessNode(visited, { cache: /* @__PURE__ */ new WeakMap() });
|
|
507
|
+
if (item instanceof _jesscss_core.Node) jessReplacement.inherit(item);
|
|
508
|
+
seq.adopt(jessReplacement);
|
|
509
|
+
seq.value[i] = jessReplacement;
|
|
510
|
+
} catch {}
|
|
511
|
+
}
|
|
512
|
+
return seq;
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
//#endregion
|
|
516
|
+
//#region src/nodes/quoted.ts
|
|
517
|
+
const transformQuotedToLess = createFromAdapter({
|
|
518
|
+
fields: {
|
|
519
|
+
value: (q) => {
|
|
520
|
+
const value = q.value;
|
|
521
|
+
if (typeof value === "string") return value;
|
|
522
|
+
if (value instanceof _jesscss_core.Any) return value.value;
|
|
523
|
+
if (value instanceof _jesscss_core.Interpolated) return String(value.source);
|
|
524
|
+
return String(value);
|
|
525
|
+
},
|
|
526
|
+
quote: (q) => q.quote || "\"",
|
|
527
|
+
escaped: (q) => q.escaped === true
|
|
528
|
+
},
|
|
529
|
+
accept: selfVisitAccept()
|
|
530
|
+
});
|
|
531
|
+
//#endregion
|
|
532
|
+
//#region src/nodes/url.ts
|
|
533
|
+
const transformUrlToLess = createFromAdapter({
|
|
534
|
+
fields: { value: (u, cache) => {
|
|
535
|
+
const value = u.value;
|
|
536
|
+
return value instanceof _jesscss_core.Quoted ? toLessNode(value, { cache }) : value;
|
|
537
|
+
} },
|
|
538
|
+
accept: selfVisitAccept()
|
|
539
|
+
});
|
|
540
|
+
//#endregion
|
|
541
|
+
//#region src/nodes/comment.ts
|
|
542
|
+
const transformCommentToLess = createFromAdapter({
|
|
543
|
+
fields: {
|
|
544
|
+
value: (c) => c.value,
|
|
545
|
+
silent: () => false
|
|
546
|
+
},
|
|
547
|
+
accept: selfVisitAccept()
|
|
548
|
+
});
|
|
549
|
+
//#endregion
|
|
550
|
+
//#region src/nodes/at-rule.ts
|
|
551
|
+
const transformAtRuleToLess = createFromAdapter({
|
|
552
|
+
fields: {
|
|
553
|
+
name: (a) => a.name,
|
|
554
|
+
value: (a, cache) => {
|
|
555
|
+
const prelude = a.prelude;
|
|
556
|
+
return prelude instanceof _jesscss_core.Node ? toLessNode(prelude, { cache }) : prelude;
|
|
557
|
+
},
|
|
558
|
+
rules: (a, cache) => {
|
|
559
|
+
const rules = a.rules;
|
|
560
|
+
return rules ? rules.map((r) => toLessNode(r, { cache })) : [];
|
|
561
|
+
}
|
|
562
|
+
},
|
|
563
|
+
accept: childrenAccept((a) => {
|
|
564
|
+
const rules = a.rules;
|
|
565
|
+
return rules.length ? [...rules] : [];
|
|
566
|
+
})
|
|
567
|
+
});
|
|
568
|
+
//#endregion
|
|
569
|
+
//#region src/nodes/import.ts
|
|
570
|
+
const transformImportToLess = createFromAdapter({
|
|
571
|
+
fields: {
|
|
572
|
+
path: (imp, cache) => {
|
|
573
|
+
const path = imp.path;
|
|
574
|
+
return path instanceof _jesscss_core.Node ? toLessNode(path, { cache }) : path;
|
|
575
|
+
},
|
|
576
|
+
options: (imp) => imp.options?.importOptions || {},
|
|
577
|
+
currentFileInfo: () => ({}),
|
|
578
|
+
index: (imp) => (0, _jesscss_core.sourceSpanOf)(imp)?.start
|
|
579
|
+
},
|
|
580
|
+
accept: selfVisitAccept()
|
|
581
|
+
});
|
|
582
|
+
//#endregion
|
|
583
|
+
//#region src/nodes/extend.ts
|
|
584
|
+
const transformExtendToLess = createFromAdapter({
|
|
585
|
+
fields: {
|
|
586
|
+
selector: (e, cache) => {
|
|
587
|
+
const selector = e.selector;
|
|
588
|
+
return selector instanceof _jesscss_core.Selector ? toLessNode(selector, { cache }) : selector;
|
|
589
|
+
},
|
|
590
|
+
option: (e) => e.flag === _jesscss_core.ExtendFlag.Exact ? "exact" : "all",
|
|
591
|
+
index: (e) => (0, _jesscss_core.sourceSpanOf)(e)?.start
|
|
592
|
+
},
|
|
593
|
+
accept: selfVisitAccept()
|
|
594
|
+
});
|
|
595
|
+
//#endregion
|
|
596
|
+
//#region src/nodes/condition.ts
|
|
597
|
+
const transformConditionToLess = createFromAdapter({
|
|
598
|
+
fields: {
|
|
599
|
+
op: (c) => c.operator || "",
|
|
600
|
+
lvalue: (c, cache) => {
|
|
601
|
+
const left = c.left;
|
|
602
|
+
return left instanceof _jesscss_core.Node ? toLessNode(left, { cache }) : left;
|
|
603
|
+
},
|
|
604
|
+
rvalue: (c, cache) => {
|
|
605
|
+
const right = c.right;
|
|
606
|
+
return right instanceof _jesscss_core.Node ? toLessNode(right, { cache }) : right;
|
|
607
|
+
},
|
|
608
|
+
negate: (c) => c.negate
|
|
609
|
+
},
|
|
610
|
+
accept: selfVisitAccept()
|
|
611
|
+
});
|
|
612
|
+
//#endregion
|
|
613
|
+
//#region src/nodes/paren.ts
|
|
614
|
+
const transformParenToLess = createFromAdapter({
|
|
615
|
+
fields: { value: (p, cache) => {
|
|
616
|
+
const value = p.value;
|
|
617
|
+
return value instanceof _jesscss_core.Node ? toLessNode(value, { cache }) : value;
|
|
618
|
+
} },
|
|
619
|
+
accept: selfVisitAccept()
|
|
620
|
+
});
|
|
621
|
+
//#endregion
|
|
622
|
+
//#region src/nodes/negative.ts
|
|
623
|
+
const transformNegativeToLess = createFromAdapter({
|
|
624
|
+
fields: { value: (n, cache) => {
|
|
625
|
+
const value = n.value;
|
|
626
|
+
return value instanceof _jesscss_core.Node ? toLessNode(value, { cache }) : value;
|
|
627
|
+
} },
|
|
628
|
+
accept: selfVisitAccept()
|
|
629
|
+
});
|
|
630
|
+
//#endregion
|
|
631
|
+
//#region src/nodes/list.ts
|
|
632
|
+
function getFilteredValue(list, cache) {
|
|
633
|
+
const data = list.value;
|
|
634
|
+
if (Array.isArray(data)) return data.map((item) => {
|
|
635
|
+
if (!item) return null;
|
|
636
|
+
if (item instanceof _jesscss_core.Node) return toLessNode(item, { cache }) || null;
|
|
637
|
+
return item;
|
|
638
|
+
}).filter((item) => item != null);
|
|
639
|
+
if (data != null) {
|
|
640
|
+
if (data && typeof data === "object" && "type" in data) {
|
|
641
|
+
const lessValue = toLessNode(data, { cache });
|
|
642
|
+
return lessValue ? [lessValue] : [];
|
|
643
|
+
}
|
|
644
|
+
return [data];
|
|
645
|
+
}
|
|
646
|
+
return [];
|
|
647
|
+
}
|
|
648
|
+
const transformListToLess = createFromAdapter({
|
|
649
|
+
fields: {
|
|
650
|
+
value: (l, cache) => getFilteredValue(l, cache),
|
|
651
|
+
length: (l, cache) => getFilteredValue(l, cache).length
|
|
652
|
+
},
|
|
653
|
+
accept: (list, visitor, cache) => {
|
|
654
|
+
const value = list.value;
|
|
655
|
+
if (Array.isArray(value) && value.length > 0) {
|
|
656
|
+
const lessItems = value.map((item) => item instanceof _jesscss_core.Node ? toLessNode(item, { cache }) : item).filter((item) => item != null);
|
|
657
|
+
if (lessItems.length > 0) {
|
|
658
|
+
if (visitor.visitArray) visitor.visitArray(lessItems);
|
|
659
|
+
else for (const li of lessItems) if (li?.accept) li.accept(visitor);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return list;
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
//#endregion
|
|
666
|
+
//#region src/nodes/var-declaration.ts
|
|
667
|
+
const transformVarDeclarationToLess = createFromAdapter({
|
|
668
|
+
fields: {
|
|
669
|
+
name: {
|
|
670
|
+
get: (v) => v.name,
|
|
671
|
+
set: (v, value) => {
|
|
672
|
+
v.name = value instanceof _jesscss_core.Interpolated ? value : String(value);
|
|
673
|
+
}
|
|
674
|
+
},
|
|
675
|
+
value: {
|
|
676
|
+
get: (v, cache) => {
|
|
677
|
+
const value = v.value;
|
|
678
|
+
return value instanceof _jesscss_core.Node ? toLessNode(value, { cache }) : value;
|
|
679
|
+
},
|
|
680
|
+
set: (v, value) => {
|
|
681
|
+
const node = value instanceof _jesscss_core.Node ? value : fromLessNode(value);
|
|
682
|
+
v.adopt(node);
|
|
683
|
+
v.value = node;
|
|
684
|
+
}
|
|
685
|
+
},
|
|
686
|
+
index: (v) => (0, _jesscss_core.sourceSpanOf)(v)?.start
|
|
687
|
+
},
|
|
688
|
+
accept: singleChildAccept((v) => {
|
|
689
|
+
const value = v.value;
|
|
690
|
+
return value instanceof _jesscss_core.Node ? value : void 0;
|
|
691
|
+
})
|
|
692
|
+
});
|
|
693
|
+
//#endregion
|
|
694
|
+
//#region src/nodes/keyword.ts
|
|
695
|
+
const transformKeywordToLess = createFromAdapter({
|
|
696
|
+
fields: { value: (k) => k.value },
|
|
697
|
+
accept: selfVisitAccept()
|
|
698
|
+
});
|
|
699
|
+
//#endregion
|
|
700
|
+
//#region src/nodes/combinator.ts
|
|
701
|
+
const transformCombinatorToLess = createFromAdapter({ fields: { value: (c) => c.value } });
|
|
702
|
+
//#endregion
|
|
703
|
+
//#region src/nodes/attribute-selector.ts
|
|
704
|
+
const transformAttributeSelectorToLess = createFromAdapter({
|
|
705
|
+
fields: {
|
|
706
|
+
key: (a, cache) => {
|
|
707
|
+
const name = a.name;
|
|
708
|
+
return name instanceof _jesscss_core.Node ? toLessNode(name, { cache }) : name;
|
|
709
|
+
},
|
|
710
|
+
op: (a) => a.op || "",
|
|
711
|
+
value: (a, cache) => {
|
|
712
|
+
const value = a.attributeValue;
|
|
713
|
+
return value instanceof _jesscss_core.Node ? toLessNode(value, { cache }) : value;
|
|
714
|
+
}
|
|
715
|
+
},
|
|
716
|
+
accept: selfVisitAccept()
|
|
717
|
+
});
|
|
718
|
+
//#endregion
|
|
719
|
+
//#region src/nodes/call.ts
|
|
720
|
+
const transformCallToLess = createFromAdapter({
|
|
721
|
+
fields: {
|
|
722
|
+
name: (c) => c.name,
|
|
723
|
+
args: (c, cache) => {
|
|
724
|
+
const args = c.args;
|
|
725
|
+
if (!args) return [];
|
|
726
|
+
return args.value.map((arg) => arg instanceof _jesscss_core.Node ? toLessNode(arg, { cache }) : arg);
|
|
727
|
+
},
|
|
728
|
+
index: (c) => (0, _jesscss_core.sourceSpanOf)(c)?.start
|
|
729
|
+
},
|
|
730
|
+
accept: childrenAccept((c) => {
|
|
731
|
+
const argsValue = c.args?.value;
|
|
732
|
+
return argsValue?.length ? argsValue.filter((a) => a instanceof _jesscss_core.Node) : [];
|
|
733
|
+
})
|
|
734
|
+
});
|
|
735
|
+
//#endregion
|
|
736
|
+
//#region src/nodes/reference.ts
|
|
737
|
+
const transformReferenceToLess = createFromAdapter({
|
|
738
|
+
lessType: (ref) => {
|
|
739
|
+
const refType = ref.options?.type || "variable";
|
|
740
|
+
if (refType === "property") return "Property";
|
|
741
|
+
if (refType === "function" || refType === "mixin") return "VariableCall";
|
|
742
|
+
return "Variable";
|
|
743
|
+
},
|
|
744
|
+
fields: {
|
|
745
|
+
name: (ref) => {
|
|
746
|
+
const refType = ref.options?.type || "variable";
|
|
747
|
+
const lessType = refType === "property" ? "Property" : refType === "function" || refType === "mixin" ? "VariableCall" : "Variable";
|
|
748
|
+
const key = ref.key;
|
|
749
|
+
if (typeof key === "string") {
|
|
750
|
+
if (lessType === "Variable" && !key.startsWith("@")) return `@${key}`;
|
|
751
|
+
return key;
|
|
752
|
+
}
|
|
753
|
+
return String(key);
|
|
754
|
+
},
|
|
755
|
+
value: (ref) => {
|
|
756
|
+
const refType = ref.options?.type || "variable";
|
|
757
|
+
if (refType === "function" || refType === "mixin") return ref;
|
|
758
|
+
},
|
|
759
|
+
index: (ref) => (0, _jesscss_core.sourceSpanOf)(ref)?.start,
|
|
760
|
+
currentFileInfo: () => ({})
|
|
761
|
+
}
|
|
762
|
+
});
|
|
763
|
+
//#endregion
|
|
764
|
+
//#region src/nodes/index.ts
|
|
765
|
+
function isRuleset(node) {
|
|
766
|
+
return node.type === "Ruleset";
|
|
767
|
+
}
|
|
768
|
+
function isDeclaration(node) {
|
|
769
|
+
return node.type === "Declaration";
|
|
770
|
+
}
|
|
771
|
+
function isMixin(node) {
|
|
772
|
+
return node.type === "Mixin";
|
|
773
|
+
}
|
|
774
|
+
function isDimension(node) {
|
|
775
|
+
return node.type === "Dimension";
|
|
776
|
+
}
|
|
777
|
+
function isNum(node) {
|
|
778
|
+
return node.type === "Num";
|
|
779
|
+
}
|
|
780
|
+
function isColor(node) {
|
|
781
|
+
return node.type === "Color";
|
|
782
|
+
}
|
|
783
|
+
function isOperation(node) {
|
|
784
|
+
return node.type === "Operation";
|
|
785
|
+
}
|
|
786
|
+
function isExpression(node) {
|
|
787
|
+
return node.type === "Expression";
|
|
788
|
+
}
|
|
789
|
+
function isSequence(node) {
|
|
790
|
+
return node.type === "Sequence";
|
|
791
|
+
}
|
|
792
|
+
function isQuoted(node) {
|
|
793
|
+
return node.type === "Quoted";
|
|
794
|
+
}
|
|
795
|
+
function isUrl(node) {
|
|
796
|
+
return node.type === "Url";
|
|
797
|
+
}
|
|
798
|
+
function isComment(node) {
|
|
799
|
+
return node.type === "Comment";
|
|
800
|
+
}
|
|
801
|
+
function isAtRule(node) {
|
|
802
|
+
return node.type === "AtRule";
|
|
803
|
+
}
|
|
804
|
+
function isStyleImport(node) {
|
|
805
|
+
return node.type === "StyleImport";
|
|
806
|
+
}
|
|
807
|
+
function isExtend(node) {
|
|
808
|
+
return node.type === "Extend";
|
|
809
|
+
}
|
|
810
|
+
function isCondition(node) {
|
|
811
|
+
return node.type === "Condition";
|
|
812
|
+
}
|
|
813
|
+
function isParen(node) {
|
|
814
|
+
return node.type === "Paren";
|
|
815
|
+
}
|
|
816
|
+
function isNegative(node) {
|
|
817
|
+
return node.type === "Negative";
|
|
818
|
+
}
|
|
819
|
+
function isList(node) {
|
|
820
|
+
return node.type === "List";
|
|
821
|
+
}
|
|
822
|
+
function isVarDeclaration(node) {
|
|
823
|
+
return node.type === "VarDeclaration";
|
|
824
|
+
}
|
|
825
|
+
function isKeyword(node) {
|
|
826
|
+
return node.type === "Keyword";
|
|
827
|
+
}
|
|
828
|
+
function isCombinator(node) {
|
|
829
|
+
return node.type === "Combinator";
|
|
830
|
+
}
|
|
831
|
+
function isAttributeSelector(node) {
|
|
832
|
+
return node.type === "AttributeSelector";
|
|
833
|
+
}
|
|
834
|
+
function isCall(node) {
|
|
835
|
+
return node.type === "Call";
|
|
836
|
+
}
|
|
837
|
+
function isSelector(node) {
|
|
838
|
+
return node.type === "SelectorList" || node.type === "ComplexSelector" || node.type === "CompoundSelector" || node.type === "BasicSelector";
|
|
839
|
+
}
|
|
840
|
+
function isReference(node) {
|
|
841
|
+
return node.type === "Reference";
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Registry of node transformers
|
|
845
|
+
*/
|
|
846
|
+
const transformers = /* @__PURE__ */ new Map();
|
|
847
|
+
transformers.set("Ruleset", (node, cache) => {
|
|
848
|
+
if (isRuleset(node)) return transformRulesetToLess(node, cache);
|
|
849
|
+
throw new Error(`Expected Ruleset node, got ${node.type}`);
|
|
850
|
+
});
|
|
851
|
+
transformers.set("Declaration", (node, cache) => {
|
|
852
|
+
if (isDeclaration(node)) return transformDeclarationToLess(node, cache);
|
|
853
|
+
throw new Error(`Expected Declaration node, got ${node.type}`);
|
|
854
|
+
});
|
|
855
|
+
transformers.set("Mixin", (node, cache) => {
|
|
856
|
+
if (isMixin(node)) return transformMixinToLess(node, cache);
|
|
857
|
+
throw new Error(`Expected Mixin node, got ${node.type}`);
|
|
858
|
+
});
|
|
859
|
+
transformers.set("Dimension", (node, cache) => {
|
|
860
|
+
if (isDimension(node)) return transformDimensionToLess(node, cache);
|
|
861
|
+
throw new Error(`Expected Dimension node, got ${node.type}`);
|
|
862
|
+
});
|
|
863
|
+
transformers.set("Num", (node, cache) => {
|
|
864
|
+
if (isNum(node)) return transformDimensionToLess(node, cache);
|
|
865
|
+
throw new Error(`Expected Num node, got ${node.type}`);
|
|
866
|
+
});
|
|
867
|
+
transformers.set("Color", (node, cache) => {
|
|
868
|
+
if (isColor(node)) return transformColorToLess(node, cache);
|
|
869
|
+
throw new Error(`Expected Color node, got ${node.type}`);
|
|
870
|
+
});
|
|
871
|
+
transformers.set("Operation", (node, cache) => {
|
|
872
|
+
if (isOperation(node)) return transformOperationToLess(node, cache);
|
|
873
|
+
throw new Error(`Expected Operation node, got ${node.type}`);
|
|
874
|
+
});
|
|
875
|
+
transformers.set("Expression", (node, cache) => {
|
|
876
|
+
if (isExpression(node)) return transformExpressionToLess(node, cache);
|
|
877
|
+
throw new Error(`Expected Expression node, got ${node.type}`);
|
|
878
|
+
});
|
|
879
|
+
transformers.set("Sequence", (node, cache) => {
|
|
880
|
+
if (isSequence(node)) return transformSequenceToLess(node, cache);
|
|
881
|
+
throw new Error(`Expected Sequence node, got ${node.type}`);
|
|
882
|
+
});
|
|
883
|
+
transformers.set("Quoted", (node, cache) => {
|
|
884
|
+
if (isQuoted(node)) return transformQuotedToLess(node, cache);
|
|
885
|
+
throw new Error(`Expected Quoted node, got ${node.type}`);
|
|
886
|
+
});
|
|
887
|
+
transformers.set("Url", (node, cache) => {
|
|
888
|
+
if (isUrl(node)) return transformUrlToLess(node, cache);
|
|
889
|
+
throw new Error(`Expected Url node, got ${node.type}`);
|
|
890
|
+
});
|
|
891
|
+
transformers.set("Comment", (node, cache) => {
|
|
892
|
+
if (isComment(node)) return transformCommentToLess(node, cache);
|
|
893
|
+
throw new Error(`Expected Comment node, got ${node.type}`);
|
|
894
|
+
});
|
|
895
|
+
transformers.set("AtRule", (node, cache) => {
|
|
896
|
+
if (isAtRule(node)) return transformAtRuleToLess(node, cache);
|
|
897
|
+
throw new Error(`Expected AtRule node, got ${node.type}`);
|
|
898
|
+
});
|
|
899
|
+
transformers.set("StyleImport", (node, cache) => {
|
|
900
|
+
if (isStyleImport(node)) return transformImportToLess(node, cache);
|
|
901
|
+
throw new Error(`Expected StyleImport node, got ${node.type}`);
|
|
902
|
+
});
|
|
903
|
+
transformers.set("Extend", (node, cache) => {
|
|
904
|
+
if (isExtend(node)) return transformExtendToLess(node, cache);
|
|
905
|
+
throw new Error(`Expected Extend node, got ${node.type}`);
|
|
906
|
+
});
|
|
907
|
+
transformers.set("Condition", (node, cache) => {
|
|
908
|
+
if (isCondition(node)) return transformConditionToLess(node, cache);
|
|
909
|
+
throw new Error(`Expected Condition node, got ${node.type}`);
|
|
910
|
+
});
|
|
911
|
+
transformers.set("Paren", (node, cache) => {
|
|
912
|
+
if (isParen(node)) return transformParenToLess(node, cache);
|
|
913
|
+
throw new Error(`Expected Paren node, got ${node.type}`);
|
|
914
|
+
});
|
|
915
|
+
transformers.set("Negative", (node, cache) => {
|
|
916
|
+
if (isNegative(node)) return transformNegativeToLess(node, cache);
|
|
917
|
+
throw new Error(`Expected Negative node, got ${node.type}`);
|
|
918
|
+
});
|
|
919
|
+
transformers.set("List", (node, cache) => {
|
|
920
|
+
if (isList(node)) return transformListToLess(node, cache);
|
|
921
|
+
throw new Error(`Expected List node, got ${node.type}`);
|
|
922
|
+
});
|
|
923
|
+
transformers.set("VarDeclaration", (node, cache) => {
|
|
924
|
+
if (isVarDeclaration(node)) return transformVarDeclarationToLess(node, cache);
|
|
925
|
+
throw new Error(`Expected VarDeclaration node, got ${node.type}`);
|
|
926
|
+
});
|
|
927
|
+
transformers.set("Keyword", (node, cache) => {
|
|
928
|
+
if (isKeyword(node)) return transformKeywordToLess(node, cache);
|
|
929
|
+
throw new Error(`Expected Keyword node, got ${node.type}`);
|
|
930
|
+
});
|
|
931
|
+
transformers.set("Combinator", (node, cache) => {
|
|
932
|
+
if (isCombinator(node)) return transformCombinatorToLess(node, cache);
|
|
933
|
+
throw new Error(`Expected Combinator node, got ${node.type}`);
|
|
934
|
+
});
|
|
935
|
+
transformers.set("AttributeSelector", (node, cache) => {
|
|
936
|
+
if (isAttributeSelector(node)) return transformAttributeSelectorToLess(node, cache);
|
|
937
|
+
throw new Error(`Expected AttributeSelector node, got ${node.type}`);
|
|
938
|
+
});
|
|
939
|
+
transformers.set("Call", (node, cache) => {
|
|
940
|
+
if (isCall(node)) return transformCallToLess(node, cache);
|
|
941
|
+
throw new Error(`Expected Call node, got ${node.type}`);
|
|
942
|
+
});
|
|
943
|
+
transformers.set("SelectorList", (node, cache) => {
|
|
944
|
+
if (isSelector(node)) return transformSelectorToLess(node, cache);
|
|
945
|
+
throw new Error(`Expected Selector node, got ${node.type}`);
|
|
946
|
+
});
|
|
947
|
+
transformers.set("ComplexSelector", (node, cache) => {
|
|
948
|
+
if (isSelector(node)) return transformSelectorToLess(node, cache);
|
|
949
|
+
throw new Error(`Expected Selector node, got ${node.type}`);
|
|
950
|
+
});
|
|
951
|
+
transformers.set("CompoundSelector", (node, cache) => {
|
|
952
|
+
if (isSelector(node)) return transformSelectorToLess(node, cache);
|
|
953
|
+
throw new Error(`Expected Selector node, got ${node.type}`);
|
|
954
|
+
});
|
|
955
|
+
transformers.set("BasicSelector", (node, cache) => {
|
|
956
|
+
if (isSelector(node)) return transformSelectorToLess(node, cache);
|
|
957
|
+
throw new Error(`Expected Selector node, got ${node.type}`);
|
|
958
|
+
});
|
|
959
|
+
transformers.set("Reference", (node, cache) => {
|
|
960
|
+
if (isReference(node)) return transformReferenceToLess(node, cache);
|
|
961
|
+
throw new Error(`Expected Reference node, got ${node.type}`);
|
|
962
|
+
});
|
|
963
|
+
/**
|
|
964
|
+
* Get the transformer for a Jess node type
|
|
965
|
+
*/
|
|
966
|
+
function getTransformer(nodeType) {
|
|
967
|
+
return transformers.get(nodeType);
|
|
968
|
+
}
|
|
969
|
+
//#endregion
|
|
970
|
+
//#region src/transform/to-less.ts
|
|
971
|
+
/**
|
|
972
|
+
* Convert a Jess node to a Less-compatible adapter
|
|
973
|
+
*
|
|
974
|
+
* @param jessNode - The Jess node to convert
|
|
975
|
+
* @param options - Conversion options
|
|
976
|
+
* @returns A Less-compatible adapter node
|
|
977
|
+
*/
|
|
978
|
+
function toLessNode(jessNode, options) {
|
|
979
|
+
if (!jessNode) return jessNode;
|
|
980
|
+
const cache = options?.cache || /* @__PURE__ */ new WeakMap();
|
|
981
|
+
if (cache.has(jessNode)) return cache.get(jessNode);
|
|
982
|
+
if (require_less_adapter.isAdaptingNode(jessNode)) return jessNode;
|
|
983
|
+
const transformer = getTransformer(jessNode.type);
|
|
984
|
+
if (transformer) return transformer(jessNode, cache);
|
|
985
|
+
return require_less_adapter.createLessAdapter(jessNode, {
|
|
986
|
+
lessType: require_less_adapter.mapJessTypeToLessType(jessNode.type),
|
|
987
|
+
fields: { value: (target) => {
|
|
988
|
+
if (!("value" in target) || target.value === void 0) return;
|
|
989
|
+
const nodeValue = target.value;
|
|
990
|
+
if (nodeValue instanceof _jesscss_core.Node) return toLessNode(nodeValue, options);
|
|
991
|
+
if (Array.isArray(nodeValue)) return nodeValue.map((item) => item instanceof _jesscss_core.Node ? toLessNode(item, options) : item);
|
|
992
|
+
if (typeof nodeValue === "object" && nodeValue !== null) {
|
|
993
|
+
const converted = {};
|
|
994
|
+
for (const [key, val] of Object.entries(nodeValue)) if (val instanceof _jesscss_core.Node) converted[key] = toLessNode(val, options);
|
|
995
|
+
else if (Array.isArray(val)) converted[key] = val.map((item) => item instanceof _jesscss_core.Node ? toLessNode(item, options) : item);
|
|
996
|
+
else converted[key] = val;
|
|
997
|
+
return converted;
|
|
998
|
+
}
|
|
999
|
+
return nodeValue;
|
|
1000
|
+
} },
|
|
1001
|
+
accept: (target) => target
|
|
1002
|
+
}, cache);
|
|
1003
|
+
}
|
|
1004
|
+
/**
|
|
1005
|
+
* Convert an entire Jess Rules tree to Less-compatible format
|
|
1006
|
+
*
|
|
1007
|
+
* @param jessRules - The Jess Rules tree to convert
|
|
1008
|
+
* @param options - Conversion options
|
|
1009
|
+
* @returns A Less-compatible tree
|
|
1010
|
+
*/
|
|
1011
|
+
function toLessTree(jessRules, options) {
|
|
1012
|
+
if (jessRules instanceof _jesscss_core.Rules) return toLessNode(jessRules, options);
|
|
1013
|
+
return toLessNode(jessRules, options);
|
|
1014
|
+
}
|
|
1015
|
+
//#endregion
|
|
1016
|
+
Object.defineProperty(exports, "fromLessNode", {
|
|
1017
|
+
enumerable: true,
|
|
1018
|
+
get: function() {
|
|
1019
|
+
return fromLessNode;
|
|
1020
|
+
}
|
|
1021
|
+
});
|
|
1022
|
+
Object.defineProperty(exports, "fromLessPluginReturnValue", {
|
|
1023
|
+
enumerable: true,
|
|
1024
|
+
get: function() {
|
|
1025
|
+
return fromLessPluginReturnValue;
|
|
1026
|
+
}
|
|
1027
|
+
});
|
|
1028
|
+
Object.defineProperty(exports, "fromLessTree", {
|
|
1029
|
+
enumerable: true,
|
|
1030
|
+
get: function() {
|
|
1031
|
+
return fromLessTree;
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
Object.defineProperty(exports, "toLessNode", {
|
|
1035
|
+
enumerable: true,
|
|
1036
|
+
get: function() {
|
|
1037
|
+
return toLessNode;
|
|
1038
|
+
}
|
|
1039
|
+
});
|
|
1040
|
+
Object.defineProperty(exports, "toLessTree", {
|
|
1041
|
+
enumerable: true,
|
|
1042
|
+
get: function() {
|
|
1043
|
+
return toLessTree;
|
|
1044
|
+
}
|
|
1045
|
+
});
|