Skip to content

Commit 15f735d

Browse files
authored
fix: cannot resolve the ast messages which has json path for v9 (#2162)
* fix: cannot resolve the ast messages which has json path for v9 * fix: skip bridge tests, because vue2 and vue-18n v8 no longer support
1 parent 7faf924 commit 15f735d

23 files changed

+533
-167
lines changed

e2e/bridge/basic.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getText } from '../helper'
22
;['composition', 'legacy'].forEach(pattern => {
3-
describe(`${pattern}`, () => {
3+
describe.skip(`${pattern}`, () => {
44
beforeAll(async () => {
55
await page.goto(
66
`http://localhost:8080/examples/bridge/${pattern}/basic.html`

e2e/bridge/component.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getText } from '../helper'
22
;['composition', 'legacy'].forEach(pattern => {
3-
describe(`${pattern}`, () => {
3+
describe.skip(`${pattern}`, () => {
44
beforeAll(async () => {
55
await page.goto(
66
`http://localhost:8080/examples/bridge/${pattern}/component.html`

e2e/bridge/components/datetime-format.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getText } from '../../helper'
2-
describe(`bridge: datetime format component`, () => {
2+
describe.skip(`bridge: datetime format component`, () => {
33
beforeAll(async () => {
44
await page.goto(
55
`http://localhost:8080/examples/bridge/composition/components/datetime-format.html`

e2e/bridge/components/number-format.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getText } from '../../helper'
2-
describe(`bridge: number format component`, () => {
2+
describe.skip(`bridge: number format component`, () => {
33
beforeAll(async () => {
44
await page.goto(
55
`http://localhost:8080/examples/bridge/composition/components/number-format.html`

e2e/bridge/components/translation.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getText } from '../../helper'
2-
describe(`bridge: translation component`, () => {
2+
describe.skip(`bridge: translation component`, () => {
33
beforeAll(async () => {
44
await page.goto(
55
`http://localhost:8080/examples/bridge/composition/components/translation.html`

e2e/bridge/plural.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getText } from '../helper'
22
;['composition', 'legacy'].forEach(pattern => {
3-
describe(`${pattern}`, () => {
3+
describe.skip(`${pattern}`, () => {
44
beforeAll(async () => {
55
await page.goto(
66
`http://localhost:8080/examples/bridge/${pattern}/plural.html`

e2e/bridge/scope/global.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getText } from '../../helper'
22
;['composition', 'legacy'].forEach(pattern => {
3-
describe(`${pattern}`, () => {
3+
describe.skip(`${pattern}`, () => {
44
beforeAll(async () => {
55
await page.goto(
66
`http://localhost:8080/examples/bridge/${pattern}/scope/global.html`

e2e/bridge/scope/inherit-locale.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getText } from '../../helper'
22
;['composition', 'legacy'].forEach(pattern => {
3-
describe(`${pattern}`, () => {
3+
describe.skip(`${pattern}`, () => {
44
beforeAll(async () => {
55
await page.goto(
66
`http://localhost:8080/examples/bridge/${pattern}/scope/inherit-locale.html`

e2e/bridge/scope/local.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getText } from '../../helper'
22
;['composition', 'legacy'].forEach(pattern => {
3-
describe(`${pattern}`, () => {
3+
describe.skip(`${pattern}`, () => {
44
beforeAll(async () => {
55
await page.goto(
66
`http://localhost:8080/examples/bridge/${pattern}/scope/local.html`

packages/core-base/src/ast.ts

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { NodeTypes } from '@intlify/message-compiler'
2+
import { hasOwn, isObject } from '@intlify/shared'
3+
4+
import type {
5+
LinkedModifierNode,
6+
LinkedNode,
7+
MessageNode,
8+
Node,
9+
PluralNode,
10+
ResourceNode
11+
} from '@intlify/message-compiler'
12+
import type { MessageType } from './runtime'
13+
14+
export function isMessageAST(val: unknown): val is ResourceNode {
15+
return (
16+
isObject(val) &&
17+
resolveType(val as Node) === 0 &&
18+
(hasOwn(val, 'b') || hasOwn(val, 'body'))
19+
)
20+
}
21+
22+
const PROPS_BODY = ['b', 'body']
23+
24+
export function resolveBody(node: ResourceNode) {
25+
return resolveProps<MessageNode | PluralNode>(node, PROPS_BODY)
26+
}
27+
28+
const PROPS_CASES = ['c', 'cases']
29+
30+
export function resolveCases(node: PluralNode) {
31+
return resolveProps<PluralNode['cases'], PluralNode['cases']>(
32+
node,
33+
PROPS_CASES,
34+
[]
35+
)
36+
}
37+
38+
const PROPS_STATIC = ['s', 'static']
39+
40+
export function resolveStatic(node: MessageNode) {
41+
return resolveProps(node, PROPS_STATIC)
42+
}
43+
44+
const PROPS_ITEMS = ['i', 'items']
45+
46+
export function resolveItems(node: MessageNode) {
47+
return resolveProps<MessageNode['items'], MessageNode['items']>(
48+
node,
49+
PROPS_ITEMS,
50+
[]
51+
)
52+
}
53+
54+
const PROPS_TYPE = ['t', 'type']
55+
56+
export function resolveType(node: Node): ReturnType<typeof resolveProps> {
57+
return resolveProps<NodeTypes>(node, PROPS_TYPE)
58+
}
59+
60+
const PROPS_VALUE = ['v', 'value']
61+
62+
export function resolveValue<Message = string>(
63+
node: { v?: MessageType<Message>; value?: MessageType<Message> },
64+
type: NodeTypes
65+
): MessageType<Message> {
66+
const resolved = resolveProps<Message>(
67+
node as Node,
68+
PROPS_VALUE
69+
) as MessageType<Message>
70+
if (resolved != null) {
71+
return resolved
72+
} else {
73+
throw createUnhandleNodeError(type)
74+
}
75+
}
76+
77+
const PROPS_MODIFIER = ['m', 'modifier']
78+
79+
export function resolveLinkedModifier(node: LinkedNode) {
80+
return resolveProps<LinkedModifierNode>(node, PROPS_MODIFIER)
81+
}
82+
83+
const PROPS_KEY = ['k', 'key']
84+
85+
export function resolveLinkedKey(node: LinkedNode) {
86+
const resolved = resolveProps<LinkedNode['key']>(node, PROPS_KEY)
87+
if (resolved) {
88+
return resolved
89+
} else {
90+
throw createUnhandleNodeError(NodeTypes.Linked)
91+
}
92+
}
93+
94+
export function resolveProps<T = string, Default = undefined>(
95+
node: Node,
96+
props: string[],
97+
defaultValue?: Default
98+
): T | Default {
99+
for (let i = 0; i < props.length; i++) {
100+
const prop = props[i]
101+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
102+
if (hasOwn(node, prop) && (node as any)[prop] != null) {
103+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
104+
return (node as any)[prop] as T
105+
}
106+
}
107+
return defaultValue as Default
108+
}
109+
110+
export const AST_NODE_PROPS_KEYS = [
111+
...PROPS_BODY,
112+
...PROPS_CASES,
113+
...PROPS_STATIC,
114+
...PROPS_ITEMS,
115+
...PROPS_KEY,
116+
...PROPS_MODIFIER,
117+
...PROPS_VALUE,
118+
...PROPS_TYPE
119+
]
120+
121+
export function createUnhandleNodeError(type: NodeTypes) {
122+
return new Error(`unhandled node type: ${type}`)
123+
}

packages/core-base/src/compilation.ts

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import {
2-
format,
3-
hasOwn,
4-
isBoolean,
5-
isObject,
6-
isString,
7-
create,
8-
warn
9-
} from '@intlify/shared'
10-
import { format as formatMessage, resolveType } from './format'
1+
import { format, isBoolean, isString, create, warn } from '@intlify/shared'
2+
import { isMessageAST } from './ast'
3+
import { format as formatMessage } from './format'
114
import {
125
baseCompile as baseCompileCore,
136
CompileWarnCodes,
@@ -21,7 +14,6 @@ import type {
2114
CompileError,
2215
CompilerResult,
2316
ResourceNode,
24-
Node,
2517
CompileWarn
2618
} from '@intlify/message-compiler'
2719
import type { MessageFunction, MessageFunctions } from './runtime'
@@ -53,14 +45,6 @@ export function clearCompileCache(): void {
5345
compileCache = create()
5446
}
5547

56-
export function isMessageAST(val: unknown): val is ResourceNode {
57-
return (
58-
isObject(val) &&
59-
resolveType(val as Node) === 0 &&
60-
(hasOwn(val, 'b') || hasOwn(val, 'body'))
61-
)
62-
}
63-
6448
function baseCompile(
6549
message: string,
6650
options: CompileOptions = {}

packages/core-base/src/format.ts

+11-93
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import { NodeTypes } from '@intlify/message-compiler'
22
import { hasOwn, isNumber } from '@intlify/shared'
3+
import {
4+
createUnhandleNodeError,
5+
resolveBody,
6+
resolveCases,
7+
resolveItems,
8+
resolveLinkedKey,
9+
resolveLinkedModifier,
10+
resolveStatic,
11+
resolveType,
12+
resolveValue
13+
} from './ast'
314

415
import type {
5-
LinkedModifierNode,
616
LinkedNode,
717
ListNode,
818
MessageNode,
@@ -53,22 +63,6 @@ export function formatParts<Message = string>(
5363
}
5464
}
5565

56-
const PROPS_BODY = ['b', 'body']
57-
58-
function resolveBody(node: ResourceNode) {
59-
return resolveProps<MessageNode | PluralNode>(node, PROPS_BODY)
60-
}
61-
62-
const PROPS_CASES = ['c', 'cases']
63-
64-
function resolveCases(node: PluralNode) {
65-
return resolveProps<PluralNode['cases'], PluralNode['cases']>(
66-
node,
67-
PROPS_CASES,
68-
[]
69-
)
70-
}
71-
7266
export function formatMessageParts<Message = string>(
7367
ctx: MessageContext<Message>,
7468
node: MessageNode
@@ -87,22 +81,6 @@ export function formatMessageParts<Message = string>(
8781
}
8882
}
8983

90-
const PROPS_STATIC = ['s', 'static']
91-
92-
function resolveStatic(node: MessageNode) {
93-
return resolveProps(node, PROPS_STATIC)
94-
}
95-
96-
const PROPS_ITEMS = ['i', 'items']
97-
98-
function resolveItems(node: MessageNode) {
99-
return resolveProps<MessageNode['items'], MessageNode['items']>(
100-
node,
101-
PROPS_ITEMS,
102-
[]
103-
)
104-
}
105-
10684
type NodeValue<Message> = {
10785
v?: MessageType<Message>
10886
value?: MessageType<Message>
@@ -160,63 +138,3 @@ export function formatMessagePart<Message = string>(
160138
throw new Error(`unhandled node on format message part: ${type}`)
161139
}
162140
}
163-
164-
const PROPS_TYPE = ['t', 'type']
165-
166-
export function resolveType(node: Node) {
167-
return resolveProps<NodeTypes>(node, PROPS_TYPE)
168-
}
169-
170-
const PROPS_VALUE = ['v', 'value']
171-
172-
function resolveValue<Message = string>(
173-
node: { v?: MessageType<Message>; value?: MessageType<Message> },
174-
type: NodeTypes
175-
): MessageType<Message> {
176-
const resolved = resolveProps<Message>(
177-
node as Node,
178-
PROPS_VALUE
179-
) as MessageType<Message>
180-
if (resolved) {
181-
return resolved
182-
} else {
183-
throw createUnhandleNodeError(type)
184-
}
185-
}
186-
187-
const PROPS_MODIFIER = ['m', 'modifier']
188-
189-
function resolveLinkedModifier(node: LinkedNode) {
190-
return resolveProps<LinkedModifierNode>(node, PROPS_MODIFIER)
191-
}
192-
193-
const PROPS_KEY = ['k', 'key']
194-
195-
function resolveLinkedKey(node: LinkedNode) {
196-
const resolved = resolveProps<LinkedNode['key']>(node, PROPS_KEY)
197-
if (resolved) {
198-
return resolved
199-
} else {
200-
throw createUnhandleNodeError(NodeTypes.Linked)
201-
}
202-
}
203-
204-
function resolveProps<T = string, Default = undefined>(
205-
node: Node,
206-
props: string[],
207-
defaultValue?: Default
208-
): T | Default {
209-
for (let i = 0; i < props.length; i++) {
210-
const prop = props[i]
211-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
212-
if (hasOwn(node, prop) && (node as any)[prop] != null) {
213-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
214-
return (node as any)[prop] as T
215-
}
216-
}
217-
return defaultValue as Default
218-
}
219-
220-
function createUnhandleNodeError(type: NodeTypes) {
221-
return new Error(`unhandled node type: ${type}`)
222-
}

packages/core-base/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export {
66
ResourceNode,
77
createCompileError
88
} from '@intlify/message-compiler'
9+
export { AST_NODE_PROPS_KEYS, isMessageAST } from './ast'
910
export * from './resolver'
1011
export * from './runtime'
1112
export * from './context'

packages/core-base/src/resolver.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { isObject, isFunction } from '@intlify/shared'
1+
import { isFunction, isObject } from '@intlify/shared'
2+
import { AST_NODE_PROPS_KEYS, isMessageAST } from './ast'
23

34
/** @VueI18nGeneral */
45
export type Path = string
@@ -344,7 +345,16 @@ export function resolveValue(obj: unknown, path: Path): PathValue {
344345
let last = obj
345346
let i = 0
346347
while (i < len) {
347-
const val = last[hit[i]]
348+
const key = hit[i]
349+
/**
350+
* NOTE:
351+
* if `key` is intlify message format AST node key and `last` is intlify message format AST, skip it.
352+
* because the AST node is not a key-value structure.
353+
*/
354+
if (AST_NODE_PROPS_KEYS.includes(key) && isMessageAST(last)) {
355+
return null
356+
}
357+
const val = last[key]
348358
if (val === undefined) {
349359
return null
350360
}

0 commit comments

Comments
 (0)