Skip to content

Commit dc538ab

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 dc538ab

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,18 @@ 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 (anchored, stateless — defined once, not per call).
61+
// Names are lowercase, alphanumeric with underscores, and may not start with a digit.
62+
// Cron, queue, and middleware names additionally allow hyphens.
63+
const NAME_RE = /^[a-z_][a-z0-9_]*$/;
64+
const NAME_WITH_HYPHENS_RE = /^[a-z_][a-z0-9_-]*$/;
65+
6066
function parseModelArgs(args: Array<string>): GenArgs {
6167
const [name, ...rest] = args;
6268
if (!name) {
6369
throw new Error('gen: Usage: void gen model <name> [columns...].');
6470
}
65-
if (!/^[a-z_][a-z0-9_]*$/.test(name)) {
71+
if (!NAME_RE.test(name)) {
6672
throw new Error(
6773
'gen: Model name must be lowercase alphanumeric with underscores and no leading digits.',
6874
);
@@ -94,7 +100,7 @@ function parseMigrationArgs(args: Array<string>): GenArgs {
94100
if (extra) {
95101
throw new Error(`gen: Unexpected argument '${extra}'.`);
96102
}
97-
if (!/^[a-z_][a-z0-9_]*$/.test(name)) {
103+
if (!NAME_RE.test(name)) {
98104
throw new Error(
99105
'gen: Migration name must be lowercase alphanumeric with underscores and no leading digits.',
100106
);
@@ -145,7 +151,7 @@ function parseMiddlewareArgs(args: Array<string>): GenArgs {
145151
if (extra) {
146152
throw new Error(`gen: Unexpected argument '${extra}'.`);
147153
}
148-
if (!/^[a-z_][a-z0-9_-]*$/.test(name)) {
154+
if (!NAME_WITH_HYPHENS_RE.test(name)) {
149155
throw new Error(
150156
'gen: Middleware name must be lowercase alphanumeric with underscores or hyphens and no leading digits.',
151157
);
@@ -186,7 +192,7 @@ function parseCronArgs(args: Array<string>): GenArgs {
186192
if (extra) {
187193
throw new Error(`gen: Unexpected argument '${extra}'.`);
188194
}
189-
if (!/^[a-z_][a-z0-9_-]*$/.test(name)) {
195+
if (!NAME_WITH_HYPHENS_RE.test(name)) {
190196
throw new Error(
191197
'gen: Cron name must be lowercase alphanumeric with underscores or hyphens and no leading digits.',
192198
);
@@ -202,7 +208,7 @@ function parseQueueArgs(args: Array<string>): GenArgs {
202208
if (extra) {
203209
throw new Error(`gen: Unexpected argument '${extra}'.`);
204210
}
205-
if (!/^[a-z_][a-z0-9_-]*$/.test(name)) {
211+
if (!NAME_WITH_HYPHENS_RE.test(name)) {
206212
throw new Error(
207213
'gen: Queue name must be lowercase alphanumeric with underscores or hyphens and no leading digits.',
208214
);

0 commit comments

Comments
 (0)