Загрузка...

Fixing the Unexpected character '@' Error in ESLint

Encountering an `Unexpected character '-'` error while running ESLint? This guide explains the issue, its cause, and how to resolve it effectively with proper documentation.
---
This video is based on the question https://stackoverflow.com/q/72106375/ asked by the user 'Roger Cooper' ( https://stackoverflow.com/u/11622350/ ) and on the answer https://stackoverflow.com/a/72120621/ provided by the user 'Roger Cooper' ( https://stackoverflow.com/u/11622350/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Why do I get an "Unexpected character '-'" parsing error when I run ESLint?

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Fixing the Unexpected character '-' Error in ESLint: A Simple Guide

If you've ever run ESLint and encountered the error message error Unexpected character '-' ParseError, you might be feeling puzzled and frustrated. This message can appear seemingly out of nowhere, especially when you don't see an - sign in the flagged lines of code. Let's explore this common issue and provide you with a clear solution for resolving it.

Understanding the Problem

In your specific case, you received this parsing error when executing the command:

[[See Video to Reveal this Text or Code Snippet]]

The error was reported at a line of code that looked like this:

[[See Video to Reveal this Text or Code Snippet]]

While it may not seem like it relates to the - character directly, this error can often stem from a lack of sufficient documentation for your functions, especially in JavaScript. Here’s a closer look at the situation.

The Code Snippet

Here's an excerpt of the problematic code you provided:

[[See Video to Reveal this Text or Code Snippet]]

You are using a GraphQL query called GET_CUSTOMER_DATA, which is defined as follows:

[[See Video to Reveal this Text or Code Snippet]]

Diagnosing the Issue

The real reason behind the Unexpected character '-' error lies in how ESLint treats undocumented functions. When your code lacks documentation comments, the ESLint pre-commit hook may try to insert comments on its own. Unfortunately, if it doesn’t place these comments correctly, it can inadvertently create parsing errors.

In Your Case

For example, the linter attempted to add comments regarding the parameters of getCustomerInformation() (i.e., id and role). Since these comments were probably missing, the linter introduced an - somewhere that it shouldn't have, hence the parsing issue.

The Solution: Adding Proper Documentation

To resolve the error, it’s crucial to provide clear documentation for your functions. Adding a JSDoc comment above your getCustomerInformation() function can help avoid such parsing errors.

Example of Proper Documentation

Here's how you can add the necessary JSDoc comments:

[[See Video to Reveal this Text or Code Snippet]]

Key Takeaways

Write Documentation: Always document your functions, especially if they have multiple parameters.

Understand ESLint Configurations: Be aware of how ESLint operates and the implications of not having adequate documentation.

Check Your ESLint Version: Keep your ESLint updated, as newer versions might handle certain errors differently.

By following these steps, you should be able to fix the Unexpected character '-' error and enhance the clarity and maintainability of your code. Happy coding!

Видео Fixing the Unexpected character '@' Error in ESLint канала vlogize
Страницу в закладки Мои закладки
Все заметки Новая заметка Страницу в заметки