@isograph/babel-plugin 0.0.0-main-37c3574c → 0.0.0-main-63adc9b6
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/compileTag.js +16 -10
- package/package.json +1 -1
package/compileTag.js
CHANGED
@@ -17,11 +17,15 @@ function compileTag(t, path, config) {
|
|
17
17
|
// This throws if the tag is invalid
|
18
18
|
compileImportStatement(t, path, type, field, 'entrypoint', config);
|
19
19
|
} else if (keyword === 'field') {
|
20
|
-
if (
|
21
|
-
|
22
|
-
path.parentPath.node.arguments.length === 1
|
23
|
-
|
24
|
-
|
20
|
+
if (t.isCallExpression(path.parentPath.node)) {
|
21
|
+
const firstArg = path.parentPath.node.arguments[0];
|
22
|
+
if (path.parentPath.node.arguments.length === 1 && firstArg != null) {
|
23
|
+
path.parentPath.replaceWith(firstArg);
|
24
|
+
} else {
|
25
|
+
throw new Error(
|
26
|
+
'Invalid iso tag usage. The iso function should be passed at most one argument.',
|
27
|
+
);
|
28
|
+
}
|
25
29
|
} else {
|
26
30
|
path.replaceWith(
|
27
31
|
t.arrowFunctionExpression([t.identifier('x')], t.identifier('x')),
|
@@ -45,26 +49,28 @@ const typeAndFieldRegex = new RegExp(
|
|
45
49
|
* @param {babel.NodePath<babel.types.CallExpression>} path
|
46
50
|
* */
|
47
51
|
function getTypeAndField(path) {
|
48
|
-
|
52
|
+
const firstArg = path.node.arguments[0];
|
53
|
+
if (path.node.arguments.length !== 1 || firstArg == null) {
|
49
54
|
throw new Error(
|
50
55
|
`BabelPluginIsograph: Iso invocation require one parameter, found ${path.node.arguments.length}`,
|
51
56
|
);
|
52
57
|
}
|
53
58
|
|
54
|
-
if (
|
59
|
+
if (firstArg.type !== 'TemplateLiteral') {
|
55
60
|
throw new Error(
|
56
61
|
'BabelPluginIsograph: Only template literals are allowed in iso fragments.',
|
57
62
|
);
|
58
63
|
}
|
59
64
|
|
60
|
-
const quasis =
|
61
|
-
|
65
|
+
const quasis = firstArg.quasis;
|
66
|
+
const firstQuasi = quasis[0];
|
67
|
+
if (quasis.length !== 1 || firstQuasi == null) {
|
62
68
|
throw new Error(
|
63
69
|
'BabelPluginIsograph: Substitutions are not allowed in iso fragments.',
|
64
70
|
);
|
65
71
|
}
|
66
72
|
|
67
|
-
const content =
|
73
|
+
const content = firstQuasi.value.raw;
|
68
74
|
const typeAndField = typeAndFieldRegex.exec(content);
|
69
75
|
|
70
76
|
const keyword = typeAndField?.[1];
|
package/package.json
CHANGED