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
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"node": ">=16.10.0"
},
"peerDependencies": {
"typescript": "~4.8.2"
"typescript": "~4.9.3"
},
"dependencies": {
"@typescript-to-lua/language-extensions": "1.0.0",
Expand Down Expand Up @@ -70,6 +70,6 @@
"prettier": "^2.3.2",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.1",
"typescript": "~4.8.2"
"typescript": "~4.9.3"
}
}
3 changes: 2 additions & 1 deletion src/LuaAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// because we don't create the AST from text

import * as ts from "typescript";
import { LuaLibFeature } from "./transformation/utils/lualib";
import { LuaLibFeature } from "./LuaLib";
import { castArray } from "./utils";

export enum SyntaxKind {
Expand Down Expand Up @@ -816,6 +816,7 @@ export function createTableIndexExpression(
}

export type AssignmentLeftHandSideExpression = Identifier | TableIndexExpression;

export function isAssignmentLeftHandSideExpression(node: Node): node is AssignmentLeftHandSideExpression {
return isIdentifier(node) || isTableIndexExpression(node);
}
Expand Down
6 changes: 3 additions & 3 deletions src/LuaPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
import * as ts from "typescript";
import { CompilerOptions, isBundleEnabled, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
import * as lua from "./LuaAST";
import { loadInlineLualibFeatures, LuaLibFeature, loadImportedLualibFeatures } from "./LuaLib";
import { loadImportedLualibFeatures, loadInlineLualibFeatures, LuaLibFeature } from "./LuaLib";
import { isValidLuaIdentifier, shouldAllowUnicode } from "./transformation/utils/safe-names";
import { EmitHost, getEmitPath } from "./transpilation";
import { intersperse, normalizeSlashes } from "./utils";
Expand Down Expand Up @@ -899,9 +899,9 @@ export class LuaPrinter {
map.addMapping(currentMapping);
}

for (const chunk of sourceNode.children) {
for (const chunk of sourceNode.children as SourceChunk[]) {
if (typeof chunk === "string") {
const lines = (chunk as string).split("\n");
const lines = chunk.split("\n");
if (lines.length > 1) {
generatedLine += lines.length - 1;
generatedColumn = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function updateParsedCommandLine(parsedCommandLine: ts.ParsedCommandLine, args:
if (!args[i].startsWith("-")) continue;

const isShorthand = !args[i].startsWith("--");
const argumentName = args[i].substr(isShorthand ? 1 : 2);
const argumentName = args[i].substring(isShorthand ? 1 : 2);
const option = optionDeclarations.find(option => {
if (option.name.toLowerCase() === argumentName.toLowerCase()) return true;
if (isShorthand && option.aliases) {
Expand Down
6 changes: 3 additions & 3 deletions src/lualib/ParseInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export function __TS__ParseInt(this: void, numberString: string, base?: number):
if (hexMatch) {
base = 16;
numberString = __TS__Match(hexMatch, "-")[0]
? "-" + numberString.substr(hexMatch.length)
: numberString.substr(hexMatch.length);
? "-" + numberString.substring(hexMatch.length)
: numberString.substring(hexMatch.length);
}
}

Expand All @@ -22,7 +22,7 @@ export function __TS__ParseInt(this: void, numberString: string, base?: number):

// Calculate string match pattern to use
const allowedDigits =
base <= 10 ? parseIntBasePattern.substring(0, base) : parseIntBasePattern.substr(0, 10 + 2 * (base - 10));
base <= 10 ? parseIntBasePattern.substring(0, base) : parseIntBasePattern.substring(0, 10 + 2 * (base - 10));
const pattern = `^%s*(-?[${allowedDigits}]*)`;

// Try to parse with Lua tonumber
Expand Down
3 changes: 1 addition & 2 deletions src/lualib/SetDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ export function __TS__SetDescriptor(
if (value !== undefined) rawset(target, key, undefined);

if (!rawget(metatable, "_descriptors")) metatable._descriptors = {};
const descriptor = __TS__CloneDescriptor(desc);
metatable._descriptors[key] = descriptor;
metatable._descriptors[key] = __TS__CloneDescriptor(desc);
metatable.__index = descriptorIndex;
metatable.__newindex = descriptorNewIndex;
}
5 changes: 4 additions & 1 deletion src/transformation/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ export class TransformationContext {
/** @internal */
public transformNodeRaw(node: ts.Node, isExpression?: boolean) {
// TODO: Move to visitors?
if (node.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.DeclareKeyword)) {
if (
ts.canHaveModifiers(node) &&
node.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.DeclareKeyword)
) {
return [];
}

Expand Down
2 changes: 2 additions & 0 deletions src/transformation/context/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ interface NodesBySyntaxKind {
[ts.SyntaxKind.AsExpression]: ts.AsExpression;
[ts.SyntaxKind.NonNullExpression]: ts.NonNullExpression;
[ts.SyntaxKind.MetaProperty]: ts.MetaProperty;
[ts.SyntaxKind.SatisfiesExpression]: ts.SatisfiesExpression;
[ts.SyntaxKind.TemplateSpan]: ts.TemplateSpan;
[ts.SyntaxKind.SemicolonClassElement]: ts.SemicolonClassElement;
[ts.SyntaxKind.Block]: ts.Block;
Expand Down Expand Up @@ -148,6 +149,7 @@ export type VisitorResult<T extends ts.Node> = T extends ExpressionLikeNode

export type Visitor<T extends ts.Node> = FunctionVisitor<T> | ObjectVisitor<T>;
export type FunctionVisitor<T extends ts.Node> = (node: T, context: TransformationContext) => VisitorResult<T>;

export interface ObjectVisitor<T extends ts.Node> {
transform: FunctionVisitor<T>;

Expand Down
13 changes: 0 additions & 13 deletions src/transformation/utils/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,13 @@ export const invalidMultiFunctionReturnType = createErrorDiagnosticFactory(
"The $multi function cannot be cast to a non-LuaMultiReturn type."
);

export const invalidMultiTypeToNonArrayLiteral = createErrorDiagnosticFactory("Expected an array literal.");

export const invalidMultiTypeToEmptyPatternOrArrayLiteral = createErrorDiagnosticFactory(
"There must be one or more elements specified here."
);

export const invalidMultiReturnAccess = createErrorDiagnosticFactory(
"The LuaMultiReturn type can only be accessed via an element access expression of a numeric type."
);

export const invalidCallExtensionUse = createErrorDiagnosticFactory(
"This function must be called directly and cannot be referred to."
);

export const annotationRemoved = createErrorDiagnosticFactory(
(kind: AnnotationKind) =>
`'@${kind}' has been removed and will no longer have any effect.` +
`See https://typescripttolua.github.io/docs/advanced/compiler-annotations#${kind.toLowerCase()} for more information.`
);

export const annotationDeprecated = createWarningDiagnosticFactory(
(kind: AnnotationKind) =>
`'@${kind}' is deprecated and will be removed in a future update. Please update your code before upgrading to the next release, otherwise your project will no longer compile. ` +
Expand Down
10 changes: 8 additions & 2 deletions src/transformation/utils/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import { getSymbolInfo } from "./symbols";
import { findFirstNodeAbove } from "./typescript";

export function hasDefaultExportModifier(node: ts.Node): boolean {
return (node.modifiers ?? []).some(modifier => modifier.kind === ts.SyntaxKind.DefaultKeyword);
return (
ts.canHaveModifiers(node) &&
node.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.DefaultKeyword) === true
);
}

export function hasExportModifier(node: ts.Node): boolean {
return (node.modifiers ?? []).some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword);
return (
ts.canHaveModifiers(node) &&
node.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword) === true
);
}

export const createDefaultExportStringLiteral = (original?: ts.Node): lua.StringLiteral =>
Expand Down
3 changes: 3 additions & 0 deletions src/transformation/visitors/binary-expression/bit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function transformBitOperatorToLuaOperator(
return lua.SyntaxKind.BitwiseLeftShiftOperator;
case ts.SyntaxKind.GreaterThanGreaterThanToken:
context.diagnostics.push(unsupportedRightShiftOperator(node));
return lua.SyntaxKind.BitwiseRightShiftOperator;
case ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken:
return lua.SyntaxKind.BitwiseRightShiftOperator;
}
Expand All @@ -66,6 +67,7 @@ export function transformBinaryBitOperation(
case LuaTarget.Lua50:
case LuaTarget.Lua51:
context.diagnostics.push(unsupportedForTarget(node, "Bitwise operations", context.luaTarget));
return transformBinaryBitLibOperation(node, left, right, operator, "bit");

case LuaTarget.LuaJIT:
return transformBinaryBitLibOperation(node, left, right, operator, "bit");
Expand Down Expand Up @@ -111,6 +113,7 @@ export function transformUnaryBitOperation(
case LuaTarget.Lua50:
case LuaTarget.Lua51:
context.diagnostics.push(unsupportedForTarget(node, "Bitwise operations", context.luaTarget));
return transformUnaryBitLibOperation(node, expression, operator, "bit");

case LuaTarget.LuaJIT:
return transformUnaryBitLibOperation(node, expression, operator, "bit");
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/binary-expression/compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as lua from "../../../LuaAST";
import { cast, assertNever } from "../../../utils";
import { TransformationContext } from "../../context";
import { transformInPrecedingStatementScope } from "../../utils/preceding-statements";
import { transformBinaryOperation } from "../binary-expression";
import { transformBinaryOperation } from "./index";
import { transformAssignmentWithRightPrecedingStatements } from "./assignments";

function isLuaExpressionWithSideEffect(expression: lua.Expression) {
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/class/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function transformClassLikeDeclaration(
// Generate a constructor if none was defined in a base class
const constructorResult = transformConstructorDeclaration(
context,
ts.factory.createConstructorDeclaration([], [], [], ts.factory.createBlock([], true)),
ts.factory.createConstructorDeclaration([], [], ts.factory.createBlock([], true)),
localClassName,
instanceFields,
classDeclaration
Expand Down
6 changes: 3 additions & 3 deletions src/transformation/visitors/class/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as ts from "typescript";
import { TransformationContext } from "../../context";

export function isStaticNode(node: ts.Node): boolean {
return (node.modifiers ?? []).some(m => m.kind === ts.SyntaxKind.StaticKeyword);
export function isStaticNode(node: ts.HasModifiers): boolean {
return node.modifiers?.some(m => m.kind === ts.SyntaxKind.StaticKeyword) === true;
}

export function getExtendsClause(node: ts.ClassLikeDeclarationBase): ts.HeritageClause | undefined {
return (node.heritageClauses ?? []).find(clause => clause.token === ts.SyntaxKind.ExtendsKeyword);
return node.heritageClauses?.find(clause => clause.token === ts.SyntaxKind.ExtendsKeyword);
}

export function getExtendedNode(node: ts.ClassLikeDeclarationBase): ts.ExpressionWithTypeArguments | undefined {
Expand Down
2 changes: 1 addition & 1 deletion src/transformation/visitors/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as lua from "../../LuaAST";
import { FunctionVisitor } from "../context";
import { ContextType, getDeclarationContextType } from "../utils/function-context";
import { wrapInToStringForConcat } from "../utils/lua-ast";
import { isStringType } from "../utils/typescript/types";
import { isStringType } from "../utils/typescript";
import { transformArguments, transformContextualCallExpression } from "./call";
import { transformOrderedExpressions } from "./expression-list";

Expand Down
1 change: 1 addition & 0 deletions src/transformation/visitors/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const typescriptVisitors: Visitors = {

[ts.SyntaxKind.NonNullExpression]: (node, context) => context.transformExpression(node.expression),
[ts.SyntaxKind.ExpressionWithTypeArguments]: (node, context) => context.transformExpression(node.expression),
[ts.SyntaxKind.SatisfiesExpression]: (node, context) => context.transformExpression(node.expression),
[ts.SyntaxKind.AsExpression]: transformAssertionExpression,
[ts.SyntaxKind.TypeAssertionExpression]: transformAssertionExpression,
[ts.SyntaxKind.NotEmittedStatement]: () => undefined,
Expand Down
2 changes: 1 addition & 1 deletion src/transpilation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function resolvePlugin(
kind: string,
optionName: string,
basedir: string,
query: string,
query: unknown,
importName = "default"
): { error?: ts.Diagnostic; result?: unknown } {
if (typeof query !== "string") {
Expand Down
2 changes: 0 additions & 2 deletions src/typescript-internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ declare module "typescript" {
parent?: Symbol;
}

function getObjectFlags(type: Type): ObjectFlags;

function transformJsx(context: TransformationContext): (x: SourceFile) => SourceFile;

export type OuterExpression =
Expand Down
2 changes: 0 additions & 2 deletions test/unit/builtins/numbers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as util from "../../util";

test.each([
"NaN === NaN",
"NaN !== NaN",
"NaN + NaN",
"NaN - NaN",
"NaN * NaN",
Expand Down
7 changes: 7 additions & 0 deletions test/unit/expressions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ test("Typescript 4.7 instantiation expression", () => {
`.expectToMatchJsResult();
});

test("Typescript 4.9 satisfies expression", () => {
util.testFunction`
const foo = { a: 1 } satisfies { a: number };
return foo.a;
`.expectToMatchJsResult();
});

test.each([
'"foobar"',
"17",
Expand Down