Skip to content

Commit 4f3f84d

Browse files
committed
Closes #10
1 parent c95b41d commit 4f3f84d

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +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 { WikiPage, WikiPageMarkupScraper } from '../../src/scrapers/wiki-page-markup-scraper';
6+
import { LibraryFunction, WikiPage, WikiPageMarkupScraper } from '../../src/scrapers/wiki-page-markup-scraper';
77
import { GluaApiWriter } from '../../src/api-writer/glua-api-writer';
88
import fetchMock from "jest-fetch-mock";
99

@@ -35,6 +35,37 @@ describe('GLua API Writer', () => {
3535
expect(api).toEqual(structApiDefinition);
3636
});
3737

38+
it('should write optional parameters with a question mark', () => {
39+
const writer = new GluaApiWriter();
40+
// Non-existant page, only to test the optional parameter
41+
const api = writer.writePage(<LibraryFunction>{
42+
name: 'Explode',
43+
address: 'Global.Explode',
44+
parent: '_G',
45+
dontDefineParent: true,
46+
description: 'Explodes with an optional intensity.',
47+
realm: 'Shared',
48+
type: 'libraryfunc',
49+
url: 'na',
50+
arguments: [
51+
{
52+
name: 'intensity',
53+
type: 'number',
54+
description: 'The intensity of the explosion.',
55+
default: '1000',
56+
},
57+
],
58+
returns: [
59+
{
60+
type: 'number',
61+
description: 'The amount of damage done.',
62+
},
63+
],
64+
});
65+
66+
expect(api).toEqual(`---[SHARED] Explodes with an optional intensity.\n---\n---[(View on wiki)](na)\n---@param intensity? number The intensity of the explosion.\n---@return number #The amount of damage done.\nfunction _G.Explode(intensity) end\n\n`);
67+
});
68+
3869
it('should allow overriding specific page addresses', () => {
3970
const writer = new GluaApiWriter();
4071
const override = Math.random().toString(36).substring(7);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class GluaApiWriter {
207207
if (arg.type === 'vararg')
208208
arg.name = '...';
209209

210-
luaDocComment += `---@param ${GluaApiWriter.safeName(arg.name)} ${this.transformType(arg.type)} ${putCommentBeforeEachLine(arg.description!)}\n`;
210+
luaDocComment += `---@param ${GluaApiWriter.safeName(arg.name)}${arg.default ? `?` : ''} ${this.transformType(arg.type)} ${putCommentBeforeEachLine(arg.description!)}\n`;
211211
});
212212
}
213213

0 commit comments

Comments
 (0)