From 719f7461f27bc454f71d689b105b8494e87ca894 Mon Sep 17 00:00:00 2001 From: Panagiotis Bakatselos Date: Mon, 18 Nov 2024 19:30:00 +0100 Subject: [PATCH] fix: Resolve runtime error with CommonJS module imports from 'typescript' Fixes #58 --- src/utils/ast-helpers.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/utils/ast-helpers.ts b/src/utils/ast-helpers.ts index 0b859ec..5f2d381 100644 --- a/src/utils/ast-helpers.ts +++ b/src/utils/ast-helpers.ts @@ -10,11 +10,21 @@ import pkg, { StringLiteral, SourceFile, PropertyDeclaration, - PropertyAccessExpression, - isPropertyAccessExpression, - isCallExpression + PropertyAccessExpression } from 'typescript'; -const { SyntaxKind, isStringLiteralLike, isArrayLiteralExpression, isBinaryExpression, isConditionalExpression } = pkg; + +// Importing non-type members from 'typescript' this way to prevent runtime errors such as: +// `SyntaxError: Named export 'isCallExpression' not found. The requested module 'typescript' is a CommonJS module, +// which may not support all module.exports as named exports.` +const { + isArrayLiteralExpression, + isBinaryExpression, + isCallExpression, + isConditionalExpression, + isPropertyAccessExpression, + isStringLiteralLike, + SyntaxKind +} = pkg; export function getAST(source: string, fileName = ''): SourceFile { const supportedScriptTypes: Record = {