From 34c55ac1d663cc604f4f548018d78e02e081a797 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Thu, 2 Jun 2022 15:24:24 +0000 Subject: [PATCH] fix: Fix typescript types for parsing --- src/babelParser.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/babelParser.ts b/src/babelParser.ts index b1dbb054849..2664f3244be 100644 --- a/src/babelParser.ts +++ b/src/babelParser.ts @@ -3,8 +3,8 @@ import { parseSync, ParserOptions, TransformOptions, + ParseResult, } from '@babel/core'; -import * as t from '@babel/types'; import path from 'path'; const TYPESCRIPT_EXTS = { @@ -46,7 +46,7 @@ function getDefaultPlugins( } export type Options = TransformOptions & { parserOptions?: ParserOptions }; -export type FileNodeWithOptions = t.File & { +export type FileNodeWithOptions = ParseResult & { program: { options: Options }; __src: string; }; @@ -104,7 +104,11 @@ export default function buildParse(options: Options = {}): Parser { return { parse(src: string): FileNodeWithOptions { - const ast = parseSync(src, opts) as FileNodeWithOptions; + const ast = parseSync(src, opts) as FileNodeWithOptions | null; + + if (!ast) { + throw new Error('Unable to parse source code.'); + } // Attach options to the Program node, for use when processing imports. ast.program.options = options; return ast;