Skip to content

Commit bee3ef9

Browse files
committed
[Stimulus] Extract normalizeIdentifier helper
1 parent 88f41b5 commit bee3ef9

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

assets/src/core/stimulus.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,23 @@ export function generateControllersModule(opts: ResolvedStimulusOptions, root: s
128128
return render([...controllers.values()], isDev);
129129
}
130130

131+
// Stimulus identifier normalization: `_` -> `-`, `/` -> `--` (e.g. `foo/bar_baz` -> `foo--bar-baz`).
132+
function normalizeIdentifier(id: string): string {
133+
return id.replace(/_/g, '-').replace(/\//g, '--');
134+
}
135+
131136
function thirdPartyIdentifier(
132137
packageName: string,
133138
controllerName: string,
134139
pkgName?: string,
135140
userName?: string
136141
): string {
142+
// Explicit user/package names keep their underscores; only the slash separator is collapsed.
137143
if (userName) return userName.replace(/\//g, '--');
138144
if (pkgName) return pkgName.replace(/\//g, '--');
139145
let id = `${packageName}/${controllerName}`;
140146
if (id.startsWith('@')) id = id.slice(1);
141-
return id.replace(/_/g, '-').replace(/\//g, '--');
147+
return normalizeIdentifier(id);
142148
}
143149

144150
function render(controllers: ResolvedController[], isDev: boolean): string {
@@ -196,5 +202,5 @@ function listLocalControllers(dir: string): string[] {
196202

197203
function localIdentifier(rel: string): string {
198204
const base = slash(rel).replace(/[-_]controller\.[jt]s$/, '');
199-
return base.replace(/_/g, '-').replace(/\//g, '--');
205+
return normalizeIdentifier(base);
200206
}

0 commit comments

Comments
 (0)