11import * as ts from "typescript" ;
22import * as tstl from "../../LuaAST" ;
33import { transformBuiltinPropertyAccessExpression } from "../builtins" ;
4- import { FunctionVisitor , TransformationContext , TransformerPlugin } from "../context" ;
4+ import { FunctionVisitor , TransformationContext } from "../context" ;
55import { AnnotationKind , getTypeAnnotations } from "../utils/annotations" ;
66import { createExpressionPlusOne } from "../utils/lua-ast" ;
77import { isArrayType , isNumberType , isStringType } from "../utils/typescript" ;
8+ import { tryGetConstEnumValue } from "./enum" ;
9+ import { transformLuaTableElementAccessExpression , transformLuaTablePropertyAccessExpression } from "./lua-table" ;
810
911export function transformElementAccessArgument (
1012 context : TransformationContext ,
11- expression : ts . ElementAccessExpression
13+ node : ts . ElementAccessExpression
1214) : tstl . Expression {
13- const index = context . transformExpression ( expression . argumentExpression ) ;
15+ const index = context . transformExpression ( node . argumentExpression ) ;
1416
15- const type = context . checker . getTypeAtLocation ( expression . expression ) ;
16- const argumentType = context . checker . getTypeAtLocation ( expression . argumentExpression ) ;
17+ const type = context . checker . getTypeAtLocation ( node . expression ) ;
18+ const argumentType = context . checker . getTypeAtLocation ( node . argumentExpression ) ;
1719 if ( isNumberType ( context , argumentType ) && isArrayType ( context , type ) ) {
1820 return createExpressionPlusOne ( index ) ;
1921 }
2022
2123 return index ;
2224}
2325
24- const transformElementAccessExpression : FunctionVisitor < ts . ElementAccessExpression > = ( expression , context ) => {
26+ export const transformElementAccessExpression : FunctionVisitor < ts . ElementAccessExpression > = ( expression , context ) => {
27+ transformLuaTableElementAccessExpression ( context , expression ) ;
28+
29+ const constEnumValue = tryGetConstEnumValue ( context , expression ) ;
30+ if ( constEnumValue ) {
31+ return constEnumValue ;
32+ }
33+
2534 let table = context . transformExpression ( expression . expression ) ;
2635 if ( tstl . isTableExpression ( table ) ) {
2736 table = tstl . createParenthesizedExpression ( table ) ;
@@ -41,7 +50,20 @@ const transformElementAccessExpression: FunctionVisitor<ts.ElementAccessExpressi
4150 return tstl . createTableIndexExpression ( table , transformElementAccessArgument ( context , expression ) , expression ) ;
4251} ;
4352
44- const transformPropertyAccessExpression : FunctionVisitor < ts . PropertyAccessExpression > = ( expression , context ) => {
53+ export const transformPropertyAccessExpression : FunctionVisitor < ts . PropertyAccessExpression > = (
54+ expression ,
55+ context
56+ ) => {
57+ const constEnumValue = tryGetConstEnumValue ( context , expression ) ;
58+ if ( constEnumValue ) {
59+ return constEnumValue ;
60+ }
61+
62+ const luaTableResult = transformLuaTablePropertyAccessExpression ( context , expression ) ;
63+ if ( luaTableResult ) {
64+ return luaTableResult ;
65+ }
66+
4567 const builtinResult = transformBuiltinPropertyAccessExpression ( context , expression ) ;
4668 if ( builtinResult ) {
4769 return builtinResult ;
@@ -73,17 +95,9 @@ const transformPropertyAccessExpression: FunctionVisitor<ts.PropertyAccessExpres
7395 return tstl . createTableIndexExpression ( callPath , tstl . createStringLiteral ( property ) , expression ) ;
7496} ;
7597
76- const transformQualifiedName : FunctionVisitor < ts . QualifiedName > = ( node , context ) => {
98+ export const transformQualifiedName : FunctionVisitor < ts . QualifiedName > = ( node , context ) => {
7799 const right = tstl . createStringLiteral ( node . right . text , node . right ) ;
78100 const left = context . transformExpression ( node . left ) ;
79101
80102 return tstl . createTableIndexExpression ( left , right , node ) ;
81103} ;
82-
83- export const accessPlugin : TransformerPlugin = {
84- visitors : {
85- [ ts . SyntaxKind . PropertyAccessExpression ] : transformPropertyAccessExpression ,
86- [ ts . SyntaxKind . ElementAccessExpression ] : transformElementAccessExpression ,
87- [ ts . SyntaxKind . QualifiedName ] : transformQualifiedName ,
88- } ,
89- } ;
0 commit comments