@timvir/macro 0.1.28 → 0.1.29
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/index.js +75 -60
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -13,71 +13,83 @@ module.exports = createMacro(({ references, babel, state }) => {
|
|
|
13
13
|
|
|
14
14
|
if (references.Sample) {
|
|
15
15
|
references.Sample.forEach((referencePath) => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
t.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
16
|
+
const { parent, parentPath } = referencePath;
|
|
17
|
+
|
|
18
|
+
if (parent.type === "JSXOpeningElement") {
|
|
19
|
+
const attrs = parent.attributes;
|
|
20
|
+
const { filename } = referencePath.hub.file.opts;
|
|
21
|
+
|
|
22
|
+
const component = findAttrValue(attrs, "component") || "..";
|
|
23
|
+
const variant = findAttrValue(attrs, "variant");
|
|
24
|
+
|
|
25
|
+
const as = findAttrValue(attrs, "as") || "component";
|
|
26
|
+
const props = findAttrExpression(attrs, "props") || t.objectExpression([]);
|
|
27
|
+
|
|
28
|
+
const otherAttributes = attrs.filter(
|
|
29
|
+
({ name }) => name.type === "JSXIdentifier" && !(name.name in { component: 1, variant: 1, as: 1, props: 1 })
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* The module which holds the sample.
|
|
34
|
+
*/
|
|
35
|
+
const module = join(dirname(filename), component, "samples", variant);
|
|
36
|
+
|
|
37
|
+
({
|
|
38
|
+
component: () => {
|
|
39
|
+
/*
|
|
40
|
+
* Genrate a unique name that will be used to hold the reference
|
|
41
|
+
* to the sample component.
|
|
42
|
+
*/
|
|
43
|
+
const name = `C${genName(filename, component, variant, "" + counter)}`;
|
|
44
|
+
counter = counter + 1;
|
|
45
|
+
|
|
46
|
+
state.file.path.node.body.unshift(
|
|
47
|
+
t.importDeclaration([t.importDefaultSpecifier(t.identifier(name))], t.stringLiteral(module))
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
parentPath.parentPath.replaceWith(
|
|
51
|
+
t.jsxElement(
|
|
52
|
+
t.jsxOpeningElement(t.jsxIdentifier(name), [...otherAttributes, t.jsxSpreadAttribute(props)], true),
|
|
53
|
+
null,
|
|
54
|
+
parentPath.parentPath.node.children,
|
|
55
|
+
true
|
|
56
|
+
)
|
|
57
|
+
);
|
|
58
|
+
},
|
|
59
|
+
source: () => {
|
|
60
|
+
const source = (() => {
|
|
61
|
+
if (fs.existsSync(module)) {
|
|
62
|
+
return fs.readFileSync(module, "utf8");
|
|
63
|
+
} else {
|
|
64
|
+
return fs.readFileSync(module + ".tsx", "utf8");
|
|
65
|
+
}
|
|
66
|
+
})();
|
|
67
|
+
|
|
68
|
+
parentPath.parentPath.replaceWith(
|
|
69
|
+
t.jsxExpressionContainer(t.templateLiteral([t.templateElement({ raw: source, cooked: source })], []))
|
|
70
|
+
);
|
|
71
|
+
},
|
|
72
|
+
}[as]());
|
|
73
|
+
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (parent.type === "JSXClosingElement") {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
throw new Error(`Unhandled type: ${parent.type}`);
|
|
72
82
|
});
|
|
73
83
|
}
|
|
74
84
|
|
|
75
85
|
if (references.sampleCode) {
|
|
76
86
|
references.sampleCode.forEach((referencePath) => {
|
|
77
87
|
const callExpression = referencePath.parent;
|
|
78
|
-
const {
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
const {
|
|
89
|
+
variant,
|
|
90
|
+
component = "..",
|
|
91
|
+
as = "module",
|
|
92
|
+
} = eval(`(${generate.default(callExpression.arguments[0]).code})`);
|
|
81
93
|
|
|
82
94
|
const { filename } = referencePath.hub.file.opts;
|
|
83
95
|
const module = join(dirname(filename), component, "samples", variant);
|
|
@@ -145,7 +157,10 @@ const genName = (...buffers) => {
|
|
|
145
157
|
return ret.toString("utf8");
|
|
146
158
|
};
|
|
147
159
|
|
|
148
|
-
const matchAttr =
|
|
160
|
+
const matchAttr =
|
|
161
|
+
(n) =>
|
|
162
|
+
({ name }) =>
|
|
163
|
+
name.type === "JSXIdentifier" && name.name === n;
|
|
149
164
|
|
|
150
165
|
const findAttrValue = (attrs, n) => {
|
|
151
166
|
const attr = attrs.find(matchAttr(n));
|