Skip to content

Commit 8525f59

Browse files
committed
minor #37 [Stimulus] Extract a normalizeIdentifier helper (Kocal)
This PR was merged into the main branch. Discussion ---------- [Stimulus] Extract a normalizeIdentifier helper | Q | A | -------------- | --- | Bug fix? | no | New feature? | no | Deprecations? | no | Documentation? | no | Issues | - | License | MIT The `_` -> `-` and `/` -> `--` Stimulus identifier normalization was inlined in both `thirdPartyIdentifier` and `localIdentifier`. This pulls it out into a small `normalizeIdentifier` helper so the rule lives in one place. Pure refactor, no behavior change. The explicit user/package-name branches keep their underscores (only the slash separator is collapsed there), so those paths are left exactly as they were. Commits ------- bee3ef9 [Stimulus] Extract normalizeIdentifier helper
2 parents 88f41b5 + bee3ef9 commit 8525f59

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)