Fix function name computation for literal values (#7435)

* Fix name computation for literal values

* Add more computed literal test cases

* Always return a string in getNameFromLiteralId

- Also concatenate quasis id for regex literal

* Add a test clarifying function name for template literals

* Remove useless else ifs
This commit is contained in:
Axel Nyffenegger 2018-04-10 03:00:55 +02:00 committed by Justin Ridgewell
parent 6a8c4ab433
commit bd98041321
4 changed files with 43 additions and 2 deletions

View File

@ -45,6 +45,26 @@ const visitor = {
},
};
function getNameFromLiteralId(id) {
if (t.isNullLiteral(id)) {
return "null";
}
if (t.isRegExpLiteral(id)) {
return `_${id.pattern}_${id.flags}`;
}
if (t.isTemplateLiteral(id)) {
return id.quasis.map(quasi => quasi.value.raw).join("");
}
if (id.value !== undefined) {
return id.value + "";
}
return "";
}
function wrap(state, method, id, scope) {
if (state.selfReference) {
if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) {
@ -168,10 +188,12 @@ export default function({ node, parent, scope, id }, localBinding = false) {
let name;
if (id && t.isLiteral(id)) {
name = id.value;
name = getNameFromLiteralId(id);
} else if (id && t.isIdentifier(id)) {
name = id.name;
} else {
}
if (name === undefined) {
return;
}

View File

@ -0,0 +1,8 @@
const x = {
[null]: function () {},
[/regex/gi]: function () {},
[`y`]: function () {},
[`abc${y}def`]: function () {},
[0]: function () {},
[false]: function () {},
};

View File

@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", "transform-function-name"]
}

View File

@ -0,0 +1,8 @@
const x = {
[null]: function _null() {},
[/regex/gi]: function _regex_gi() {},
[`y`]: function y() {},
[`abc${y}def`]: function abcdef() {},
[0]: function _() {},
[false]: function _false() {}
};