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:
parent
6a8c4ab433
commit
bd98041321
@ -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) {
|
function wrap(state, method, id, scope) {
|
||||||
if (state.selfReference) {
|
if (state.selfReference) {
|
||||||
if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) {
|
if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) {
|
||||||
@ -168,10 +188,12 @@ export default function({ node, parent, scope, id }, localBinding = false) {
|
|||||||
|
|
||||||
let name;
|
let name;
|
||||||
if (id && t.isLiteral(id)) {
|
if (id && t.isLiteral(id)) {
|
||||||
name = id.value;
|
name = getNameFromLiteralId(id);
|
||||||
} else if (id && t.isIdentifier(id)) {
|
} else if (id && t.isIdentifier(id)) {
|
||||||
name = id.name;
|
name = id.name;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
if (name === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
8
packages/babel-plugin-transform-function-name/test/fixtures/issues/7199/input.js
vendored
Normal file
8
packages/babel-plugin-transform-function-name/test/fixtures/issues/7199/input.js
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
const x = {
|
||||||
|
[null]: function () {},
|
||||||
|
[/regex/gi]: function () {},
|
||||||
|
[`y`]: function () {},
|
||||||
|
[`abc${y}def`]: function () {},
|
||||||
|
[0]: function () {},
|
||||||
|
[false]: function () {},
|
||||||
|
};
|
||||||
3
packages/babel-plugin-transform-function-name/test/fixtures/issues/7199/options.json
vendored
Normal file
3
packages/babel-plugin-transform-function-name/test/fixtures/issues/7199/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"plugins": ["external-helpers", "transform-function-name"]
|
||||||
|
}
|
||||||
8
packages/babel-plugin-transform-function-name/test/fixtures/issues/7199/output.js
vendored
Normal file
8
packages/babel-plugin-transform-function-name/test/fixtures/issues/7199/output.js
vendored
Normal 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() {}
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user