Repository: babel/babel
File: packages/babel-parser/src/index.ts (TypeScript)

Code before:
  RecordAndTuplePluginOptions,
  TypeScriptPluginOptions,
} from "./typings.ts";
import Parser, { type PluginsMap } from "./parser/index.ts";
import type { ParseError as ParseErrorGeneric } from "./parse-error.ts";

import type { ExportedTokenType } from "./tokenizer/types.ts";
import {
  getExportedToken,
  tt as internalTokenTypes,
  type InternalTokenTypes,
} from "./tokenizer/types.ts";
export type { Token } from "./tokenizer/index.ts";

// TODO: Rather than type-casting the internal AST definitions to the
// @babel/types one, we should actually unify them.
import type { Expression, File } from "@babel/types";
export type { Expression, File };

export type ParserOptions = Partial<Options>;

export type ParseError = ParseErrorGeneric<object>;
export type ParseResult<Result extends File | Expression = File> = Result & {
  comments: File["comments"];
  errors: null | ParseError[];
  tokens: File["tokens"];
};

/**
 * Parse the provided code as an entire ECMAScript program.
 */
export function parse(
  input: string,
  options?: ParserOptions,
): ParseResult<File> {
  if (options?.sourceType === "unambiguous") {
    options = {
      ...options,
    };
    try {
      options.sourceType = "module";
      const parser = getParser(options, input);
      const ast = parser.parse();

      if (parser.sawUnambiguousESM) {
        return ast;
      }

      if (parser.ambiguousScriptDifferentAst) {
        // Top level await introduces code which can be both a valid script and
        // a valid module, but which produces different ASTs:

Review comment (suggestion):
```suggestion
  tokens?: File["tokens"];
```
This should be an optional property since, iirc, it will not be attached to the result if the `tokens` option is set to `false`.

Code after:
  RecordAndTuplePluginOptions,
  TypeScriptPluginOptions,
} from "./typings.ts";
import Parser, { type PluginsMap } from "./parser/index.ts";
import type { ParseError as ParseErrorGeneric } from "./parse-error.ts";

import type { ExportedTokenType } from "./tokenizer/types.ts";
import {
  getExportedToken,
  tt as internalTokenTypes,
  type InternalTokenTypes,
} from "./tokenizer/types.ts";
export type { Token } from "./tokenizer/index.ts";

// TODO: Rather than type-casting the internal AST definitions to the
// @babel/types one, we should actually unify them.
import type { Expression, File } from "@babel/types";
export type { Expression, File };

export type ParserOptions = Partial<Options>;

export type ParseError = ParseErrorGeneric<object>;
export type ParseResult<Result extends File | Expression = File> = Result & {
  comments: File["comments"];
  errors: null | ParseError[];
  tokens?: File["tokens"];
};

/**
 * Parse the provided code as an entire ECMAScript program.
 */
export function parse(
  input: string,
  options?: ParserOptions,
): ParseResult<File> {
  if (options?.sourceType === "unambiguous") {
    options = {
      ...options,
    };
    try {
      options.sourceType = "module";
      const parser = getParser(options, input);
      const ast = parser.parse();

      if (parser.sawUnambiguousESM) {
        return ast;
      }

      if (parser.ambiguousScriptDifferentAst) {
        // Top level await introduces code which can be both a valid script and
        // a valid module, but which produces different ASTs: