]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): fix duplicated component identifier for names with non-ascii...
authorshadowings-zy <shadowingszy@outlook.com>
Tue, 24 Aug 2021 15:48:08 +0000 (23:48 +0800)
committerGitHub <noreply@github.com>
Tue, 24 Aug 2021 15:48:08 +0000 (11:48 -0400)
fix #4422

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

index 0cd73060c5bce27f6858daeb32ae07665e3a3d10..3514391b74ff59fc689782a0c58a20b74fe2e629 100644 (file)
@@ -2,7 +2,8 @@ import { Position } from '../src/ast'
 import {
   getInnerRange,
   advancePositionWithClone,
-  isMemberExpression
+  isMemberExpression,
+  toValidAssetId
 } from '../src/utils'
 
 function p(line: number, column: number, offset: number): Position {
@@ -107,3 +108,13 @@ test('isMemberExpression', () => {
   expect(isMemberExpression('a?b:c')).toBe(false)
   expect(isMemberExpression(`state['text'] = $event`)).toBe(false)
 })
+
+test('toValidAssetId', () => {
+  expect(toValidAssetId('foo', 'component')).toBe('_component_foo')
+  expect(toValidAssetId('p', 'directive')).toBe('_directive_p')
+  expect(toValidAssetId('div', 'filter')).toBe('_filter_div')
+  expect(toValidAssetId('foo-bar', 'component')).toBe('_component_foo_bar')
+  expect(toValidAssetId('test-测试-1', 'component')).toBe(
+    '_component_test_2797935797_1'
+  )
+})
index 88e91d6c9be48d1846d9224aa6e7c35323728ea2..e3bc04bec69262aa2efc69841ae5824dbd4412cb 100644 (file)
@@ -430,7 +430,10 @@ export function toValidAssetId(
   name: string,
   type: 'component' | 'directive' | 'filter'
 ): string {
-  return `_${type}_${name.replace(/[^\w]/g, '_')}`
+  // see issue#4422, we need adding identifier on validAssetId if variable `name` has specific character
+  return `_${type}_${name.replace(/[^\w]/g, (searchValue, replaceValue) => {
+    return searchValue === '-' ? '_' : name.charCodeAt(replaceValue).toString()
+  })}`
 }
 
 // Check if a node contains expressions that reference current context scope ids