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: compile error for key with included hyphen in named interpolation #1797

Merged
merged 1 commit into from
Apr 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/message-compiler/src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,16 @@ export function createTokenizer(
return takeChar(scnr, closure)
}

// NOTE: It is assumed that this function is used in conjunction with `takeIdentifierChar` for named.
// TODO: we need to refactor this function ...
function takeNamedIdentifierChar(scnr: Scanner): string | undefined | null {
const closure = (ch: string) => {
const cc = ch.charCodeAt(0)
return cc === 45 // -
}
return takeChar(scnr, closure)
}

function takeDigit(scnr: Scanner): string | undefined | null {
const closure = (ch: string) => {
const cc = ch.charCodeAt(0)
Expand Down Expand Up @@ -503,7 +513,8 @@ export function createTokenizer(

let ch: string | undefined | null = ''
let name = ''
while ((ch = takeIdentifierChar(scnr))) {
// eslint-disable-next-line no-cond-assign
while ((ch = takeIdentifierChar(scnr) || takeNamedIdentifierChar(scnr))) {
name += ch
}

Expand Down
51 changes: 51 additions & 0 deletions packages/message-compiler/test/tokenizer/named.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,57 @@ test('new line', () => {
})
})

test('include hyphen', () => {
const tokenizer = createTokenizer('My Nickname is {nick-name}.')
expect(tokenizer.nextToken()).toEqual({
type: TokenTypes.Text,
value: 'My Nickname is ',
loc: {
start: { line: 1, column: 1, offset: 0 },
end: { line: 1, column: 16, offset: 15 }
}
})
expect(tokenizer.nextToken()).toEqual({
type: TokenTypes.BraceLeft,
value: '{',
loc: {
start: { line: 1, column: 16, offset: 15 },
end: { line: 1, column: 17, offset: 16 }
}
})
expect(tokenizer.nextToken()).toEqual({
type: TokenTypes.Named,
value: 'nick-name',
loc: {
start: { line: 1, column: 17, offset: 16 },
end: { line: 1, column: 26, offset: 25 }
}
})
expect(tokenizer.nextToken()).toEqual({
type: TokenTypes.BraceRight,
value: '}',
loc: {
start: { line: 1, column: 26, offset: 25 },
end: { line: 1, column: 27, offset: 26 }
}
})
expect(tokenizer.nextToken()).toEqual({
type: TokenTypes.Text,
value: '.',
loc: {
start: { line: 1, column: 27, offset: 26 },
end: { line: 1, column: 28, offset: 27 }
}
})
expect(tokenizer.nextToken()).toEqual({
type: TokenTypes.EOF,
loc: {
start: { line: 1, column: 28, offset: 27 },
end: { line: 1, column: 28, offset: 27 }
}
})
})

describe('modulo cases', () => {
test('basic named: hi %{name} !', () => {
const tokenizer = createTokenizer('hi %{name} !')
Expand Down
18 changes: 18 additions & 0 deletions packages/vue-i18n-core/test/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1358,3 +1358,21 @@ test('issue #1738', async () => {
expect(wrapper.find('#te1')?.textContent).toEqual(`true - expected true`)
expect(wrapper.find('#te2')?.textContent).toEqual(`true - expected true`)
})

test('#1796', async () => {
const i18n = createI18n({
locale: 'en',
messages: {
en: {
hello: 'hello world',
'message-with-placeholder-using-hyphens':
'My message with {placeholder-hyphens}.'
}
}
})
expect(
i18n.global.t('message-with-placeholder-using-hyphens', {
'placeholder-hyphens': i18n.global.t('hello')
})
).toEqual('My message with hello world.')
})
5 changes: 3 additions & 2 deletions spec/syntax.ebnf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(*
* Inltify message syntax v0.2
* Inltify message syntax v0.3
* (vue-i18n compatible)
*)

Expand All @@ -14,7 +14,7 @@ Message ::= (Text? (Placeholder | Linked)? Text?)+;
Text ::= TextChar+;
Placeholder ::= Named | List | StringLiteral;
Modulo ::= "%";
Named ::= Modulo? "{" Space? (Identifier) Space? "}";
Named ::= Modulo? "{" Space? (NamedIdentifier) Space? "}";
List ::= "{" Space? (NumberLiteral) Space? "}";
Linked ::= "@" (LinkedModifier)? LinkedDelimiter LinkedRefer;
LinkedRefer ::= LinkedKey | Placeholder;
Expand All @@ -41,6 +41,7 @@ Digits ::= [0-9]+;

(* identifier *)
Identifier ::= [a-zA-Z_] [a-zA-Z0-9_$]*;
NamedIdentifier ::= [a-zA-Z_] [a-zA-Z0-9_\-$]*;

(* whitespaces *)
SpaceInline ::= #x0020; (* "\u0020" *)
Expand Down