Skip to content

Commit 4932904

Browse files
committed
fix(dts-generator): fix runCheck for TS6 compatibility
TS6 introduced three breaking changes that affected runCheck: 1. moduleResolution "Node16" rejects bare-specifier ambient module declarations (declare module "sap/ui/core/Control") — switched to module: ESNext + moduleResolution: Bundler which supports them. 2. The default types list is now empty, so @types packages are no longer auto-included — added discovery logic that reads declared @types/* from the nearest package.json and walks up from CWD to locate them in potentially hoisted node_modules directories. 3. In monorepo/workspace setups the walk-up could find unrelated @types packages (e.g. @types/openui5) that conflict with the .d.ts files being checked — scoped discovery to only declared dependencies.
1 parent 35b99d5 commit 4932904

1 file changed

Lines changed: 51 additions & 3 deletions

File tree

‎packages/dts-generator/src/runCheck.ts‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const log = getLogger("@ui5/dts-generator/runCheck");
33
import esMain from "es-main";
44

55
import * as path from "path";
6-
import { promises as fsp } from "fs";
6+
import { promises as fsp, readdirSync, readFileSync } from "fs";
77
const readdir = fsp.readdir;
88

99
import {
@@ -48,6 +48,53 @@ async function main() {
4848

4949
const dtsFiles = await findFiles(dtsDir, "d.ts");
5050

51+
// TS6 no longer auto-includes @types packages; discover which ones are declared
52+
// as dependencies and walk up from CWD to find where they're installed.
53+
const declaredTypes = new Set<string>();
54+
let dir = process.cwd();
55+
while (true) {
56+
try {
57+
const pkg = JSON.parse(
58+
readFileSync(path.join(dir, "package.json"), "utf8"),
59+
);
60+
for (const deps of [pkg.dependencies, pkg.devDependencies]) {
61+
if (deps) {
62+
for (const name of Object.keys(deps)) {
63+
if (name.startsWith("@types/")) {
64+
declaredTypes.add(name.slice("@types/".length));
65+
}
66+
}
67+
}
68+
}
69+
break;
70+
} catch {
71+
// no package.json at this level
72+
}
73+
const parent = path.dirname(dir);
74+
if (parent === dir) break;
75+
dir = parent;
76+
}
77+
78+
const typeRoots: string[] = [];
79+
const types = new Set<string>();
80+
dir = process.cwd();
81+
while (true) {
82+
const candidate = path.join(dir, "node_modules", "@types");
83+
try {
84+
for (const entry of readdirSync(candidate, { withFileTypes: true })) {
85+
if (entry.isDirectory() && declaredTypes.has(entry.name)) {
86+
types.add(entry.name);
87+
}
88+
}
89+
typeRoots.push(candidate);
90+
} catch {
91+
// doesn't exist at this level
92+
}
93+
const parent = path.dirname(dir);
94+
if (parent === dir) break;
95+
dir = parent;
96+
}
97+
5198
log.verbose(`Running a compile check for ${dtsFiles}`);
5299
const success = checkCompile({
53100
dependencyFiles: dtsFiles,
@@ -56,8 +103,9 @@ async function main() {
56103
noImplicitAny: true,
57104
strict: true,
58105
target: ScriptTarget.ES2015,
59-
module: ModuleKind.Node16,
60-
moduleResolution: ModuleResolutionKind.Node16,
106+
module: ModuleKind.ESNext,
107+
moduleResolution: ModuleResolutionKind.Bundler,
108+
...(typeRoots.length > 0 && { typeRoots, types: [...types] }),
61109
},
62110
});
63111

0 commit comments

Comments
 (0)