2

I can post my whole config and JavaScript file if needed, but I am trying to run ESLint on some JavaScript I'm writing. My 'eslintrc.json' file has this in the config (with some other rules):

"rules": 
{
  // Thought this was my issue and hoped it would solve it.
  "env": 
  { 
    "es6": true 
  },

  "prefer-template": "error", //template literals

  "quotes": 
  [
    "error", 
    "double", 
    { "avoidEscape": true, "allowTemplateLiterals": true }
  ]
}

Here is the error code that spits out in the .log file for ESLint, and the code it is failing at.

Parsing error: Unexpected character '`' FolderName\FileName.js:31:17
function Something()
{
  // Seperated to try and debug the issue.
  var message = `Starting Something: ${ arguments.callee.name}`;
  //            ^
  Log.Message(message);

  SomeOtherFile.UpdateEnvironmentVariables();
 }

I know by default ESLint uses ECMAScript 5 (Specifying Parser Options) so I tried setting it to ECMA 6 (that has template strings - See above config file), but that didn't seem to help.

What's weird is that the ESLint documentation (rule : quotes) explains backticks and mentions that it is only in ECMAScript 6, etc.. But it seems like the parser that ESLint uses (Espree - On ESLint) is having an issue or something.

I really don't want to go through and replace all of these with some string concatenations, any suggestions?

ruffin
  • 16,507
  • 9
  • 88
  • 138
  • You need to set parser options too... – jonrsharpe Mar 20 '19 at 20:44
  • If the 'parserOptions' change you are referring to is the 'ecmaVersion' that is set by default with the env setting. ("es6 - enable all ECMAScript 6 features except for modules (this automatically sets the ecmaVersion parser option to 6).") I also tried a few other parserOption changes and they didn't work. I can update the post when I get home – Andrew Thomas Mar 20 '19 at 21:17
  • I tried ' "parserOptions": { "ecmaVersion": 6, "ecmaFeatures": { "jsx": true } } ' with the "env" set to "es6" : true, and it still has a parse error – Andrew Thomas Mar 20 '19 at 23:56
  • I should read the comments before answering! ;^D Where did you set parseroptions? In eslintrc or the file? I think they *have* to be in eslintrc. – ruffin Feb 18 '21 at 18:30

2 Answers2

3

Your code lints as written on eslint.org/demo when ECMA Version 2015 is set. Well, there are errors, but they aren't the template literal usage.

What this tells me is that you're running into a parsing error, not a linting error and need to set your parser settings.

Parsing error when ECMA Version set to 5

parsing error when ECMA Version set to 5


Parse is fine and you have LINTING errors when ECMA is 2015

Parse is fine and you have LINTING errors when ECMA is 2015


Solution

To fix this, I think you have to provide an .eslintrc file somewhere that sets parser options to es2015 or later.

Double-check and try changing what's in yours to include this:

    "parserOptions": {
        "ecmaVersion": 6,
        "sourceType": "script",
        "ecmaFeatures": {}
    }

So even if your file includes the comment at the top...

/*eslint-env es6 */

... the parser options will win out and kill the template literal. And, as you show, your error is a parsing error, not a linting one:

Parsing error: Unexpected character '`' FolderName\FileName.js:31:17

I will readily admit that's a very confusing distinction. I didn't figure it out until banging my head on this answer for a while, but it explains why my env settings in my files don't always seem to "take"; the parser options override them.

ruffin
  • 16,507
  • 9
  • 88
  • 138
0

In .eslintrc.json, I used babel-eslint and installed babel-eslint as devDependency in package.json. Hope that works for you too

.eslintrc.json file

{
  "env": {...},
  "parser": "babel-eslint",
  "parserOptions": {...}
}

Edit 1:

babel-eslint is archived and it is suggested to use @babel/eslint-parser. More info below

babel eslint

@babel/eslint-parser

{
   "env": {...},
   "parser": "@babel/eslint-parser",
   "parserOptions": {...}
}

Edit 2:

@babel/eslint-parser needs @babel-core installed. If you are not already using @babel, then its best to upgrade your eslint to 7.5 and above. Here is the link to similar question:

bhargav
  • 619
  • 2
  • 14
  • 30