Skip to content

Commit 46367c1

Browse files
committed
1.6.0 (fixed compilation)
2 parents be21c8d + b854f3f commit 46367c1

File tree

15 files changed

+2442
-2552
lines changed

15 files changed

+2442
-2552
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 1.6.0
4+
5+
- **[Breaking]** Upgraded TypeScript to 4.7
6+
- Fixed a bug where EmitOptions plugins were ignored
7+
- Fixed a bug where sometimes function calls (like those to a custom jsx factory) would have a context argument even though `noImplicitSelf` was specified.
8+
- Fixed a bug where sometimes `noImplicitSelf` was ignored because of incorrect file path separators.
9+
- Fixed lualib_bundle files not correctly being included from node_module packages.
10+
- Fixed compound assignment operators (e.g. ??= or ||=) not correctly updating the lhs if used as expression instead of statement.
11+
312
## 1.5.0
413

514
- Added support for `Array.from` and `Array.of`

package-lock.json

Lines changed: 2329 additions & 2498 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jackmacwindows/typescript-to-lua",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua! (With ComputerCraft support)",
55
"repository": "https://github.com/MCJack123/TypeScriptToLua",
66
"homepage": "https://typescripttolua.github.io/",
@@ -65,7 +65,7 @@
6565
"javascript-stringify": "^2.0.1",
6666
"jest": "^27.3.0",
6767
"jest-circus": "^27.3.0",
68-
"@jackmacwindows/lua-types": "2.10.1",
68+
"@jackmacwindows/lua-types": "^2.11.0",
6969
"lua-wasm-bindings": "^0.2.2",
7070
"prettier": "^2.3.2",
7171
"ts-jest": "^27.1.3",

src/lualib/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"outDir": "../../dist/lualib",
44
"target": "esnext",
55
"lib": ["esnext"],
6-
"types": ["lua-types/5.4"],
6+
"types": ["@jackmacwindows/lua-types/cc"],
77
"skipLibCheck": true,
88

99
"noUnusedLocals": true,

