Skip to content
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

Function declarations/expressions with # names not properly handled #10

Closed
Conduitry opened this issue Nov 11, 2019 · 2 comments · Fixed by #12
Closed

Function declarations/expressions with # names not properly handled #10

Conduitry opened this issue Nov 11, 2019 · 2 comments · Fixed by #12

Comments

@Conduitry
Copy link
Collaborator

cr.print(cr.b`function #foo() { }`).code // => 'function #foo() {\n\t\n}'

If this were handled correctly, it would allow a nice solution to sveltejs/svelte#3900

@Conduitry
Copy link
Collaborator Author

Conduitry commented Nov 11, 2019

The only situation in which this really needs deconflicting is when there's code inside the function trying to access a variable foo declared outside the function. I don't know how this library handles this though, so it might just be easier to deconflict function #foo() { } as though it were a regular variable declaration.

@Conduitry
Copy link
Collaborator Author

Actually, no, we can only cut corners when it's a named function expression, not a function declaration. It seems easiest to just always deconflict this.

diff --git a/src/print/handlers.ts b/src/print/handlers.ts
index 240fc8e..fd2240a 100644
--- a/src/print/handlers.ts
+++ b/src/print/handlers.ts
@@ -569,7 +569,7 @@ const handlers: Record<string, Handler> = {
 
 		if (node.async) chunks.push(c('async '));
 		chunks.push(c(node.generator ? 'function* ' : 'function '));
-		if (node.id) chunks.push(c(node.id.name, node.id));
+		if (node.id) chunks.push(...handle(node.id, state));
 		chunks.push(c('('));
 
 		const params = node.params.map(p => handle(p, {

This looks like it works. PR coming soon.

Conduitry added a commit to Conduitry/Rich-Harris_code-red that referenced this issue Nov 12, 2019
Rich-Harris added a commit that referenced this issue Nov 12, 2019
deconflict #-identifiers in function names
tanhauhau pushed a commit to tanhauhau/code-red that referenced this issue Sep 13, 2022
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 a pull request may close this issue.

1 participant