From e970ef762cf371d8b9faefd38b26bc337dd2135e Mon Sep 17 00:00:00 2001 From: Robert Balicki Date: Wed, 1 Jan 2025 15:41:19 +0900 Subject: [PATCH] fix a few TS errors in compileTag --- libs/isograph-babel-plugin/compileTag.js | 26 +++++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/libs/isograph-babel-plugin/compileTag.js b/libs/isograph-babel-plugin/compileTag.js index 17d5ddb64..4f56f1c6a 100644 --- a/libs/isograph-babel-plugin/compileTag.js +++ b/libs/isograph-babel-plugin/compileTag.js @@ -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')), @@ -45,26 +49,28 @@ const typeAndFieldRegex = new RegExp( * @param {babel.NodePath} 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];