All files / lib/bundler parseModule.js

95.36% Statements 556/583
88.09% Branches 148/168
100% Functions 10/10
95.36% Lines 556/583

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 1x 1x 1x 1x 1x 1x 1x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 19487x 19487x 19487x 19487x 19487x 19487x 34912x 34912x 34912x 34912x 19487x 19487x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 36064x 36064x 36064x 36064x 15726x 15726x 36064x 11454x 11454x 36064x 11271x 11271x 11271x 11271x 11271x 11271x 11271x 36064x 45093x       45093x 45093x 45093x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 2x 2x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 17x       17x 5x 5x 5x 5x 5x 5x 11x 17x 2x 2x 2x 2x 45091x 45093x       45091x 45093x 11271x 11271x 11271x 11271x 3675586x 2062185x 2062185x 2062185x 1613401x 3675586x 1589321x 1565241x 1565239x 1602130x 1602130x 11271x 11271x 11271x 33820x 45093x 3x 3x 3x 3x 33817x 45093x 11272x 11272x 11272x 11272x 11272x 11272x 11272x 11272x 22545x 45093x 11274x 11274x 11271x 11271x 36064x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 8811x 8811x 2240x 2240x 2240x 2240x 6571x 6571x 6571x 6571x 8811x 281x 281x 281x 281x 281x 281x 281x 281x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 3883x 3883x 3883x 3637x 6571x 6571x 6571x 24520x 19507x 19507x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 5x 5x 4x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6571x 6571x 6571x 6571x 6571x 6571x 6571x 14231445x 14038823x 14038823x 14038823x 192622x 192622x 14231445x         14231445x         14231445x 135708x 135708x 135708x 56914x 56914x 14231445x 14231445x 14231445x 16140x 16140x 16139x 16139x 16139x 16139x 16139x 16139x 16140x 16140x 40775x 14231445x 25771x 25771x 25771x 8294x 8294x 8294x 25771x 85x 85x 85x 25770x 25770x 25770x 25770x 25770x 25770x 25770x 25770x 25770x 25770x 25770x 25770x 25771x 25771x 25771x 25771x 25771x 15004x 15004x 15004x 15004x 15004x 15004x 14231445x 14231445x 14231445x 14231445x 14231445x 6570x 6570x 6571x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 16140x 16140x 16140x 16140x 16140x 1x 1x 16139x 16139x 16140x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 15980x 15980x 16140x     15980x 15980x 15980x 15980x 15980x 15980x 15980x 15980x 16140x 16140x 16140x 294x 294x 2x 2x 294x 292x 292x     292x 292x 294x 15980x 16135x 15691x 15691x     15691x 15691x 15980x 15980x 16140x 16140x 15980x 15980x 15980x 15980x 15980x 15980x 15980x 15980x 15980x 15980x 15980x 16140x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 281x 25771x 25771x 25771x 25771x 25771x 25771x 25771x 87x 87x 1x 1x 87x 1x 87x 85x 85x 86x 87x 87x 87x 25684x 25684x 25684x 25771x 3796x 3796x 3796x 3796x 3796x 3796x 3551x 8293x 8293x 3551x 3551x 3551x 3551x 245x 3796x 573x 573x 245x 3796x 3796x 3796x 21888x 21888x 21888x 25771x 201x 201x 201x 201x 201x 201x 201x 201x 201x 201x 201x 201x 201x 201x 21687x 21687x 21687x 21687x 21687x 21687x 21687x 21687x     21687x 21688x 21688x 21687x 21687x 21687x 21687x 21687x 21687x     25771x  
/**
 * @file parseModule.js
 * @description Reads an ES module's shape: what it imports, what it exports,
 * and where its top-level statements sit.
 *
 * ## Scope, stated up front
 *
 * This is a *module-structure* reader, not an ECMAScript parser. It answers
 * exactly the questions the bundler asks:
 *
 * - which specifiers does this module depend on, and under what local names?
 * - which names does it export, and where does each one come from?
 * - where does each top-level statement begin and end, and what does it declare?
 *
 * Everything between those declarations is opaque text that the emitter copies
 * verbatim. That is deliberate: the less of JavaScript the bundler claims to
 * understand, the fewer ways it can silently miscompile a module. Where a
 * construct is outside what the reader can describe, it says so
 * ({@link ModuleParseError}) and the build fails with a location — the same
 * house rule the compiler follows everywhere else.
 *
 * Import and export declarations are only legal at the top level of a module,
 * so scanning for them at bracket depth zero, skipping every string, template,
 * regex and comment via {@link module:lib/bundler/scanner}, finds all of them
 * and nothing else.
 * @module lib/bundler/parseModule
 */
 
