Skip to content

Commit

Permalink
fix: handle NodeJS prefix in new typescript versions (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Mar 12, 2024
1 parent 017c076 commit e181446
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare module "typescript" {
export function signatureHasRestParameter(signature: ts.Signature): boolean;
interface Symbol {
type?: ts.Type;
parent: Symbol | undefined
}
interface Declaration {
expression?: ts.Expression;
Expand Down
30 changes: 30 additions & 0 deletions generate-types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,24 @@ const printError = (diagnostic) => {
);
};

/**
* @param {ts.Symbol} current current
* @returns {string} full escaped name
*/
const getFullEscapedName = (current) => {
let name = current.escapedName.toString();
while (current.parent) {
current = current.parent;
if (
current.escapedName === undefined ||
current.escapedName.toString() === "__global"
)
break;
name = `${current.escapedName.toString()}.${name}`;
}
return name;
}

const getTypeOfSymbol = (symbol, isValue) => {
let decl;
const type = (() => {
Expand Down Expand Up @@ -969,6 +987,18 @@ const printError = (diagnostic) => {
}

if (symbol) {
// Handle `NodeJS` prefix for global types
if (symbol.escapedName !== undefined) {
const fullEscapedName = getFullEscapedName(symbol);

if (fullEscapedName.includes("NodeJS.")) {
return {
type: "primitive",
name: fullEscapedName,
};
}
}

const decl = getDeclaration(symbol);
if (decl && isNodeModulesSource(decl.getSourceFile())) {
let symbol = /** @type {any} */ (decl).symbol;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@types/node": "^14.14.14",
"@types/yargs": "^15.0.12",
"prettier": "^3.0.1",
"typescript": "^5.0.4"
"typescript": "^5.4.2"
},
"dependencies": {
"@yarnpkg/lockfile": "^1.1.0",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,10 @@ type@^2.0.0:
resolved "https://registry.yarnpkg.com/type/-/type-2.1.0.tgz#9bdc22c648cf8cf86dd23d32336a41cfb6475e3f"
integrity sha512-G9absDWvhAWCV2gmF1zKud3OyC61nZDwWvBL2DApaVFogI07CprggiQAOOjvp2NRjYWFzPyu7vwtDrQFq8jeSA==

typescript@^5.0.4:
version "5.0.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.0.4.tgz#b217fd20119bd61a94d4011274e0ab369058da3b"
integrity sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==
typescript@^5.4.2:
version "5.4.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.4.2.tgz#0ae9cebcfae970718474fe0da2c090cad6577372"
integrity sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ==

uri-js@^4.2.2:
version "4.4.1"
Expand Down

0 comments on commit e181446

Please sign in to comment.