Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions code/core/src/babel/babelParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ export const babelPrint = (ast: ASTNode): string => {
}).code;
};

export const babelParseExpression = (code: string): parser.ParseResult<t.Expression> => {
return parser.parseExpression(code, parserOptions);
};
/**
* Thin wrapper around `@babel/parser`'s `parse` so callers do not have to import the parser
* namespace directly. Use this for parsing arbitrary source files; for recast-compatible parsing
* (source-preserving transforms) use {@link babelParse} instead.
*/
export const parseAst = (
code: string,
options?: parser.ParserOptions
): parser.ParseResult<t.File> => parser.parse(code, options);

/** Thin wrapper around `@babel/parser`'s `parseExpression`. */
export const parseAstExpression = (
code: string,
options?: parser.ParserOptions
): parser.ParseResult<t.Expression> => parser.parseExpression(code, options);
2 changes: 0 additions & 2 deletions code/core/src/babel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as core from '@babel/core';
// @ts-expect-error File is not yet exposed, see https://github.com/babel/babel/issues/11350#issuecomment-644118606
import { File } from '@babel/core';
import bg from '@babel/generator';
import * as parser from '@babel/parser';
import bt from '@babel/traverse';
import * as types from '@babel/types';
import * as recast from 'recast';
Expand Down Expand Up @@ -38,7 +37,6 @@ export {
generate,
traverse,
types,
parser,
transformSync,
BabelFileClass,

Expand Down
4 changes: 2 additions & 2 deletions code/core/src/core-server/utils/parser/generic-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parser, types as t } from 'storybook/internal/babel';
import { parseAst, types as t } from 'storybook/internal/babel';

import type { Parser, ParserResult } from './types.ts';

Expand All @@ -11,7 +11,7 @@ export class GenericParser implements Parser {
* @returns The exports of the file
*/
async parse(content: string): Promise<ParserResult> {
const ast = parser.parse(content, {
const ast = parseAst(content, {
allowImportExportEverywhere: true,
allowAwaitOutsideFunction: true,
allowNewTargetOutsideFunction: true,
Expand Down
4 changes: 2 additions & 2 deletions code/core/src/core-server/utils/save-story/valueToAST.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { parser, types as t } from 'storybook/internal/babel';
import { parseAst, types as t } from 'storybook/internal/babel';

export function valueToAST<T>(literal: T): any {
if (literal === null) {
return t.nullLiteral();
}
switch (typeof literal) {
case 'function':
const ast = parser.parse(literal.toString(), {
const ast = parseAst(literal.toString(), {
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
});
Expand Down
4 changes: 2 additions & 2 deletions code/core/src/mocking-utils/extract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'node:fs';

import { generate, parser, types as t } from 'storybook/internal/babel';
import { generate, parseAst, types as t } from 'storybook/internal/babel';
import { logger } from 'storybook/internal/node-logger';
import { telemetry } from 'storybook/internal/telemetry';
import type { CoreConfig } from 'storybook/internal/types';
Expand Down Expand Up @@ -42,7 +42,7 @@ interface ExtractMockCallsOptions {
* @returns The parsed code.
*/
export const babelParser = (code: string) => {
return parser.parse(code, {
return parseAst(code, {
sourceType: 'module',
// Enable plugins to handle modern JavaScript features, including TSX.
plugins: ['typescript', 'jsx', 'classProperties', 'objectRestSpread'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';

import { types as t } from 'storybook/internal/babel';
import { generate, parser } from 'storybook/internal/babel';
import { generate, parseAst } from 'storybook/internal/babel';

import {
cleanupTypeImports,
Expand All @@ -23,7 +23,7 @@ expect.addSnapshotSerializer({
});

function parseCodeToProgramNode(code: string): t.Program {
return parser.parse(code, { sourceType: 'unambiguous', plugins: ['typescript'] }).program;
return parseAst(code, { sourceType: 'unambiguous', plugins: ['typescript'] }).program;
}

function generateCodeFromAST(node: t.Program) {
Expand Down
Loading