import { CodeMask, statementEnd, matchBracket } from './scanner.js';
import { LruCache } from '../core/utils/LruCache.js';
 
/**
 * Module records by source text.
 *
 * Reading a module is a pure function of its source -- the file name is used
 * only in diagnostics -- so the same text always yields the same record. That
 * matters because the runtime's modules are re-read on every build, and
 * `avenx watch` rebuilds on every keystroke: without this, a watch rebuild
 * re-parsed seventy modules that had not changed. Bounded, because a long
 * watch session should not accumulate every version of every file a developer
 * has typed.
 * @type {LruCache}
 */
const recordCache = new LruCache(400);
 
/**
 * Raised when a module contains structure the reader cannot describe.
 */
export class ModuleParseError extends Error {
  /**
   * @param {string} message - What could not be read.
   * @param {string} file - The module's path or virtual id.
   * @param {number} [index] - Character offset in the module.
   */
  constructor(message, file, index = -1) {
    super(message);
    this.name = 'ModuleParseError';
    /** @type {string} */
    this.file = file;
    /** @type {number} */
    this.index = index;
  }
}
 
/**
 * Declaration keywords that introduce a binding at the top level.
 * @type {Set<string>}
 */
const BINDING_KEYWORDS = new Set(['const', 'let', 'var', 'function', 'class', 'async']);
 
/**
 * Splits a `{ a, b as c }` clause into binding pairs.
 * @param {string} inner - The text between the braces.
 * @returns {Array<{imported: string, local: string}>} The bindings, in order.
 */
