-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathindex.ts
More file actions
67 lines (64 loc) · 1.81 KB
/
Copy pathindex.ts
File metadata and controls
67 lines (64 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {ErrorKind} from '#packages/icu-messageformat-parser/error.js'
import {
Parser,
type ParserOptions,
} from '#packages/icu-messageformat-parser/parser.js'
import {
isDateElement,
isDateTimeSkeleton,
isNumberElement,
isNumberSkeleton,
isPluralElement,
isSelectElement,
isTagElement,
isTimeElement,
type MessageFormatElement,
} from '#packages/icu-messageformat-parser/types.js'
function pruneLocation(els: MessageFormatElement[]): void {
els.forEach(el => {
delete el.location
if (isSelectElement(el) || isPluralElement(el)) {
for (const k in el.options) {
delete el.options[k].location
pruneLocation(el.options[k].value)
}
} else if (isNumberElement(el) && isNumberSkeleton(el.style)) {
delete el.style.location
} else if (
(isDateElement(el) || isTimeElement(el)) &&
isDateTimeSkeleton(el.style)
) {
delete el.style.location
} else if (isTagElement(el)) {
pruneLocation(el.children)
}
})
}
export function parse(
message: string,
opts: ParserOptions = {}
): MessageFormatElement[] {
opts = {
shouldParseSkeletons: true,
requiresOtherClause: true,
...opts,
}
const result = new Parser(message, opts).parse()
if (result.err) {
const error = SyntaxError(ErrorKind[result.err.kind])
// @ts-expect-error Assign to error object
error.location = result.err.location
// @ts-expect-error Assign to error object
error.originalMessage = result.err.message
throw error
}
if (!opts?.captureLocation) {
pruneLocation(result.val)
}
return result.val
}
export * from '#packages/icu-messageformat-parser/types.js'
export type {ParserOptions}
// only for testing
export const _Parser: typeof Parser = Parser
export {isStructurallySame} from '#packages/icu-messageformat-parser/manipulator.js'