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 | |
parent | c549ee89b9a0641ea78f57472168963811c7e111 (diff) | |
download | tracifyjs-81254f67e473f2a1db8341de26cd923f9c592e17.tar.gz tracifyjs-81254f67e473f2a1db8341de26cd923f9c592e17.zip |
support limited `ufuzz` testing for `import` (#4707)
Diffstat (limited to 'test')
-rw-r--r-- | test/reduce.js | 2 | ||||
-rw-r--r-- | test/sandbox.js | 20 | ||||
-rw-r--r-- | test/ufuzz/index.js | 31 |
3 files changed, 43 insertions, 10 deletions
diff --git a/test/reduce.js b/test/reduce.js index dd0cb6f3..b7f5c030 100644 --- a/test/reduce.js +++ b/test/reduce.js @@ -732,7 +732,7 @@ function run_code(code, toplevel, result_cache, timeout) { if (!value) { var start = Date.now(); result_cache[key] = value = { - result: sandbox.run_code(sandbox.strip_exports(code), toplevel, timeout), + result: sandbox.run_code(sandbox.patch_module_statements(code), toplevel, timeout), elapsed: Date.now() - start, }; } 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) { diff --git a/test/ufuzz/index.js b/test/ufuzz/index.js index e756b747..b066e2fa 100644 --- a/test/ufuzz/index.js +++ b/test/ufuzz/index.js @@ -382,7 +382,7 @@ function strictMode() { } function appendExport(stmtDepth, allowDefault) { - if (stmtDepth == 1 && rng(20) == 0) { + if (SUPPORT.destructuring && stmtDepth == 1 && rng(20) == 0) { if (allowDefault && !export_default && rng(5) == 0) { export_default = true; return "export default "; @@ -1009,7 +1009,7 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn case STMT_SEMI: return use_strict && rng(20) === 0 ? '"use strict";' : ";"; case STMT_EXPR: - if (stmtDepth == 1 && !export_default && rng(20) == 0) { + if (SUPPORT.destructuring && stmtDepth == 1 && !export_default && rng(20) == 0) { export_default = true; return "export default " + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ";"; } @@ -1019,7 +1019,26 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn // note: default does not _need_ to be last return "switch (" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") { " + createSwitchParts(recurmax, 4, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + "}"; case STMT_VAR: - if (SUPPORT.destructuring && rng(20) == 0) { + if (SUPPORT.destructuring && stmtDepth == 1 && rng(5) == 0) { + unique_vars.push("c"); + var s = rng(2) ? " " + createVarName(MANDATORY) : ""; + if (rng(10)) { + if (s) s += ","; + if (rng(2)) { + s += " * as " + createVarName(MANDATORY); + } else { + var names = []; + for (var i = rng(4); --i >= 0;) { + var name = createVarName(MANDATORY); + names.push(rng(2) ? getDotKey() + " as " + name : name); + } + s += " { " + names.join(", ") + " }"; + } + } + unique_vars.pop(); + if (s) s += " from"; + return "import" + s + ' "path/to/module.js";'; + } else if (SUPPORT.destructuring && rng(20) == 0) { var pairs = createAssignmentPairs(recurmax, stmtDepth, canThrow); return appendExport(stmtDepth) + "var " + pairs.names.map(function(name, index) { return index in pairs.values ? name + " = " + pairs.values[index] : name; @@ -1966,7 +1985,7 @@ if (require.main !== module) { } function run_code(code, toplevel) { - return sandbox.run_code(sandbox.strip_exports(code), toplevel); + return sandbox.run_code(sandbox.patch_module_statements(code), toplevel); } function writeln(stream, msg) { @@ -2384,8 +2403,8 @@ for (var round = 1; round <= num_iterations; round++) { if (!ok && /\bclass\b/.test(original_code)) { var original_strict = run_code('"use strict";' + original_code, toplevel); var uglify_strict = run_code('"use strict";' + uglify_code, toplevel); - if (typeof original_strict != "string" && /strict/.test(original_strict.message)) { - ok = typeof uglify_strict != "string" && /strict/.test(uglify_strict.message); + if (typeof original_strict != "string") { + ok = typeof uglify_strict != "string"; } else { ok = sandbox.same_stdout(original_strict, uglify_strict); } |