MCPcopy Create free account
hub / github.com/babel/babel / parseExport

Function parseExport

packages/babel-parser/src/parser/statement.ts:2283–2392  ·  view source on GitHub ↗
(
    this: Parser,
    node: Undone<
      | N.ExportDefaultDeclaration
      | N.ExportAllDeclaration
      | N.ExportNamedDeclaration
    >,
    decorators: N.Decorator[] | null | undefined,
  )

Source from the content-addressed store, hash-verified

2281 // https://tc39.es/ecma262/#prod-ExportDeclaration
2282
2283 parseExport(
2284 this: Parser,
2285 node: Undone<
2286 | N.ExportDefaultDeclaration
2287 | N.ExportAllDeclaration
2288 | N.ExportNamedDeclaration
2289 >,
2290 decorators: N.Decorator[] | null | undefined,
2291 ): N.AnyExport {
2292 const maybeDefaultIdentifier = this.parseMaybeImportPhase(
2293 node,
2294 /* isExport */ true,
2295 );
2296 const hasDefault = this.maybeParseExportDefaultSpecifier(
2297 node,
2298 maybeDefaultIdentifier,
2299 );
2300 const parseAfterDefault = !hasDefault || this.eat(tt.comma);
2301 const hasStar = parseAfterDefault && this.eatExportStar(node);
2302 const hasNamespace =
2303 hasStar && this.maybeParseExportNamespaceSpecifier(node);
2304 // `export * as ns` must be followed by `from` (ExportFromClause).
2305 const parseAfterNamespace = parseAfterDefault && !hasNamespace;
2306
2307 if (hasStar && !hasNamespace) {
2308 if (hasDefault) this.unexpected();
2309 if (decorators) {
2310 throw this.raise(Errors.UnsupportedDecoratorExport, node);
2311 }
2312 if ((node as N.ExportNamedDeclaration).phase === "defer") {
2313 this.raise(Errors.DeferExportInvalidAll, node);
2314 }
2315 this.parseExportFrom(node, true);
2316
2317 this.sawUnambiguousESM = true;
2318
2319 return this.finishNode(node, "ExportAllDeclaration");
2320 }
2321
2322 const hasSpecifiers =
2323 parseAfterNamespace && this.maybeParseExportNamedSpecifiers(node);
2324 const isFromRequired =
2325 hasDefault ||
2326 hasStar ||
2327 (node as Undone<N.ExportNamedDeclaration>).phase === "defer" ||
2328 // export { x, y } as ns from '...'
2329 (hasSpecifiers &&
2330 node.specifiers[0]?.type === "ExportNamespaceSpecifier");
2331
2332 if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
2333 this.unexpected(null, tt.braceL);
2334 }
2335
2336 let hasDeclaration;
2337 if (isFromRequired || hasSpecifiers) {
2338 hasDeclaration = false;
2339 if (decorators) {
2340 throw this.raise(Errors.UnsupportedDecoratorExport, node);

Callers

nothing calls this directly

Calls 9

eatExportStarMethod · 0.80
finishNodeMethod · 0.80
checkNamedExportMethod · 0.80
hasPluginMethod · 0.80
checkDuplicateExportsMethod · 0.80

Tested by

no test coverage detected