Skip to content

Commit

Permalink
Should close #19
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Jan 1, 2024
1 parent 1f99090 commit 55fba80
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
38 changes: 36 additions & 2 deletions __tests__/api-writer/glua-api-writer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { apiDefinition as hookApiDefinition, json as hookJson } from '../test-da
// import { apiDefinition as libraryFunctionApiDefinition, json as libraryFunctionJson } from '../test-data/offline-sites/gmod-wiki/library-function-ai-getscheduleid';
import { apiDefinition as structApiDefinition, markup as structMarkup, json as structJson } from '../test-data/offline-sites/gmod-wiki/struct-custom-entity-fields';
// import { apiDefinition as enumApiDefinition, json as enumJson } from '../test-data/offline-sites/gmod-wiki/enums-use';
import { markup as panelMarkup, structApiDefinition as panelApiDefinition } from '../test-data/offline-sites/gmod-wiki/panel-slider';
import { markup as panelMarkup, apiDefinition as panelApiDefinition } from '../test-data/offline-sites/gmod-wiki/panel-slider';
import { markup as multiReturnFuncMarkup, apiDefinition as multiReturnFuncApiDefinition } from '../test-data/offline-sites/gmod-wiki/library-function-concommand-gettable';
import { markup as varargsFuncMarkup, apiDefinition as varargsFuncApiDefinition } from '../test-data/offline-sites/gmod-wiki/library-function-coroutine-resume';
import { LibraryFunction, WikiPage, WikiPageMarkupScraper } from '../../src/scrapers/wiki-page-markup-scraper';
import { GluaApiWriter } from '../../src/api-writer/glua-api-writer';
import fetchMock from "jest-fetch-mock";
Expand Down Expand Up @@ -52,6 +54,38 @@ describe('GLua API Writer', () => {
expect(api).toEqual(panelApiDefinition);
});

it('should properly handle multiple return types', async () => {
const writer = new GluaApiWriter();

fetchMock.mockResponseOnce(multiReturnFuncMarkup);

const responseMock = <Response>{
url: 'https://wiki.facepunch.com/gmod/concommand.GetTable?format=text',
};

const scrapeCallback = new WikiPageMarkupScraper(responseMock.url).getScrapeCallback();
const panel = await scrapeCallback(responseMock, multiReturnFuncMarkup);
expect(panel).toHaveLength(1);
const api = writer.writePages(panel);
expect(api).toEqual(multiReturnFuncApiDefinition);
});

it('should properly handle varargs in parameter and return types', async () => {
const writer = new GluaApiWriter();

fetchMock.mockResponseOnce(varargsFuncMarkup);

const responseMock = <Response>{
url: 'https://wiki.facepunch.com/gmod/coroutine.resume?format=text',
};

const scrapeCallback = new WikiPageMarkupScraper(responseMock.url).getScrapeCallback();
const panel = await scrapeCallback(responseMock, varargsFuncMarkup);
expect(panel).toHaveLength(1);
const api = writer.writePages(panel);
expect(api).toEqual(varargsFuncApiDefinition);
});

it('should write optional parameters with a question mark', () => {
const writer = new GluaApiWriter();
// Non-existant page, only to test the optional parameter
Expand Down Expand Up @@ -80,7 +114,7 @@ describe('GLua API Writer', () => {
],
});

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`);
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`);
});

it('should allow overriding specific page addresses', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const markup = `<function name="GetTable" parent="concommand" type="libraryfunc">
<description>Returns the tables of all console command callbacks, and autocomplete functions, that were added to the game with <page>concommand.Add</page>.
</description>
<realm>Shared and Menu</realm>
<file line="16-L22">lua/includes/modules/concommand.lua</file>
<rets>
<ret name="" type="table">Table of command callback functions.</ret>
<ret name="" type="table">Table of command autocomplete functions.</ret>
</rets>
</function>`;

export const apiDefinition =
`concommand = {}
---[SHARED AND MENU] Returns the tables of all console command callbacks, and autocomplete functions, that were added to the game with concommand.Add.
---
---[(View on wiki)](https://wiki.facepunch.com/gmod/concommand.GetTable)
---@return table # Table of command callback functions.
---@return table # Table of command autocomplete functions.
function concommand.GetTable() end\n\n`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const markup = `<function name="resume" parent="coroutine" type="libraryfunc">
<description>Resumes the given coroutine and passes the given vararg to either the function arguments or the <page>coroutine.yield</page> that is inside that function and returns whatever yield is called with the next time or by the final return in the function.</description>
<realm>Shared and Menu</realm>
<args>
<arg name="coroutine" type="thread">Coroutine to resume.</arg>
<arg name="args" type="vararg">Arguments to be returned by <page>coroutine.yield</page>.</arg>
</args>
<rets>
<ret name="" type="boolean">If the executed thread code had no errors occur within it.</ret>
<ret name="" type="vararg">If an error occurred, this will be a string containing the error message. Otherwise, this will be arguments that were yielded.</ret>
</rets>
</function>`;

export const apiDefinition =
`coroutine = {}
---[SHARED AND MENU] Resumes the given coroutine and passes the given vararg to either the function arguments or the coroutine.yield that is inside that function and returns whatever yield is called with the next time or by the final return in the function.
---
---[(View on wiki)](https://wiki.facepunch.com/gmod/coroutine.resume)
---@param coroutine thread Coroutine to resume.
---@param ... any Arguments to be returned by coroutine.yield.
---@return boolean # If the executed thread code had no errors occur within it.
---@return any ... # If an error occurred, this will be a string containing the error message. Otherwise, this will be arguments that were yielded.
function coroutine.resume(coroutine, ...) end\n\n`;
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ end
</example>`;

export const structApiDefinition =
export const apiDefinition =
`---@deprecated Panel:SetActionFunction and Panel:PostMessage. Use DNumSlider instead.
---@class Slider : Panel
local Slider = {}\n\n`;
16 changes: 8 additions & 8 deletions src/api-writer/glua-api-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class GluaApiWriter {

private transformType(type: string) {
if (type === 'vararg')
return '...';
return 'any';

return type;
}
Expand All @@ -270,17 +270,17 @@ export class GluaApiWriter {
}

if (func.returns) {
const returns = `---@return ${func.returns.map(ret => this.transformType(ret.type)).join(', ')}`;

func.returns.forEach(ret => {
const description = removeNewlines(ret.description ?? '');

if (func.returns!.length === 1) {
luaDocComment += `${returns} #${description}\n`;
return;
}
luaDocComment += `---@return `;

if (ret.type === 'vararg')
luaDocComment += 'any ...';
else
luaDocComment += `${this.transformType(ret.type)}`;

luaDocComment += `${returns} #${this.transformType(ret.type)} - ${description}\n`;
luaDocComment += ` # ${description}\n`;
});
}

Expand Down

0 comments on commit 55fba80

Please sign in to comment.