1+ import {
2+ luaTableCannotBeAccessedDynamically ,
3+ luaTableCannotBeExtended ,
4+ luaTableForbiddenUsage ,
5+ luaTableMustBeAmbient ,
6+ unsupportedProperty ,
7+ luaTableInvalidInstanceOf ,
8+ } from "../../../src/transformation/utils/diagnostics" ;
19import * as util from "../../util" ;
210
311const tableLibClass = `
412/** @luaTable */
513declare class Table<K extends {} = {}, V = any> {
614 length: number;
7- constructor(notAllowed?: boolean );
8- set(key?: K, value?: V): void;
9- get(key?: K, notAllowed?: K ): V;
15+ constructor(notAllowed?: any );
16+ set(key?: K, value?: V, notAllowed?: any ): void;
17+ get(key?: K, notAllowed?: any ): V;
1018 other(): void;
1119}
1220declare let tbl: Table;
@@ -17,31 +25,35 @@ const tableLibInterface = `
1725/** @luaTable */
1826declare interface Table<K extends {} = {}, V = any> {
1927 length: number;
20- constructor(notAllowed?: boolean );
21- set(key?: K, value?: V): void;
22- get(key?: K, notAllowed?: K ): V;
28+ constructor(notAllowed?: any );
29+ set(key?: K, value?: V, notAllowed?: any ): void;
30+ get(key?: K, notAllowed?: any ): V;
2331 other(): void;
2432}
2533declare let tbl: Table;
2634` ;
2735
2836test . each ( [ tableLibClass ] ) ( "LuaTables cannot be constructed with arguments" , tableLib => {
29- util . testModule ( tableLib + `const table = new Table(true);` ) . expectDiagnosticsToMatchSnapshot ( ) ;
37+ util . testModule ( tableLib + `const table = new Table(true);` ) . expectDiagnosticsToMatchSnapshot ( [
38+ luaTableForbiddenUsage . code ,
39+ ] ) ;
3040} ) ;
3141
3242test . each ( [ tableLibClass , tableLibInterface ] ) (
3343 "LuaTable set() cannot be used in a LuaTable call expression" ,
3444 tableLib => {
35- util . testModule ( tableLib + `const exp = tbl.set("value", 5)` ) . expectDiagnosticsToMatchSnapshot ( ) ;
45+ util . testModule ( tableLib + `const exp = tbl.set("value", 5)` ) . expectDiagnosticsToMatchSnapshot ( [
46+ unsupportedProperty . code ,
47+ ] ) ;
3648 }
3749) ;
3850
3951test . each ( [ tableLibClass , tableLibInterface ] ) ( "LuaTables cannot have other members" , tableLib => {
40- util . testModule ( tableLib + `tbl.other()` ) . expectDiagnosticsToMatchSnapshot ( ) ;
52+ util . testModule ( tableLib + `tbl.other()` ) . expectDiagnosticsToMatchSnapshot ( [ unsupportedProperty . code ] ) ;
4153} ) ;
4254
4355test . each ( [ tableLibClass , tableLibInterface ] ) ( "LuaTables cannot have other members" , tableLib => {
44- util . testModule ( tableLib + `let x = tbl.other()` ) . expectDiagnosticsToMatchSnapshot ( ) ;
56+ util . testModule ( tableLib + `let x = tbl.other()` ) . expectDiagnosticsToMatchSnapshot ( [ unsupportedProperty . code ] ) ;
4557} ) ;
4658
4759test . each ( [ tableLibClass ] ) ( "LuaTable new" , tableLib => {
@@ -56,7 +68,7 @@ test.each([tableLibClass])("LuaTable length", tableLib => {
5668} ) ;
5769
5870test . each ( [ tableLibClass , tableLibInterface ] ) ( "Cannot set LuaTable length" , tableLib => {
59- util . testModule ( tableLib + `tbl.length = 2;` ) . expectDiagnosticsToMatchSnapshot ( ) ;
71+ util . testModule ( tableLib + `tbl.length = 2;` ) . expectDiagnosticsToMatchSnapshot ( [ luaTableForbiddenUsage . code ] ) ;
6072} ) ;
6173
6274test . each ( [ tableLibClass , tableLibInterface ] ) ( "Forbidden LuaTable use" , tableLib => {
@@ -69,15 +81,15 @@ test.each([tableLibClass, tableLibInterface])("Forbidden LuaTable use", tableLib
6981 'tbl.set(...(["field", 0] as const))' ,
7082 'tbl.set("field", ...([0] as const))' ,
7183 ] ) ( "Forbidden LuaTable use (%p)" , invalidCode => {
72- util . testModule ( tableLib + invalidCode ) . expectDiagnosticsToMatchSnapshot ( ) ;
84+ util . testModule ( tableLib + invalidCode ) . expectDiagnosticsToMatchSnapshot ( [ luaTableForbiddenUsage . code ] ) ;
7385 } ) ;
7486} ) ;
7587
7688test . each ( [ tableLibClass ] ) ( "Cannot extend LuaTable class" , tableLib => {
7789 test . each ( [ `class Ext extends Table {}` , `const c = class Ext extends Table {}` ] ) (
7890 "Cannot extend LuaTable class (%p)" ,
7991 code => {
80- util . testModule ( tableLib + code ) . expectDiagnosticsToMatchSnapshot ( ) ;
92+ util . testModule ( tableLib + code ) . expectDiagnosticsToMatchSnapshot ( [ luaTableCannotBeExtended . code ] ) ;
8193 }
8294 ) ;
8395} ) ;
@@ -87,27 +99,31 @@ test.each([
8799 `/** @luaTable */ export class Table {}` ,
88100 `/** @luaTable */ const c = class Table {}` ,
89101] ) ( "LuaTable classes must be ambient (%p)" , code => {
90- util . testModule ( code ) . expectDiagnosticsToMatchSnapshot ( ) ;
102+ util . testModule ( code ) . expectDiagnosticsToMatchSnapshot ( [ luaTableMustBeAmbient . code ] ) ;
91103} ) ;
92104
93105test . each ( [ tableLibClass ] ) ( "Cannot extend LuaTable class" , tableLib => {
94106 test . each ( [ `tbl instanceof Table` ] ) ( "Cannot use instanceof on a LuaTable class (%p)" , code => {
95- util . testModule ( tableLib + code ) . expectDiagnosticsToMatchSnapshot ( ) ;
107+ util . testModule ( tableLib + code ) . expectDiagnosticsToMatchSnapshot ( [ luaTableInvalidInstanceOf . code ] ) ;
96108 } ) ;
97109} ) ;
98110
99111test . each ( [ tableLibClass , tableLibInterface ] ) ( "Cannot use ElementAccessExpression on a LuaTable" , tableLib => {
100112 test . each ( [ `tbl["get"]("field")` , `tbl["set"]("field")` , `tbl["length"]` ] ) (
101113 "Cannot use ElementAccessExpression on a LuaTable (%p)" ,
102114 code => {
103- util . testModule ( tableLib + code ) . expectDiagnosticsToMatchSnapshot ( ) ;
115+ util . testModule ( tableLib + code ) . expectDiagnosticsToMatchSnapshot ( [
116+ luaTableCannotBeAccessedDynamically . code ,
117+ ] ) ;
104118 }
105119 ) ;
106120} ) ;
107121
108122test . each ( [ tableLibClass , tableLibInterface ] ) ( "Cannot isolate LuaTable methods" , tableLib => {
109123 test . each ( [ `set` , `get` ] ) ( "Cannot isolate LuaTable method (%p)" , propertyName => {
110- util . testModule ( `${ tableLib } let property = tbl.${ propertyName } ` ) . expectDiagnosticsToMatchSnapshot ( ) ;
124+ util . testModule ( `${ tableLib } let property = tbl.${ propertyName } ` ) . expectDiagnosticsToMatchSnapshot ( [
125+ unsupportedProperty . code ,
126+ ] ) ;
111127 } ) ;
112128} ) ;
113129
0 commit comments