Fixed keywords and end blocks
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { LuaFile, BlockType, CodeBlock, RequireBlock } from './CodeBlocks';
|
||||
import { LuaFile, BlockType, CodeBlock, RequireBlock, ReturnBlock, FunctionBlock, TableBlock } from './CodeBlocks';
|
||||
import { CompilationError } from './CompilationError';
|
||||
|
||||
export interface ScriptCompilerOptions {
|
||||
@@ -130,7 +130,7 @@ function fileReferenceToLuaVariable(fileReference: string): string {
|
||||
|
||||
// Split by / to get path parts
|
||||
const parts = fileReference.split('/');
|
||||
|
||||
|
||||
// Convert to ScriptGlobals.folder.FileName format
|
||||
let result = LUA_SCRIPT_GLOBAL_KEYWORD;
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
@@ -174,6 +174,64 @@ class ParsedFile {
|
||||
searchBlock(luaFile);
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
public replaceRequireWithGlobal(): void {
|
||||
|
||||
function replaceRequireInBlock(block: CodeBlock) {
|
||||
|
||||
if (block instanceof RequireBlock) {
|
||||
const requiredString = block.getRequiredString();
|
||||
if (requiredString) {
|
||||
const luaReference = fileReferenceToLuaVariable(requiredString);
|
||||
block.toLines = () => [`${luaReference}\n`];
|
||||
}
|
||||
}
|
||||
|
||||
for (const child of block.getChildren()) {
|
||||
replaceRequireInBlock(child);
|
||||
}
|
||||
}
|
||||
|
||||
for (const child of this.luaFile.getChildren()) {
|
||||
replaceRequireInBlock(child);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public replaceModuleReturnWithGlobalAssignement(): void {
|
||||
|
||||
const replaceReturnInBlock = (block: CodeBlock) => {
|
||||
if (block instanceof ReturnBlock) {
|
||||
const parent = block.getParentBlock();
|
||||
if (parent) {
|
||||
const luaReference = fileReferenceToLuaVariable(this.fileKey);
|
||||
const resultLines : string[] = [];
|
||||
|
||||
const splitCount = luaReference.split('.').length;
|
||||
for(let i = 2; i <= splitCount -1 ; i++){
|
||||
const partialReference = luaReference.split('.').slice(0, i).join('.');
|
||||
resultLines.push(`if not ${partialReference} then ${partialReference} = {} end`);
|
||||
}
|
||||
|
||||
resultLines.push(`${luaReference} = ${block.returnParams.join(', ')}`);
|
||||
block.toLines = () => resultLines.map(line => line + '\n');
|
||||
}
|
||||
}
|
||||
|
||||
if(block instanceof FunctionBlock || block instanceof TableBlock){
|
||||
return; // Do not traverse into FunctionBlock or TableBlock
|
||||
}
|
||||
|
||||
for (const child of block.getChildren()) {
|
||||
replaceReturnInBlock(child);
|
||||
}
|
||||
}
|
||||
|
||||
for (const child of this.luaFile.getChildren()) {
|
||||
replaceReturnInBlock(child);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class Writer {
|
||||
@@ -291,11 +349,41 @@ class Writer {
|
||||
}
|
||||
}
|
||||
|
||||
outputLines.push("do -- " + parsedFile.fileKey + "\n");
|
||||
parsedFile.replaceRequireWithGlobal();
|
||||
parsedFile.replaceModuleReturnWithGlobalAssignement();
|
||||
|
||||
const lines = parsedFile.luaFile.toLines();
|
||||
metrics.totalLinesWritten += lines.length;
|
||||
outputLines.push(...lines.filter(line => line.trim() !== '')); // Filter out empty lines
|
||||
outputLines.push("end -- " + parsedFile.fileKey + "\n");
|
||||
const newLineFilteredLines : string[] = [];
|
||||
|
||||
function trimEndPreserveNewlines(str: string): string {
|
||||
return str.replace(/[ \t]+$/gm, '');
|
||||
}
|
||||
|
||||
|
||||
let wasLastEmpty = false;
|
||||
let lastEndedWithNewline = false;
|
||||
for(const line of lines){
|
||||
if(trimEndPreserveNewlines(line) !== ''){
|
||||
if(line.trim() === ''){
|
||||
if(!wasLastEmpty && !lastEndedWithNewline){
|
||||
newLineFilteredLines.push(line);
|
||||
wasLastEmpty = true;
|
||||
}
|
||||
} else {
|
||||
newLineFilteredLines.push(line);
|
||||
wasLastEmpty = false;
|
||||
|
||||
lastEndedWithNewline = line.endsWith('\n');
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
outputLines.push("do -- " + parsedFile.fileKey + "\n");
|
||||
|
||||
metrics.totalLinesWritten += newLineFilteredLines.length;
|
||||
outputLines.push(...newLineFilteredLines);
|
||||
outputLines.push("\nend -- " + parsedFile.fileKey + "\n");
|
||||
writtenFiles.add(parsedFile.fileKey);
|
||||
metrics.filesWritten++;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user