@@ -464,7 +464,7 @@ const SELF_TEST_BATTERIES = Object.freeze({
464464 'WS (#26220): ui workspace packages, `.tsx` modules, aliases and shims': 11,
465465 'IX (#26220): predicate 4 reads an `any` index signature; the entry may be `src/index.tsx`': 52,
466466 'WP (#26220): a deleted published package is retired whole, `<package>#*`': 59,
467- 'DX (#26324): an export named `default` is reached through an import, never through the word': 11 ,
467+ 'DX (#26324): an export named `default` is reached through an import, never through the word': 19 ,
468468 'G6: a changeset that was ALREADY breaking at base is inherited': 1,
469469 'R15: a changeset RENAMED AND turned breaking in the same commit': 5,
470470 'G10: a PURE rename of an ALREADY-breaking stock changeset': 3,
@@ -497,7 +497,7 @@ const SELF_TEST_BATTERIES = Object.freeze({
497497
498498// DELETING an entry silences that battery's floor exactly as effectively as
499499// zeroing it, so the roster's own size is pinned too.
500- const SELF_TEST_BATTERY_FLOOR = 58 ;
500+ const SELF_TEST_BATTERY_FLOOR = 59 ;
501501
502502// The key an assertion is filed under when no battery is open. It is not a
503503// declared battery, so it reds by the same set difference rather than silently
@@ -3215,30 +3215,41 @@ export function metadataSurfaceReference(symbol, declPath, surfaceTexts, package
32153215 * import x, { y } from 'm' beside it or not, `type` or not
32163216 * import { default as x } from 'm' `default` named inside the braces, an
32173217 * export { default } from 'm' `export ... from` re-export included
3218+ *
3219+ * and the three that bind the whole MODULE, read as reaching its default too:
3220+ *
3221+ * import * as ns from 'm' a namespace import or re-export, `ns.default`
3222+ * export * as ns from 'm' being one member read away
3223+ * import('m') require('m') a dynamic load, whose `.default` likewise
32183224 */
32193225const DEFAULT_BINDING_IMPORT_RE =
32203226 /(?:^|;)[ \t]*import\s+(?:type\s+)?[A-Za-z_$][\w$]*\s*(?:,\s*(?:\{[^}]*\}|\*\s*as\s+[A-Za-z_$][\w$]*)\s*)?from\s*(['"])([^'"\n]+)\1/gm;
32213227const BRACED_IMPORT_RE = /(?:^|;)[ \t]*(?:import|export)\s+(?:type\s+)?\{([^}]*)\}\s*from\s*(['"])([^'"\n]+)\2/gm;
3228+ const NAMESPACE_IMPORT_RE = /(?:^|;)[ \t]*(?:import|export)\s+(?:type\s+)?\*\s*as\s+[A-Za-z_$][\w$]*\s*from\s*(['"])([^'"\n]+)\1/gm;
3229+ const DYNAMIC_IMPORT_RE = /\b(?:import|require)\s*\(\s*(['"])([^'"\n]+)\1\s*\)/g;
32223230
32233231/**
32243232 * `metadataSurfaceReference` for an export named `default` (#26324): the IMPORT arm
32253233 * only. `default` is an ordinary object key and member name in exactly the files
32263234 * this step reads -- a field option `{ value: 'technology', default: true }`, a Zod
32273235 * `.default(...)` -- so the bare-word arm hit on every tree, and no retirement of a
32283236 * default export could ever certify. A metadata surface reaches a default export
3229- * through an import that binds it (the two shapes above). The specifier is
3230- * attributed as for any other name (`specifierNamesModule`), and comment spans are
3231- * blanked first, so a docblock quoting the import is not one.
3232- * ⛔ A namespace import's `ns.default` and a dynamic `import()` are not read here:
3233- * both reach the export through a member named `default`, the very spelling that
3234- * cannot be told from the object keys above.
3237+ * through an import that binds it (the two shapes above), or through one that binds
3238+ * the whole module (the three after them). Those three are refused on the module
3239+ * alone, never on a `.default` read after them: that member spelling is the one
3240+ * that cannot be told from the object keys above, and refusing on the binding is
3241+ * the conservative direction -- every other export name refuses them too, through
3242+ * its bare-word arm. The specifier is attributed as for any other name
3243+ * (`specifierNamesModule`), and comment spans are blanked first, so a docblock
3244+ * quoting the import is not one.
32353245 */
32363246function defaultImportReference(declPath, surfaceTexts, packages) {
32373247 for (const [hitPath, rawText] of surfaceTexts) {
3238- if (hitPath === declPath || !(rawText.includes('import') || rawText.includes('export'))) continue;
3248+ if (hitPath === declPath || !(rawText.includes('import') || rawText.includes('export') || rawText.includes('require') )) continue;
32393249 const code = maskComments(rawText);
32403250 const specifiers = [...code.matchAll(DEFAULT_BINDING_IMPORT_RE)].map((m) => m[2]);
32413251 for (const m of code.matchAll(BRACED_IMPORT_RE)) if (/\bdefault\b/.test(m[1])) specifiers.push(m[3]);
3252+ for (const re of [NAMESPACE_IMPORT_RE, DYNAMIC_IMPORT_RE]) for (const m of code.matchAll(re)) specifiers.push(m[2]);
32423253 if (specifiers.length === 0) continue;
32433254 const pkgs = packages();
32443255 if (specifiers.some((s) => specifierNamesModule(s, hitPath, declPath, pkgs))) return { kind: 'import', hitPath };
@@ -8369,6 +8380,20 @@ function selfTest() {
83698380 red('DX-R2 ...and the same default named inside the braces, `{ default as x }`', dx({
83708381 [DX_HOST]: `import { default as emailPlugin } from '${REX_PKG}';\nexport const Host = { name: 'host', plugin: emailPlugin };\n`,
83718382 }), [DX_P2]);
8383+ // A binding of the whole MODULE reaches its default one member read away; every
8384+ // other export name refuses these through its bare-word arm, so `default` must too.
8385+ red('DX-R3 a NAMESPACE import of the package (`ns.default` one read away)', dx({
8386+ [DX_HOST]: `import * as email from '${REX_PKG}';\nexport const Host = { name: 'host', plugin: email.default };\n`,
8387+ }), [DX_P2]);
8388+ red('DX-R4 a DYNAMIC `import()` of the package', dx({
8389+ [DX_HOST]: `export const Host = { name: 'host', load: () => import('${REX_PKG}').then((m) => m.default) };\n`,
8390+ }), [DX_P2]);
8391+ red('DX-R5 a `require()` of the package', dx({
8392+ [DX_HOST]: `export const Host = { name: 'host', plugin: require('${REX_PKG}').default };\n`,
8393+ }), [DX_P2]);
8394+ red('DX-R6 a NAMESPACE re-export, `export * as ns from`', dx({
8395+ [DX_HOST]: `export * as email from '${REX_PKG}';\nexport const Host = { name: 'host' };\n`,
8396+ }), [DX_P2]);
83728397
83738398 const DX_DECL = rexPath('src/index.ts');
83748399 const dxPkgs = () => new Map([[REX_PKG, { private: false, file: rexPath('package.json') }]]);
0 commit comments