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

error if javascript block has unnamed function #254

Closed
iambumblehead opened this issue Jun 13, 2024 · 2 comments
Closed

error if javascript block has unnamed function #254

iambumblehead opened this issue Jun 13, 2024 · 2 comments
Labels

Comments

@iambumblehead
Copy link

to reproduce the error, apply the below patch and run the tests,

diff --git a/tests/fixtures/long.md b/tests/fixtures/long.md
index 78f2940..73cd48f 100644
--- a/tests/fixtures/long.md
+++ b/tests/fixtures/long.md
@@ -15,6 +15,10 @@ console.log(42);
 function foo() {
     console.log("Hello");
 }
+// Unnamed
+function () {
+    console.log("Hello");
+}

the error looks like this,

error  Parsing error: Unexpected token (

a workaround is to wrap the anonymous function with open and close parens

(function () {
    console.log("Hello");
})

I believe this error might be "recent" it occurred when I updated eslint from 9.0.0 to 9.4.0 and eslint-plugin-markdown from 4.0.1 to 5.0.0 but am not certain about that.

thanks for the helpful plugin :)

@fasttime
Copy link
Member

Hi @iambumblehead! This behavior is intended. Note that function declarations, unlike function expressions, are required to have a name in JavaScript in most cases. So this code contains a syntax error, and it will be rejected by a browser or Node.js if you try to run it without modifications:

function () {
    console.log("Hello");
}

This is even noted it the spec: https://tc39.es/ecma262/#sec-runtime-semantics-instantiateordinaryfunctionobject

An anonymous FunctionDeclaration can only occur as part of an export default declaration, and its function code is therefore always strict mode code.

ESLint v9.0.0 with the default parser reports the same error you are seeing with ESLint v9.4.0. But if you were using a different parser previously, it's entirely possible that the syntax error went unnoticed.

@fasttime fasttime closed this as not planned Won't fix, can't repro, duplicate, stale Jun 14, 2024
@iambumblehead
Copy link
Author

@fasttime thanks for the incredibly informative reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

No branches or pull requests

2 participants