Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-core): adding identifier on validAssetId if variable name has specific character #4429

Merged
merged 1 commit into from Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/compiler-core/__tests__/utils.spec.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'
)
})
5 changes: 4 additions & 1 deletion packages/compiler-core/src/utils.ts
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer this:

return `_${type}_${name.replace(/[^\w]/g, function($1,$2){
	return $1==='-'?'_': name.charCodeAt($2)
})}`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think change it in this way is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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
Expand Down