MCPcopy Create free account
hub / github.com/formatjs/formatjs / extractMessageComponent

Function extractMessageComponent

packages/cli-lib/svelte_extractor.ts:98–135  ·  view source on GitHub ↗
(
  node: SvelteNode,
  source: string,
  parseScriptFn: ScriptParseFn
)

Source from the content-addressed store, hash-verified

96}
97
98function extractMessageComponent(
99 node: SvelteNode,
100 source: string,
101 parseScriptFn: ScriptParseFn
102): void {
103 if (typeof node.name !== 'string' || !isValidIdentifier(node.name)) {
104 return
105 }
106
107 const props: string[] = []
108 for (const attr of node.attributes || []) {
109 if (
110 attr.type !== 'Attribute' ||
111 typeof attr.name !== 'string' ||
112 !MESSAGE_DESCRIPTOR_PROPS.has(attr.name)
113 ) {
114 continue
115 }
116
117 const expression = getAttributeExpression(attr, source)
118 if (expression != null) {
119 props.push(`${attr.name}: ${expression}`)
120 }
121 }
122
123 if (!props.length) {
124 return
125 }
126
127 try {
128 parseScriptFn(`${node.name}({${props.join(', ')}})`)
129 } catch (e) {
130 console.warn(
131 `Failed to parse ${node.name} message component. Ignore this if component has no extractable message`,
132 e
133 )
134 }
135}
136
137function walkNode(
138 node: SvelteNode,

Callers 1

walkNodeFunction · 0.85

Calls 2

getAttributeExpressionFunction · 0.85
isValidIdentifierFunction · 0.70

Tested by

no test coverage detected