Skip to content

Commit f422b41

Browse files
authored
fix: composition setup warning (#115)
1 parent c565612 commit f422b41

File tree

12 files changed

+213
-143
lines changed

12 files changed

+213
-143
lines changed

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@
3131
"@rollup/plugin-node-resolve": "^9.0.0",
3232
"@rollup/plugin-replace": "^2.3.0",
3333
"@types/jest": "^26.0.0",
34-
"@typescript-eslint/eslint-plugin": "^4.0.0",
35-
"@typescript-eslint/parser": "^4.0.0",
36-
"@vue/server-renderer": "^3.0.0-rc.8",
34+
"@typescript-eslint/eslint-plugin": "^4.1.0",
35+
"@typescript-eslint/parser": "^4.1.0",
36+
"@vue/server-renderer": "^3.0.0-rc.12",
3737
"brotli": "^1.3.2",
3838
"chalk": "^4.1.0",
3939
"convert-hrtime": "^3.0.0",
4040
"cross-env": "^7.0.2",
41-
"eslint": "^7.7.0",
41+
"eslint": "^7.9.0",
4242
"eslint-config-prettier": "^6.11.0",
4343
"eslint-plugin-prettier": "^3.1.2",
44-
"eslint-plugin-vue": "^7.0.0-beta.2",
44+
"eslint-plugin-vue": "^7.0.0-beta.3",
4545
"eslint-plugin-vue-libs": "^4.0.0",
4646
"execa": "^4.0.0",
4747
"fs-extra": "^9.0.0",
@@ -65,7 +65,7 @@
6565
"tsd": "^0.13.1",
6666
"typescript": "^4.0.0",
6767
"typescript-eslint-language-service": "^4.0.0",
68-
"vue": "^3.0.0-rc.8"
68+
"vue": "^3.0.0-rc.12"
6969
},
7070
"engines": {
7171
"node": ">= 10"
@@ -88,7 +88,7 @@
8888
"main": "dist/vue-i18n.cjs.js",
8989
"module": "dist/vue-i18n.esm-bundler.js",
9090
"peerDependencies": {
91-
"vue": "^3.0.0-rc.8"
91+
"vue": "^3.0.0-rc.12"
9292
},
9393
"repository": {
9494
"type": "git",

src/components/DatetimeFormat.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RenderFunction, SetupContext } from 'vue'
22
import { useI18n } from '../i18n'
33
import { DateTimeOptions } from '../core'
4-
import { Composer, ComposerInternal } from '../composer'
4+
import { Composer, ComposerInternal, DatetimePartsSymbol } from '../composer'
55
import { renderFormatter, FormattableProps } from './formatRenderer'
66
import { baseFormatProps } from './base'
77

@@ -57,7 +57,8 @@ export const DatetimeFormat = {
5757
DateTimeOptions,
5858
Intl.DateTimeFormatPart
5959
>(props, context, DATETIME_FORMAT_KEYS, (...args: unknown[]) =>
60-
i18n.__datetimeParts(...args)
60+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
61+
(i18n as any)[DatetimePartsSymbol](...args)
6162
)
6263
}
6364
}

src/components/NumberFormat.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SetupContext, RenderFunction } from 'vue'
22
import { useI18n } from '../i18n'
33
import { NumberOptions } from '../core'
4-
import { Composer, ComposerInternal } from '../composer'
4+
import { Composer, ComposerInternal, NumberPartsSymbol } from '../composer'
55
import { renderFormatter, FormattableProps } from './formatRenderer'
66
import { baseFormatProps } from './base'
77

@@ -52,7 +52,8 @@ export const NumberFormat = {
5252
NumberOptions,
5353
Intl.NumberFormatPart
5454
>(props, context, NUMBER_FORMAT_KEYS, (...args: unknown[]) =>
55-
i18n.__numberParts(...args)
55+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
56+
(i18n as any)[NumberPartsSymbol](...args)
5657
)
5758
}
5859
}

src/components/Translation.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { h, Fragment, SetupContext, VNodeChild, RenderFunction } from 'vue'
2-
import { Composer, ComposerInternal } from '../composer'
2+
import { Composer, ComposerInternal, TransrateVNodeSymbol } from '../composer'
33
import { useI18n } from '../i18n'
44
import { TranslateOptions } from '../core'
55
import { NamedValue } from '../message/runtime'
@@ -42,7 +42,12 @@ export const Translation = {
4242
options.plural = isString(props.plural) ? +props.plural : props.plural
4343
}
4444
const arg = getInterpolateArg(context, keys)
45-
const children = i18n.__transrateVNode(props.keypath, arg, options)
45+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
46+
const children = (i18n as any)[TransrateVNodeSymbol](
47+
props.keypath,
48+
arg,
49+
options
50+
)
4651
// prettier-ignore
4752
return isString(props.tag)
4853
? h(props.tag, { ...attrs }, children)

src/composer.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,23 @@ import {
7979
isString,
8080
isRegExp,
8181
isBoolean,
82-
isPlainObject
82+
isPlainObject,
83+
makeSymbol
8384
} from './utils'
8485

8586
// extend VNode interface
8687
declare module '@vue/runtime-core' {
88+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8789
interface VNode<HostNode = RendererNode, HostElement = RendererElement> {
8890
toString: () => string // mark for vue-i18n message runtime
8991
}
9092
}
9193

