Skip to content

Commit

Permalink
fix: 配置对象下属性名非字符串格式时,无法定位到 token 位置
Browse files Browse the repository at this point in the history
  • Loading branch information
showlotus committed Apr 29, 2024
1 parent 1aadfaa commit 5cda09c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 14 deletions.
3 changes: 1 addition & 2 deletions demos/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
// "ti18n.customLanguages": ["zh-CN"],
// "ti18n.enable": true
"prettier.enable": false,
"ti18n.shortcutLanguages": ["zh-CN", "en-US", "zh_CN", "en_US", "zh_CN-with-prop", "en_US-with-prop"],
// "ti18n.configDirs": ["i18n", "locale"],
"ti18n.configDirs": ["v0.1.2/i18n"],
Expand Down
4 changes: 2 additions & 2 deletions demos/v0.1.2/i18n/expanded/js/expanded-with-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
export default {
messages: {
'Expanded.JS.WithProp.message.one': {
zh_CN: 'JS 默认导出(展开式)使用 Prop 消息 1',
"zh_CN": 'JS 默认导出(展开式)使用 Prop 消息 1',
en_US: 'JS Export Default Expanded With Prop Message One',
},
'Expanded.JS.WithProp.message.two': {
zh_CN: 'JS 默认导出(展开式)使用 Prop 消息 2',
"zh_CN": 'JS 默认导出(展开式)使用 Prop 消息 2',
en_US: 'JS Export Default Expanded With Prop Message Two',
},
},
Expand Down
4 changes: 2 additions & 2 deletions demos/v0.1.2/i18n/expanded/js/expanded.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default {
'Expanded.JS.message.one': {
zh_CN: 'JS 默认导出(展开式)消息 1',
"zh_CN": 'JS 默认导出(展开式)消息 1',
en_US: 'JS Export Default Expanded Message One',
},
'Expanded.JS.message.two': {
zh_CN: 'JS 默认导出(展开式)消息 2',
"zh_CN": 'JS 默认导出(展开式)消息 2',
en_US: 'JS Export Default Expanded Message Two',
},
}
13 changes: 13 additions & 0 deletions demos/v0.1.2/i18n/expanded/js/language-at-outer-with-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @Ti18n prop=messages
export default {
messages: {
"zh_CN": {
'Expanded.JS.LanguageAtOuterWithProp.message.one' : 'JS(展开式)外部语言且使用 Prop 消息 1',
'Expanded.JS.LanguageAtOuterWithProp.message.two' : 'JS(展开式)外部语言且使用 Prop 消息 2',
},
en_US: {
'Expanded.JS.LanguageAtOuterWithProp.message.one' : 'JS Expanded Outer Language With Prop Message One',
'Expanded.JS.LanguageAtOuterWithProp.message.two' : 'JS Expanded Outer Language With Prop Message Two',
},
},
}
2 changes: 1 addition & 1 deletion demos/v0.1.2/i18n/expanded/js/language-at-outer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
zh_CN: {
"zh_CN": {
'Expanded.JS.LanguageAtOuter.message.one': 'JS(展开式)外部语言 消息 1',
'Expanded.JS.LanguageAtOuter.message.two': 'JS(展开式)外部语言 消息 2',
},
Expand Down
2 changes: 1 addition & 1 deletion demos/v0.1.2/i18n/expanded/js/zh_CN-with-prop.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @Ti18n prop=messages
export default {
messages: {
"messages": {
'Expanded.JS.FileNameIsLanguageWithProp.message.one': '展开式 JS 文件名为语言且使用 Prop,消息 1',
'Expanded.JS.FileNameIsLanguageWithProp.message.two': '展开式 JS 文件名为语言且使用 Prop,消息 2',
},
Expand Down
4 changes: 4 additions & 0 deletions demos/v0.1.2/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const expanded = [
'Expanded.JS.WithProp.message.one',
'Expanded.JS.WithProp.message.two',

// js language at outer with prop
'Expanded.JS.LanguageAtOuterWithProp.message.one',
'Expanded.JS.LanguageAtOuterWithProp.message.two',

// js fileName is language
'Expanded.JS.FileNameIsLanguage.message.one',
'Expanded.JS.FileNameIsLanguage.message.two',
Expand Down
26 changes: 20 additions & 6 deletions src/modules/Render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,17 @@ export class Render extends PubSub {
return -1
}

if (rootNode.properties.every((p: any) => isLanguageProp(p.key.name))) {
rootNode = (rootNode.properties as t.ObjectProperty[]).find(
(p: any) => p.key.name === language,
)?.value as t.ObjectExpression
if (
rootNode.properties.every(property => {
return this.comparePropertyKey(
property as t.ObjectProperty,
isLanguageProp,
)
})
) {
rootNode = (rootNode.properties as t.ObjectProperty[]).find(property => {
return this.comparePropertyKey(property, value => value === language)
})?.value as t.ObjectExpression
}

let targetNode: t.ObjectProperty | undefined
Expand Down Expand Up @@ -265,9 +272,16 @@ export class Render extends PubSub {
}

isEqualKey(property: t.ObjectProperty, key: string) {
return this.comparePropertyKey(property, value => value === key)
}

comparePropertyKey(
property: t.ObjectProperty,
callback: (value: string) => boolean,
) {
return (
(t.isStringLiteral(property.key) && property.key.value === key) ||
(t.isIdentifier(property.key) && property.key.name === key)
(t.isStringLiteral(property.key) && callback(property.key.value)) ||
(t.isIdentifier(property.key) && callback(property.key.name))
)
}

Expand Down

0 comments on commit 5cda09c

Please sign in to comment.