Skip to content

Commit 8a449c2

Browse files
committed
refactor(cli): hoist gen name validators to module constants
The name-validation regexes in the gen arg parsers were inline literals, recreating a RegExp on every parser call and duplicating two identical patterns across five sites. Hoist them to two module-level constants (NAME_RE for model/migration, NAME_WITH_HYPHENS_RE for middleware/cron/ queue). Behavior is unchanged (anchored, non-global validators).
1 parent 4794832 commit 8a449c2

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

‎packages/void/src/cli/gen-args.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ const COLUMN_TYPES = new Set(['string', 'text', 'datetime', 'integer', 'boolean'
5757

5858
const VALID_METHODS = new Set(['get', 'post', 'put', 'patch', 'delete']);
5959

60+
// Generated-file name validators (hoisted once); *_HYPHENS variant also allows hyphens (cron/queue/middleware).
61+
const NAME_RE = /^[a-z_][a-z0-9_]*$/;
62+
const NAME_WITH_HYPHENS_RE = /^[a-z_][a-z0-9_-]*$/;
63+
6064
function parseModelArgs(args: Array<string>): GenArgs {
6165
const [name, ...rest] = args;
6266
if (!name) {
6367
throw new Error('gen: Usage: void gen model <name> [columns...].');
6468
}
65-
if (!/^[a-z_][a-z0-9_]*$/.test(name)) {
69+
if (!NAME_RE.test(name)) {
6670
throw new Error(
6771
'gen: Model name must be lowercase alphanumeric with underscores and no leading digits.',
6872
);
@@ -94,7 +98,7 @@ function parseMigrationArgs(args: Array<string>): GenArgs {
9498
if (extra) {
9599
throw new Error(`gen: Unexpected argument '${extra}'.`);
96100
}
97-
if (!/^[a-z_][a-z0-9_]*$/.test(name)) {
101+
if (!NAME_RE.test(name)) {
98102
throw new Error(
99103
'gen: Migration name must be lowercase alphanumeric with underscores and no leading digits.',
100104
);
@@ -145,7 +149,7 @@ function parseMiddlewareArgs(args: Array<string>): GenArgs {
145149
if (extra) {
146150
throw new Error(`gen: Unexpected argument '${extra}'.`);
147151
}
148-
if (!/^[a-z_][a-z0-9_-]*$/.test(name)) {
152+
if (!NAME_WITH_HYPHENS_RE.test(name)) {
149153
throw new Error(
150154
'gen: Middleware name must be lowercase alphanumeric with underscores or hyphens and no leading digits.',
151155
);
@@ -186,7 +190,7 @@ function parseCronArgs(args: Array<string>): GenArgs {
186190
if (extra) {
187191
throw new Error(`gen: Unexpected argument '${extra}'.`);
188192
}
189-
if (!/^[a-z_][a-z0-9_-]*$/.test(name)) {
193+
if (!NAME_WITH_HYPHENS_RE.test(name)) {
190194
throw new Error(
191195
'gen: Cron name must be lowercase alphanumeric with underscores or hyphens and no leading digits.',
192196
);
@@ -202,7 +206,7 @@ function parseQueueArgs(args: Array<string>): GenArgs {
202206
if (extra) {
203207
throw new Error(`gen: Unexpected argument '${extra}'.`);
204208
}
205-
if (!/^[a-z_][a-z0-9_-]*$/.test(name)) {
209+
if (!NAME_WITH_HYPHENS_RE.test(name)) {
206210
throw new Error(
207211
'gen: Queue name must be lowercase alphanumeric with underscores or hyphens and no leading digits.',
208212
);

‎packages/void/test/unit/project-args.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ describe('parseProjectArgs', () => {
103103
});
104104

105105
describe('unknown-flag rejection', () => {
106-
// `delete`/`logs`/`purge-cache` already reject unknown flags; status/link/
107-
// rollback/cancel previously captured a leading flag as the positional id.
106+
// status/link/rollback/cancel previously captured a leading flag as the positional id.
108107
it('rejects an unknown flag for status instead of capturing it as the name', () => {
109108
expect(() => parseProjectArgs(['status', '--foo'])).toThrow("project: Unknown flag '--foo'.");
110109
});

0 commit comments

Comments
 (0)