94+
export const ComposerIdSymbol = makeSymbol('__id')
95+
export const TransrateVNodeSymbol = makeSymbol('__transrateVNode')
96+
export const DatetimePartsSymbol = makeSymbol('__datetimeParts')
97+
export const NumberPartsSymbol = makeSymbol('__numberParts')
98+
9299
export type VueMessageType = string | VNode
93100
export type MissingHandler = (
94101
locale: Locale,
@@ -667,7 +674,8 @@ export function createComposer<
667674
},
668675
() => parseTranslateArgs(...args)[0],
669676
'translate',
670-
root => root.__transrateVNode(...args),
677+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
678+
root => (root as any)[TransrateVNodeSymbol](...args),
671679
key => [createVNode(Text, null, key, 0)],
672680
val => isArray(val)
673681
)
@@ -679,7 +687,8 @@ export function createComposer<
679687
context => number(context as RuntimeContext<Messages, string>, ...args),
680688
() => parseNumberArgs(...args)[0],
681689
'number format',
682-
root => root.__numberParts(...args),
690+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
691+
root => (root as any)[NumberPartsSymbol](...args),
683692
() => [],
684693
val => isString(val) || isArray(val)
685694
)
@@ -693,7 +702,8 @@ export function createComposer<
693702
context => datetime(context as RuntimeContext<Messages, string>, ...args),
694703
() => parseDateTimeArgs(...args)[0],
695704
'datetime format',
696-
root => root.__datetimeParts(...args),
705+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
706+
root => (root as any)[DatetimePartsSymbol](...args),
697707
() => [],
698708
val => isString(val) || isArray(val)
699709
)
@@ -879,7 +889,7 @@ export function createComposer<
879889
_warnHtmlMessage = val
880890
_context.warnHtmlMessage = val
881891
},
882-
__id: composerID,
892+
[ComposerIdSymbol]: composerID,
883893
// methods
884894
t,
885895
d,
@@ -898,9 +908,9 @@ export function createComposer<
898908
setPostTranslationHandler,
899909
getMissingHandler,
900910
setMissingHandler,
901-
__transrateVNode,
902-
__numberParts,
903-
__datetimeParts
911+
[TransrateVNodeSymbol]: __transrateVNode,
912+
[NumberPartsSymbol]: __numberParts,
913+
[DatetimePartsSymbol]: __datetimeParts
904914
}
905915

906916
return composer

src/i18n.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ import { I18nWarnCodes, getWarnMessage } from './warnings'
2727
import { I18nErrorCodes, createI18nError } from './errors'
2828
import { apply } from './plugin'
2929
import { defineMixin } from './mixin'
30-
import { isEmptyObject, warn } from './utils'
30+
import { isEmptyObject, warn, makeSymbol } from './utils'
3131
import { devtoolsRegisterI18n } from './devtools'
3232
import { VERSION } from './misc'
3333

3434
declare module '@vue/runtime-core' {
3535
// eslint-disable-next-line
3636
interface App<HostElement = any> {
3737
__VUE_I18N__?: I18n & I18nInternal
38-
__VUE_I18N_SYMBOL__?: InjectionKey<I18n>
38+
__VUE_I18N_SYMBOL__?: InjectionKey<I18n> | string
3939
}
4040
}
4141

@@ -245,7 +245,9 @@ export function createI18n<
245245
const __global = __legacyMode
246246
? createVueI18n(options)
247247
: createComposer(options)
248-
const symbol: InjectionKey<I18n> = Symbol(__DEV__ ? 'vue-i18n' : '')
248+
const symbol: InjectionKey<I18n> | string = makeSymbol(
249+
__DEV__ ? 'vue-i18n' : ''
250+
)
249251

250252
const i18n = {
251253
// mode

src/legacy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
ComposerOptions,
3434
ComposerInternalOptions,
3535
createComposer,
36-
ComposerInternal
36+
ComposerIdSymbol
3737
} from './composer'
3838
import { I18nWarnCodes, getWarnMessage } from './warnings'
3939
import { createI18nError, I18nErrorCodes } from './errors'
@@ -436,7 +436,7 @@ export function createVueI18n<
436436
},
437437

438438
// for internal
439-
__id: ((composer as unknown) as ComposerInternal).__id,
439+
__id: (composer as any)[ComposerIdSymbol], // eslint-disable-line @typescript-eslint/no-explicit-any
440440
__composer: composer,
441441

442442
/**

src/mixin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
235235
}
236236
},
237237

238-
beforeDestroy(): void {
238+
beforeUnmount(): void {
239239
const instance = getCurrentInstance()
240240
/* istanbul ignore if */
241241
if (!instance) {

src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export function format(message: string, ...args: any): string {
2121
)
2222
}
2323

24+
const hasSymbol =
25+
typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'
26+
27+
/** @internal */
28+
export const makeSymbol = (name: string): symbol | string => hasSymbol ? Symbol(name) : name
29+
2430
/** @internal */
2531
export const generateFormatCacheKey = (
2632
locale: string,

test/__snapshots__/composer.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Array [
1919
"ref": null,
2020
"scopeId": null,
2121
"shapeFlag": 8,
22+
"ssContent": null,
23+
"ssFallback": null,
2224
"staticCount": 0,
2325
"suspense": null,
2426
"target": null,

0 commit comments

Comments
 (0)