TIP Strongly typed names
basarat opened this issue · 5 comments
getName : https://github.com/TypeStrong/atom-typescript/blob/master/lib/main/lang/utils.ts#L300-L308
var nameExtractorRegex = /return (.*);/;
/** Get the name using a lambda so that you don't have magic strings */
export function getName(nameLambda: () => any) {
var m = nameExtractorRegex.exec(nameLambda + "");
if (m == null)
throw new Error("The function does not contain a statement matching 'return variableName;'");
var access = m[1].split('.');
return access[access.length - 1];
}
David's function : http://stackoverflow.com/a/32542368/390330
function getPropertyName(propertyFunction) {
return /\.([^\.;]+);?\s*\}$/.exec(propertyFunction.toString())[1];
}
Handy! Thanks :)
The one from the TS compiler doesn't always work after running through the fairly simple ASP.Net bundling & minification as it can strip the ';' off the end of the generated function's return statement. By that I mean you get
function(){return x.something}
rather than
function(){ return x.something; }
and the ';' makes all the difference
I've slightly tweaked it for my use now, with some brief smoke tests
var nameExtractorRegex = /return (.*);/;
var nameExtractorRegexNoTrailingSemi = /return (.*)/;
/** Get the name using a lambda so that you don't have magic strings */
export function getName(nameLambda: (o?:any) => any) {
var m = nameExtractorRegex.exec(nameLambda + "");
if (m == null) {
m = nameExtractorRegexNoTrailingSemi.exec(nameLambda + "");
if (m == null) {
throw new Error("The function does not contain a statement matching 'return variableName'");
}
}
var access = m[1].split('.');
var name = access[access.length - 1];
//prevent trailing whitespace, '}'
while (name[name.length - 1] === '}' || name[name.length - 1] === ' ') {
name = name.substr(0, name.length - 1);
}
return name;
}You can do const nameExtractorRegex = /return (.*);?/; to say that ; is optional but still not a part of the capture group 🌹
Yes that's a better approach 👍
I'll update my code later and report back if I have issues.
My RegEx foo leaves a lot to be desired