Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit 4f98ccd

Browse files
committed
Create a cleanFunctionName function extracted from getFunctionName.
This way we can expose it for other part of the code to use it.
1 parent 475a925 commit 4f98ccd

1 file changed

Lines changed: 43 additions & 30 deletions

File tree

packages/devtools-reps/src/reps/function.js

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,12 @@ function getTitle(grip, props) {
9999
}, title);
100100
}
101101

102-
// Decodes an anonymous naming scheme that
103-
// spider monkey implements based on "Naming Anonymous JavaScript Functions"
104-
// http://johnjbarton.github.io/nonymous/index.html
105-
const objectProperty = /([\w\d]+)$/;
106-
const arrayProperty = /\[(.*?)\]$/;
107-
const functionProperty = /([\w\d]+)[\/\.<]*?$/;
108-
const annonymousProperty = /([\w\d]+)\(\^\)$/;
109-
102+
/**
103+
* Returns a ReactElement representing the function name.
104+
*
105+
* @param {Object} grip : Function grip
106+
* @param {Object} props: Function rep props
107+
*/
110108
function getFunctionName(grip, props = {}) {
111109
let { functionName } = props;
112110
let name;
@@ -126,33 +124,47 @@ function getFunctionName(grip, props = {}) {
126124
) {
127125
name = functionName + ":" + grip.displayName;
128126
} else {
129-
name =
130-
grip.userDisplayName ||
131-
grip.displayName ||
132-
grip.name ||
133-
props.functionName ||
134-
"";
135-
136-
const scenarios = [
137-
objectProperty,
138-
arrayProperty,
139-
functionProperty,
140-
annonymousProperty
141-
];
142-
143-
scenarios.some(reg => {
144-
const match = reg.exec(name);
145-
if (match) {
146-
name = match[1];
147-
return true;
148-
}
149-
return false;
150-
});
127+
name = cleanFunctionName(
128+
grip.userDisplayName ||
129+
grip.displayName ||
130+
grip.name ||
131+
props.functionName ||
132+
""
133+
);
151134
}
152135

153136
return cropString(name, 100);
154137
}
155138

139+
const objectProperty = /([\w\d]+)$/;
140+
const arrayProperty = /\[(.*?)\]$/;
141+
const functionProperty = /([\w\d]+)[\/\.<]*?$/;
142+
const annonymousProperty = /([\w\d]+)\(\^\)$/;
143+
144+
/**
145+
* Decodes an anonymous naming scheme that
146+
* spider monkey implements based on "Naming Anonymous JavaScript Functions"
147+
* http://johnjbarton.github.io/nonymous/index.html
148+
*
149+
* @param {String} name : Function name to clean up
150+
* @returns String
151+
*/
152+
function cleanFunctionName(name) {
153+
for (const reg of [
154+
objectProperty,
155+
arrayProperty,
156+
functionProperty,
157+
annonymousProperty
158+
]) {
159+
const match = reg.exec(name);
160+
if (match) {
161+
return match[1];
162+
}
163+
}
164+
165+
return name;
166+
}
167+
156168
function renderParams(props) {
157169
const {
158170
parameterNames = []
@@ -184,6 +196,7 @@ function supportsObject(grip, noGrip = false) {
184196
module.exports = {
185197
rep: wrapRender(FunctionRep),
186198
supportsObject,
199+
cleanFunctionName,
187200
// exported for testing purpose.
188201
getFunctionName,
189202
};

0 commit comments

Comments
 (0)