Skip to content

Commit

Permalink
fix: Fix typescript types for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Jun 2, 2022
1 parent 94eefad commit 34c55ac
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/babelParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 34c55ac

Please sign in to comment.