| 134 | } |
| 135 | |
| 136 | export function postProcess(dryRun = false) { |
| 137 | for (const name of methods.keys()) { |
| 138 | // We use the set jsonParents to track the set of different signatures across parent for this function name. |
| 139 | // If all signatures are identical, we leave out the parent and emit a single NativeFunction entry without receiver. |
| 140 | const jsonParents = new Set(); |
| 141 | for (const [parent, signatures] of methods.get(name)) { |
| 142 | signatures.sort((a, b) => a.length - b.length); |
| 143 | const filteredSignatures = []; |
| 144 | for (const signature of signatures) { |
| 145 | const smallerIndex = filteredSignatures.findIndex(smaller => startsTheSame(smaller, signature)); |
| 146 | if (smallerIndex !== -1) { |
| 147 | filteredSignatures[smallerIndex] = (signature.map((arg, index) => { |
| 148 | const otherArg = filteredSignatures[smallerIndex][index]; |
| 149 | if (otherArg) { |
| 150 | return otherArg.length > arg.length ? otherArg : arg; |
| 151 | } |
| 152 | if (arg.startsWith('?') || arg.startsWith('...')) { |
| 153 | return arg; |
| 154 | } |
| 155 | return '?' + arg; |
| 156 | })); |
| 157 | } else { |
| 158 | filteredSignatures.push(signature); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | function startsTheSame(smaller, bigger) { |
| 163 | for (let i = 0; i < smaller.length; i++) { |
| 164 | const withoutQuestion = str => /[\?]?(.*)/.exec(str)[1]; |
| 165 | if (withoutQuestion(smaller[i]) !== withoutQuestion(bigger[i])) { |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | methods.get(name).set(parent, filteredSignatures); |
| 173 | jsonParents.add(JSON.stringify(filteredSignatures)); |
| 174 | } |
| 175 | // If all parents had the same signature for this name, we put a `*` as parent for this entry. |
| 176 | if (jsonParents.size === 1) { |
| 177 | methods.set(name, new Map([['*', JSON.parse(jsonParents.values().next().value)]])); |
| 178 | } |
| 179 | for (const [parent, signatures] of methods.get(name)) { |
| 180 | if (signatures.length === 1 && !signatures[0].length) { |
| 181 | methods.get(name).delete(parent); |
| 182 | } |
| 183 | } |
| 184 | if (methods.get(name).size === 0) { |
| 185 | methods.delete(name); |
| 186 | } |
| 187 | } |
| 188 | const functions = []; |
| 189 | for (const [name, method] of methods) { |
| 190 | if (method.has('*')) { |
| 191 | // All parents had the same signature so we emit an entry without receiver. |
| 192 | functions.push({name, signatures: method.get('*')}); |
| 193 | } else { |