diff --git a/package-lock.json b/package-lock.json index 434f029..be1e6c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "js-yaml": "^4.1.0", "json5": "^2.2.0", "jsonschema": "^1.4.0", - "typescript-to-lua": "^1.0.1", + "typescript-to-lua": "^1.10.1", "unzipper": "^0.10.11", "xml-js": "^1.6.11" }, @@ -29,6 +29,7 @@ "@types/unzipper": "^0.10.4", "@typescript-eslint/eslint-plugin": "^5.8.0", "@typescript-eslint/parser": "^5.8.0", + "@typescript-to-lua/language-extensions": "^1.0.0", "concat-stream": "^2.0.0", "eslint": "^8.5.0", "jest": "^27.4.5", @@ -37,7 +38,7 @@ "prettier": "^2.5.1", "ts-jest": "^27.1.2", "ts-node": "^10.4.0", - "typescript": "~4.5.2" + "typescript": "^4.5.2" } }, "node_modules/@apidevtools/json-schema-ref-parser": { @@ -1577,6 +1578,11 @@ "url": "https://opencollective.com/typescript-eslint" } }, + "node_modules/@typescript-to-lua/language-extensions": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@typescript-to-lua/language-extensions/-/language-extensions-1.0.0.tgz", + "integrity": "sha512-GtmhFqyg+txpGgGLM3mlS3R6AEG9MQhKALxxcbr6SBzg9u7YAXcPKqUBaBYd6nH+Pi/eQLcWz4BNOLhz8v4TjQ==" + }, "node_modules/abab": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.5.tgz", @@ -4519,9 +4525,9 @@ } }, "node_modules/minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", "dependencies": { "brace-expansion": "^1.1.7" }, @@ -4530,9 +4536,12 @@ } }, "node_modules/minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } }, "node_modules/mkdirp": { "version": "1.0.4", @@ -5624,9 +5633,9 @@ } }, "node_modules/typescript": { - "version": "4.5.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.5.tgz", - "integrity": "sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==", + "version": "4.8.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.8.4.tgz", + "integrity": "sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==", "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -5636,10 +5645,11 @@ } }, "node_modules/typescript-to-lua": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/typescript-to-lua/-/typescript-to-lua-1.3.1.tgz", - "integrity": "sha512-kizX/U0f1rXIUgs8MMqRiZkAi1t30+9cm8ZKuK6hZalJgX30IEQk4KPzgr1AQ17Bd+kBTJTw9XKHpVLKVC0Lyw==", + "version": "1.10.1", + "resolved": "https://registry.npmjs.org/typescript-to-lua/-/typescript-to-lua-1.10.1.tgz", + "integrity": "sha512-R6KQc1YzFcejyaZ9tAvVK0MFTlRS1b10vON5sxjtGVSUWl+W4KRLik0LDCgKfI0wnDQH+LNVnKBNUsBfTtEoQQ==", "dependencies": { + "@typescript-to-lua/language-extensions": "1.0.0", "enhanced-resolve": "^5.8.2", "resolve": "^1.15.1", "source-map": "^0.7.3" @@ -5648,10 +5658,10 @@ "tstl": "dist/tstl.js" }, "engines": { - "node": ">=12.13.0" + "node": ">=16.10.0" }, "peerDependencies": { - "typescript": "~4.5.2" + "typescript": "~4.8.2" } }, "node_modules/typescript-to-lua/node_modules/source-map": { @@ -7149,6 +7159,11 @@ "eslint-visitor-keys": "^3.0.0" } }, + "@typescript-to-lua/language-extensions": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@typescript-to-lua/language-extensions/-/language-extensions-1.0.0.tgz", + "integrity": "sha512-GtmhFqyg+txpGgGLM3mlS3R6AEG9MQhKALxxcbr6SBzg9u7YAXcPKqUBaBYd6nH+Pi/eQLcWz4BNOLhz8v4TjQ==" + }, "abab": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.5.tgz", @@ -9438,17 +9453,17 @@ "dev": true }, "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==" }, "mkdirp": { "version": "1.0.4", @@ -10230,15 +10245,16 @@ } }, "typescript": { - "version": "4.5.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.5.tgz", - "integrity": "sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==" + "version": "4.8.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.8.4.tgz", + "integrity": "sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==" }, "typescript-to-lua": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/typescript-to-lua/-/typescript-to-lua-1.3.1.tgz", - "integrity": "sha512-kizX/U0f1rXIUgs8MMqRiZkAi1t30+9cm8ZKuK6hZalJgX30IEQk4KPzgr1AQ17Bd+kBTJTw9XKHpVLKVC0Lyw==", + "version": "1.10.1", + "resolved": "https://registry.npmjs.org/typescript-to-lua/-/typescript-to-lua-1.10.1.tgz", + "integrity": "sha512-R6KQc1YzFcejyaZ9tAvVK0MFTlRS1b10vON5sxjtGVSUWl+W4KRLik0LDCgKfI0wnDQH+LNVnKBNUsBfTtEoQQ==", "requires": { + "@typescript-to-lua/language-extensions": "1.0.0", "enhanced-resolve": "^5.8.2", "resolve": "^1.15.1", "source-map": "^0.7.3" diff --git a/package.json b/package.json index b74ea26..47a41a6 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "@types/unzipper": "^0.10.4", "@typescript-eslint/eslint-plugin": "^5.8.0", "@typescript-eslint/parser": "^5.8.0", + "@typescript-to-lua/language-extensions": "^1.0.0", "concat-stream": "^2.0.0", "eslint": "^8.5.0", "jest": "^27.4.5", @@ -55,14 +56,14 @@ "prettier": "^2.5.1", "ts-jest": "^27.1.2", "ts-node": "^10.4.0", - "typescript": "~4.5.2" + "typescript": "^4.5.2" }, "dependencies": { "enquirer": "^2.3.6", "js-yaml": "^4.1.0", "json5": "^2.2.0", "jsonschema": "^1.4.0", - "typescript-to-lua": "^1.0.1", + "typescript-to-lua": "^1.10.1", "unzipper": "^0.10.11", "xml-js": "^1.6.11" } diff --git a/src/ast/prepareImports.ts b/src/ast/prepareImports.ts index 08cd048..834160a 100644 --- a/src/ast/prepareImports.ts +++ b/src/ast/prepareImports.ts @@ -151,7 +151,11 @@ function resolveImportsFromAnotherModules( createIdentifier( declarationName.text, declarationName, - getIdentifierSymbolId(context, declarationName), + getIdentifierSymbolId( + context, + declarationName, + undefined, + ), ), createIdentifier( `function(...) return exports["${resourceName}"]:${declarationName.text}(...) end`, @@ -214,7 +218,11 @@ const prepareGlobalImports: FunctionVisitor = function ( createIdentifier( value.name.text, value.name, - getIdentifierSymbolId(context, value.name), + getIdentifierSymbolId( + context, + value.name, + undefined, + ), ), createIdentifier(value.propertyName?.text ?? 'nil'), value, @@ -235,7 +243,7 @@ const prepareGlobalImports: FunctionVisitor = function ( createIdentifier( value.name.text, value.name, - getIdentifierSymbolId(context, value.name), + getIdentifierSymbolId(context, value.name, undefined), ), createIdentifier(getGlobalsTableName()), value, diff --git a/src/ast/removeExportDeclaration.ts b/src/ast/removeExportDeclaration.ts index 71f5644..bd6e39b 100644 --- a/src/ast/removeExportDeclaration.ts +++ b/src/ast/removeExportDeclaration.ts @@ -29,7 +29,7 @@ const removeExportDeclaration: FunctionVisitor = } const expression = assignment.left[0] as TableIndexExpression; - if (expression.table.kind !== 28) { + if (expression.table.kind !== SyntaxKind.Identifier) { continue; } diff --git a/src/ast/removeImports.ts b/src/ast/removeImports.ts index b5baea9..bab049d 100644 --- a/src/ast/removeImports.ts +++ b/src/ast/removeImports.ts @@ -27,7 +27,11 @@ const removeImportDeclarations: FunctionVisitor = createIdentifier( name, namespaceImport.name, - getIdentifierSymbolId(context, namespaceImport.name), + getIdentifierSymbolId( + context, + namespaceImport.name, + undefined, + ), ), createIdentifier(getGlobalsTableName()), node, diff --git a/src/compiler/compile.ts b/src/compiler/compile.ts index 8aef245..4307913 100644 --- a/src/compiler/compile.ts +++ b/src/compiler/compile.ts @@ -159,7 +159,7 @@ export function compileLuaLib( ...extendOptions(options, meta, data), rootDir: path.dirname(getEmptyTsFilePath()), outDir: data.outDir, - luaLibImport: LuaLibImportKind.Always, + luaLibImport: LuaLibImportKind.Require, }; if (options.tstlVerbose) { @@ -182,6 +182,17 @@ export function compileLuaLib( return; } + const returnRegexp = + /return\s({\n(?:\s+?__TS__\w+(?:\s=|,)){2}[\W\w]+})/gi; + const returnValue = dataWrite.match(returnRegexp)?.[0]; + const returnScript = + 'for key, value in pairs(MTA_EXPORTS) do\n\t_G[key] = value\nend'; + if (!returnValue) throw new Error('cannot replace exports'); + dataWrite = dataWrite.replace( + returnRegexp, + `local MTA_EXPORTS = $1\n\n${returnScript}`, + ); + return writeData( fileName, dataWrite, diff --git a/src/compiler/validate.ts b/src/compiler/validate.ts index c3602ea..f1b5157 100644 --- a/src/compiler/validate.ts +++ b/src/compiler/validate.ts @@ -21,13 +21,13 @@ const validateLuaTarget: ValidatorFunction = function (options) { }; const validateLuaLibImport: ValidatorFunction = function (options) { - if (options.luaLibImport === 'always') { + if (options.luaLibImport === 'require') { return []; } return [ simpleTsDiagnostic( - 'Expected luaLibImport property value: "always". ' + + 'Expected luaLibImport property value: "require". ' + 'Check the tsconfig.json\n' + 'https://github.com/mtasa-typescript/mtasa-lua-utils/blob/master/tsconfig.json', DiagnosticCategory.Warning, diff --git a/src/debug.ts b/src/debug.ts new file mode 100644 index 0000000..dfb8cb0 --- /dev/null +++ b/src/debug.ts @@ -0,0 +1,4 @@ +import { buildProject } from './cli/build'; + +const filepath = 'src/tests/resources.spec/importPlugin/mtasa-meta.yml'; +buildProject(['--meta', filepath, '--project', 'src/tests/tsconfig.json']); diff --git a/src/tests/tsconfig.json b/src/tests/tsconfig.json index e260743..ae92a66 100644 --- a/src/tests/tsconfig.json +++ b/src/tests/tsconfig.json @@ -1,5 +1,5 @@ { - "$schema": "https://raw.githubusercontent.com/TypeScriptToLua/vscode-typescript-to-lua/master/tsconfig-schema.json", + "$schema": "https://raw.githubusercontent.com/TypeScriptToLua/TypeScriptToLua/master/tsconfig-schema.json", "compilerOptions": { "target": "ES6", "lib": ["es6"], @@ -17,7 +17,7 @@ "stripInternal": true, "baseUrl": ".", "types": [ - "typescript-to-lua/language-extensions", + "@typescript-to-lua/language-extensions", "@types/jest", "@types/node" ] @@ -30,7 +30,7 @@ // Provides debug.traceback function "sourceMapTraceback": true, // Lualib should be always included in the resource - "luaLibImport": "always" + "luaLibImport": "require" }, "include": ["src/**/*"], "exclude": ["src/tests/**/*"] diff --git a/tsconfig.json b/tsconfig.json index e92659f..3fdb318 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "$schema": "https://raw.githubusercontent.com/TypeScriptToLua/vscode-typescript-to-lua/master/tsconfig-schema.json", + "$schema": "https://raw.githubusercontent.com/TypeScriptToLua/TypeScriptToLua/master/tsconfig-schema.json", "compilerOptions": { "target": "ES6", "lib": ["es6"], @@ -25,8 +25,7 @@ "noImplicitSelf": true, // Provides debug.traceback function "sourceMapTraceback": true, - // Lualib should be always included in the resource - "luaLibImport": "always", + "luaLibImport": "require", // Verbose output "tstlVerbose": true },