src/transformation/utils/function-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function getDeclarationContextType(
7979

8080
// When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule.
8181
const options = program.getCompilerOptions() as CompilerOptions;
82-
if (options.noImplicitSelf && program.getRootFileNames().includes(signatureDeclaration.getSourceFile().fileName)) {
82+
if (options.noImplicitSelf && program.getSourceFile(signatureDeclaration.getSourceFile().fileName) !== undefined) {
8383
return ContextType.Void;
8484
}
8585

src/transformation/visitors/binary-expression/compound.ts

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function transformCompoundAssignment(
7676
context.transformExpression(rhs)
7777
);
7878

79-
if (lua.isTableIndexExpression(left) && shouldCacheTableIndexExpressions(left, rightPrecedingStatements)) {
79+
if (lua.isTableIndexExpression(left)) {
8080
// Complex property/element accesses need to cache object/index expressions to avoid repeating side-effects
8181
// local __obj, __index = ${objExpression}, ${indexExpression};
8282
const obj = context.createTempNameForLuaExpression(left.table);
@@ -105,6 +105,20 @@ export function transformCompoundAssignment(
105105
result: tmp,
106106
};
107107
} else {
108+
if (isSetterSkippingCompoundAssignmentOperator(operator)) {
109+
return {
110+
statements: [
111+
objAndIndexDeclaration,
112+
...transformSetterSkippingCompoundAssignment(
113+
accessExpression,
114+
operator,
115+
right,
116+
rightPrecedingStatements
117+
),
118+
],
119+
result: left,
120+
};
121+
}
108122
// local ____tmp = ____obj[____index] ${replacementOperator} ${right};
109123
// ____obj[____index] = ____tmp;
110124
// return ____tmp
@@ -145,37 +159,6 @@ export function transformCompoundAssignment(
145159
rightPrecedingStatements
146160
);
147161
return { statements: [tmpDeclaration, ...precedingStatements, ...assignStatements], result: tmpIdentifier };
148-
} else if (ts.isPropertyAccessExpression(lhs) || ts.isElementAccessExpression(lhs)) {
149-
// Simple property/element access expressions need to cache in temp to avoid double-evaluation
150-
// local ____tmp = ${left} ${replacementOperator} ${right};
151-
// ${left} = ____tmp;
152-
// return ____tmp
153-
const tmpIdentifier = context.createTempNameForLuaExpression(left);
154-
const [precedingStatements, operatorExpression] = transformBinaryOperation(
155-
context,
156-
left,
157-
right,
158-
rightPrecedingStatements,
159-
operator,
160-
expression
161-
);
162-
const tmpDeclaration = lua.createVariableDeclarationStatement(tmpIdentifier, operatorExpression);
163-
164-
if (isSetterSkippingCompoundAssignmentOperator(operator)) {
165-
const statements = [
166-
tmpDeclaration,
167-
...transformSetterSkippingCompoundAssignment(tmpIdentifier, operator, right, precedingStatements),
168-
];
169-
return { statements, result: tmpIdentifier };
170-
}
171-
172-
const assignStatements = transformAssignmentWithRightPrecedingStatements(
173-
context,
174-
lhs,
175-
tmpIdentifier,
176-
precedingStatements
177-
);
178-
return { statements: [tmpDeclaration, ...assignStatements], result: tmpIdentifier };
179162
} else {
180163
if (rightPrecedingStatements.length > 0 && isSetterSkippingCompoundAssignmentOperator(operator)) {
181164
return {

src/transformation/visitors/call.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ export const transformCallExpression: FunctionVisitor<ts.CallExpression> = (node
288288
calledExpression,
289289
node.arguments,
290290
signature,
291-
callContext
291+
// Only pass context if noImplicitSelf is not configured
292+
context.options.noImplicitSelf ? undefined : callContext
292293
);
293294
}
294295

src/transpilation/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "path";
33
import * as ts from "typescript";
44
import { parseConfigFileWithSystem } from "../cli/tsconfig";
55
import { CompilerOptions } from "../CompilerOptions";
6+
import { normalizeSlashes } from "../utils";
67
import { createEmitOutputCollector, TranspiledFile } from "./output-collector";
78
import { EmitResult, Transpiler } from "./transpiler";
89

@@ -44,8 +45,12 @@ const libCache: { [key: string]: ts.SourceFile } = {};
4445

4546
/** @internal */
4647
export function createVirtualProgram(input: Record<string, string>, options: CompilerOptions = {}): ts.Program {
48+
const normalizedFiles: Record<string, string> = {};
49+
for (const [path, file] of Object.entries(input)) {
50+
normalizedFiles[normalizeSlashes(path)] = file;
51+
}
4752
const compilerHost: ts.CompilerHost = {
48-
fileExists: fileName => fileName in input || ts.sys.fileExists(fileName),
53+
fileExists: fileName => fileName in normalizedFiles || ts.sys.fileExists(fileName),
4954
getCanonicalFileName: fileName => fileName,
5055
getCurrentDirectory: () => "",
5156
getDefaultLibFileName: ts.getDefaultLibFileName,
@@ -55,8 +60,8 @@ export function createVirtualProgram(input: Record<string, string>, options: Com
5560
writeFile() {},
5661

5762
getSourceFile(fileName) {
58-
if (fileName in input) {
59-
return ts.createSourceFile(fileName, input[fileName], ts.ScriptTarget.Latest, false);
63+
if (fileName in normalizedFiles) {
64+
return ts.createSourceFile(fileName, normalizedFiles[fileName], ts.ScriptTarget.Latest, false);
6065
}
6166

6267
let filePath: string | undefined;
@@ -80,7 +85,7 @@ export function createVirtualProgram(input: Record<string, string>, options: Com
8085
},
8186
};
8287

83-
return ts.createProgram(Object.keys(input), options, compilerHost);
88+
return ts.createProgram(Object.keys(normalizedFiles), options, compilerHost);
8489
}
8590

8691
export interface TranspileVirtualProjectResult {

src/transpilation/resolve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ function resolveFileDependencies(file: ProcessedFile, context: ResolutionContext
138138
const diagnostics: ts.Diagnostic[] = [];
139139

140140
for (const required of findRequiredPaths(file.code)) {
141-
// Do no resolve lualib
142-
if (required === "lualib_bundle") {
141+
// Do no resolve lualib, unless it is included from node_modules
142+
if (required === "lualib_bundle" && !isNodeModulesFile(file.fileName)) {
143143
dependencies.push({ fileName: "lualib_bundle", code: "" });
144144
continue;
145145
}

test/transpile/module-resolution.spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,21 @@ describe("module resolution project with dependencies built by tstl library mode
247247
};
248248

249249
test("can resolve lua dependencies", () => {
250-
util.testProject(path.join(projectPath, "tsconfig.json"))
250+
const transpileResult = util
251+
.testProject(path.join(projectPath, "tsconfig.json"))
251252
.setMainFileName(path.join(projectPath, "main.ts"))
252253
.setOptions({ outDir: "tstl-out" })
253-
.expectToEqual(expectedResult);
254+
.expectToEqual(expectedResult)
255+
.getLuaResult();
256+
257+
// Assert lualib_bundle from node_module is include
258+
const expectedLualibBundle = path.join("lua_modules", "dependency1", "lualib_bundle.lua");
259+
expect(transpileResult.transpiledFiles.some(f => f.outPath.endsWith(expectedLualibBundle))).toBe(true);
260+
// Assert node_modules file requires the correct lualib_bundle
261+
const requiringLuaFile = path.join("lua_modules", "dependency1", "index.lua");
262+
const lualibRequiringFile = transpileResult.transpiledFiles.find(f => f.outPath.endsWith(requiringLuaFile));
263+
expect(lualibRequiringFile).toBeDefined();
264+
expect(lualibRequiringFile?.lua).toContain('require("lua_modules.dependency1.lualib_bundle")');
254265
});
255266

256267
test("can resolve dependencies and bundle", () => {

0 commit comments

Comments
 (0)