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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

- Fixed iteration over generators stopping at first yielded `nil` value

- Fixed extending a class not keeping `toString` implementation from a super class

## 0.33.0

- Added support for nullish coalescing `A ?? B`.
Expand Down
1 change: 1 addition & 0 deletions src/lualib/ClassExtends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ function __TS__ClassExtends(this: void, target: LuaClass, base: LuaClass): void
// Re-add metatable events defined by accessors with `__TS__SetDescriptor`
if (typeof base.prototype.__index === "function") target.prototype.__index = base.prototype.__index;
if (typeof base.prototype.__newindex === "function") target.prototype.__newindex = base.prototype.__newindex;
if (typeof base.prototype.__tostring === "function") target.prototype.__tostring = base.prototype.__tostring;
}
1 change: 1 addition & 0 deletions src/lualib/declarations/tstl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface Metatable {
_descriptors?: Record<string, PropertyDescriptor>;
__index?: any;
__newindex?: any;
__tostring?: any;
}

interface LuaClass extends Metatable {
Expand Down
93 changes: 93 additions & 0 deletions test/unit/builtins/object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,96 @@ test.each(["[]", '[["a", 1], ["b", 2]]', '[["a", 1], ["a", 2]]', 'new Map([["foo
util.testExpression`Object.fromEntries(${entries})`.expectToMatchJsResult();
}
);

describe(".toString()", () => {
const toStringDeclaration = `
function toString(value: object) {
const result = value.toString();
return result === "[object Object]" || result.startsWith("table: ") ? "table" : result;
}
`;

test("class override", () => {
util.testFunction`
${toStringDeclaration}
class A {
public toString() {
return "A";
}
}

return toString(new A());
`.expectToMatchJsResult();
});

test("inherited class override", () => {
util.testFunction`
${toStringDeclaration}
class A {
public toString() {
return "A";
}
}

class B extends A {}

return { A: toString(new A()), B: toString(new B()) };
`.expectToMatchJsResult();
});

test("don't affect inherited class", () => {
util.testFunction`
${toStringDeclaration}
class A {}

class B extends A {
public toString() {
return "B";
}
}

return { A: toString(new A()), B: toString(new B()) };
`.expectToMatchJsResult();
});

test("override inherited class override", () => {
util.testFunction`
${toStringDeclaration}
class A {
public toString() {
return "A";
}
}

class B extends A {
public toString() {
return "B";
}
}

return { A: toString(new A()), B: toString(new B()) };
`.expectToMatchJsResult();
});
});

describe(".hasOwnProperty()", () => {
test("class field", () => {
util.testFunction`
class A {
public field = true;
}

return new A().hasOwnProperty("field");
`.expectToMatchJsResult();
});

test("class method", () => {
util.testFunction`
class A {
public method() {}
}

return new A().hasOwnProperty("method");
`.expectToMatchJsResult();
});
});
36 changes: 0 additions & 36 deletions test/unit/classes/classes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,42 +352,6 @@ test("ClassComputedMethodCall", () => {
`.expectToMatchJsResult();
});

test("ClassToString", () => {
util.testFunction`
class a {
public toString(): string {
return "instance of a";
}
}
let inst = new a();
return inst.toString();
`.expectToMatchJsResult();
});

test("HasOwnProperty true", () => {
util.testFunction`
class a {
public test(): void {
}
}
let inst = new a();
inst["prop"] = 17;
return inst.hasOwnProperty("prop");
`.expectToMatchJsResult();
});

test("HasOwnProperty false", () => {
util.testFunction`
class a {
public test(): void {
}
}
let inst = new a();
inst["prop"] = 17;
return inst.hasOwnProperty("test");
`.expectToMatchJsResult();
});

test("CastClassMethodCall", () => {
util.testFunction`
interface result
Expand Down