function parseNamedClause(inner) {
  return inner
    .split(',')
    .map((part) => part.trim())
    .filter(Boolean)
    .map((part) => {
      const aliased = part.split(/\s+as\s+/);
      const imported = aliased[0].trim().replace(/^["']|["']$/g, '');
      const local = (aliased[1] || aliased[0]).trim();
      return { imported, local };
    });
}
 
/**
 * Reads the names a top-level declaration introduces.
 *
 * Handles `const`/`let`/`var` (including object and array destructuring
 * patterns), `function`, `async function`, `function*` and `class`. Anything
 * else — an expression statement, a bare call — declares nothing, which is the
 * correct answer rather than a failure.
 * @param {string} text - The statement source.
 * @returns {string[]} Declared binding names.
 */
export function declaredNames(text) {
  const trimmed = text.replace(/^\s*export\s+default\s+/, '').replace(/^\s*export\s+/, '').trimStart();
 
  const fn = trimmed.match(/^(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)/);
  if (fn) return [fn[1]];
 
  const cls = trimmed.match(/^class\s+([A-Za-z_$][\w$]*)/);
  if (cls) return [cls[1]];
 
  const decl = trimmed.match(/^(const|let|var)\s+/);
  if (!decl) return [];
 
  const names = [];
  const body = trimmed.slice(decl[0].length);
  const mask = new CodeMask(body);
  let i = 0;
  let expectName = true;
 
  while (i < body.length) {
    if (!mask.isCode(i)) {
      i += 1;
      continue;
    }
    const char = body[i];
 
    if (char === '{' || char === '[') {
      // A destructuring pattern. Every identifier that is not a property key
      // and not a default value is a binding; taking the identifier after each
      // `:` (and each bare identifier otherwise) covers the shapes that appear
      // in practice.
      const end = matchBracket(body, mask, i);
      const pattern = body.slice(i + 1, end - 1);
      const patternMask = new CodeMask(pattern);
      let inner = 0;
      let assigned = false;
      let token = '';
      let pendingKey = false;
      const push = (name) => {
        if (name && /^[A-Za-z_$][\w$]*$/.test(name) && !names.includes(name)) names.push(name);
      };
      for (let k = 0; k <= pattern.length; k += 1) {
        const ch = k < pattern.length ? pattern[k] : ',';
        const isCode = k < pattern.length ? patternMask.isCode(k) : true;
        if (!isCode) continue;
        if (ch === '{' || ch === '[' || ch === '(') inner += 1;
        else if (ch === '}' || ch === ']' || ch === ')') inner -= 1;
 
        if (inner === 0 && ch === ':') {
          token = '';
          pendingKey = true;
          continue;
        }
        if (inner === 0 && ch === '=') {
          assigned = true;
          continue;
        }
        if (inner === 0 && ch === ',') {
          if (!assigned || pendingKey || token) push(token.trim());
          token = '';
          assigned = false;
          pendingKey = false;
          continue;
        }
        if (!assigned) token += ch;
      }
      i = end;
      expectName = false;
      continue;
    }
 
    if (char === '(') {
      i = matchBracket(body, mask, i);
      continue;
    }
 
    if (char === '=' && body[i + 1] !== '=') {
      // Skip the initialiser: everything up to the next top-level comma.
      let j = i + 1;
      let d = 0;
      while (j < body.length) {
        if (!mask.isCode(j)) {
          j += 1;
          continue;
        }
        const c = body[j];
        if (c === '(' || c === '[' || c === '{') d += 1;
        else if (c === ')' || c === ']' || c === '}') d -= 1;
        else if (c === ',' && d === 0) break;
        else if (c === ';' && d === 0) break;
        j += 1;
      }
      i = j;
      continue;
    }
 
    if (char === ',') {
      expectName = true;
      i += 1;
      continue;
    }
 
    if (expectName && /[A-Za-z_$]/.test(char)) {
      let j = i;
      while (j < body.length && /[\w$]/.test(body[j])) j += 1;
      const name = body.slice(i, j);
      if (!names.includes(name)) names.push(name);
      expectName = false;
      i = j;
      continue;
    }
 
    if (char === ';') break;
    i += 1;
  }
 
  return names;
}
 
/**
 * The structure of one ES module.
 * @typedef {object} ModuleRecord
 * @property {string} source - The original source text.
 * @property {string} file - Path or virtual id, for diagnostics.
 * @property {Array<object>} imports - Import declarations, in source order.
 * @property {Array<object>} exports - Names this module exports from its own scope.
 * @property {Array<object>} reExports - `export { x } from 'm'` entries.
 * @property {Array<object>} starReExports - `export * from 'm'` entries.
 * @property {Array<object>} statements - Top-level statements with their spans.
 * @property {Array<object>} dynamicImports - `import('…')` expressions, with spans.
 * @property {string[]} dependencies - Distinct specifiers this module needs.
 */
 
/**
 * Reads a module's import/export structure.
 * @param {string} source - The module source.
 * @param {string} file - Path or virtual id, used in diagnostics.
 * @returns {ModuleRecord} The module's structure.
 * @throws {ModuleParseError} When a declaration cannot be read.
 */
export function parseModule(source, file) {
  const cached = recordCache.get(source);
  if (cached) {
    // The record is read-only to every consumer; only `file` differs, and it is
    // wanted for diagnostics about *this* module.
    return cached.file === file ? cached : { ...cached, file };
  }
 
  const record = readModule(source, file);
  recordCache.set(source, record);
  return record;
}
 
/**
 * Reads a module's structure, without the memo.
 * @param {string} source - The module source.
 * @param {string} file - Path or virtual id, used in diagnostics.
 * @returns {ModuleRecord} The module's structure.
 * @throws {ModuleParseError} When a declaration cannot be read.
 */
function readModule(source, file) {
  const mask = new CodeMask(source);
  const text = mask.withoutComments();
 
  /** @type {ModuleRecord} */
  const record = {
    source,
    file,
    imports: [],
    exports: [],
    reExports: [],
    starReExports: [],
    statements: [],
    dynamicImports: [],
    dependencies: [],
  };
 
  /**
   * Reads the `from '...'` tail of a declaration.
   * @param {number} from - Offset to start looking at.
   * @returns {{specifier: string, end: number}|null} The specifier and where it ends.
   */
  const readSpecifier = (from) => {
    const rest = text.slice(from);
    const match = rest.match(/^\s*from\s*(['"])([^'"]*)\1/);
    if (!match) return null;
    return { specifier: match[2], end: from + match[0].length };
  };
 
  const addDependency = (specifier) => {
    if (!record.dependencies.includes(specifier)) {
      record.dependencies.push(specifier);
    }
  };
 
  // `import('./x.js')` is an expression, not a declaration, so it is found by
  // scanning rather than by walking statement starts. It is collected because
  // ignoring it is the one remaining way a specifier could reach a browser
  // unresolved: the emitted bundle is a classic script, so a surviving
  // `import()` would request a path that was never written.
  const DYNAMIC_IMPORT = /\bimport\s*\(\s*(?:(['"])([^'"]*)\1\s*\))?/g;
  let dynamic;
  while ((dynamic = DYNAMIC_IMPORT.exec(text)) !== null) {
    if (!mask.isCode(dynamic.index)) continue;
    if (text.startsWith('import.', dynamic.index)) continue;
 
    if (dynamic[2] === undefined) {
      // `import(name)` with a computed specifier. No build-time analysis can
      // say what it names, and leaving it in a classic script would request a
      // path that was never written -- so it is recorded and reported rather
      // than passed through.
      record.dynamicImports.push({ specifier: null, start: dynamic.index, end: dynamic.index + dynamic[0].length });
      continue;
    }
 
    record.dynamicImports.push({
      specifier: dynamic[2],
      start: dynamic.index,
      end: dynamic.index + dynamic[0].length,
    });
    addDependency(dynamic[2]);
  }
 
 
 
  let i = 0;
  let depth = 0;
 
  while (i < source.length) {
    if (!mask.isCode(i)) {
      i += 1;
      continue;
    }
    const char = source[i];
 
    if (char === '(' || char === '[' || char === '{') {
      depth += 1;
      i += 1;
      continue;
    }
    if (char === ')' || char === ']' || char === '}') {
      depth -= 1;
      i += 1;
      continue;
    }
    if (depth !== 0 || /\s/.test(char) || char === ';') {
      i += 1;
      continue;
    }
 
    // A statement starts here. Identify it, record its span, and move past it.
    const word = (text.slice(i).match(/^[A-Za-z_$][\w$]*/) || [''])[0];
 
    if (word === 'import') {
      const parsed = readImport(text, mask, i, file);
      if (parsed) {
        record.imports.push(parsed);
        addDependency(parsed.specifier);
        record.statements.push({ start: i, end: parsed.end, declares: parsed.locals, kind: 'import' });
        i = parsed.end;
        continue;
      }
      // `import(` — a dynamic import expression, not a declaration.
    }
 
    if (word === 'export') {
      const parsed = readExport(text, mask, i, file, readSpecifier);
      for (const entry of parsed.exports) record.exports.push(entry);
      for (const entry of parsed.reExports) {
        record.reExports.push(entry);
        addDependency(entry.specifier);
      }
      for (const entry of parsed.starReExports) {
        record.starReExports.push(entry);
        addDependency(entry.specifier);
      }
      record.statements.push({
        start: i,
        end: parsed.end,
        declares: parsed.declares,
        kind: parsed.kind,
        exported: parsed.exports.map((entry) => entry.exported),
        bodyStart: parsed.bodyStart,
        // The entries this particular statement produced. The emitter needs
        // them per statement, not per module: two `export { … } from` lines
        // naming the same specifier are two separate rewrites.
        entries: parsed.exports,
        reExportEntries: parsed.reExports,
        starSpecifier: parsed.starReExports.length > 0 ? parsed.starReExports[0].specifier : null,
      });
      i = parsed.end;
      continue;
    }
 
    const end = statementEnd(source, mask, i);
    const statementText = text.slice(i, end);
    record.statements.push({
      start: i,
      end,
      declares: BINDING_KEYWORDS.has(word) ? declaredNames(statementText) : [],
      kind: 'statement',
    });
    i = Math.max(end, i + 1);
  }
 
  return record;
}
 
/**
 * Reads one `import` declaration.
 * @param {string} text - Comment-blanked source.
 * @param {CodeMask} mask - Mask for the source.
 * @param {number} start - Offset of the `import` keyword.
 * @param {string} file - Module path, for diagnostics.
 * @returns {object|null} The import record, or null when this is `import(`.
 * @throws {ModuleParseError} When the declaration is malformed.
 */
function readImport(text, mask, start, file) {
  const rest = text.slice(start);
 
  // `import(` and `import.meta` are expressions, not declarations.
  if (/^import\s*[(.]/.test(rest)) {
    return null;
  }
 
  const bare = rest.match(/^import\s*(['"])([^'"]*)\1\s*;?/);
  if (bare) {
    return {
      specifier: bare[2],
      bindings: [],
      namespace: null,
      defaultLocal: null,
      locals: [],
      sideEffectOnly: true,
      start,
      end: start + bare[0].length,
    };
  }
 
  const clauseMatch = rest.match(/^import\s+([\s\S]*?)\s+from\s*(['"])([^'"]*)\2\s*;?/);
  if (!clauseMatch) {
    throw new ModuleParseError('could not read this import declaration', file, start);
  }
 
  const clause = clauseMatch[1].trim();
  const specifier = clauseMatch[3];
  const bindings = [];
  let namespace = null;
  let defaultLocal = null;
 
  const namedIndex = clause.indexOf('{');
  const head = (namedIndex === -1 ? clause : clause.slice(0, namedIndex)).replace(/,\s*$/, '').trim();
 
  if (head) {
    const starMatch = head.match(/^(?:([A-Za-z_$][\w$]*)\s*,\s*)?\*\s+as\s+([A-Za-z_$][\w$]*)$/);
    if (starMatch) {
      if (starMatch[1]) defaultLocal = starMatch[1];
      namespace = starMatch[2];
    } else {
      const defaultMatch = head.match(/^([A-Za-z_$][\w$]*)$/);
      if (!defaultMatch) {
        throw new ModuleParseError(`could not read the import clause "${clause}"`, file, start);
      }
      defaultLocal = defaultMatch[1];
    }
  }
 
  if (namedIndex !== -1) {
    const close = clause.indexOf('}', namedIndex);
    if (close === -1) {
      throw new ModuleParseError(`could not read the import clause "${clause}"`, file, start);
    }
    bindings.push(...parseNamedClause(clause.slice(namedIndex + 1, close)));
  }
 
  const locals = bindings.map((entry) => entry.local);
  if (namespace) locals.push(namespace);
  if (defaultLocal) locals.push(defaultLocal);
 
  return {
    specifier,
    bindings,
    namespace,
    defaultLocal,
    locals,
    sideEffectOnly: false,
    start,
    end: start + clauseMatch[0].length,
  };
}
 
/**
 * Reads one `export` declaration.
 * @param {string} text - Comment-blanked source.
 * @param {CodeMask} mask - Mask for the source.
 * @param {number} start - Offset of the `export` keyword.
 * @param {string} file - Module path, for diagnostics.
 * @param {function(number): ({specifier: string, end: number}|null)} readSpecifier - Reads a `from` tail.
 * @returns {object} What the declaration exports and where it ends.
 * @throws {ModuleParseError} When the declaration is malformed.
 */
function readExport(text, mask, start, file, readSpecifier) {
  const rest = text.slice(start);
  const result = { exports: [], reExports: [], starReExports: [], declares: [], end: start, kind: 'export', bodyStart: start };
 
  // export * from 'm'   |   export * as ns from 'm'
  const star = rest.match(/^export\s*\*\s*(?:as\s+([A-Za-z_$][\w$]*)\s*)?/);
  if (star) {
    const tail = readSpecifier(start + star[0].length);
    if (!tail) {
      throw new ModuleParseError('an `export *` declaration needs a `from` clause', file, start);
    }
    if (star[1]) {
      result.reExports.push({ specifier: tail.specifier, imported: '*', exported: star[1] });
    } else {
      result.starReExports.push({ specifier: tail.specifier });
    }
    result.kind = 'star-reexport';
    result.end = text[tail.end] === ';' ? tail.end + 1 : tail.end;
    return result;
  }
 
  // export { ... }  |  export { ... } from 'm'
  const braceMatch = rest.match(/^export\s*\{/);
  if (braceMatch) {
    const open = start + braceMatch[0].length - 1;
    const close = matchBracket(text, mask, open);
    const clause = parseNamedClause(text.slice(open + 1, close - 1));
    const tail = readSpecifier(close);
 
    if (tail) {
      for (const entry of clause) {
        result.reExports.push({ specifier: tail.specifier, imported: entry.imported, exported: entry.local });
      }
      result.kind = 'reexport';
      result.end = text[tail.end] === ';' ? tail.end + 1 : tail.end;
      return result;
    }
 
    for (const entry of clause) {
      result.exports.push({ exported: entry.local, local: entry.imported, kind: 'binding' });
    }
    result.kind = 'export-list';
    result.end = text[close] === ';' ? close + 1 : close;
    return result;
  }
 
  // export default ...
  const defaultMatch = rest.match(/^export\s+default\s+/);
  if (defaultMatch) {
    const bodyStart = start + defaultMatch[0].length;
    const end = statementEnd(text, mask, bodyStart);
    const declared = declaredNames(text.slice(bodyStart, end));
    result.exports.push({
      exported: 'default',
      local: declared[0] || null,
      kind: declared.length > 0 ? 'declaration' : 'expression',
    });
    result.declares = declared;
    result.kind = 'export-default';
    result.bodyStart = bodyStart;
    result.end = end;
    return result;
  }
 
  // export <declaration>
  const declMatch = rest.match(/^export\s+(?=(const|let|var|function|class|async)\b)/);
  if (declMatch) {
    const bodyStart = start + declMatch[0].length;
    const end = statementEnd(text, mask, bodyStart);
    const declared = declaredNames(text.slice(bodyStart, end));
    if (declared.length === 0) {
      throw new ModuleParseError('could not read the names this export declares', file, start);
    }
    for (const name of declared) {
      result.exports.push({ exported: name, local: name, kind: 'declaration' });
    }
    result.declares = declared;
    result.kind = 'export-declaration';
    result.bodyStart = bodyStart;
    result.end = end;
    return result;
  }

  throw new ModuleParseError('could not read this export declaration', file, start);
}