Skip to content

fix(export-constants): honor from for constant re-exports - #129

Open
mliemm wants to merge 2 commits into
vercel-labs:mainfrom
mliemm:fix/export-constants-from
Open

mliemm wants to merge 2 commits into
vercel-labs:mainfrom
mliemm:fix/export-constants-from

Conversation

@mliemm

@mliemm mliemm commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem

exportConstants accepted only local const declarations. The schema did not expose from, and the evaluator ignored module sources, so a constant re-export could not be constrained by its module. Structural schema and type checks also ran without a local declaration to inspect.

Fixes #74.

Change

  • Add from to ExportConstantDefinitionV1Schema, reusing the existing constant variants and regenerating both JSON schemas.
  • With from, match a named re-export from that module. Without from, match a constant declared in the file, exported inline or with export { name }. A local function exported by name and a re-export from another module are still reported, as on main.
  • Apply schema and type checks only when a local constant declaration exists.
  • Document from in the predicate reference and violation guide.

Tests

Added coverage for a correct re-export, a re-export from the wrong module, a local constant when from is required, and a re-export with a structural constraint. Unit tests also cover a local constant exported by name, a local function exported by name, a re-export when no from is set, and a type-only re-export. The export-constants-from and export-constants-from-broken e2e fixtures run the same cases through the CLI. Updated the config schema tests for the new accepted and rejected shapes.

  • pnpm build
  • pnpm --filter konsistent typecheck
  • pnpm --filter konsistent exec vitest run --testTimeout=30000
  • pnpm --filter @konsistent/convention test
  • pnpm check
  • pnpm test:e2e

Copilot AI balanced review requested due to automatic review settings November 11, 2026 06:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@mliemm
mliemm force-pushed the fix/export-constants-from branch from 8158cfd to 8f9663e Compare November 11, 2026 06:53
@felixarntz

Copy link
Copy Markdown
Collaborator

@mliemm Can you provide an example of a konsistent.json and file(s) and for how you would want these to behave please?

Ideally you could add e2e coverage with fixtures to the PR too.

exportConstants ignored the from field and only recognized local const declarations. Accept constant re-exports from the configured module, skip schema and type checks when no local declaration exists, and add from to the generated config schema.

Fixes vercel-labs#74
Without `from`, a named export now counts only if it names a constant
declared in the file, as the predicate reference says. Before this, any
named export passed, so `export { createClient }` of a local function
and `export { X } from "./config"` both satisfied `exportConstants`; on
main both are reported. With `from`, only a re-export from that module
counts.

Add e2e fixtures for a config that uses `from`: re-exports from the
configured module, a local constant beside them, and in the broken
fixture a wrong module, a type-only re-export, a local constant where a
re-export is required, a local function and a re-export without `from`.
@mliemm
mliemm force-pushed the fix/export-constants-from branch from 8f9663e to ae95104 Compare November 25, 2026 14:26
@mliemm

mliemm commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Sure. This is the setup I had in mind, and it's now in the PR as the export-constants-from e2e fixture:

{
  "version": "v1",
  "conventions": [
    {
      "name": "export-constants-from",
      "paths": "src/index.ts",
      "must": {
        "exportConstants": [
          { "name": "API_URL", "from": "./config" },
          { "name": "TIMEOUT", "from": "./config" },
          { "name": "VERSION", "type": "string" },
          { "name": "MAX_RETRIES" }
        ]
      }
    }
  ]
}
// src/index.ts
export { API_URL, TIMEOUT } from "./config";

export const VERSION: string = "1.0.0";

const MAX_RETRIES = 3;
export { MAX_RETRIES };

That passes. An entry with from needs a named re-export from that module. An entry without it needs a constant declared in the file, exported either inline or with export { name }.

export-constants-from-broken checks this file, and each export below is reported as Missing export constant:

export { API_URL } from "./other";          // config wants it from "./config"
export type { RETRIES } from "./config";    // type-only, so not a constant
export { DEFAULT_LOCALE } from "./config";  // the entry has no `from`
export const TIMEOUT = 30;                  // config wants a re-export from "./config"

function createClient() {}
export { createClient };                    // a function, not a constant

Also fixed a bug these caught (ae95104): without from, any export with the right name passed, including a local function like createClient or a re-export from another module. Now only a local constant counts, like on main.

@felixarntz

Copy link
Copy Markdown
Collaborator

@mliemm Thank you for sharing the example.

Based on this, I think this PR is only partially be valid:

  • For re-exports of symbols from other files, konsistent already provides exportValues. Re-exports aren't qualified for what they are for, you can't tell from an export { ... } from ... whether that value is a constant, a function, or something else. So adding a from to exportConstants isn't necessary, that's what exportValues is for.
  • The more relevant example is the const MAX_RETRIES = 3; export { MAX_RETRIES }; you provided: That is something exportConstants should catch, but it isn't currently. The same bug probably exists for the other specific export* predicates, exportFunctions etc. Though it is arguable whether those should even catch it, or whether it should used a combination of declareConstants / declareFunctions with an exportValues without from. Functionally you could argue it should be covered by exportConstants / exportFunctions, but it's still a stylistic difference you may want to enforce, so I'm not sure we should cover those there. Doesn't it work if you use declareConstants with exportValues for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Honor from for constant export predicates

3 participants