diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-02-28 18:27:41 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-01 02:27:41 +0800 |
commit | 81254f67e473f2a1db8341de26cd923f9c592e17 (patch) | |
tree | 019134f04ffde47d554cf0c1a381806c7e9bb2bc /test/sandbox.js | |
parent | c549ee89b9a0641ea78f57472168963811c7e111 (diff) | |
download | tracifyjs-81254f67e473f2a1db8341de26cd923f9c592e17.tar.gz tracifyjs-81254f67e473f2a1db8341de26cd923f9c592e17.zip |
support limited `ufuzz` testing for `import` (#4707)
Diffstat (limited to 'test/sandbox.js')
-rw-r--r-- | test/sandbox.js | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/test/sandbox.js b/test/sandbox.js index adee26cb..b2b5e00c 100644 --- a/test/sandbox.js +++ b/test/sandbox.js @@ -49,13 +49,27 @@ exports.same_stdout = semver.satisfies(process.version, "0.12") ? function(expec } : function(expected, actual) { return typeof expected == typeof actual && strip_func_ids(expected) == strip_func_ids(actual); }; -exports.strip_exports = function(code) { - var count = 0; - return code.replace(/\bexport(?:\s*\{[^}]*}\s*?(?:$|\n|;)|\s+default\b(?:\s*(\(|\{|class\s*\{|class\s+(?=extends\b)|(?:async\s+)?function\s*(?:\*\s*)?\())?|\b)/g, function(match, header) { +exports.patch_module_statements = function(code) { + var count = 0, imports = []; + code = code.replace(/\bexport(?:\s*\{[^}]*}\s*?(?:$|\n|;)|\s+default\b(?:\s*(\(|\{|class\s*\{|class\s+(?=extends\b)|(?:async\s+)?function\s*(?:\*\s*)?\())?|\b)/g, function(match, header) { if (!header) return ""; if (header.length == 1) return "0, " + header; return header.slice(0, -1) + " _" + ++count + header.slice(-1); + }).replace(/\bimport\b(?:\s*([^('"]+)\bfrom\b)?\s*(['"]).*?\2(?:$|\n|;)/g, function(match, symbols) { + if (symbols) { + if (!/^[{*]/.test(symbols)) symbols = "default:" + symbols; + symbols = symbols.replace(/[{}]/g, "").trim().replace(/\s*,\s*/g, ","); + symbols = symbols.replace(/\*/, '"*"').replace(/\bas\s+(?!$|,|as\s)/g, ":"); + imports.push([ + "var {", + symbols, + "} = new Proxy(Object.create(null), { get(_, value) { return { value }; } });", + ].join("")); + } + return ""; }); + imports.push(""); + return imports.join("\n") + code; }; function is_error(result) { |