@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.
Files changed (2) hide show
  1. package/index.js +75 -60
  2. 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
- if (referencePath.parent.type === "JSXClosingElement") return;
17
-
18
- const attrs = referencePath.parent.attributes;
19
- const { filename } = referencePath.hub.file.opts;
20
-
21
- const component = findAttrValue(attrs, "component") || "..";
22
- const variant = findAttrValue(attrs, "variant");
23
-
24
- const as = findAttrValue(attrs, "as") || "component";
25
- const props = findAttrExpression(attrs, "props") || t.objectExpression([]);
26
-
27
- const otherAttributes = attrs.filter(
28
- ({ name }) => name.type === "JSXIdentifier" && !(name.name in { component: 1, variant: 1, as: 1, props: 1 })
29
- );
30
-
31
- /*
32
- * The module which holds the sample.
33
- */
34
- const module = join(dirname(filename), component, "samples", variant);
35
-
36
- ({
37
- component: () => {
38
- /*
39
- * Genrate a unique name that will be used to hold the reference
40
- * to the sample component.
41
- */
42
- const name = `C${genName(filename, component, variant, "" + counter)}`;
43
- counter = counter + 1;
44
-
45
- state.file.path.node.body.unshift(
46
- t.importDeclaration([t.importDefaultSpecifier(t.identifier(name))], t.stringLiteral(module))
47
- );
48
-
49
- referencePath.parentPath.parentPath.replaceWith(
50
- t.jsxElement(
51
- t.jsxOpeningElement(t.jsxIdentifier(name), [...otherAttributes, t.jsxSpreadAttribute(props)], true),
52
- null,
53
- referencePath.parentPath.parentPath.node.children,
54
- true
55
- )
56
- );
57
- },
58
- source: () => {
59
- const source = (() => {
60
- if (fs.existsSync(module)) {
61
- return fs.readFileSync(module, "utf8");
62
- } else {
63
- return fs.readFileSync(module + ".tsx", "utf8");
64
- }
65
- })();
66
-
67
- referencePath.parentPath.parentPath.replaceWith(
68
- t.jsxExpressionContainer(t.templateLiteral([t.templateElement({ raw: source, cooked: source })], []))
69
- );
70
- },
71
- }[as]());
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 { variant, component = "..", as = "module" } = eval(
79
- `(${generate.default(callExpression.arguments[0]).code})`
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 = (n) => ({ name }) => name.type === "JSXIdentifier" && name.name === n;
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));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@timvir/macro",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "dependencies": {