Skip to content

Commit 34d2c33

Browse files
committed
Fix #3003
1 parent 17347c9 commit 34d2c33

File tree

8 files changed

+73
-2
lines changed

8 files changed

+73
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ title: Changelog
44

55
## Unreleased
66

7+
### Bug Fixes
8+
9+
- Variables marked with `@enum` now work for symbols imported from another module, #3003.
10+
- Improved magic introduced with #2999 to work with imported symbols, #3003.
11+
712
## v0.28.11 (2025-08-25)
813

914
### Features

src/lib/converter/comments/discovery.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CommentStyle } from "../../utils/options/declaration.js";
44
import { nicePath } from "../../utils/paths.js";
55
import { ok } from "assert";
66
import { assertNever, filter, firstDefined, i18n, type Logger } from "#utils";
7+
import { resolveAliasedSymbol } from "../utils/symbols.js";
78

89
const variablePropertyKinds = [
910
ts.SyntaxKind.PropertyDeclaration,
@@ -539,6 +540,20 @@ function declarationToCommentNodes(
539540
);
540541
}
541542
}
543+
544+
// #3003 even more magic for handling an imported symbol which appears in a shorthand property assignment
545+
const originalSymbol = sourceSymbol && resolveAliasedSymbol(sourceSymbol, checker);
546+
if (originalSymbol !== sourceSymbol && originalSymbol?.valueDeclaration) {
547+
const commentNode = declarationToCommentNodeIgnoringParents(originalSymbol?.valueDeclaration);
548+
if (commentNode) {
549+
result.push(
550+
{
551+
node: commentNode,
552+
inheritedFromParentDeclaration: true,
553+
},
554+
);
555+
}
556+
}
542557
}
543558

544559
// With overloaded functions/methods, TypeScript will use the comment on the first signature

src/lib/converter/symbols.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,17 @@ function convertVariableAsEnum(
11331133
context.finalizeDeclarationReflection(reflection);
11341134
const rc = context.withScope(reflection);
11351135

1136-
const declaration = symbol.declarations!.find(ts.isVariableDeclaration)!;
1136+
const declaration = symbol.valueDeclaration;
1137+
if (!declaration) {
1138+
context.logger.error(
1139+
i18n.converting_0_as_enum_requires_value_declaration(
1140+
symbol.name,
1141+
),
1142+
symbol.declarations?.[0],
1143+
);
1144+
return;
1145+
}
1146+
11371147
const type = context.checker.getTypeAtLocation(declaration);
11381148

11391149
for (const prop of type.getProperties()) {

src/lib/internationalization/locales/en.cts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export = {
5151
`Converting {0} as a class requires a declaration which represents a non-type value`,
5252
converting_0_as_class_without_construct_signatures:
5353
`{0} is being converted as a class, but does not have any construct signatures`,
54+
converting_0_as_enum_requires_value_declaration:
55+
`Converting {0} as an enum requires a declaration which represents a non-type value`,
5456

5557
comment_for_0_should_not_contain_block_or_modifier_tags:
5658
`The comment for {0} should not contain any block or modifier tags`,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* This is a custom enum that is exported as a constant object.
3+
*
4+
* @enum
5+
*/
6+
export const CustomEnum = {
7+
/** A */
8+
A: "A",
9+
} as const;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { CustomEnum } from "./enums.js";
2+
3+
/**
4+
* This is a second custom enum that is exported as a constant object.
5+
*
6+
* @enum
7+
*/
8+
const CustomEnum2 = {
9+
...CustomEnum,
10+
D: "D",
11+
} as const;
12+
13+
/** @namespace */
14+
export const ExportedObject = {
15+
CustomEnum,
16+
CustomEnum2,
17+
};

src/test/issues.c2.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,4 +2158,17 @@ describe("Issue Tests", () => {
21582158
const local = opts.type.declaration.getChildByName("LocalObject");
21592159
equal(Comment.combineDisplayParts(local?.comment?.summary), "A test object with property a.");
21602160
});
2161+
2162+
it("#3003 handles @enum on properties within @namespace", () => {
2163+
const project = convert();
2164+
2165+
const a = query(project, "ExportedObject.CustomEnum");
2166+
equalKind(a, ReflectionKind.Enum);
2167+
equal(a.children?.map(c => c.name), ["A"]);
2168+
equal(a.children?.map(c => c.type?.toString()), ['"A"']);
2169+
const b = query(project, "ExportedObject.CustomEnum2");
2170+
equalKind(b, ReflectionKind.Enum);
2171+
equal(b.children?.map(c => c.name), ["A", "D"]);
2172+
equal(b.children?.map(c => c.type?.toString()), ['"A"', '"D"']);
2173+
});
21612174
});

src/test/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export function equalKind(refl: Reflection, kind: ReflectionKind) {
108108
equal(
109109
refl.kind,
110110
kind,
111-
`Expected ${ReflectionKind[kind]} but got ${ReflectionKind[refl.kind]}`,
111+
`Expected ${ReflectionKind[kind]} but got ${ReflectionKind[refl.kind]} for ${refl.getFullName()}`,
112112
);
113113
}
114114

0 commit comments

Comments
 (0)