Repository navigation
Conversation
8158cfd to
8f9663e
Compare
|
@mliemm Can you provide an example of a 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`.
8f9663e to
ae95104
Compare
|
Sure. This is the setup I had in mind, and it's now in the PR as the {
"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
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 constantAlso fixed a bug these caught (ae95104): without |
|
@mliemm Thank you for sharing the example. Based on this, I think this PR is only partially be valid:
|
Problem
exportConstantsaccepted only localconstdeclarations. The schema did not exposefrom, and the evaluator ignored module sources, so a constant re-export could not be constrained by its module. Structuralschemaandtypechecks also ran without a local declaration to inspect.Fixes #74.
Change
fromtoExportConstantDefinitionV1Schema, reusing the existing constant variants and regenerating both JSON schemas.from, match a named re-export from that module. Withoutfrom, match a constant declared in the file, exported inline or withexport { name }. A local function exported by name and a re-export from another module are still reported, as onmain.schemaandtypechecks only when a local constant declaration exists.fromin 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
fromis 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 nofromis set, and a type-only re-export. Theexport-constants-fromandexport-constants-from-brokene2e fixtures run the same cases through the CLI. Updated the config schema tests for the new accepted and rejected shapes.pnpm buildpnpm --filter konsistent typecheckpnpm --filter konsistent exec vitest run --testTimeout=30000pnpm --filter @konsistent/convention testpnpm checkpnpm test:e2e