|
| 1 | +// This script generates the TypeScript definitions |
| 2 | + |
| 3 | +import { execSync } from 'child_process'; |
| 4 | +import { readFileSync, writeFileSync, readdirSync, existsSync, copyFileSync, statSync } from 'fs'; |
| 5 | + |
| 6 | +execSync('tsc -p src/compiler --emitDeclarationOnly && tsc -p src/runtime --emitDeclarationOnly', { stdio: 'inherit' }); |
| 7 | + |
| 8 | +function modify(path, modifyFn) { |
| 9 | + const content = readFileSync(path, 'utf8'); |
| 10 | + writeFileSync(path, modifyFn(content)); |
| 11 | +} |
| 12 | + |
| 13 | +function adjust(input) { |
| 14 | + // Remove typedef jsdoc (duplicated in the type definition) |
| 15 | + input = input.replace(/\/\*\*\n(\r)? \* @typedef .+?\*\//gs, ''); |
| 16 | + input = input.replace(/\/\*\* @typedef .+?\*\//gs, ''); |
| 17 | + |
| 18 | + // Extract the import paths and types |
| 19 | + const import_regex = /import\(("|')(.+?)("|')\)\.(\w+)/g; |
| 20 | + let import_match; |
| 21 | + const import_map = new Map(); |
| 22 | + |
| 23 | + while ((import_match = import_regex.exec(input)) !== null) { |
| 24 | + const imports = import_map.get(import_match[2]) || new Map(); |
| 25 | + let name = import_match[4]; |
| 26 | + if ([...imports.keys()].includes(name)) continue; |
| 27 | + |
| 28 | + let i = 1; |
| 29 | + if (name === 'default') { |
| 30 | + name = import_match[2].split('/').pop().split('.').shift().replace(/[^a-z0-9]/gi, '_'); |
| 31 | + } |
| 32 | + while ([...import_map].some(([path, names]) => path !== import_match[2] && names.has(name))) { |
| 33 | + name = `${name}${i++}`; |
| 34 | + } |
| 35 | + |
| 36 | + imports.set(import_match[4], name); |
| 37 | + import_map.set(import_match[2], imports); |
| 38 | + } |
| 39 | + |
| 40 | + // Replace inline imports with their type names |
| 41 | + const transformed = input.replace(import_regex, (_match, _quote, path, _quote2, name) => { |
| 42 | + return import_map.get(path).get(name); |
| 43 | + }); |
| 44 | + |
| 45 | + // Remove/adjust @template, @param and @returns lines |
| 46 | + // TODO rethink if we really need to do this for @param and @returns, doesn't show up in hover so unnecessary |
| 47 | + const lines = transformed.split("\n"); |
| 48 | + |
| 49 | + let filtered_lines = []; |
| 50 | + let removing = null; |
| 51 | + let openCount = 1; |
| 52 | + let closedCount = 0; |
| 53 | + |
| 54 | + for (let line of lines) { |
| 55 | + let start_removing = false; |
| 56 | + if (line.trim().startsWith("* @template")) { |
| 57 | + removing = "template"; |
| 58 | + start_removing = true; |
| 59 | + } |
| 60 | + |
| 61 | + if (line.trim().startsWith("* @param {")) { |
| 62 | + openCount = 1; |
| 63 | + closedCount = 0; |
| 64 | + removing = "param"; |
| 65 | + start_removing = true; |
| 66 | + } |
| 67 | + |
| 68 | + if (line.trim().startsWith("* @returns {")) { |
| 69 | + openCount = 1; |
| 70 | + closedCount = 0; |
| 71 | + removing = "returns"; |
| 72 | + start_removing = true; |
| 73 | + } |
| 74 | + |
| 75 | + if (removing === "returns" || removing === "param") { |
| 76 | + let i = start_removing ? line.indexOf('{') + 1 : 0; |
| 77 | + for (; i < line.length; i++) { |
| 78 | + if (line[i] === "{") openCount++; |
| 79 | + if (line[i] === "}") closedCount++; |
| 80 | + if (openCount === closedCount) break; |
| 81 | + } |
| 82 | + if (openCount === closedCount) { |
| 83 | + line = start_removing ? (line.slice(0, line.indexOf('{')) + line.slice(i + 1)) : (` * @${removing} ` + line.slice(i + 1)); |
| 84 | + removing = null; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (removing && !start_removing && (line.trim().startsWith("* @") || line.trim().startsWith("*/"))) { |
| 89 | + removing = null; |
| 90 | + } |
| 91 | + |
| 92 | + if (!removing) { |
| 93 | + filtered_lines.push(line); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Replace generic type names with their plain versions |
| 98 | + const renamed_generics = filtered_lines.map(line => { |
| 99 | + return line.replace(/(\W|\s)([A-Z][\w\d$]*)_\d+(\W|\s)/g, "$1$2$3"); |
| 100 | + }); |
| 101 | + |
| 102 | + // Generate the import statement for the types used |
| 103 | + const import_statements = Array.from(import_map.entries()) |
| 104 | + .map(([path, names]) => { |
| 105 | + const default_name = names.get('default'); |
| 106 | + names.delete('default'); |
| 107 | + const default_import = default_name ? (default_name + (names.size ? ', ' : ' ')) : ''; |
| 108 | + const named_imports = names.size ? `{ ${[...names.values()].join(', ')} } ` : ''; |
| 109 | + return `import ${default_import}${named_imports}from '${path}';` |
| 110 | + }) |
| 111 | + .join("\n"); |
| 112 | + |
| 113 | + return [import_statements, ...renamed_generics].join("\n"); |
| 114 | +} |
| 115 | + |
| 116 | +let did_replace = false; |
| 117 | + |
| 118 | +function walk(dir) { |
| 119 | + const files = readdirSync(dir); |
| 120 | + const _dir = dir.slice('types/'.length) |
| 121 | + |
| 122 | + for (const file of files) { |
| 123 | + const path = `${dir}/${file}`; |
| 124 | + if (file.endsWith('.d.ts')) { |
| 125 | + modify(path, content => { |
| 126 | + content = adjust(content); |
| 127 | + |
| 128 | + if (file === 'index.d.ts' && existsSync(`src/${_dir}/public.d.ts`)) { |
| 129 | + copyFileSync(`src/${_dir}/public.d.ts`, `${dir}/public.d.ts`); |
| 130 | + content = "export * from './public.js';\n" + content; |
| 131 | + } |
| 132 | + |
| 133 | + if (file === 'Component.d.ts' && dir.includes('runtime')) { |
| 134 | + if (!content.includes('$set(props: Partial<Props>): void;\n}')) { |
| 135 | + throw new Error('Component.js was modified in a way that automatic patching of d.ts file no longer works. Please adjust it'); |
| 136 | + } else { |
| 137 | + content = content.replace('$set(props: Partial<Props>): void;\n}', '$set(props: Partial<Props>): void;\n [accessor:string]: any;\n}'); |
| 138 | + did_replace = true; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return content; |
| 143 | + }); |
| 144 | + } else if (statSync(path).isDirectory()) { |
| 145 | + if (existsSync(`src/${_dir}/${file}/private.d.ts`)) { |
| 146 | + copyFileSync(`src/${_dir}/${file}/private.d.ts`, `${path}/private.d.ts`); |
| 147 | + } |
| 148 | + if (existsSync(`src/${_dir}/${file}/interfaces.d.ts`)) { |
| 149 | + copyFileSync(`src/${_dir}/${file}/interfaces.d.ts`, `${path}/interfaces.d.ts`); |
| 150 | + } |
| 151 | + walk(path); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +walk('types'); |
| 157 | + |
| 158 | +if (!did_replace) { |
| 159 | + throw new Error('Component.js file in runtime does no longer exist so that automatic patching of the d.ts file no longer works. Please adjust it'); |
| 160 | +} |
| 161 | + |
| 162 | +copyFileSync(`src/runtime/ambient.d.ts`, `types/runtime/ambient.d.ts`); |
0 commit comments