Skip to content

Commit fd9d61a

Browse files
chore: TS to JSDoc Conversion (#8569)
--------- Co-authored-by: Simon Holthausen <[email protected]>
1 parent 783bd98 commit fd9d61a

File tree

247 files changed

+9751
-8608
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

247 files changed

+9751
-8608
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ _output
44
test/*/samples/*/output.js
55

66
# automatically generated
7-
internal_exports.ts
7+
internal_exports.js
88

99
# output files
1010
animate/*.js

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.vscode
44
node_modules
55
*.map
6-
/src/compiler/compile/internal_exports.ts
6+
/src/compiler/compile/internal_exports.js
77
/compiler.d.ts
88
/compiler.*js
99
/index.*js

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# TODO: after launch new site, format site dir.
55
/site
66
!/src
7-
src/compiler/compile/internal_exports.ts
7+
src/compiler/compile/internal_exports.js
88
!/test
99
/test/**/*.svelte
1010
/test/**/_expected*

generate-types.mjs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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`);

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "svelte",
33
"version": "4.0.0-next.0",
44
"description": "Cybernetically enhanced web apps",
5+
"type": "module",
56
"module": "index.mjs",
67
"main": "index",
78
"files": [
@@ -89,7 +90,7 @@
8990
"dev": "rollup -cw",
9091
"posttest": "agadoo internal/index.mjs",
9192
"prepublishOnly": "node check_publish_env.js && npm run lint && npm run build && npm test",
92-
"tsd": "tsc -p src/compiler --emitDeclarationOnly && tsc -p src/runtime --emitDeclarationOnly",
93+
"tsd": "node ./generate-types.mjs",
9394
"lint": "eslint \"{src,test}/**/*.{ts,js}\" --cache"
9495
},
9596
"repository": {
@@ -147,6 +148,7 @@
147148
"prettier": "^2.8.8",
148149
"prettier-plugin-svelte": "^2.10.0",
149150
"rollup": "^3.20.2",
151+
"rollup-plugin-dts": "^5.3.0",
150152
"source-map": "^0.7.4",
151153
"source-map-support": "^0.5.21",
152154
"tiny-glob": "^0.2.9",

pnpm-lock.yaml

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)