Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions __tests__/api-writer/glua-api-writer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ describe('GLua API Writer', () => {
expect(api).toContain('---@field GetEntityDriveMode function');
});

it('should create aliasses for global enumerations', () => {
it('should create flat enums for global enumerations', () => {
const writer = new GluaApiWriter();
const api = writer.writePage(<Enum>{
type: 'enum',
Expand Down Expand Up @@ -527,9 +527,14 @@ describe('GLua API Writer', () => {
expect(api).toContain('---@readonly\nMATERIAL_FOG_NONE = 0');
expect(api).toContain('---@readonly\nMATERIAL_FOG_LINEAR = 1');
expect(api).toContain('---@readonly\nMATERIAL_FOG_LINEAR_BELOW_FOG_Z = -2147483648');
expect(api).toContain('---@alias MATERIAL_FOG');
expect(api).toContain('---| number # Raw numeric enum value');
expect(api).toContain('---| 0 # MATERIAL_FOG_NONE');
expect(api).toContain('---@enum MATERIAL_FOG : number');
expect(api).toContain('---| MATERIAL_FOG_NONE # No fog');
expect(api).toContain('---| MATERIAL_FOG_LINEAR # Linear fog');
// A member without a wiki description carries no detail.
expect(api).toContain('---| MATERIAL_FOG_LINEAR_BELOW_FOG_Z\n');
// TODO values are skipped, and the enum no longer widens to bare `number`.
expect(api).not.toContain('MATERIAL_FOG_NEW_FAKE');
expect(api).not.toContain('---@alias MATERIAL_FOG');
});

it('should create enums for table enumerations', () => {
Expand Down
13 changes: 9 additions & 4 deletions src/api-writer/glua-api-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,18 @@ export class GluaApiWriter {
const enumAliasValue = literalUnion.length > 0 ? `${literalUnion} | number` : 'number';
api += `--- @alias ${_enum.name} ${enumAliasValue}\n`;
} else {
// Advanced annotation: emit numeric literals to help literal-type inference for enum-backed numbers.
api += `\n---@alias ${_enum.name}\n`;
api += '---| number # Raw numeric enum value\n';
// Garry's Mod enums are flat globals, so the field list names each constant.
// Completion then offers `EF_BONEMERGE` rather than the raw value it holds.
// The `: number` base keeps bitwise combinations such as `bit.bor(EF_A, EF_B)`
// assignable, which the previous `---| number` alias member allowed.
api += `\n---@enum ${_enum.name} : number\n`;

for (const item of _enum.items) {
if (item.key !== '' && !isNaN(Number(item.value.trim()))) {
api += `---| ${item.value} # ${item.key}\n`;
const description = item.description?.trim()
? ` # ${removeNewlines(item.description)}`
: '';
api += `---| ${item.key}${description}\n`;
}
}
}
Expand Down
Loading