From 7e460ba4bb816883fb27f3585848a256cabbbf62 Mon Sep 17 00:00:00 2001 From: "Xavier(Home)" Date: Fri, 20 Oct 2023 21:01:08 +0800 Subject: [PATCH 1/2] Literal string surrounded with [[ & ]] includes require('path') cause could not resolve lua source file error --- src/transpilation/find-lua-requires.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/transpilation/find-lua-requires.ts b/src/transpilation/find-lua-requires.ts index 90be465e6..da4543649 100644 --- a/src/transpilation/find-lua-requires.ts +++ b/src/transpilation/find-lua-requires.ts @@ -28,7 +28,11 @@ function findRequire(lua: string, offset: number): LuaRequire[] { } else { offset = m.end; } - } else if (c === '"' || c === "'") { + } else if ( + c === '"' || c === "'" + // string literal surrounded by [[ & ]] + || (c === "[" && offset + 1 < lua.length && lua[offset + 1] === "[") + ) { offset = readString(lua, offset, c).offset; // Skip string and surrounding quotes } else if (c === "-" && offset + 1 < lua.length && lua[offset + 1] === "-") { offset = skipComment(lua, offset); @@ -106,7 +110,11 @@ function readString(lua: string, offset: number, delimiter: string): { value: st if (lua[offset] === "\\" && !escaped) { escaped = true; } else { - if (lua[offset] === delimiter) { + if ( + (delimiter !== "[" && lua[offset] === delimiter) + // string literal surrounded by [[ & ]] + || (delimiter === "[" && offset + 1 < lua.length && lua[offset] === "]" && lua[offset + 1] === "]") + ) { result += lua.slice(start, offset - 1); start = offset; } From 52f102f953621a9fc0107ad0eacd1053e7de8b73 Mon Sep 17 00:00:00 2001 From: "Xavier(Home)" Date: Fri, 20 Oct 2023 21:06:49 +0800 Subject: [PATCH 2/2] add missint unit tests --- test/unit/find-lua-requires.spec.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/unit/find-lua-requires.spec.ts b/test/unit/find-lua-requires.spec.ts index bb879d642..2068a5921 100644 --- a/test/unit/find-lua-requires.spec.ts +++ b/test/unit/find-lua-requires.spec.ts @@ -56,12 +56,15 @@ test("ignores requires that should not be included", () => { require('req2') local b = 'require("This should not be included")' require("req3") - -- require("this should not be included") + local c = [[require("This should not be included")]] require("req4") - --[[ require("this should not be included") ]] + -- require("this should not be included") require("req5") + --[[ require("this should not be included") ]] + require("req6") + `; - expect(requirePaths(findLuaRequires(lua))).toEqual(["req1", "req2", "req3", "req4", "req5"]); + expect(requirePaths(findLuaRequires(lua))).toEqual(["req1", "req2", "req3", "req4", "req5", "req6"]); }); test("non-terminated require", () => {