Prechádzať zdrojové kódy

meta: improve js2ts script (#4786)

* meta: improve js2ts script

- add `@ts-ignore` comment on `package.json` imports.
- better detect imports of local `.js` files.
- do not remove non-JS files from the source.

* fixup! meta: improve js2ts script

* fixup! fixup! meta: improve js2ts script

* fixup! meta: improve js2ts script

* Update index.mjs
Antoine du Hamel 1 rok pred
rodič
commit
73fdcaf4a6
1 zmenil súbory, kde vykonal 16 pridanie a 5 odobranie
  1. 16 5
      private/js2ts/index.mjs

+ 16 - 5
private/js2ts/index.mjs

@@ -60,12 +60,23 @@ for await (const dirent of dir) {
   if (!dirent.isDirectory()) {
     const { path: filepath } = dirent
     const ext = extname(filepath)
+    if (ext !== '.js' && ext !== '.jsx') continue // eslint-disable-line no-continue
     await writeFile(
-      filepath.replace(ext, ext.replace('js', 'ts')),
-      (await readFile(filepath, 'utf-8')).replace(
-        /((?:^|\n)import[^\n]*["']\.\.?\/[^'"]+\.)js(x?["'])/g,
-        '$1ts$2',
-      ),
+      filepath.slice(0, -ext.length) + ext.replace('js', 'ts'),
+      (await readFile(filepath, 'utf-8'))
+        .replace(
+          // The following regex aims to capture all imports and reexports of local .js(x) files to replace it to .ts(x)
+          // It's far from perfect and will have false positives and false negatives.
+          /((?:^|\n)(?:import(?:\s+\w+\s+from)?|export\s*\*\s*from|(?:import|export)\s*(?:\{[^}]*\}|\*\s*as\s+\w+\s)\s*from)\s*["']\.\.?\/[^'"]+\.)js(x?["'])/g, // eslint-disable-line max-len
+          '$1ts$2',
+        )
+        .replace(
+          // The following regex aims to capture all local package.json imports.
+          /\nimport \w+ from ['"]..\/([^'"]+\/)*package.json['"]\n/g,
+          (originalImport) =>
+            `// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n` +
+            `// @ts-ignore We don't want TS to generate types for the package.json${originalImport}`,
+        ),
     )
     await rm(filepath)
   }