@linaria/react 4.1.4 → 4.2.0
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/dist/index.js +132 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +99 -0
- package/dist/index.mjs.map +1 -0
- package/dist/processors/styled.js +274 -0
- package/dist/processors/styled.js.map +1 -0
- package/dist/processors/styled.mjs +260 -0
- package/dist/processors/styled.mjs.map +1 -0
- package/package.json +23 -15
- package/processors/styled.js +1 -1
- package/types/processors/styled.d.ts +6 -3
- package/esm/index.js +0 -2
- package/esm/index.js.map +0 -1
- package/esm/processors/styled.js +0 -205
- package/esm/processors/styled.js.map +0 -1
- package/esm/styled.js +0 -130
- package/esm/styled.js.map +0 -1
- package/lib/index.js +0 -11
- package/lib/index.js.map +0 -1
- package/lib/processors/styled.js +0 -216
- package/lib/processors/styled.js.map +0 -1
- package/lib/styled.js +0 -143
- package/lib/styled.js.map +0 -1
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
var __accessCheck = (obj, member, msg) => {
|
|
8
|
+
if (!member.has(obj))
|
|
9
|
+
throw TypeError("Cannot " + msg);
|
|
10
|
+
};
|
|
11
|
+
var __privateGet = (obj, member, getter) => {
|
|
12
|
+
__accessCheck(obj, member, "read from private field");
|
|
13
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
14
|
+
};
|
|
15
|
+
var __privateAdd = (obj, member, value) => {
|
|
16
|
+
if (member.has(obj))
|
|
17
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
18
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
19
|
+
};
|
|
20
|
+
var __privateSet = (obj, member, value, setter) => {
|
|
21
|
+
__accessCheck(obj, member, "write to private field");
|
|
22
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
23
|
+
return value;
|
|
24
|
+
};
|
|
25
|
+
var __privateWrapper = (obj, member, setter, getter) => ({
|
|
26
|
+
set _(value) {
|
|
27
|
+
__privateSet(obj, member, value, setter);
|
|
28
|
+
},
|
|
29
|
+
get _() {
|
|
30
|
+
return __privateGet(obj, member, getter);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// src/processors/styled.ts
|
|
35
|
+
import {
|
|
36
|
+
buildSlug,
|
|
37
|
+
hasMeta,
|
|
38
|
+
TaggedTemplateProcessor,
|
|
39
|
+
validateParams,
|
|
40
|
+
ValueType,
|
|
41
|
+
toValidCSSIdentifier
|
|
42
|
+
} from "@linaria/tags";
|
|
43
|
+
import { slugify } from "@linaria/utils";
|
|
44
|
+
var isNotNull = (x) => x !== null;
|
|
45
|
+
var singleQuotedStringLiteral = (value) => ({
|
|
46
|
+
type: "StringLiteral",
|
|
47
|
+
value,
|
|
48
|
+
extra: {
|
|
49
|
+
rawValue: value,
|
|
50
|
+
raw: `'${value}'`
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
var _variableIdx, _variablesCache;
|
|
54
|
+
var StyledProcessor = class extends TaggedTemplateProcessor {
|
|
55
|
+
constructor(params, ...args) {
|
|
56
|
+
validateParams(
|
|
57
|
+
params,
|
|
58
|
+
["tag", ["call", "member"], ["template", "call"]],
|
|
59
|
+
"Invalid usage of `styled` tag"
|
|
60
|
+
);
|
|
61
|
+
const [tag, tagOp, template] = params;
|
|
62
|
+
if (template[0] === "call") {
|
|
63
|
+
throw TaggedTemplateProcessor.SKIP;
|
|
64
|
+
}
|
|
65
|
+
super([tag, template], ...args);
|
|
66
|
+
__publicField(this, "component");
|
|
67
|
+
__privateAdd(this, _variableIdx, 0);
|
|
68
|
+
__privateAdd(this, _variablesCache, /* @__PURE__ */ new Map());
|
|
69
|
+
let component;
|
|
70
|
+
if (tagOp[0] === "call" && tagOp.length === 2) {
|
|
71
|
+
const value = tagOp[1];
|
|
72
|
+
if (value.kind === ValueType.FUNCTION) {
|
|
73
|
+
component = "FunctionalComponent";
|
|
74
|
+
} else {
|
|
75
|
+
component = {
|
|
76
|
+
node: value.ex,
|
|
77
|
+
source: value.source
|
|
78
|
+
};
|
|
79
|
+
this.dependencies.push(value);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (tagOp[0] === "member") {
|
|
83
|
+
[, component] = tagOp;
|
|
84
|
+
}
|
|
85
|
+
if (!component) {
|
|
86
|
+
throw new Error("Invalid usage of `styled` tag");
|
|
87
|
+
}
|
|
88
|
+
this.component = component;
|
|
89
|
+
}
|
|
90
|
+
addInterpolation(node, precedingCss, source, unit = "") {
|
|
91
|
+
const id = this.getVariableId(source, unit, precedingCss);
|
|
92
|
+
this.interpolations.push({
|
|
93
|
+
id,
|
|
94
|
+
node,
|
|
95
|
+
source,
|
|
96
|
+
unit
|
|
97
|
+
});
|
|
98
|
+
return id;
|
|
99
|
+
}
|
|
100
|
+
doEvaltimeReplacement() {
|
|
101
|
+
this.replacer(this.value, false);
|
|
102
|
+
}
|
|
103
|
+
doRuntimeReplacement() {
|
|
104
|
+
const t = this.astService;
|
|
105
|
+
const props = this.getProps();
|
|
106
|
+
this.replacer(
|
|
107
|
+
t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]),
|
|
108
|
+
true
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
extractRules(valueCache, cssText, loc) {
|
|
112
|
+
const rules = {};
|
|
113
|
+
let selector = `.${this.className}`;
|
|
114
|
+
let value = typeof this.component === "string" ? null : valueCache.get(this.component.node.name);
|
|
115
|
+
while (hasMeta(value)) {
|
|
116
|
+
selector += `.${value.__linaria.className}`;
|
|
117
|
+
value = value.__linaria.extends;
|
|
118
|
+
}
|
|
119
|
+
rules[selector] = {
|
|
120
|
+
cssText,
|
|
121
|
+
className: this.className,
|
|
122
|
+
displayName: this.displayName,
|
|
123
|
+
start: (loc == null ? void 0 : loc.start) ?? null
|
|
124
|
+
};
|
|
125
|
+
return rules;
|
|
126
|
+
}
|
|
127
|
+
get asSelector() {
|
|
128
|
+
return `.${this.className}`;
|
|
129
|
+
}
|
|
130
|
+
get tagExpressionArgument() {
|
|
131
|
+
const t = this.astService;
|
|
132
|
+
if (typeof this.component === "string") {
|
|
133
|
+
if (this.component === "FunctionalComponent") {
|
|
134
|
+
return t.arrowFunctionExpression([], t.blockStatement([]));
|
|
135
|
+
}
|
|
136
|
+
return singleQuotedStringLiteral(this.component);
|
|
137
|
+
}
|
|
138
|
+
return t.callExpression(t.identifier(this.component.node.name), []);
|
|
139
|
+
}
|
|
140
|
+
get tagExpression() {
|
|
141
|
+
const t = this.astService;
|
|
142
|
+
return t.callExpression(this.tag, [this.tagExpressionArgument]);
|
|
143
|
+
}
|
|
144
|
+
get value() {
|
|
145
|
+
const t = this.astService;
|
|
146
|
+
const extendsNode = typeof this.component === "string" ? null : this.component.node.name;
|
|
147
|
+
return t.objectExpression([
|
|
148
|
+
t.objectProperty(
|
|
149
|
+
t.stringLiteral("displayName"),
|
|
150
|
+
t.stringLiteral(this.displayName)
|
|
151
|
+
),
|
|
152
|
+
t.objectProperty(
|
|
153
|
+
t.stringLiteral("__linaria"),
|
|
154
|
+
t.objectExpression([
|
|
155
|
+
t.objectProperty(
|
|
156
|
+
t.stringLiteral("className"),
|
|
157
|
+
t.stringLiteral(this.className)
|
|
158
|
+
),
|
|
159
|
+
t.objectProperty(
|
|
160
|
+
t.stringLiteral("extends"),
|
|
161
|
+
extendsNode ? t.callExpression(t.identifier(extendsNode), []) : t.nullLiteral()
|
|
162
|
+
)
|
|
163
|
+
])
|
|
164
|
+
)
|
|
165
|
+
]);
|
|
166
|
+
}
|
|
167
|
+
toString() {
|
|
168
|
+
const res = (arg) => `${this.tagSourceCode()}(${arg})\`\u2026\``;
|
|
169
|
+
if (typeof this.component === "string") {
|
|
170
|
+
if (this.component === "FunctionalComponent") {
|
|
171
|
+
return res("() => {\u2026}");
|
|
172
|
+
}
|
|
173
|
+
return res(`'${this.component}'`);
|
|
174
|
+
}
|
|
175
|
+
return res(this.component.source);
|
|
176
|
+
}
|
|
177
|
+
getCustomVariableId(source, unit, precedingCss) {
|
|
178
|
+
const context = this.getVariableContext(source, unit, precedingCss);
|
|
179
|
+
const customSlugFn = this.options.variableNameSlug;
|
|
180
|
+
if (!customSlugFn) {
|
|
181
|
+
return void 0;
|
|
182
|
+
}
|
|
183
|
+
return typeof customSlugFn === "function" ? customSlugFn(context) : buildSlug(customSlugFn, context);
|
|
184
|
+
}
|
|
185
|
+
getProps() {
|
|
186
|
+
const propsObj = {
|
|
187
|
+
name: this.displayName,
|
|
188
|
+
class: this.className
|
|
189
|
+
};
|
|
190
|
+
if (this.interpolations.length) {
|
|
191
|
+
propsObj.vars = {};
|
|
192
|
+
this.interpolations.forEach(({ id, unit, node }) => {
|
|
193
|
+
const items = [this.astService.callExpression(node, [])];
|
|
194
|
+
if (unit) {
|
|
195
|
+
items.push(this.astService.stringLiteral(unit));
|
|
196
|
+
}
|
|
197
|
+
propsObj.vars[id] = items;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return propsObj;
|
|
201
|
+
}
|
|
202
|
+
getTagComponentProps(props) {
|
|
203
|
+
const t = this.astService;
|
|
204
|
+
const propExpressions = Object.entries(props).map(([key, value]) => {
|
|
205
|
+
if (!value) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
const keyNode = t.identifier(key);
|
|
209
|
+
if (typeof value === "string") {
|
|
210
|
+
return t.objectProperty(keyNode, t.stringLiteral(value));
|
|
211
|
+
}
|
|
212
|
+
if (typeof value === "boolean") {
|
|
213
|
+
return t.objectProperty(keyNode, t.booleanLiteral(value));
|
|
214
|
+
}
|
|
215
|
+
const vars = Object.entries(value).map(([propName, propValue]) => {
|
|
216
|
+
return t.objectProperty(
|
|
217
|
+
t.stringLiteral(propName),
|
|
218
|
+
t.arrayExpression(propValue)
|
|
219
|
+
);
|
|
220
|
+
});
|
|
221
|
+
return t.objectProperty(keyNode, t.objectExpression(vars));
|
|
222
|
+
}).filter(isNotNull);
|
|
223
|
+
return t.objectExpression(propExpressions);
|
|
224
|
+
}
|
|
225
|
+
getVariableContext(source, unit, precedingCss) {
|
|
226
|
+
const getIndex = () => {
|
|
227
|
+
return __privateWrapper(this, _variableIdx)._++;
|
|
228
|
+
};
|
|
229
|
+
return {
|
|
230
|
+
componentName: this.displayName,
|
|
231
|
+
componentSlug: this.slug,
|
|
232
|
+
get index() {
|
|
233
|
+
return getIndex();
|
|
234
|
+
},
|
|
235
|
+
precedingCss,
|
|
236
|
+
processor: this.constructor.name,
|
|
237
|
+
source,
|
|
238
|
+
unit,
|
|
239
|
+
valueSlug: slugify(source + unit)
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
getVariableId(source, unit, precedingCss) {
|
|
243
|
+
const value = source + unit;
|
|
244
|
+
if (!__privateGet(this, _variablesCache).has(value)) {
|
|
245
|
+
const id = this.getCustomVariableId(source, unit, precedingCss);
|
|
246
|
+
if (id) {
|
|
247
|
+
return toValidCSSIdentifier(id);
|
|
248
|
+
}
|
|
249
|
+
const context = this.getVariableContext(source, unit, precedingCss);
|
|
250
|
+
__privateGet(this, _variablesCache).set(value, `${this.slug}-${context.index}`);
|
|
251
|
+
}
|
|
252
|
+
return __privateGet(this, _variablesCache).get(value);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
_variableIdx = new WeakMap();
|
|
256
|
+
_variablesCache = new WeakMap();
|
|
257
|
+
export {
|
|
258
|
+
StyledProcessor as default
|
|
259
|
+
};
|
|
260
|
+
//# sourceMappingURL=styled.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/processors/styled.ts"],"sourcesContent":["import type {\n CallExpression,\n Expression,\n ObjectExpression,\n SourceLocation,\n StringLiteral,\n} from '@babel/types';\n\nimport type {\n Params,\n Rules,\n TailProcessorParams,\n ValueCache,\n WrappedNode,\n} from '@linaria/tags';\nimport {\n buildSlug,\n hasMeta,\n TaggedTemplateProcessor,\n validateParams,\n ValueType,\n toValidCSSIdentifier,\n} from '@linaria/tags';\nimport type { IVariableContext } from '@linaria/utils';\nimport { slugify } from '@linaria/utils';\n\nconst isNotNull = <T>(x: T | null): x is T => x !== null;\n\nexport interface IProps {\n atomic?: boolean;\n class?: string;\n name: string;\n vars?: Record<string, Expression[]>;\n}\n\nconst singleQuotedStringLiteral = (value: string): StringLiteral => ({\n type: 'StringLiteral',\n value,\n extra: {\n rawValue: value,\n raw: `'${value}'`,\n },\n});\n\nexport default class StyledProcessor extends TaggedTemplateProcessor {\n public component: WrappedNode;\n\n #variableIdx = 0;\n\n #variablesCache = new Map<string, string>();\n\n constructor(params: Params, ...args: TailProcessorParams) {\n validateParams(\n params,\n ['tag', ['call', 'member'], ['template', 'call']],\n 'Invalid usage of `styled` tag'\n );\n\n const [tag, tagOp, template] = params;\n\n if (template[0] === 'call') {\n // It is already transformed styled-literal. Skip it.\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw TaggedTemplateProcessor.SKIP;\n }\n\n super([tag, template], ...args);\n\n let component: WrappedNode | undefined;\n if (tagOp[0] === 'call' && tagOp.length === 2) {\n const value = tagOp[1];\n if (value.kind === ValueType.FUNCTION) {\n component = 'FunctionalComponent';\n } else {\n component = {\n node: value.ex,\n source: value.source,\n };\n\n this.dependencies.push(value);\n }\n }\n\n if (tagOp[0] === 'member') {\n [, component] = tagOp;\n }\n\n if (!component) {\n throw new Error('Invalid usage of `styled` tag');\n }\n\n this.component = component;\n }\n\n public override addInterpolation(\n node: Expression,\n precedingCss: string,\n source: string,\n unit = ''\n ): string {\n const id = this.getVariableId(source, unit, precedingCss);\n\n this.interpolations.push({\n id,\n node,\n source,\n unit,\n });\n\n return id;\n }\n\n public override doEvaltimeReplacement(): void {\n this.replacer(this.value, false);\n }\n\n public override doRuntimeReplacement(): void {\n const t = this.astService;\n\n const props = this.getProps();\n\n this.replacer(\n t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]),\n true\n );\n }\n\n public override extractRules(\n valueCache: ValueCache,\n cssText: string,\n loc?: SourceLocation | null\n ): Rules {\n const rules: Rules = {};\n\n let selector = `.${this.className}`;\n\n // If `styled` wraps another component and not a primitive,\n // get its class name to create a more specific selector\n // it'll ensure that styles are overridden properly\n let value =\n typeof this.component === 'string'\n ? null\n : valueCache.get(this.component.node.name);\n while (hasMeta(value)) {\n selector += `.${value.__linaria.className}`;\n value = value.__linaria.extends;\n }\n\n rules[selector] = {\n cssText,\n className: this.className,\n displayName: this.displayName,\n start: loc?.start ?? null,\n };\n\n return rules;\n }\n\n public override get asSelector(): string {\n return `.${this.className}`;\n }\n\n protected get tagExpressionArgument(): Expression {\n const t = this.astService;\n if (typeof this.component === 'string') {\n if (this.component === 'FunctionalComponent') {\n return t.arrowFunctionExpression([], t.blockStatement([]));\n }\n\n return singleQuotedStringLiteral(this.component);\n }\n\n return t.callExpression(t.identifier(this.component.node.name), []);\n }\n\n protected get tagExpression(): CallExpression {\n const t = this.astService;\n return t.callExpression(this.tag, [this.tagExpressionArgument]);\n }\n\n public override get value(): ObjectExpression {\n const t = this.astService;\n const extendsNode =\n typeof this.component === 'string' ? null : this.component.node.name;\n\n return t.objectExpression([\n t.objectProperty(\n t.stringLiteral('displayName'),\n t.stringLiteral(this.displayName)\n ),\n t.objectProperty(\n t.stringLiteral('__linaria'),\n t.objectExpression([\n t.objectProperty(\n t.stringLiteral('className'),\n t.stringLiteral(this.className)\n ),\n t.objectProperty(\n t.stringLiteral('extends'),\n extendsNode\n ? t.callExpression(t.identifier(extendsNode), [])\n : t.nullLiteral()\n ),\n ])\n ),\n ]);\n }\n\n public override toString(): string {\n const res = (arg: string) => `${this.tagSourceCode()}(${arg})\\`…\\``;\n\n if (typeof this.component === 'string') {\n if (this.component === 'FunctionalComponent') {\n return res('() => {…}');\n }\n\n return res(`'${this.component}'`);\n }\n\n return res(this.component.source);\n }\n\n protected getCustomVariableId(\n source: string,\n unit: string,\n precedingCss: string\n ) {\n const context = this.getVariableContext(source, unit, precedingCss);\n const customSlugFn = this.options.variableNameSlug;\n if (!customSlugFn) {\n return undefined;\n }\n\n return typeof customSlugFn === 'function'\n ? customSlugFn(context)\n : buildSlug(customSlugFn, context);\n }\n\n protected getProps(): IProps {\n const propsObj: IProps = {\n name: this.displayName,\n class: this.className,\n };\n\n // If we found any interpolations, also pass them, so they can be applied\n if (this.interpolations.length) {\n propsObj.vars = {};\n this.interpolations.forEach(({ id, unit, node }) => {\n const items: Expression[] = [this.astService.callExpression(node, [])];\n\n if (unit) {\n items.push(this.astService.stringLiteral(unit));\n }\n\n propsObj.vars![id] = items;\n });\n }\n\n return propsObj;\n }\n\n protected getTagComponentProps(props: IProps): ObjectExpression {\n const t = this.astService;\n\n const propExpressions = Object.entries(props)\n .map(([key, value]: [key: string, value: IProps[keyof IProps]]) => {\n if (!value) {\n return null;\n }\n\n const keyNode = t.identifier(key);\n\n if (typeof value === 'string') {\n return t.objectProperty(keyNode, t.stringLiteral(value));\n }\n\n if (typeof value === 'boolean') {\n return t.objectProperty(keyNode, t.booleanLiteral(value));\n }\n\n const vars = Object.entries(value).map(([propName, propValue]) => {\n return t.objectProperty(\n t.stringLiteral(propName),\n t.arrayExpression(propValue)\n );\n });\n\n return t.objectProperty(keyNode, t.objectExpression(vars));\n })\n .filter(isNotNull);\n\n return t.objectExpression(propExpressions);\n }\n\n protected getVariableContext(\n source: string,\n unit: string,\n precedingCss: string\n ): IVariableContext {\n const getIndex = () => {\n // eslint-disable-next-line no-plusplus\n return this.#variableIdx++;\n };\n\n return {\n componentName: this.displayName,\n componentSlug: this.slug,\n get index() {\n return getIndex();\n },\n precedingCss,\n processor: this.constructor.name,\n source,\n unit,\n valueSlug: slugify(source + unit),\n };\n }\n\n protected getVariableId(\n source: string,\n unit: string,\n precedingCss: string\n ): string {\n const value = source + unit;\n if (!this.#variablesCache.has(value)) {\n const id = this.getCustomVariableId(source, unit, precedingCss);\n if (id) {\n return toValidCSSIdentifier(id);\n }\n\n const context = this.getVariableContext(source, unit, precedingCss);\n\n // make the variable unique to this styled component\n this.#variablesCache.set(value, `${this.slug}-${context.index}`);\n }\n\n return this.#variablesCache.get(value)!;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAeA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,eAAe;AAExB,IAAM,YAAY,CAAI,MAAwB,MAAM;AASpD,IAAM,4BAA4B,CAAC,WAAkC;AAAA,EACnE,MAAM;AAAA,EACN;AAAA,EACA,OAAO;AAAA,IACL,UAAU;AAAA,IACV,KAAK,IAAI;AAAA,EACX;AACF;AA1CA;AA4CA,IAAqB,kBAArB,cAA6C,wBAAwB;AAAA,EAOnE,YAAY,WAAmB,MAA2B;AACxD;AAAA,MACE;AAAA,MACA,CAAC,OAAO,CAAC,QAAQ,QAAQ,GAAG,CAAC,YAAY,MAAM,CAAC;AAAA,MAChD;AAAA,IACF;AAEA,UAAM,CAAC,KAAK,OAAO,QAAQ,IAAI;AAE/B,QAAI,SAAS,OAAO,QAAQ;AAG1B,YAAM,wBAAwB;AAAA,IAChC;AAEA,UAAM,CAAC,KAAK,QAAQ,GAAG,GAAG,IAAI;AArBhC,wBAAO;AAEP,qCAAe;AAEf,wCAAkB,oBAAI,IAAoB;AAmBxC,QAAI;AACJ,QAAI,MAAM,OAAO,UAAU,MAAM,WAAW,GAAG;AAC7C,YAAM,QAAQ,MAAM;AACpB,UAAI,MAAM,SAAS,UAAU,UAAU;AACrC,oBAAY;AAAA,MACd,OAAO;AACL,oBAAY;AAAA,UACV,MAAM,MAAM;AAAA,UACZ,QAAQ,MAAM;AAAA,QAChB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ChE,eAAO,EAAE;AAAA,UACP,EAAE,cAAc,QAAQ;AAAA,UACxB,EAAE,gBAAgB,SAAS;AAAA,QAC7B;AAAA,MACF,CAAC;AAED,aAAO,EAAE,eAAe,SAAS,EAAE,iBAAiB,IAAI,CAAC;AAAA,IAC3D,CAAC,EACA,OAAO,SAAS;AAEnB,WAAO,EAAE,iBAAiB,eAAe;AAAA,EAC3C;AAAA,EAEU,mBACR,QACA,MACA,cACkB;AAClB,UAAM,WAAW,MAAM;AAErB,aAAO,uBAAK,cAAL;AAAA,IACT;AAEA,WAAO;AAAA,MACL,eAAe,KAAK;AAAA,MACpB,eAAe,KAAK;AAAA,MACpB,IAAI,QAAQ;AACV,eAAO,SAAS;AAAA,MAClB;AAAA,MACA;AAAA,MACA,WAAW,KAAK,YAAY;AAAA,MAC5B;AAAA,MACA;AAAA,MACA,WAAW,QAAQ,SAAS,IAAI;AAAA,IAClC;AAAA,EACF;AAAA,EAEU,cACR,QACA,MACA,cACQ;AACR,UAAM,QAAQ,SAAS;AACvB,QAAI,CAAC,mBAAK,iBAAgB,IAAI,KAAK,GAAG;AACpC,YAAM,KAAK,KAAK,oBAAoB,QAAQ,MAAM,YAAY;AAC9D,UAAI,IAAI;AACN,eAAO,qBAAqB,EAAE;AAAA,MAChC;AAEA,YAAM,UAAU,KAAK,mBAAmB,QAAQ,MAAM,YAAY;AAGlE,yBAAK,iBAAgB,IAAI,OAAO,GAAG,KAAK,QAAQ,QAAQ,OAAO;AAAA,IACjE;AAEA,WAAO,mBAAK,iBAAgB,IAAI,KAAK;AAAA,EACvC;AACF;AAnSE;AAEA;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@linaria/react",
|
|
3
3
|
"description": "Blazing fast zero-runtime CSS in JS library",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.2.0",
|
|
5
5
|
"bugs": "https://github.com/callstack/linaria/issues",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@emotion/is-prop-valid": "^0.8.8",
|
|
8
|
-
"@linaria/core": "^4.
|
|
9
|
-
"@linaria/tags": "^4.1.
|
|
8
|
+
"@linaria/core": "^4.2.0",
|
|
9
|
+
"@linaria/tags": "^4.1.4",
|
|
10
|
+
"@linaria/utils": "^4.2.2",
|
|
10
11
|
"ts-invariant": "^0.10.3"
|
|
11
12
|
},
|
|
12
13
|
"devDependencies": {
|
|
@@ -24,18 +25,17 @@
|
|
|
24
25
|
"./package.json": "./package.json",
|
|
25
26
|
".": {
|
|
26
27
|
"types": "./types/index.d.ts",
|
|
27
|
-
"import": "./
|
|
28
|
-
"default": "./
|
|
28
|
+
"import": "./dist/index.mjs",
|
|
29
|
+
"default": "./dist/index.js"
|
|
29
30
|
},
|
|
30
31
|
"./*": {
|
|
31
32
|
"types": "./types/*.d.ts",
|
|
32
|
-
"import": "./
|
|
33
|
-
"default": "./
|
|
33
|
+
"import": "./dist/*.mjs",
|
|
34
|
+
"default": "./dist/*.js"
|
|
34
35
|
}
|
|
35
36
|
},
|
|
36
37
|
"files": [
|
|
37
|
-
"
|
|
38
|
-
"lib/",
|
|
38
|
+
"dist/",
|
|
39
39
|
"processors/",
|
|
40
40
|
"types/"
|
|
41
41
|
],
|
|
@@ -50,11 +50,11 @@
|
|
|
50
50
|
"license": "MIT",
|
|
51
51
|
"linaria": {
|
|
52
52
|
"tags": {
|
|
53
|
-
"styled": "./
|
|
53
|
+
"styled": "./dist/processors/styled.js"
|
|
54
54
|
}
|
|
55
55
|
},
|
|
56
|
-
"main": "
|
|
57
|
-
"module": "
|
|
56
|
+
"main": "dist/index.js",
|
|
57
|
+
"module": "dist/index.mjs",
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"react": ">=16"
|
|
60
60
|
},
|
|
@@ -63,6 +63,15 @@
|
|
|
63
63
|
},
|
|
64
64
|
"repository": "git@github.com:callstack/linaria.git",
|
|
65
65
|
"sideEffects": false,
|
|
66
|
+
"tsup": {
|
|
67
|
+
"entry": [
|
|
68
|
+
"src/index.ts",
|
|
69
|
+
"src/processors/styled.ts"
|
|
70
|
+
],
|
|
71
|
+
"splitting": false,
|
|
72
|
+
"sourcemap": true,
|
|
73
|
+
"clean": true
|
|
74
|
+
},
|
|
66
75
|
"types": "types/index.d.ts",
|
|
67
76
|
"typesVersions": {
|
|
68
77
|
"*": {
|
|
@@ -72,11 +81,10 @@
|
|
|
72
81
|
}
|
|
73
82
|
},
|
|
74
83
|
"scripts": {
|
|
75
|
-
"build": "npm run build:
|
|
84
|
+
"build": "npm run build:dist && npm run build:declarations",
|
|
76
85
|
"build:corejs-test": "cross-env NODE_ENV=legacy babel src --out-dir lib --extensions '.js,.jsx,.ts,.tsx' --ignore \"src/processors/**/*\"",
|
|
77
86
|
"build:declarations": "tsc --emitDeclarationOnly --outDir types",
|
|
78
|
-
"build:
|
|
79
|
-
"build:lib": "cross-env NODE_ENV=legacy babel src --out-dir lib --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
|
|
87
|
+
"build:dist": "tsup --format cjs,esm",
|
|
80
88
|
"test": "jest --config ../../jest.config.js --rootDir .",
|
|
81
89
|
"test:dts": "dtslint --localTs ../../node_modules/typescript/lib __dtslint__",
|
|
82
90
|
"typecheck": "tsc --noEmit --composite false",
|
package/processors/styled.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { CallExpression, Expression, ObjectExpression, SourceLocation } from '@babel/types';
|
|
2
|
-
import type { Rules,
|
|
2
|
+
import type { Params, Rules, TailProcessorParams, ValueCache, WrappedNode } from '@linaria/tags';
|
|
3
3
|
import { TaggedTemplateProcessor } from '@linaria/tags';
|
|
4
|
+
import type { IVariableContext } from '@linaria/utils';
|
|
4
5
|
export interface IProps {
|
|
5
6
|
atomic?: boolean;
|
|
6
7
|
class?: string;
|
|
@@ -11,7 +12,7 @@ export default class StyledProcessor extends TaggedTemplateProcessor {
|
|
|
11
12
|
#private;
|
|
12
13
|
component: WrappedNode;
|
|
13
14
|
constructor(params: Params, ...args: TailProcessorParams);
|
|
14
|
-
addInterpolation(node: Expression, source: string, unit?: string): string;
|
|
15
|
+
addInterpolation(node: Expression, precedingCss: string, source: string, unit?: string): string;
|
|
15
16
|
doEvaltimeReplacement(): void;
|
|
16
17
|
doRuntimeReplacement(): void;
|
|
17
18
|
extractRules(valueCache: ValueCache, cssText: string, loc?: SourceLocation | null): Rules;
|
|
@@ -20,7 +21,9 @@ export default class StyledProcessor extends TaggedTemplateProcessor {
|
|
|
20
21
|
protected get tagExpression(): CallExpression;
|
|
21
22
|
get value(): ObjectExpression;
|
|
22
23
|
toString(): string;
|
|
24
|
+
protected getCustomVariableId(source: string, unit: string, precedingCss: string): string | undefined;
|
|
23
25
|
protected getProps(): IProps;
|
|
24
26
|
protected getTagComponentProps(props: IProps): ObjectExpression;
|
|
25
|
-
protected
|
|
27
|
+
protected getVariableContext(source: string, unit: string, precedingCss: string): IVariableContext;
|
|
28
|
+
protected getVariableId(source: string, unit: string, precedingCss: string): string;
|
|
26
29
|
}
|
package/esm/index.js
DELETED
package/esm/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["default","styled"],"sources":["../src/index.ts"],"sourcesContent":["export { default as styled } from './styled';\nexport type { StyledJSXIntrinsics, Styled } from './styled';\nexport type { CSSProperties } from '@linaria/core';\nexport type { StyledMeta } from '@linaria/tags';\n"],"mappings":"AAAA,SAASA,OAAO,IAAIC,MAApB,QAAkC,UAAlC"}
|
package/esm/processors/styled.js
DELETED
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import { TaggedTemplateProcessor, ValueType, hasMeta, validateParams } from '@linaria/tags';
|
|
2
|
-
|
|
3
|
-
const isNotNull = x => x !== null;
|
|
4
|
-
|
|
5
|
-
const singleQuotedStringLiteral = value => ({
|
|
6
|
-
type: 'StringLiteral',
|
|
7
|
-
value,
|
|
8
|
-
extra: {
|
|
9
|
-
rawValue: value,
|
|
10
|
-
raw: `'${value}'`
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
export default class StyledProcessor extends TaggedTemplateProcessor {
|
|
15
|
-
#variableIdx = 0;
|
|
16
|
-
#variablesCache = new Map();
|
|
17
|
-
|
|
18
|
-
constructor(params, ...args) {
|
|
19
|
-
validateParams(params, ['tag', ['call', 'member'], ['template', 'call']], 'Invalid usage of `styled` tag');
|
|
20
|
-
const [tag, tagOp, template] = params;
|
|
21
|
-
|
|
22
|
-
if (template[0] === 'call') {
|
|
23
|
-
// It is already transformed styled-literal. Skip it.
|
|
24
|
-
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
25
|
-
throw TaggedTemplateProcessor.SKIP;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
super([tag, template], ...args);
|
|
29
|
-
let component;
|
|
30
|
-
|
|
31
|
-
if (tagOp[0] === 'call' && tagOp.length === 2) {
|
|
32
|
-
const value = tagOp[1];
|
|
33
|
-
|
|
34
|
-
if (value.kind === ValueType.FUNCTION) {
|
|
35
|
-
component = 'FunctionalComponent';
|
|
36
|
-
} else {
|
|
37
|
-
component = {
|
|
38
|
-
node: value.ex,
|
|
39
|
-
source: value.source
|
|
40
|
-
};
|
|
41
|
-
this.dependencies.push(value);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (tagOp[0] === 'member') {
|
|
46
|
-
[, component] = tagOp;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (!component) {
|
|
50
|
-
throw new Error('Invalid usage of `styled` tag');
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
this.component = component;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
addInterpolation(node, source, unit = '') {
|
|
57
|
-
const id = this.getVariableId(source + unit);
|
|
58
|
-
this.interpolations.push({
|
|
59
|
-
id,
|
|
60
|
-
node,
|
|
61
|
-
source,
|
|
62
|
-
unit
|
|
63
|
-
});
|
|
64
|
-
return id;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
doEvaltimeReplacement() {
|
|
68
|
-
this.replacer(this.value, false);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
doRuntimeReplacement() {
|
|
72
|
-
const t = this.astService;
|
|
73
|
-
const props = this.getProps();
|
|
74
|
-
this.replacer(t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]), true);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
extractRules(valueCache, cssText, loc) {
|
|
78
|
-
const rules = {};
|
|
79
|
-
let selector = `.${this.className}`; // If `styled` wraps another component and not a primitive,
|
|
80
|
-
// get its class name to create a more specific selector
|
|
81
|
-
// it'll ensure that styles are overridden properly
|
|
82
|
-
|
|
83
|
-
let value = typeof this.component === 'string' ? null : valueCache.get(this.component.node.name);
|
|
84
|
-
|
|
85
|
-
while (hasMeta(value)) {
|
|
86
|
-
selector += `.${value.__linaria.className}`;
|
|
87
|
-
value = value.__linaria.extends;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
rules[selector] = {
|
|
91
|
-
cssText,
|
|
92
|
-
className: this.className,
|
|
93
|
-
displayName: this.displayName,
|
|
94
|
-
start: loc?.start ?? null
|
|
95
|
-
};
|
|
96
|
-
return rules;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
get asSelector() {
|
|
100
|
-
return `.${this.className}`;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
get tagExpressionArgument() {
|
|
104
|
-
const t = this.astService;
|
|
105
|
-
|
|
106
|
-
if (typeof this.component === 'string') {
|
|
107
|
-
if (this.component === 'FunctionalComponent') {
|
|
108
|
-
return t.arrowFunctionExpression([], t.blockStatement([]));
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return singleQuotedStringLiteral(this.component);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return t.callExpression(t.identifier(this.component.node.name), []);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
get tagExpression() {
|
|
118
|
-
const t = this.astService;
|
|
119
|
-
return t.callExpression(this.tag, [this.tagExpressionArgument]);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
get value() {
|
|
123
|
-
const t = this.astService;
|
|
124
|
-
const extendsNode = typeof this.component === 'string' ? null : this.component.node.name;
|
|
125
|
-
return t.objectExpression([t.objectProperty(t.stringLiteral('displayName'), t.stringLiteral(this.displayName)), t.objectProperty(t.stringLiteral('__linaria'), t.objectExpression([t.objectProperty(t.stringLiteral('className'), t.stringLiteral(this.className)), t.objectProperty(t.stringLiteral('extends'), extendsNode ? t.callExpression(t.identifier(extendsNode), []) : t.nullLiteral())]))]);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
toString() {
|
|
129
|
-
const res = arg => `${this.tagSourceCode()}(${arg})\`…\``;
|
|
130
|
-
|
|
131
|
-
if (typeof this.component === 'string') {
|
|
132
|
-
if (this.component === 'FunctionalComponent') {
|
|
133
|
-
return res('() => {…}');
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return res(`'${this.component}'`);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return res(this.component.source);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
getProps() {
|
|
143
|
-
const propsObj = {
|
|
144
|
-
name: this.displayName,
|
|
145
|
-
class: this.className
|
|
146
|
-
}; // If we found any interpolations, also pass them, so they can be applied
|
|
147
|
-
|
|
148
|
-
if (this.interpolations.length) {
|
|
149
|
-
propsObj.vars = {};
|
|
150
|
-
this.interpolations.forEach(({
|
|
151
|
-
id,
|
|
152
|
-
unit,
|
|
153
|
-
node
|
|
154
|
-
}) => {
|
|
155
|
-
const items = [this.astService.callExpression(node, [])];
|
|
156
|
-
|
|
157
|
-
if (unit) {
|
|
158
|
-
items.push(this.astService.stringLiteral(unit));
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
propsObj.vars[id] = items;
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return propsObj;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
getTagComponentProps(props) {
|
|
169
|
-
const t = this.astService;
|
|
170
|
-
const propExpressions = Object.entries(props).map(([key, value]) => {
|
|
171
|
-
if (!value) {
|
|
172
|
-
return null;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const keyNode = t.identifier(key);
|
|
176
|
-
|
|
177
|
-
if (typeof value === 'string') {
|
|
178
|
-
return t.objectProperty(keyNode, t.stringLiteral(value));
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (typeof value === 'boolean') {
|
|
182
|
-
return t.objectProperty(keyNode, t.booleanLiteral(value));
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const vars = Object.entries(value).map(([propName, propValue]) => {
|
|
186
|
-
return t.objectProperty(t.stringLiteral(propName), t.arrayExpression(propValue));
|
|
187
|
-
});
|
|
188
|
-
return t.objectProperty(keyNode, t.objectExpression(vars));
|
|
189
|
-
}).filter(isNotNull);
|
|
190
|
-
return t.objectExpression(propExpressions);
|
|
191
|
-
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
getVariableId(value) {
|
|
195
|
-
if (!this.#variablesCache.has(value)) {
|
|
196
|
-
// make the variable unique to this styled component
|
|
197
|
-
// eslint-disable-next-line no-plusplus
|
|
198
|
-
this.#variablesCache.set(value, `${this.slug}-${this.#variableIdx++}`);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return this.#variablesCache.get(value);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
}
|
|
205
|
-
//# sourceMappingURL=styled.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"styled.js","names":["TaggedTemplateProcessor","ValueType","hasMeta","validateParams","isNotNull","x","singleQuotedStringLiteral","value","type","extra","rawValue","raw","StyledProcessor","variableIdx","variablesCache","Map","constructor","params","args","tag","tagOp","template","SKIP","component","length","kind","FUNCTION","node","ex","source","dependencies","push","Error","addInterpolation","unit","id","getVariableId","interpolations","doEvaltimeReplacement","replacer","doRuntimeReplacement","t","astService","props","getProps","callExpression","tagExpression","getTagComponentProps","extractRules","valueCache","cssText","loc","rules","selector","className","get","name","__linaria","extends","displayName","start","asSelector","tagExpressionArgument","arrowFunctionExpression","blockStatement","identifier","extendsNode","objectExpression","objectProperty","stringLiteral","nullLiteral","toString","res","arg","tagSourceCode","propsObj","class","vars","forEach","items","propExpressions","Object","entries","map","key","keyNode","booleanLiteral","propName","propValue","arrayExpression","filter","has","set","slug"],"sources":["../../src/processors/styled.ts"],"sourcesContent":["import type {\n CallExpression,\n Expression,\n ObjectExpression,\n SourceLocation,\n StringLiteral,\n} from '@babel/types';\n\nimport type {\n Rules,\n WrappedNode,\n ValueCache,\n Params,\n TailProcessorParams,\n} from '@linaria/tags';\nimport {\n TaggedTemplateProcessor,\n ValueType,\n hasMeta,\n validateParams,\n} from '@linaria/tags';\n\nconst isNotNull = <T>(x: T | null): x is T => x !== null;\n\nexport interface IProps {\n atomic?: boolean;\n class?: string;\n name: string;\n vars?: Record<string, Expression[]>;\n}\n\nconst singleQuotedStringLiteral = (value: string): StringLiteral => ({\n type: 'StringLiteral',\n value,\n extra: {\n rawValue: value,\n raw: `'${value}'`,\n },\n});\n\nexport default class StyledProcessor extends TaggedTemplateProcessor {\n public component: WrappedNode;\n\n #variableIdx = 0;\n\n #variablesCache = new Map<string, string>();\n\n constructor(params: Params, ...args: TailProcessorParams) {\n validateParams(\n params,\n ['tag', ['call', 'member'], ['template', 'call']],\n 'Invalid usage of `styled` tag'\n );\n\n const [tag, tagOp, template] = params;\n\n if (template[0] === 'call') {\n // It is already transformed styled-literal. Skip it.\n // eslint-disable-next-line @typescript-eslint/no-throw-literal\n throw TaggedTemplateProcessor.SKIP;\n }\n\n super([tag, template], ...args);\n\n let component: WrappedNode | undefined;\n if (tagOp[0] === 'call' && tagOp.length === 2) {\n const value = tagOp[1];\n if (value.kind === ValueType.FUNCTION) {\n component = 'FunctionalComponent';\n } else {\n component = {\n node: value.ex,\n source: value.source,\n };\n\n this.dependencies.push(value);\n }\n }\n\n if (tagOp[0] === 'member') {\n [, component] = tagOp;\n }\n\n if (!component) {\n throw new Error('Invalid usage of `styled` tag');\n }\n\n this.component = component;\n }\n\n public override addInterpolation(\n node: Expression,\n source: string,\n unit = ''\n ): string {\n const id = this.getVariableId(source + unit);\n\n this.interpolations.push({\n id,\n node,\n source,\n unit,\n });\n\n return id;\n }\n\n public override doEvaltimeReplacement(): void {\n this.replacer(this.value, false);\n }\n\n public override doRuntimeReplacement(): void {\n const t = this.astService;\n\n const props = this.getProps();\n\n this.replacer(\n t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]),\n true\n );\n }\n\n public override extractRules(\n valueCache: ValueCache,\n cssText: string,\n loc?: SourceLocation | null\n ): Rules {\n const rules: Rules = {};\n\n let selector = `.${this.className}`;\n\n // If `styled` wraps another component and not a primitive,\n // get its class name to create a more specific selector\n // it'll ensure that styles are overridden properly\n let value =\n typeof this.component === 'string'\n ? null\n : valueCache.get(this.component.node.name);\n while (hasMeta(value)) {\n selector += `.${value.__linaria.className}`;\n value = value.__linaria.extends;\n }\n\n rules[selector] = {\n cssText,\n className: this.className,\n displayName: this.displayName,\n start: loc?.start ?? null,\n };\n\n return rules;\n }\n\n public override get asSelector(): string {\n return `.${this.className}`;\n }\n\n protected get tagExpressionArgument(): Expression {\n const t = this.astService;\n if (typeof this.component === 'string') {\n if (this.component === 'FunctionalComponent') {\n return t.arrowFunctionExpression([], t.blockStatement([]));\n }\n\n return singleQuotedStringLiteral(this.component);\n }\n\n return t.callExpression(t.identifier(this.component.node.name), []);\n }\n\n protected get tagExpression(): CallExpression {\n const t = this.astService;\n return t.callExpression(this.tag, [this.tagExpressionArgument]);\n }\n\n public override get value(): ObjectExpression {\n const t = this.astService;\n const extendsNode =\n typeof this.component === 'string' ? null : this.component.node.name;\n\n return t.objectExpression([\n t.objectProperty(\n t.stringLiteral('displayName'),\n t.stringLiteral(this.displayName)\n ),\n t.objectProperty(\n t.stringLiteral('__linaria'),\n t.objectExpression([\n t.objectProperty(\n t.stringLiteral('className'),\n t.stringLiteral(this.className)\n ),\n t.objectProperty(\n t.stringLiteral('extends'),\n extendsNode\n ? t.callExpression(t.identifier(extendsNode), [])\n : t.nullLiteral()\n ),\n ])\n ),\n ]);\n }\n\n public override toString(): string {\n const res = (arg: string) => `${this.tagSourceCode()}(${arg})\\`…\\``;\n\n if (typeof this.component === 'string') {\n if (this.component === 'FunctionalComponent') {\n return res('() => {…}');\n }\n\n return res(`'${this.component}'`);\n }\n\n return res(this.component.source);\n }\n\n protected getProps(): IProps {\n const propsObj: IProps = {\n name: this.displayName,\n class: this.className,\n };\n\n // If we found any interpolations, also pass them, so they can be applied\n if (this.interpolations.length) {\n propsObj.vars = {};\n this.interpolations.forEach(({ id, unit, node }) => {\n const items: Expression[] = [this.astService.callExpression(node, [])];\n\n if (unit) {\n items.push(this.astService.stringLiteral(unit));\n }\n\n propsObj.vars![id] = items;\n });\n }\n\n return propsObj;\n }\n\n protected getTagComponentProps(props: IProps): ObjectExpression {\n const t = this.astService;\n\n const propExpressions = Object.entries(props)\n .map(([key, value]: [key: string, value: IProps[keyof IProps]]) => {\n if (!value) {\n return null;\n }\n\n const keyNode = t.identifier(key);\n\n if (typeof value === 'string') {\n return t.objectProperty(keyNode, t.stringLiteral(value));\n }\n\n if (typeof value === 'boolean') {\n return t.objectProperty(keyNode, t.booleanLiteral(value));\n }\n\n const vars = Object.entries(value).map(([propName, propValue]) => {\n return t.objectProperty(\n t.stringLiteral(propName),\n t.arrayExpression(propValue)\n );\n });\n\n return t.objectProperty(keyNode, t.objectExpression(vars));\n })\n .filter(isNotNull);\n\n return t.objectExpression(propExpressions);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected getVariableId(value: string): string {\n if (!this.#variablesCache.has(value)) {\n // make the variable unique to this styled component\n // eslint-disable-next-line no-plusplus\n this.#variablesCache.set(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