Skip to content

Commit

Permalink
fix: when render function is optimized inside script setup, not work (#…
Browse files Browse the repository at this point in the history
…225)

* fix: when render function is optimized inside script setup, not work

* update

* fix
  • Loading branch information
kazupon committed Jun 11, 2024
1 parent 47fc7b9 commit 0369e7b
Show file tree
Hide file tree
Showing 4 changed files with 327 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This changelog is generated by [GitHub Releases](https://github.com/intlify/vue-

###    🐞 Bug Fixes

- Fully cjs/esm dual pacakges &nbsp;-&nbsp; by @kazupon in https://github.com/intlify/vue-i18n-extensions/issues/222 [<samp>(05597)</samp>](https://github.com/intlify/vue-i18n-extensions/commit/05597cb)
- Fully cjs/esm dual packages &nbsp;-&nbsp; by @kazupon in https://github.com/intlify/vue-i18n-extensions/issues/222 [<samp>(05597)</samp>](https://github.com/intlify/vue-i18n-extensions/commit/05597cb)

##### &nbsp;&nbsp;&nbsp;&nbsp;[View changes on GitHub](https://github.com/intlify/vue-i18n-extensions/compare/v6.0.4...v6.0.5)

Expand Down
9 changes: 7 additions & 2 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ const enum ConstantTypes {
CAN_STRINGIFY
}

const GLOBAL_TRANSLATE_SIGNATURE = '$t'

/**
* Transform `v-t` custom directive
*
Expand Down Expand Up @@ -166,7 +168,7 @@ export function transformVTDirective<
: isArray(options.translationSignatures)
? options.translationSignatures
: ['t']
translationSignatures.push(`$t`) // fallback to global scope
translationSignatures.push(GLOBAL_TRANSLATE_SIGNATURE) // fallback to global scope

return (dir, node, context) => {
const { exp, loc } = dir
Expand Down Expand Up @@ -407,9 +409,12 @@ function generateTranslationCallableSignatures(
context: TransformContext,
translationSignatures: string[]
): string {
const { prefixIdentifiers, bindingMetadata } = context
const { prefixIdentifiers, bindingMetadata, inline } = context
return translationSignatures
.map(signature => {
if (inline && signature !== GLOBAL_TRANSLATE_SIGNATURE) {
return signature
}
const type = hasOwn(bindingMetadata, signature) && bindingMetadata[signature]
const bindingContext = prefixIdentifiers
? (type && type.startsWith('setup')) || type === BindingTypes.LITERAL_CONST
Expand Down
301 changes: 301 additions & 0 deletions test/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5839,3 +5839,304 @@ exports[`script setup > <div v-t="'hello'"/> 2`] = `
"type": 0,
}
`;

exports[`script setup inline > <div v-t="'hello'"/> 1`] = `
"(_ctx, _cache) => {
return (_openBlock(), _createElementBlock("div", null, _toDisplayString((t || _ctx.$t)('hello', { }, { })), 1 /* TEXT */))
}"
`;

exports[`script setup inline > <div v-t="'hello'"/> 2`] = `
{
"cached": 0,
"children": [
{
"children": [
{
"content": {
"children": [
{
"constType": 0,
"content": "(t || _ctx.$t)('hello', { }, { })",
"isStatic": false,
"loc": {
"end": {
"column": 19,
"line": 1,
"offset": 18,
},
"source": "v-t="'hello'"",
"start": {
"column": 6,
"line": 1,
"offset": 5,
},
},
"type": 4,
},
],
"loc": {
"end": {
"column": 1,
"line": 1,
"offset": 0,
},
"source": "",
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": 8,
},
"type": 5,
},
],
"codegenNode": {
"children": {
"content": {
"children": [
{
"constType": 0,
"content": "(t || _ctx.$t)('hello', { }, { })",
"isStatic": false,
"loc": {
"end": {
"column": 19,
"line": 1,
"offset": 18,
},
"source": "v-t="'hello'"",
"start": {
"column": 6,
"line": 1,
"offset": 5,
},
},
"type": 4,
},
],
"loc": {
"end": {
"column": 1,
"line": 1,
"offset": 0,
},
"source": "",
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": 8,
},
"type": 5,
},
"directives": undefined,
"disableTracking": false,
"dynamicProps": undefined,
"isBlock": true,
"isComponent": false,
"loc": {
"end": {
"column": 21,
"line": 1,
"offset": 20,
},
"source": "<div v-t="'hello'"/>",
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"patchFlag": "1 /* TEXT */",
"props": undefined,
"tag": ""div"",
"type": 13,
},
"isSelfClosing": true,
"loc": {
"end": {
"column": 21,
"line": 1,
"offset": 20,
},
"source": "<div v-t="'hello'"/>",
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"ns": 0,
"props": [
{
"arg": undefined,
"exp": {
"ast": Node {
"comments": [],
"end": 8,
"errors": [],
"extra": {
"parenStart": 0,
"parenthesized": true,
"raw": "'hello'",
"rawValue": "hello",
},
"loc": SourceLocation {
"end": Position {
"column": 8,
"index": 8,
"line": 1,
},
"filename": undefined,
"identifierName": undefined,
"start": Position {
"column": 1,
"index": 1,
"line": 1,
},
},
"start": 1,
"type": "StringLiteral",
"value": "hello",
},
"constType": 3,
"content": "'hello'",
"identifiers": [],
"isStatic": false,
"loc": {
"end": {
"column": 18,
"line": 1,
"offset": 17,
},
"source": "'hello'",
"start": {
"column": 11,
"line": 1,
"offset": 10,
},
},
"type": 4,
},
"loc": {
"end": {
"column": 19,
"line": 1,
"offset": 18,
},
"source": "v-t="'hello'"",
"start": {
"column": 6,
"line": 1,
"offset": 5,
},
},
"modifiers": [],
"name": "t",
"rawName": "v-t",
"type": 7,
},
],
"tag": "div",
"tagType": 0,
"type": 1,
},
],
"codegenNode": {
"children": {
"content": {
"children": [
{
"constType": 0,
"content": "(t || _ctx.$t)('hello', { }, { })",
"isStatic": false,
"loc": {
"end": {
"column": 19,
"line": 1,
"offset": 18,
},
"source": "v-t="'hello'"",
"start": {
"column": 6,
"line": 1,
"offset": 5,
},
},
"type": 4,
},
],
"loc": {
"end": {
"column": 1,
"line": 1,
"offset": 0,
},
"source": "",
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": 8,
},
"type": 5,
},
"directives": undefined,
"disableTracking": false,
"dynamicProps": undefined,
"isBlock": true,
"isComponent": false,
"loc": {
"end": {
"column": 21,
"line": 1,
"offset": 20,
},
"source": "<div v-t="'hello'"/>",
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"patchFlag": "1 /* TEXT */",
"props": undefined,
"tag": ""div"",
"type": 13,
},
"components": [],
"directives": [],
"filters": [],
"helpers": Set {
Symbol(toDisplayString),
Symbol(openBlock),
Symbol(createElementBlock),
},
"hoists": [],
"imports": [],
"loc": {
"end": {
"column": 21,
"line": 1,
"offset": 20,
},
"source": "<div v-t="'hello'"/>",
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"source": "<div v-t="'hello'"/>",
"temps": 0,
"transformed": true,
"type": 0,
}
`;
18 changes: 18 additions & 0 deletions test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ test('script setup', () => {
expect(ast).toMatchSnapshot(source)
})

test('script setup inline', () => {
const transformVT = transformVTDirective()
const source = `<div v-t="'hello'"/>`
const bindingMetadata = {
t: BindingTypes.SETUP_CONST
}
const { code, ast } = compile(source, {
mode: 'function',
hoistStatic: false,
prefixIdentifiers: true,
bindingMetadata,
inline: true,
directiveTransforms: { t: transformVT }
})
expect(code).toMatchSnapshot(source)
expect(ast).toMatchSnapshot(source)
})

describe('legacy', () => {
test('path', () => {
const transformVT = transformVTDirective({ mode: 'legacy' })
Expand Down

0 comments on commit 0369e7b

Please sign in to comment.