Skip to content

Commit 0369e7b

Browse files
authored
fix: when render function is optimized inside script setup, not work (#225)
* fix: when render function is optimized inside script setup, not work * update * fix
1 parent 47fc7b9 commit 0369e7b

File tree

4 files changed

+327
-3
lines changed

4 files changed

+327
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This changelog is generated by [GitHub Releases](https://github.com/intlify/vue-
44

55
###    🐞 Bug Fixes
66

7-
- 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)
7+
- 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)
88

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

src/transform.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ const enum ConstantTypes {
9999
CAN_STRINGIFY
100100
}
101101

102+
const GLOBAL_TRANSLATE_SIGNATURE = '$t'
103+
102104
/**
103105
* Transform `v-t` custom directive
104106
*
@@ -166,7 +168,7 @@ export function transformVTDirective<
166168
: isArray(options.translationSignatures)
167169
? options.translationSignatures
168170
: ['t']
169-
translationSignatures.push(`$t`) // fallback to global scope
171+
translationSignatures.push(GLOBAL_TRANSLATE_SIGNATURE) // fallback to global scope
170172

171173
return (dir, node, context) => {
172174
const { exp, loc } = dir
@@ -407,9 +409,12 @@ function generateTranslationCallableSignatures(
407409
context: TransformContext,
408410
translationSignatures: string[]
409411
): string {
410-
const { prefixIdentifiers, bindingMetadata } = context
412+
const { prefixIdentifiers, bindingMetadata, inline } = context
411413
return translationSignatures
412414
.map(signature => {
415+
if (inline && signature !== GLOBAL_TRANSLATE_SIGNATURE) {
416+
return signature
417+
}
413418
const type = hasOwn(bindingMetadata, signature) && bindingMetadata[signature]
414419
const bindingContext = prefixIdentifiers
415420
? (type && type.startsWith('setup')) || type === BindingTypes.LITERAL_CONST

test/__snapshots__/transform.test.ts.snap

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5839,3 +5839,304 @@ exports[`script setup > <div v-t="'hello'"/> 2`] = `
58395839
"type": 0,
58405840
}
58415841
`;
5842+
5843+
exports[`script setup inline > <div v-t="'hello'"/> 1`] = `
5844+
"(_ctx, _cache) => {
5845+
return (_openBlock(), _createElementBlock("div", null, _toDisplayString((t || _ctx.$t)('hello', { }, { })), 1 /* TEXT */))
5846+
}"
5847+
`;
5848+
5849+
exports[`script setup inline > <div v-t="'hello'"/> 2`] = `
5850+
{
5851+
"cached": 0,
5852+
"children": [
5853+
{
5854+
"children": [
5855+
{
5856+
"content": {
5857+
"children": [
5858+
{
5859+
"constType": 0,
5860+
"content": "(t || _ctx.$t)('hello', { }, { })",
5861+
"isStatic": false,
5862+
"loc": {
5863+
"end": {
5864+
"column": 19,
5865+
"line": 1,
5866+
"offset": 18,
5867+
},
5868+
"source": "v-t="'hello'"",
5869+
"start": {
5870+
"column": 6,
5871+
"line": 1,
5872+
"offset": 5,
5873+
},
5874+
},
5875+
"type": 4,
5876+
},
5877+
],
5878+
"loc": {
5879+
"end": {
5880+
"column": 1,
5881+
"line": 1,
5882+
"offset": 0,
5883+
},
5884+
"source": "",
5885+
"start": {
5886+
"column": 1,
5887+
"line": 1,
5888+
"offset": 0,
5889+
},
5890+
},
5891+
"type": 8,
5892+
},
5893+
"type": 5,
5894+
},
5895+
],
5896+
"codegenNode": {
5897+
"children": {
5898+
"content": {
5899+
"children": [
5900+
{
5901+
"constType": 0,
5902+
"content": "(t || _ctx.$t)('hello', { }, { })",
5903+
"isStatic": false,
5904+
"loc": {
5905+
"end": {
5906+
"column": 19,
5907+
"line": 1,
5908+
"offset": 18,
5909+
},
5910+
"source": "v-t="'hello'"",
5911+
"start": {
5912+
"column": 6,
5913+
"line": 1,
5914+
"offset": 5,
5915+
},
5916+
},
5917+
"type": 4,
5918+
},
5919+
],
5920+
"loc": {
5921+
"end": {
5922+
"column": 1,
5923+
"line": 1,
5924+
"offset": 0,
5925+
},
5926+
"source": "",
5927+
"start": {
5928+
"column": 1,
5929+
"line": 1,
5930+
"offset": 0,
5931+
},
5932+
},
5933+
"type": 8,
5934+
},
5935+
"type": 5,
5936+
},
5937+
"directives": undefined,
5938+
"disableTracking": false,
5939+
"dynamicProps": undefined,
5940+
"isBlock": true,
5941+
"isComponent": false,
5942+
"loc": {
5943+
"end": {
5944+
"column": 21,
5945+
"line": 1,
5946+
"offset": 20,
5947+
},
5948+
"source": "<div v-t="'hello'"/>",
5949+
"start": {
5950+
"column": 1,
5951+
"line": 1,
5952+
"offset": 0,
5953+
},
5954+
},
5955+
"patchFlag": "1 /* TEXT */",
5956+
"props": undefined,
5957+
"tag": ""div"",
5958+
"type": 13,
5959+
},
5960+
"isSelfClosing": true,
5961+
"loc": {
5962+
"end": {
5963+
"column": 21,
5964+
"line": 1,
5965+
"offset": 20,
5966+
},
5967+
"source": "<div v-t="'hello'"/>",
5968+
"start": {
5969+
"column": 1,
5970+
"line": 1,
5971+
"offset": 0,
5972+
},
5973+
},
5974+
"ns": 0,
5975+
"props": [
5976+
{
5977+
"arg": undefined,
5978+
"exp": {
5979+
"ast": Node {
5980+
"comments": [],
5981+
"end": 8,
5982+
"errors": [],
5983+
"extra": {
5984+
"parenStart": 0,
5985+
"parenthesized": true,
5986+
"raw": "'hello'",
5987+
"rawValue": "hello",
5988+
},
5989+
"loc": SourceLocation {
5990+
"end": Position {
5991+
"column": 8,
5992+
"index": 8,
5993+
"line": 1,
5994+
},
5995+
"filename": undefined,
5996+
"identifierName": undefined,
5997+
"start": Position {
5998+
"column": 1,
5999+
"index": 1,
6000+
"line": 1,
6001+
},
6002+
},
6003+
"start": 1,
6004+
"type": "StringLiteral",
6005+
"value": "hello",
6006+
},
6007+
"constType": 3,
6008+
"content": "'hello'",
6009+
"identifiers": [],
6010+
"isStatic": false,
6011+
"loc": {
6012+
"end": {
6013+
"column": 18,
6014+
"line": 1,
6015+
"offset": 17,
6016+
},
6017+
"source": "'hello'",
6018+
"start": {
6019+
"column": 11,
6020+
"line": 1,
6021+
"offset": 10,
6022+
},
6023+
},
6024+
"type": 4,
6025+
},
6026+
"loc": {
6027+
"end": {
6028+
"column": 19,
6029+
"line": 1,
6030+
"offset": 18,
6031+
},
6032+
"source": "v-t="'hello'"",
6033+
"start": {
6034+
"column": 6,
6035+
"line": 1,
6036+
"offset": 5,
6037+
},
6038+
},
6039+
"modifiers": [],
6040+
"name": "t",
6041+
"rawName": "v-t",
6042+
"type": 7,
6043+
},
6044+
],
6045+
"tag": "div",
6046+
"tagType": 0,
6047+
"type": 1,
6048+
},
6049+
],
6050+
"codegenNode": {
6051+
"children": {
6052+
"content": {
6053+
"children": [
6054+
{
6055+
"constType": 0,
6056+
"content": "(t || _ctx.$t)('hello', { }, { })",
6057+
"isStatic": false,
6058+
"loc": {
6059+
"end": {
6060+
"column": 19,
6061+
"line": 1,
6062+
"offset": 18,
6063+
},
6064+
"source": "v-t="'hello'"",
6065+
"start": {
6066+
"column": 6,
6067+
"line": 1,
6068+
"offset": 5,
6069+
},
6070+
},
6071+
"type": 4,
6072+
},
6073+
],
6074+
"loc": {
6075+
"end": {
6076+
"column": 1,
6077+
"line": 1,
6078+
"offset": 0,
6079+
},
6080+
"source": "",
6081+
"start": {
6082+
"column": 1,
6083+
"line": 1,
6084+
"offset": 0,
6085+
},
6086+
},
6087+
"type": 8,
6088+
},
6089+
"type": 5,
6090+
},
6091+
"directives": undefined,
6092+
"disableTracking": false,
6093+
"dynamicProps": undefined,
6094+
"isBlock": true,
6095+
"isComponent": false,
6096+
"loc": {
6097+
"end": {
6098+
"column": 21,
6099+
"line": 1,
6100+
"offset": 20,
6101+
},
6102+
"source": "<div v-t="'hello'"/>",
6103+
"start": {
6104+
"column": 1,
6105+
"line": 1,
6106+
"offset": 0,
6107+
},
6108+
},
6109+
"patchFlag": "1 /* TEXT */",
6110+
"props": undefined,
6111+
"tag": ""div"",
6112+
"type": 13,
6113+
},
6114+
"components": [],
6115+
"directives": [],
6116+
"filters": [],
6117+
"helpers": Set {
6118+
Symbol(toDisplayString),
6119+
Symbol(openBlock),
6120+
Symbol(createElementBlock),
6121+
},
6122+
"hoists": [],
6123+
"imports": [],
6124+
"loc": {
6125+
"end": {
6126+
"column": 21,
6127+
"line": 1,
6128+
"offset": 20,
6129+
},
6130+
"source": "<div v-t="'hello'"/>",
6131+
"start": {
6132+
"column": 1,
6133+
"line": 1,
6134+
"offset": 0,
6135+
},
6136+
},
6137+
"source": "<div v-t="'hello'"/>",
6138+
"temps": 0,
6139+
"transformed": true,
6140+
"type": 0,
6141+
}
6142+
`;

test/transform.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ test('script setup', () => {
135135
expect(ast).toMatchSnapshot(source)
136136
})
137137

138+
test('script setup inline', () => {
139+
const transformVT = transformVTDirective()
140+
const source = `<div v-t="'hello'"/>`
141+
const bindingMetadata = {
142+
t: BindingTypes.SETUP_CONST
143+
}
144+
const { code, ast } = compile(source, {
145+
mode: 'function',
146+
hoistStatic: false,
147+
prefixIdentifiers: true,
148+
bindingMetadata,
149+
inline: true,
150+
directiveTransforms: { t: transformVT }
151+
})
152+
expect(code).toMatchSnapshot(source)
153+
expect(ast).toMatchSnapshot(source)
154+
})
155+
138156
describe('legacy', () => {
139157
test('path', () => {
140158
const transformVT = transformVTDirective({ mode: 'legacy' })

0 commit comments

Comments
 (0)