Skip to content

Commit 693190b

Browse files
committed
Fake enums should not be exposed as globals (fixes #76)
1 parent 7dd966d commit 693190b

File tree

4 files changed

+126
-10
lines changed

4 files changed

+126
-10
lines changed

__tests__/api-writer/glua-api-writer.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { apiDefinition as hookApiDefinition, json as hookJson } from '../test-da
33
// import { apiDefinition as libraryFunctionApiDefinition, json as libraryFunctionJson } from '../test-data/offline-sites/gmod-wiki/library-function-ai-getscheduleid';
44
import { apiDefinition as structApiDefinition, markup as structMarkup, json as structJson } from '../test-data/offline-sites/gmod-wiki/struct-custom-entity-fields';
55
// import { apiDefinition as enumApiDefinition, json as enumJson } from '../test-data/offline-sites/gmod-wiki/enums-use';
6+
import { apiDefinition as enumNavCornerApiDefinition, markup as enumNavCornerMarkup } from '../test-data/offline-sites/gmod-wiki/enums-navcorner';
67
import { markup as panelMarkup, apiDefinition as panelApiDefinition } from '../test-data/offline-sites/gmod-wiki/panel-slider';
78
import { markup as multiReturnFuncMarkup, apiDefinition as multiReturnFuncApiDefinition } from '../test-data/offline-sites/gmod-wiki/library-function-concommand-gettable';
89
import { markup as varargsFuncMarkup, apiDefinition as varargsFuncApiDefinition } from '../test-data/offline-sites/gmod-wiki/library-function-coroutine-resume';
@@ -42,6 +43,23 @@ describe('GLua API Writer', () => {
4243
expect(api).toEqual(structApiDefinition);
4344
});
4445

46+
it('should be able to write Lua API definitions directly from wiki json data for a fake enum', async () => {
47+
const writer = new GluaApiWriter();
48+
49+
fetchMock.mockResponseOnce(enumNavCornerMarkup);
50+
51+
const responseMock = <Response>{
52+
url: 'https://wiki.facepunch.com/gmod/Enums/NavCorner?format=text',
53+
};
54+
55+
const scrapeCallback = new WikiPageMarkupScraper(responseMock.url).getScrapeCallback();
56+
const enums = await scrapeCallback(responseMock, enumNavCornerMarkup);
57+
expect(enums).toHaveLength(1);
58+
writer.writePages(enums, mockFilePath);
59+
const api = writer.makeApiFromPages(writer.getPages(mockFilePath));
60+
expect(api).toEqual(enumNavCornerApiDefinition);
61+
});
62+
4563
it('should handle deprecated in description', async () => {
4664
const writer = new GluaApiWriter();
4765

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
export const markup = `<enum>
2+
<realm>Server</realm>
3+
<description>
4+
Enumerations used by <page>CNavArea</page> methods.
5+
These Enums correspond to each corner of a <page>CNavArea</page>
6+
7+
<warning>These enumerations do not exist in game and are listed here only for reference</warning>
8+
</description>
9+
<items>
10+
<item key="NORTH_WEST" value="0">North West Corner</item>
11+
<item key="NORTH_EAST" value="1">North East Corner</item>
12+
<item key="SOUTH_EAST" value="2">South East Corner</item>
13+
<item key="SOUTH_WEST" value="3">South West Corner</item>
14+
<item key="NUM_CORNERS" value="4">Represents all corners, only applicable to certain functions, such as <page>CNavArea:PlaceOnGround</page>.</item>
15+
</items>
16+
17+
</enum>`;
18+
19+
export const json = {
20+
url: 'https://wiki.facepunch.com/gmod/Enums/NavCorner',
21+
type: 'enum',
22+
name: 'NavCorner',
23+
address: 'NavCorner',
24+
description: `\nEnumerations used by <page>CNavArea</page> methods.\nThese Enums correspond to each corner of a <page>CNavArea</page>\n\n<warning>These enumerations do not exist in game and are listed here only for reference</warning>\n`,
25+
realm: 'server',
26+
items: [
27+
{
28+
key: 'NORTH_WEST',
29+
value: '0',
30+
description: 'North West Corner',
31+
},
32+
{
33+
key: 'NORTH_EAST',
34+
value: '1',
35+
description: 'North East Corner',
36+
},
37+
{
38+
key: 'SOUTH_EAST',
39+
value: '2',
40+
description: 'South East Corner',
41+
},
42+
{
43+
key: 'SOUTH_WEST',
44+
value: '3',
45+
description: 'South West Corner',
46+
},
47+
{
48+
key: 'NUM_CORNERS',
49+
value: '4',
50+
description: 'Represents all corners, only applicable to certain functions, such as CNavArea:PlaceOnGround.',
51+
},
52+
],
53+
};
54+
55+
export const apiDefinition = `---![(Server)](https://github.com/user-attachments/assets/d8fbe13a-6305-4e16-8698-5be874721ca1) Enumerations used by [CNavArea](https://wiki.facepunch.com/gmod/CNavArea) methods.
56+
--- These Enums correspond to each corner of a [CNavArea](https://wiki.facepunch.com/gmod/CNavArea)
57+
---
58+
--- **WARNING**: These enumerations do not exist in game and are listed here only for reference
59+
--- * \`NORTH_WEST\` = \`0\`
60+
--- * \`NORTH_EAST\` = \`1\`
61+
--- * \`SOUTH_EAST\` = \`2\`
62+
--- * \`SOUTH_WEST\` = \`3\`
63+
--- * \`NUM_CORNERS\` = \`4\`
64+
---
65+
--- [View wiki](https://wiki.facepunch.com/gmod/Enums/NavCorner)
66+
--- @alias NavCorner 0 | 1 | 2 | 3 | 4
67+
68+
69+
`;

src/api-writer/glua-api-writer.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ export class GluaApiWriter {
7878
this.pageOverrides.set(safeFileName(pageAddress, '.'), override);
7979
}
8080

81+
private isFakeEnum(_enum: Enum) {
82+
// TODO: Kindly ask Rubat to add a <isFake> marker of sorts to the wiki
83+
return _enum.description.includes('**WARNING**: These enumerations do not exist in game and are listed here only for reference');
84+
}
85+
8186
public writePage(page: WikiPage) {
8287
const fileSafeAddress = safeFileName(page.address, '.');
8388
if (this.pageOverrides.has(fileSafeAddress)) {
@@ -280,22 +285,46 @@ export class GluaApiWriter {
280285
}
281286
};
282287

283-
for (const item of _enum.items)
284-
writeItem(item.key, item);
288+
if (!this.isFakeEnum(_enum)) {
289+
for (const item of _enum.items)
290+
writeItem(item.key, item);
291+
}
285292

286293
if (isContainedInTable) {
287294
api += '}';
288295
} else {
289296
// TODO: Clean up this workaround when LuaLS supports global enumerations.
290297
// Until LuaLS supports global enumerations (https://github.com/LuaLS/lua-language-server/issues/2721) we
291298
// will use @alias as a workaround.
292-
// LuaLS doesn't nicely display annotations for aliasses, hence this is commented
293-
//api += `\n---${this.formatRealm(_enum.realm)} ${_enum.description ? `${wrapInComment(_enum.description)}` : ''}\n`;
294-
api += `\n---@alias ${_enum.name}\n`;
295299

296-
for (const item of _enum.items) {
297-
if (item.key !== '' && !isNaN(Number(item.value.trim()))) {
298-
api += `---| \`${item.key}\`\n`;
300+
// Some enums like SNDLVL are fake in the wiki and only listed for reference, so we render those such that the enums
301+
// are explained in the annotation
302+
if (this.isFakeEnum(_enum)) {
303+
// First output the description
304+
api += `---${this.formatRealm(_enum.realm)} ${_enum.description ? `${wrapInComment(_enum.description)}` : ''}\n`;
305+
let enumValues = '';
306+
307+
for (const item of _enum.items) {
308+
if (item.key !== '' && !isNaN(Number(item.value.trim()))) {
309+
api += `--- * \`${item.key}\` = \`${item.value}\`\n`;
310+
enumValues += `${item.value} | `;
311+
}
312+
}
313+
314+
enumValues = enumValues.slice(0, -3); // Remove trailing " | "
315+
316+
// Add wikilink
317+
api += `---\n--- [View wiki](${_enum.url})\n`;
318+
api += `--- @alias ${_enum.name} ${enumValues}\n`;
319+
} else {
320+
// LuaLS doesn't nicely display annotations for aliasses, hence this is commented
321+
//api += `\n---${this.formatRealm(_enum.realm)} ${_enum.description ? `${wrapInComment(_enum.description)}` : ''}\n`;
322+
api += `\n---@alias ${_enum.name}\n`;
323+
324+
for (const item of _enum.items) {
325+
if (item.key !== '' && !isNaN(Number(item.value.trim()))) {
326+
api += `---| \`${item.key}\`\n`;
327+
}
299328
}
300329
}
301330
}

src/utils/metadata.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type Metadata = {
1212
}
1313

1414
export async function writeMetadata(url: string, outputDirectory: string): Promise<Metadata> {
15-
const results = await scrapeAndCollect(new WikiHistoryPageScraper(path.join(url, '~recentchanges')));
15+
const results = await scrapeAndCollect(new WikiHistoryPageScraper(url + '/~recentchanges'));
1616
const exists = promisify(fs.exists);
1717
const lastUpdate = results[0].history[0].dateTime;
1818

@@ -29,7 +29,7 @@ export async function writeMetadata(url: string, outputDirectory: string): Promi
2929
return metadata;
3030
}
3131

32-
export async function readMetadata(outputDirectory: string) : Promise<Metadata | undefined> {
32+
export async function readMetadata(outputDirectory: string): Promise<Metadata | undefined> {
3333
const metadataPath = path.join(outputDirectory, metadataFilename);
3434
const exists = promisify(fs.exists);
3535

0 commit comments

Comments
 (0)