introduce templates (#4603)

This commit is contained in:
Alex Lam S.L
2021-02-01 09:20:13 +00:00
committed by GitHub
parent d4685640a0
commit ba6e29d6fd
9 changed files with 281 additions and 74 deletions

View File

@@ -53,6 +53,26 @@ tagged_chain: {
node_version: ">=4"
}
tag_parenthesis_arrow: {
input: {
console.log((s => s.raw[0])`\tPASS`.slice(2));
}
expect_exact: "console.log((s=>s.raw[0])`\\tPASS`.slice(2));"
expect_stdout: "PASS"
node_version: ">=4"
}
tag_parenthesis_new: {
input: {
(new function() {
return console.log;
})`foo`;
}
expect_exact: "(new function(){return console.log})`foo`;"
expect_stdout: true
node_version: ">=4"
}
malformed_escape: {
input: {
(function(s) {
@@ -68,6 +88,7 @@ malformed_escape: {
evaluate: {
options = {
evaluate: true,
templates: false,
}
input: {
console.log(`foo ${ function(a, b) {
@@ -80,3 +101,100 @@ evaluate: {
expect_stdout: "foo 42"
node_version: ">=4"
}
evaluate_templates: {
options = {
evaluate: true,
templates: true,
}
input: {
console.log(`foo ${ function(a, b) {
return a * b;
}(6, 7) }`);
}
expect: {
console.log("foo 42");
}
expect_stdout: "foo 42"
node_version: ">=4"
}
partial_evaluate: {
options = {
evaluate: true,
templates: true,
}
input: {
console.log(`${6 * 7} foo ${console ? `PA` + "SS" : `FA` + `IL`}`);
}
expect: {
console.log(`42 foo ${console ? "PASS" : "FAIL"}`);
}
expect_stdout: "42 foo PASS"
node_version: ">=4"
}
malformed_evaluate: {
options = {
evaluate: true,
templates: true,
}
input: {
console.log(`\67 ${6 * 7}`);
}
expect: {
console.log(`\67 42`);
}
expect_stdout: true
node_version: ">=4"
}
unsafe_evaluate: {
options = {
evaluate: true,
templates: true,
unsafe: true,
}
input: {
console.log(String.raw`\uFo`);
}
expect: {
console.log("\\uFo");
}
expect_stdout: "\\uFo"
node_version: ">=8"
}
side_effects: {
options = {
side_effects: true,
}
input: {
`42`;
`${console.log("foo")}`;
console.log`\nbar`;
}
expect: {
console.log("foo");
console.log`\nbar`;
}
expect_stdout: true
node_version: ">=4"
}
unsafe_side_effects: {
options = {
side_effects: true,
unsafe: true,
}
input: {
`42`;
`${console.log("foo")}`;
String.raw`\nbar`;
}
expect: {
console.log("foo");
}
expect_stdout: "foo"
node_version: ">=4"
}

View File

@@ -115,8 +115,8 @@ describe("String literals", function() {
UglifyJS.parse(test);
}, function(e) {
return e instanceof UglifyJS.JS_Parse_Error
&& e.message === "Invalid hex-character pattern in string";
});
&& /^Invalid escape sequence: \\u/.test(e.message);
}, test);
});
});
it("Should reject invalid code points in Unicode escape sequence", function() {
@@ -130,8 +130,8 @@ describe("String literals", function() {
UglifyJS.parse(test);
}, function(e) {
return e instanceof UglifyJS.JS_Parse_Error
&& /^Invalid character code: /.test(e.message);
});
&& /^Invalid escape sequence: \\u{1/.test(e.message);
}, test);
});
});
});

View File

@@ -53,7 +53,10 @@ describe("Template literals", function() {
[ "`foo\\\\r\nbar`", "`foo\\\\r\nbar`" ],
].forEach(function(test) {
var input = "console.log(" + test[0] + ");";
var result = UglifyJS.minify(input);
var result = UglifyJS.minify(input, {
compress: false,
mangle: false,
});
if (result.error) throw result.error;
var expected = "console.log(" + test[1] + ");";
assert.strictEqual(result.code, expected, test[0]);

View File

@@ -410,8 +410,9 @@ function createParams(was_async, noDuplicate) {
return addTrailingComma(params.join(", "));
}
function createArgs(recurmax, stmtDepth, canThrow) {
function createArgs(recurmax, stmtDepth, canThrow, noTemplate) {
recurmax--;
if (SUPPORT.template && !noTemplate && rng(20) == 0) return createTemplateLiteral(recurmax, stmtDepth, canThrow);
var args = [];
for (var n = rng(4); --n >= 0;) switch (SUPPORT.spread ? rng(50) : 3) {
case 0:
@@ -430,7 +431,7 @@ function createArgs(recurmax, stmtDepth, canThrow) {
args.push(rng(2) ? createValue() : createExpression(recurmax, NO_COMMA, stmtDepth, canThrow));
break;
}
return addTrailingComma(args.join(", "));
return "(" + addTrailingComma(args.join(", ")) + ")";
}
function createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, was_async) {
@@ -731,7 +732,7 @@ function createFunction(recurmax, allowDefun, canThrow, stmtDepth) {
var pairs = createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, save_async);
params = pairs.names.join(", ");
if (!pairs.has_rest) params = addTrailingComma(params);
args = addTrailingComma(pairs.values.join(", "));
args = "(" + addTrailingComma(pairs.values.join(", ")) + ")";
} else {
params = createParams(save_async);
}
@@ -753,10 +754,10 @@ function createFunction(recurmax, allowDefun, canThrow, stmtDepth) {
if (!allowDefun) {
// avoid "function statements" (decl inside statements)
s = "var " + createVarName(MANDATORY) + " = " + s;
s += "(" + (args || createArgs(recurmax, stmtDepth, canThrow)) + ")";
s += args || createArgs(recurmax, stmtDepth, canThrow);
} else if (!(name in called) || args || rng(3)) {
s += "var " + createVarName(MANDATORY) + " = " + name;
s += "(" + (args || createArgs(recurmax, stmtDepth, canThrow)) + ")";
s += args || createArgs(recurmax, stmtDepth, canThrow);
}
return s + ";";
@@ -1039,7 +1040,11 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
case p++:
return rng(2) + " === 1 ? a : b";
case p++:
if (SUPPORT.template && rng(20) == 0) return createTemplateLiteral(recurmax, stmtDepth, canThrow);
if (SUPPORT.template && rng(20) == 0) {
var tmpl = createTemplateLiteral(recurmax, stmtDepth, canThrow);
if (rng(10) == 0) tmpl = "String.raw" + tmpl;
return tmpl;
}
case p++:
return createValue();
case p++:
@@ -1093,7 +1098,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
var pairs = createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, save_async);
params = pairs.names.join(", ");
if (!pairs.has_rest) params = addTrailingComma(params);
args = addTrailingComma(pairs.values.join(", "));
args = "(" + addTrailingComma(pairs.values.join(", ")) + ")";
} else {
params = createParams(save_async, NO_DUPLICATE);
}
@@ -1125,7 +1130,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
async = save_async;
VAR_NAMES.length = nameLenBefore;
if (!args && rng(2)) args = createArgs(recurmax, stmtDepth, canThrow);
if (args) suffix += "(" + args + ")";
if (args) suffix += args;
s.push(suffix);
} else {
s.push(
@@ -1162,8 +1167,8 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
break;
default:
async = false;
var instantiate = rng(4) ? "new " : "";
createBlockVariables(recurmax, stmtDepth, canThrow, function(defns) {
var instantiate = rng(4) ? "new " : "";
s.push(
instantiate + "function " + name + "(" + createParams(save_async) + "){",
strictMode(),
@@ -1177,7 +1182,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
});
async = save_async;
VAR_NAMES.length = nameLenBefore;
s.push(rng(2) ? "}" : "}(" + createArgs(recurmax, stmtDepth, canThrow) + ")");
s.push(rng(2) ? "}" : "}" + createArgs(recurmax, stmtDepth, canThrow, instantiate));
break;
}
async = save_async;
@@ -1255,7 +1260,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
case p++:
var name = getVarName();
var s = name + "." + getDotKey();
s = "typeof " + s + ' == "function" && --_calls_ >= 0 && ' + s + "(" + createArgs(recurmax, stmtDepth, canThrow) + ")";
s = "typeof " + s + ' == "function" && --_calls_ >= 0 && ' + s + createArgs(recurmax, stmtDepth, canThrow);
return canThrow && rng(8) == 0 ? s : name + " && " + s;
case p++:
case p++:
@@ -1266,7 +1271,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
name = rng(3) == 0 ? getVarName() : "f" + rng(funcs + 2);
} while (name in called && !called[name]);
called[name] = true;
return "typeof " + name + ' == "function" && --_calls_ >= 0 && ' + name + "(" + createArgs(recurmax, stmtDepth, canThrow) + ")";
return "typeof " + name + ' == "function" && --_calls_ >= 0 && ' + name + createArgs(recurmax, stmtDepth, canThrow);
}
_createExpression.N = p;
return _createExpression(recurmax, noComma, stmtDepth, canThrow);
@@ -1308,7 +1313,7 @@ function createTemplateLiteral(recurmax, stmtDepth, canThrow) {
s.push("${", createExpression(recurmax, COMMA_OK, stmtDepth, canThrow), "}");
addText();
}
return (rng(10) ? "`" : "String.raw`") + s.join(rng(5) ? "" : "\n") + "`";
return "`" + s.join(rng(5) ? "" : "\n") + "`";
function addText() {
while (rng(5) == 0) s.push([