Skip to content

Commit

Permalink
Fixes #13 bug caused structs to be ignored if class was already defined
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Jan 1, 2024
1 parent 83dac26 commit 493aec5
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 24 deletions.
2 changes: 1 addition & 1 deletion __tests__/api-writer/glua-api-writer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('GLua API Writer', () => {
it('should allow overriding specific class declarations', () => {
const writer = new GluaApiWriter();
const overrideStart = `---@class Custom_Entity_Fields : Parent`;
const override = `${overrideStart}\n---{{CLASS_FIELDS}}\nlocal Custom_Entity_Fields = {}`;
const override = `${overrideStart}\nlocal Custom_Entity_Fields = {}`;
writer.addOverride('class.Custom_Entity_Fields', override);

const api = writer.writePages([<WikiPage>structJson]);
Expand Down
2 changes: 0 additions & 2 deletions custom/class.Color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@
---@field b number The blue component of the color.
---@field a number The alpha component of the color.
local Color = {}

---{{CLASS_FIELDS}}
2 changes: 0 additions & 2 deletions custom/class.ENT.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
---@class ENT : Entity
ENT = {}

---{{CLASS_FIELDS}}
2 changes: 0 additions & 2 deletions custom/class.NPC.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
---@class NPC : Entity
local NPC = {}

---{{CLASS_FIELDS}}
2 changes: 0 additions & 2 deletions custom/class.Player.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
---@class Player : Entity
local Player = {}

---{{CLASS_FIELDS}}
2 changes: 0 additions & 2 deletions custom/class.SWEP.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
---@class SWEP : WEAPON
SWEP = {}

---{{CLASS_FIELDS}}
1 change: 0 additions & 1 deletion custom/class.WEAPON.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
---@class WEAPON : Entity
---{{CLASS_FIELDS}}
local WEAPON = {}
24 changes: 12 additions & 12 deletions src/api-writer/glua-api-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class GluaApiWriter {
let api = '';

if (isClassFunction(page))
api += this.writeClass(page.parent, undefined, undefined, page.deprecated);
api += this.writeClassStart(page.parent, undefined, page.deprecated);
else if (isLibraryFunction(page))
api += this.writeLibraryGlobal(page);

Expand All @@ -94,14 +94,13 @@ export class GluaApiWriter {
return this.writeStruct(page);
}

private writeClass(className: string, parent?: string, classFields: string = '', deprecated?: string) {
private writeClassStart(className: string, parent?: string, deprecated?: string) {
let api: string = '';

if (!this.writtenClasses.has(className)) {
const classOverride = `class.${className}`;
if (this.pageOverrides.has(classOverride)) {
api += this.pageOverrides.get(classOverride)!.replace(/\n$/g, '') + '\n\n';
api = api.replace('---{{CLASS_FIELDS}}', classFields);
} else {
if (deprecated)
api += `---@deprecated ${removeNewlines(deprecated)}\n`
Expand All @@ -113,7 +112,6 @@ export class GluaApiWriter {

api += '\n';
api += `local ${className} = {}\n\n`;
api += classFields;
}

this.writtenClasses.add(className);
Expand All @@ -140,7 +138,7 @@ export class GluaApiWriter {
}

private writeClassFunction(func: ClassFunction) {
let api: string = this.writeClass(func.parent, undefined, undefined, func.deprecated);
let api: string = this.writeClassStart(func.parent, undefined, func.deprecated);

api += this.writeFunctionLuaDocComment(func, func.realm);
api += this.writeFunctionDeclaration(func, func.realm, ':');
Expand All @@ -162,7 +160,9 @@ export class GluaApiWriter {
}

private writePanel(panel: Panel) {
return this.writeClass(panel.name, panel.parent, undefined, panel.deprecated);
let api: string = this.writeClassStart(panel.name, panel.parent, panel.deprecated);

return api;
}

private writePanelFunction(func: PanelFunction) {
Expand Down Expand Up @@ -220,20 +220,20 @@ export class GluaApiWriter {
}

private writeStruct(struct: Struct) {
let fields: string = '';
let api: string = this.writeClassStart(struct.name, undefined, struct.deprecated);

for (const field of struct.fields) {
if (field.deprecated)
fields += `---@deprecated ${removeNewlines(field.deprecated)}\n`;
api += `---@deprecated ${removeNewlines(field.deprecated)}\n`;

fields += `---${removeNewlines(field.description).replace(/\s+/g, ' ')}\n`;
api += `---${removeNewlines(field.description).replace(/\s+/g, ' ')}\n`;

const type = this.transformType(field.type)
fields += `---@type ${type}\n`
fields += `${struct.name}.${GluaApiWriter.safeName(field.name)} = ${field.default ? this.writeType(type, field.default) : 'nil'}\n\n`;
api += `---@type ${type}\n`
api += `${struct.name}.${GluaApiWriter.safeName(field.name)} = ${field.default ? this.writeType(type, field.default) : 'nil'}\n\n`;
}

return this.writeClass(struct.name, undefined, fields, struct.deprecated);
return api;
}

public writePages(pages: WikiPage[]) {
Expand Down

0 comments on commit 493aec5

Please sign in to comment.