| 69 | } |
| 70 | |
| 71 | export function transform( |
| 72 | code: string, |
| 73 | id: string, |
| 74 | options: Options |
| 75 | ): {code: string; map: ReturnType<MagicString['generateMap']>} | undefined { |
| 76 | const { |
| 77 | overrideIdFn, |
| 78 | idInterpolationPattern = DEFAULT_ID_INTERPOLATION_PATTERN, |
| 79 | removeDefaultMessage = false, |
| 80 | additionalComponentNames = [], |
| 81 | additionalFunctionNames = [], |
| 82 | ast: preParseAst = false, |
| 83 | preserveWhitespace = false, |
| 84 | flatten = true, |
| 85 | } = options |
| 86 | |
| 87 | const componentNames = new Set([ |
| 88 | 'FormattedMessage', |
| 89 | ...additionalComponentNames, |
| 90 | ]) |
| 91 | const functionNames = new Set([ |
| 92 | 'formatMessage', |
| 93 | '$t', |
| 94 | '$formatMessage', |
| 95 | 'defineMessage', |
| 96 | 'defineMessages', |
| 97 | ...additionalFunctionNames, |
| 98 | ]) |
| 99 | // Compiled JSX runtime functions: _jsx(Component, props), React.createElement(Component, props) |
| 100 | const jsxRuntimeFunctions = new Set([ |
| 101 | 'jsx', |
| 102 | '_jsx', |
| 103 | 'jsxs', |
| 104 | '_jsxs', |
| 105 | 'jsxDEV', |
| 106 | '_jsxDEV', |
| 107 | 'createElement', |
| 108 | ]) |
| 109 | |
| 110 | const result = parseSync(id, code, {sourceType: 'module'}) |
| 111 | const program = result.program |
| 112 | |
| 113 | let s: MagicString | undefined |
| 114 | |
| 115 | function unwrapTransparentTypeScriptExpression(node: any): any { |
| 116 | let current = node |
| 117 | while ( |
| 118 | current?.type === 'TSAsExpression' || |
| 119 | current?.type === 'TSSatisfiesExpression' || |
| 120 | current?.type === 'TSTypeAssertion' || |
| 121 | current?.type === 'TSNonNullExpression' |
| 122 | ) { |
| 123 | current = current.expression |
| 124 | } |
| 125 | return current |
| 126 | } |
| 127 | |
| 128 | function unwrapObjectExpression(node: any): any { |