Skip to content

Name remapping support for lambda functions #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

pablomatiasgomez
Copy link
Contributor

I'm trying to find a way to get original function names for lambda functions, which currently looks like it's not supported, or I'm not finding the right way to make it work.
In order to explain this, I created this simple test that is based on this simple source code:

const SOME_CONST = 3;

function outer() {
    const aFunctionAsConst = function() {
        console.log("A function as const");
        aFunctionAsConst();
    };

    const aLambdaAsConst = () => {
        console.log("A lambda as const");
        aLambdaAsConst();
    };

    function aRegularFunction() {
        console.log("A regular function");
        aRegularFunction();
    }

    aFunctionAsConst();
    aLambdaAsConst();
    aRegularFunction();
}

outer();

And used uglifyjs to generate the minified version and sourcemap:
uglifyjs source.js --compress --mangle --output source.min.js --source-map includeSources

The problem is when trying to get the original function name for (0, 106, "n", "aLambdaAsConst"), .

The reason I think why this doesn't work is because here

if let Some(item) = iter.peek();
if item.1 == Some("function");
then {
return token.get_name();
}

we just check for function preceding the token name.. I wonder if const should be included, but I think it's going to cause some false positives.. Any ideas? Am I doing something wrong here?

I found it interesting that I saw some cases in sentry where the actual function name gets correctly translated in these cases, so maybe I'm just missing something here. e.g. here renderPath is picked up from a lambda const:
image

// a stacktrace
let locs = &[
(0, 56, "o", "aFunctionAsConst"),
(0, 106, "n", "aLambdaAsConst"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently failing, but the two other work just fine

@loewenheim
Copy link
Contributor

Hi, thank you for the report. We'll discuss it in the team. In the meantime, do you have a link to the event shown in your screenshot?

@loewenheim
Copy link
Contributor

Actually, the reason this works in Sentry is because we additionally use the https://github.com/getsentry/js-source-scopes crate to resolve scope names. The easiest way to demonstrate this on your example is with the sourcemapcache_debug tool from the symbolic repo:

❯ sourcemapcache_debug -s test.min.js -m test.js.map -l 1 -c 107
Source: source.js
Line: 9
Column: 16
Function: aLambdaAsConst
Context line:         console.log("A lambda as const");
[pre/post context snipped]

As such, it's unlikely we will pull this functionality into this crate. You could have a look at js-source-scopes to see if you can use it to achieve what you want to do.

@pablomatiasgomez
Copy link
Contributor Author

@loewenheim Nice! Thanks for the quick reply! That actually works for me, I didn't see that there was another lib with more stuff, but as far as I can see, that one should have everything I need.

I wonder if maybe this get_original_function_name function should have some context for others to know that it only work for functions and not lambdas? Other than that, I think it's ok to just close this.

@Swatinem
Copy link
Member

Swatinem commented Jun 2, 2025

We were actually thinking about completely removing get_original_function_name and similar functionality from this crate/repo here, as we have implemented that in a completely different way in the other crate.

The idea would be to make the sourcemap crate as low-level as possible, and just expose the internals of sourcemaps. And then to build more functionality around it, as we did.

Alas, removing deprecated code in order to reduce the maintenance workload is still work, and we never got around to do it :-D

@loewenheim
Copy link
Contributor

@pablomatiasgomez Please let us know if we can help you further!

@loewenheim loewenheim closed this Jun 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants