Skip to content

Commit

Permalink
revert: support constructor types and option parameters in function t…
Browse files Browse the repository at this point in the history
…ypes (#485) (#486)

This reverts commit 77f8226.
  • Loading branch information
codeworrior authored Feb 8, 2025
1 parent 77f8226 commit 27ce957
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
5 changes: 1 addition & 4 deletions packages/dts-generator/src/phases/dts-code-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,13 +861,10 @@ function genType(ast: Type, usage: string = "unknown"): string {
return intersectionTypes.join(" & ");
case "FunctionType":
text = "";
if (ast.isConstructor) {
text += "new ";
}
if (!_.isEmpty(ast.typeParameters)) {
text += `<${_.map(ast.typeParameters, (param) => param.name).join(", ")}>`; // TODO defaults, constraints, expressions
}
text += `(${_.map(ast.parameters, (param) => `${param.name}${param.optional ? "?" : ""}: ${genType(param.type, "parameter")}`).join(", ")})`;
text += `(${_.map(ast.parameters, (param) => `${param.name}: ${genType(param.type, "parameter")}`).join(", ")})`;
text += ` => ${ast.type ? genType(ast.type, "returnValue") : "void"}`;
return text;
case "NativeTSTypeExpression":
Expand Down
1 change: 0 additions & 1 deletion packages/dts-generator/src/types/ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ export interface FunctionType {
parameters: Parameter[];
typeParameters?: TypeParameter[];
type?: Type;
isConstructor?: boolean;
}

export interface LiteralType {
Expand Down
28 changes: 10 additions & 18 deletions packages/dts-generator/src/utils/ts-ast-type-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
TypeReference,
UnionType,
TypeLiteral,
Parameter,
} from "../types/ast.js";

/**
Expand Down Expand Up @@ -110,25 +109,18 @@ export class TSASTTypeBuilder {
thisType: Type,
constructorType: Type,
): FunctionType {
const parameters: Parameter[] = paramTypes.map((param, idx) => ({
kind: "Parameter",
name: "p" + (idx + 1), // JSDoc function types don't allow parameter names -> generate names
type: param,
optional: (param as any).optional,
}));
if (thisType != null) {
// for TS, a 'this' type is specified as the first parameter type of a function
parameters.unshift({
kind: "Parameter",
name: "this",
type: thisType,
});
}
return {
kind: "FunctionType",
parameters,
type: constructorType ?? returnType,
isConstructor: constructorType != null,
parameters: paramTypes.map((param, idx) => ({
kind: "Parameter",
name: "p" + (idx + 1), // JSDoc function types don't allow parameter names -> generate names
type: param,
})),
type: returnType,
/* TODO not supported yet:
"this": thisType,
constructor: constructorType
*/
};
}
structure(structure: {
Expand Down
6 changes: 0 additions & 6 deletions packages/dts-generator/src/utils/type-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ export function TypeParser(
next(":");
returnType = parseType();
}
if (constructorType != null && returnType != null) {
throw new SyntaxError(
`A function signature must either use the 'new' keyword or have a return type, ` +
`but not both (pos: ${rLexer.lastIndex}, input='${input}')`,
);
}
type = builder.function(
paramTypes,
returnType,
Expand Down

0 comments on commit 27ce957

Please sign in to comment.