Skip to content

Commit 5e2c8e1

Browse files
committed
fix: resolve kebab-case directive names
1 parent b6c17b3 commit 5e2c8e1

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/babel-plugin-jsx/src/parseDirectives.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as t from '@babel/types';
22
import { type NodePath } from '@babel/traverse';
3-
import { createIdentifier } from './utils';
3+
import { camelize, capitalize, createIdentifier } from './utils';
44
import type { State } from './interface';
55

66
export type Tag =
@@ -184,8 +184,7 @@ const resolveDirective = (
184184
}
185185
return modelToUse;
186186
}
187-
const referenceName =
188-
'v' + directiveName[0].toUpperCase() + directiveName.slice(1);
187+
const referenceName = 'v' + capitalize(camelize(directiveName));
189188
let scope = path.scope;
190189
do {
191190
if (scope.references[referenceName]) {

packages/babel-plugin-jsx/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ const onRE = /^on[^a-z]/;
254254

255255
export const isOn = (key: string) => onRE.test(key);
256256

257+
const camelizeRE = /-(\w)/g;
258+
export const camelize = (str: string): string => {
259+
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''));
260+
};
261+
export const capitalize = <T extends string>(str: T): Capitalize<T> => {
262+
return (str.charAt(0).toUpperCase() + str.slice(1)) as Capitalize<T>;
263+
};
264+
257265
const mergeAsArray = (
258266
existing: t.ObjectProperty,
259267
incoming: t.ObjectProperty

0 commit comments

Comments
 (0)