Skip to content

Commit b258c48

Browse files
fix: bring bug fixes from v9.2.0-beta.11 (#720)
* fix: Maximum call stack size (#713) * fix: vue-i18n components scope resolving in Legacy API mode (#715) Co-authored-by: Fabian Kranewitter <[email protected]>
1 parent 77de669 commit b258c48

File tree

11 files changed

+753
-21
lines changed

11 files changed

+753
-21
lines changed

packages/message-compiler/src/tokenizer.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -438,45 +438,40 @@ export function createTokenizer(
438438
}
439439

440440
function readText(scnr: Scanner): string {
441-
const fn = (buf: string): string => {
441+
let buf = ''
442+
while (true) {
442443
const ch = scnr.currentChar()
443444
if (
444445
ch === TokenChars.BraceLeft ||
445446
ch === TokenChars.BraceRight ||
446447
ch === TokenChars.LinkedAlias ||
448+
ch === TokenChars.Pipe ||
447449
!ch
448450
) {
449-
return buf
451+
break
450452
} else if (ch === TokenChars.Modulo) {
451453
if (isTextStart(scnr)) {
452454
buf += ch
453455
scnr.next()
454-
return fn(buf)
455456
} else {
456-
return buf
457+
break
457458
}
458-
} else if (ch === TokenChars.Pipe) {
459-
return buf
460459
} else if (ch === SPACE || ch === NEW_LINE) {
461460
if (isTextStart(scnr)) {
462461
buf += ch
463462
scnr.next()
464-
return fn(buf)
465463
} else if (isPluralStart(scnr)) {
466-
return buf
464+
break
467465
} else {
468466
buf += ch
469467
scnr.next()
470-
return fn(buf)
471468
}
472469
} else {
473470
buf += ch
474471
scnr.next()
475-
return fn(buf)
476472
}
477473
}
478-
479-
return fn('')
474+
return buf
480475
}
481476

482477
function readNamedIdentifier(scnr: Scanner): string {

packages/message-compiler/test/fixtures/20_newsgroups_alt_atheism_51060.txt

Lines changed: 623 additions & 0 deletions
Large diffs are not rendered by default.

packages/message-compiler/test/tokenizer.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { parse } from '../src/tokenizer'
22
import { TokenizeOptions } from '../src/options'
33
import { CompileError } from '../src/errors'
4+
import path from 'path'
5+
import { promises as fs } from 'fs'
46

57
test('token analysis', () => {
68
const cases = [
@@ -98,3 +100,20 @@ test('token analysis', () => {
98100
}
99101
}
100102
})
103+
104+
describe('edge cases', () => {
105+
test('long text', async () => {
106+
const data = await fs.readFile(
107+
path.join(__dirname, './fixtures/20_newsgroups_alt_atheism_51060.txt'),
108+
'utf8'
109+
)
110+
let err = null
111+
try {
112+
parse(data)
113+
} catch (e) {
114+
console.error(e)
115+
err = e
116+
}
117+
expect(err).toEqual(null)
118+
})
119+
})

packages/vue-i18n/src/components/DatetimeFormat.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ export const DatetimeFormat = {
7878
setup(props: DatetimeFormatProps, context: SetupContext): RenderFunction {
7979
const i18n =
8080
props.i18n ||
81-
(useI18n({ useScope: 'parent' }) as Composer & ComposerInternal)
81+
(useI18n({ useScope: 'parent', __useComponent: true }) as Composer &
82+
ComposerInternal)
8283

8384
return renderFormatter<
8485
FormattableProps<number | Date, Intl.DateTimeFormatOptions>,

packages/vue-i18n/src/components/NumberFormat.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export const NumberFormat = {
7373
setup(props: NumberFormatProps, context: SetupContext): RenderFunction {
7474
const i18n =
7575
props.i18n ||
76-
(useI18n({ useScope: 'parent' }) as Composer & ComposerInternal)
76+
(useI18n({ useScope: 'parent', __useComponent: true }) as Composer &
77+
ComposerInternal)
7778

7879
return renderFormatter<
7980
FormattableProps<number, Intl.NumberFormatOptions>,

packages/vue-i18n/src/components/Translation.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ export const Translation = {
100100
const { slots, attrs } = context
101101
const i18n =
102102
props.i18n ||
103-
(useI18n({ useScope: props.scope }) as Composer & ComposerInternal)
103+
(useI18n({
104+
useScope: props.scope as 'global' | 'parent',
105+
__useComponent: true
106+
}) as Composer & ComposerInternal)
104107
const keys = Object.keys(slots).filter(key => key !== '_')
105108

106109
return (): VNodeChild => {

packages/vue-i18n/src/composer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export const EnableEmitter = makeSymbol('__enableEmitter')
9898
export const DisableEmitter = makeSymbol('__disableEmitter')
9999
export const SetPluralRulesSymbol = makeSymbol('__setPluralRules')
100100
export const DevToolsMetaSymbol = makeSymbol('__intlifyMeta')
101+
export const InejctWithOption = makeSymbol('__injectWithOption')
101102

102103
/** @VueI18nComposition */
103104
export type VueMessageType = string | VNode
@@ -326,6 +327,7 @@ export interface ComposerInternalOptions<
326327
__i18n?: CustomBlocks<Message>
327328
__i18nGlobal?: CustomBlocks<Message>
328329
__root?: Composer<Messages, DateTimeFormats, NumberFormats, Message>
330+
__injectWithOption?: boolean
329331
}
330332

331333
/**
@@ -1862,7 +1864,8 @@ export function createComposer<
18621864
[TransrateVNodeSymbol]: transrateVNode,
18631865
[NumberPartsSymbol]: numberParts,
18641866
[DatetimePartsSymbol]: datetimeParts,
1865-
[SetPluralRulesSymbol]: setPluralRules
1867+
[SetPluralRulesSymbol]: setPluralRules,
1868+
[InejctWithOption]: (options as any).__injectWithOption // eslint-disable-line @typescript-eslint/no-explicit-any
18661869
}
18671870

18681871
// for vue-devtools timeline event

packages/vue-i18n/src/i18n.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
getLocaleMessages,
2020
createComposer,
2121
EnableEmitter,
22-
DisableEmitter
22+
DisableEmitter,
23+
InejctWithOption
2324
} from './composer'
2425
import { createVueI18n } from './legacy'
2526
import { I18nWarnCodes, getWarnMessage } from './warnings'
@@ -598,7 +599,8 @@ export function useI18n<
598599
}
599600

600601
if (scope === 'parent') {
601-
let composer = getComposer(i18n, instance)
602+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
603+
let composer = getComposer(i18n, instance, (options as any).__useComponent)
602604
if (composer == null) {
603605
if (__DEV__) {
604606
warn(getWarnMessage(I18nWarnCodes.NOT_FOUND_PARENT_SCOPE))
@@ -666,7 +668,8 @@ function getComposer<
666668
Legacy extends boolean
667669
>(
668670
i18n: I18n<Messages, DateTimeFormats, NumberFormats, Legacy>,
669-
target: ComponentInternalInstance
671+
target: ComponentInternalInstance,
672+
useComponent = false
670673
): Composer<Messages, DateTimeFormats, NumberFormats> | null {
671674
let composer: Composer<Messages, DateTimeFormats, NumberFormats> | null = null
672675
const root = target.root
@@ -696,6 +699,10 @@ function getComposer<
696699
VueI18nInternal<Messages, DateTimeFormats, NumberFormats>)
697700
.__composer as Composer<Messages, DateTimeFormats, NumberFormats>
698701
}
702+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
703+
if (useComponent && !(composer as any)[InejctWithOption]) {
704+
composer = null
705+
}
699706
}
700707
if (composer != null) {
701708
break

packages/vue-i18n/src/legacy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ function convertComposerOptions<
11131113
return messages
11141114
}, (messages || {}) as LocaleMessages<VueMessageType>) as typeof options.messages
11151115
}
1116-
const { __i18n, __root } = options
1116+
const { __i18n, __root, __injectWithOption } = options
11171117

11181118
const datetimeFormats = options.datetimeFormats
11191119
const numberFormats = options.numberFormats
@@ -1138,7 +1138,8 @@ function convertComposerOptions<
11381138
escapeParameter,
11391139
inheritLocale,
11401140
__i18n,
1141-
__root
1141+
__root,
1142+
__injectWithOption
11421143
}
11431144
}
11441145

packages/vue-i18n/src/mixin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
5252
if (this === this.$root) {
5353
this.$i18n = mergeToRoot(vuei18n, optionsI18n)
5454
} else {
55+
optionsI18n.__injectWithOption = true
5556
this.$i18n = createVueI18n(optionsI18n)
5657
}
5758
} else if (options.__i18n) {
@@ -60,6 +61,7 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
6061
} else {
6162
this.$i18n = createVueI18n({
6263
__i18n: (options as ComposerInternalOptions<Messages>).__i18n,
64+
__injectWithOption: true,
6365
__root: composer
6466
} as VueI18nOptions)
6567
}

0 commit comments

Comments
 (0)