Skip to content

Commit 3282750

Browse files
fix(compiler-core): fix duplicated component identifier for names with non-ascii chars (#4429)
fix #4422
1 parent 586ec51 commit 3282750

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/compiler-core/__tests__/utils.spec.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { Position } from '../src/ast'
22
import {
33
getInnerRange,
44
advancePositionWithClone,
5-
isMemberExpression
5+
isMemberExpression,
6+
toValidAssetId
67
} from '../src/utils'
78

89
function p(line: number, column: number, offset: number): Position {
@@ -107,3 +108,13 @@ test('isMemberExpression', () => {
107108
expect(isMemberExpression('a?b:c')).toBe(false)
108109
expect(isMemberExpression(`state['text'] = $event`)).toBe(false)
109110
})
111+
112+
test('toValidAssetId', () => {
113+
expect(toValidAssetId('foo', 'component')).toBe('_component_foo')
114+
expect(toValidAssetId('p', 'directive')).toBe('_directive_p')
115+
expect(toValidAssetId('div', 'filter')).toBe('_filter_div')
116+
expect(toValidAssetId('foo-bar', 'component')).toBe('_component_foo_bar')
117+
expect(toValidAssetId('test-测试-1', 'component')).toBe(
118+
'_component_test_2797935797_1'
119+
)
120+
})

packages/compiler-core/src/utils.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,10 @@ export function toValidAssetId(
430430
name: string,
431431
type: 'component' | 'directive' | 'filter'
432432
): string {
433-
return `_${type}_${name.replace(/[^\w]/g, '_')}`
433+
// see issue#4422, we need adding identifier on validAssetId if variable `name` has specific character
434+
return `_${type}_${name.replace(/[^\w]/g, (searchValue, replaceValue) => {
435+
return searchValue === '-' ? '_' : name.charCodeAt(replaceValue).toString()
436+
})}`
434437
}
435438

436439
// Check if a node contains expressions that reference current context scope ids

0 commit comments

Comments
 (0)