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; } 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", () => {