Skip to content

Commit

Permalink
fix a few TS errors in compileTag
Browse files Browse the repository at this point in the history
  • Loading branch information
rbalicki2 committed Jan 1, 2025
1 parent 37c3574 commit e970ef7
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions libs/isograph-babel-plugin/compileTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ function compileTag(t, path, config) {
// This throws if the tag is invalid
compileImportStatement(t, path, type, field, 'entrypoint', config);
} else if (keyword === 'field') {
if (
t.isCallExpression(path.parentPath.node) &&
path.parentPath.node.arguments.length === 1
) {
path.parentPath.replaceWith(path.parentPath.node.arguments[0]);
if (t.isCallExpression(path.parentPath.node)) {
const firstArg = path.parentPath.node.arguments[0];
if (path.parentPath.node.arguments.length === 1 && firstArg != null) {
path.parentPath.replaceWith(firstArg);
} else {
throw new Error(
'Invalid iso tag usage. The iso function should be passed at most one argument.',
);
}
} else {
path.replaceWith(
t.arrowFunctionExpression([t.identifier('x')], t.identifier('x')),
Expand All @@ -45,26 +49,28 @@ const typeAndFieldRegex = new RegExp(
* @param {babel.NodePath<babel.types.CallExpression>} path
* */
function getTypeAndField(path) {
if (path.node.arguments.length !== 1) {
const firstArg = path.node.arguments[0];
if (path.node.arguments.length !== 1 || firstArg == null) {
throw new Error(
`BabelPluginIsograph: Iso invocation require one parameter, found ${path.node.arguments.length}`,
);
}

if (path.node.arguments[0].type !== 'TemplateLiteral') {
if (firstArg.type !== 'TemplateLiteral') {
throw new Error(
'BabelPluginIsograph: Only template literals are allowed in iso fragments.',
);
}

const quasis = path.node.arguments[0].quasis;
if (quasis.length !== 1) {
const quasis = firstArg.quasis;
const firstQuasi = quasis[0];
if (quasis.length !== 1 || firstQuasi == null) {
throw new Error(
'BabelPluginIsograph: Substitutions are not allowed in iso fragments.',
);
}

const content = quasis[0].value.raw;
const content = firstQuasi.value.raw;
const typeAndField = typeAndFieldRegex.exec(content);

const keyword = typeAndField?.[1];
Expand Down

0 comments on commit e970ef7

Please sign in to comment.