Skip to content

bug(scanner): preserve default imports when mixed with type specifiers #3

Closed

Description

@Kaap10

Summary

While testing the monorepo-doctor audit pipeline on TypeScript and React packages, I noticed that scan.mjs silently discards default runtime imports whenever they are imported alongside named type specifiers.

In TypeScript codebases, mixing default value imports with type imports on a single line is common:

import Button, { type ButtonProps } from '@acme/ui';
import React, { type FC, type ReactNode } from 'react';
import apiClient, { type RequestConfig } from '@acme/api';

When scanning these statements, scan.mjs marks the entire import statement as type-only and skips it, losing the runtime dependency.

Root Cause

In skills/monorepo-doctor/references/scan.md (lines 114 to 139):

  1. The scanner regex matches the import statement and extracts the import clause into m[3] ("Button, { type ButtonProps } from ").
  2. Line 138 calls if (allNamedSpecifiersAreTypeOnly(m[3])) continue;.
  3. allNamedSpecifiersAreTypeOnly isolates only the content within { ... }:
function allNamedSpecifiersAreTypeOnly(clause = '') {
  const m = clause.match(/\{([\s\S]*?)\}/);
  if (!m) return false;
  const specs = stripComments(m[1]).split(',').map((s) => s.trim()).filter(Boolean);
  return specs.length > 0 && specs.every((s) => /^type\s+[$\w]+(?:\s+as\s+[$\w]+)?$/.test(s));
}

Because every specifier inside the curly braces begins with type, specs.every(...) evaluates to true. The function does not check whether a default value identifier precedes the opening brace {. The scanner executes continue and discards the runtime value import (Button).

Reproduction

Run the following script with Node.js:

function allNamedSpecifiersAreTypeOnly(clause = '') {
  const m = clause.match(/\{([\s\S]*?)\}/);
  if (!m) return false;
  const specs = m[1].split(',').map((s) => s.trim()).filter(Boolean);
  return specs.length > 0 && specs.every((s) => /^type\s+[$\w]+(?:\s+as\s+[$\w]+)?$/.test(s));
}

const importLine = "import Button, { type ButtonProps } from '@acme/ui';";
const importRe = /^[ \t]*(import|export)\s+(type\s+)?((?:(?!\n[ \t]*(?:import|export)\b)[\w$*{},\s]|\/\/[^\n]*\n|\/\*[\s\S]*?\*\/)*?\bfrom\s*)?['"]([^'"\n]+)['"]/gm;

const match = [...importLine.matchAll(importRe)][0];

console.log("Clause:", JSON.stringify(match[3]));
console.log("Skipped as type-only:", allNamedSpecifiersAreTypeOnly(match[3]));

Expected behavior: Button is recognized as a runtime value import and retained.
Actual behavior: allNamedSpecifiersAreTypeOnly returns true, and the import is dropped.

Impact

Because all checks depend on scan.json, dropping these imports creates false negatives:

  • ci-coverage/undeclared-workspace-dependency: Misses undeclared workspace packages imported as default values.
  • boundaries/deep-import: Misses deep imports of default exports.
  • circular-deps/file-cycle: Misses intra-package edges, failing to detect dependency cycles.

Proposed Fix

Inspect the substring preceding the opening brace in clause. If non-whitespace characters exist before {, treat the statement as containing a runtime default import:

function allNamedSpecifiersAreTypeOnly(clause = '') {
  const m = clause.match(/\{([\s\S]*?)\}/);
  if (!m) return false;

  const beforeBrace = clause.slice(0, m.index).replace(/[\s,]/g, '');
  if (beforeBrace.length > 0) return false;

  const specs = stripComments(m[1]).split(',').map((s) => s.trim()).filter(Boolean);
  return specs.length > 0 && specs.every((s) => /^type\s+[$\w]+(?:\s+as\s+[$\w]+)?$/.test(s));
}

Activity

  1. Kaap10 commented on Oct 12, 2026

    @Kaap10
    ContributorAuthor

    This covers a parser edge-case in scan.mjs where mixed default and type imports silently drop runtime dependencies.
    Happy to open a PR with the fix if this looks good @santoshyadavdev

  2. santoshyadavdev commented on Oct 12, 2026

    @santoshyadavdev
    Owner

    This covers a parser edge-case in scan.mjs where mixed default and type imports silently drop runtime dependencies.
    Happy to open a PR with the fix if this looks good @santoshyadavdev

    Thank you 💙 go for it

  3. added 2 commits that reference this issue on Oct 12, 2026
    dcac2d7
    b19c430
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions