1

I want to do this:

const prefDateFormat = {
    weekday: 'long',
    day: 'numeric',
    month: 'long'
}

function formatDates(dates, dateFormat = prefDateFormat){ ... }

but Eslint throws:

error  'prefDateFormat' is assigned a value but never used  no-unused-vars

Is there a way to make eslint take into account a variable usage in a default argument value assignment?

Eslint version: "^6.7.2".

.eslintr.js file:

module.exports = {
  root: true,

  env: {
    node: true
  },

  'extends': [
    'plugin:vue/vue3-essential',
    'eslint:recommended'
  ],

  parserOptions: {
    parser: 'babel-eslint'
  },

  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
  },

  overrides: [
    {
      files: [
        '**/__tests__/*.{j,t}s?(x)',
        '**/tests/unit/**/*.spec.{j,t}s?(x)'
      ],
      env: {
        jest: true
      }
    }
  ]
}
  • 2
    Can you post your version and config (eg eslintrc)? – Jared Smith Oct 24 '22 at 13:46
  • No repro https://eslint.org/play/#eyJ0ZXh0IjoiY29uc3QgcHJlZkRhdGVGb3JtYXQgPSB7XG4gICAgd2Vla2RheTogJ2xvbmcnLFxuICAgIGRheTogJ251bWVyaWMnLFxuICAgIG1vbnRoOiAnbG9uZydcbn1cblxuZnVuY3Rpb24gZm9ybWF0RGF0ZXMoZGF0ZXMsIGRhdGVGb3JtYXQgPSBwcmVmRGF0ZUZvcm1hdCl7IFxuICAgIGlmIChkYXRlRm9ybWF0LndlZWtkYXkgPT09IFwibG9uZ1wiKSB7fVxufVxuXG5mb3JtYXREYXRlcyhbXSk7XG5cbiIsIm9wdGlvbnMiOnsicGFyc2VyT3B0aW9ucyI6eyJlY21hVmVyc2lvbiI6MTMsInNvdXJjZVR5cGUiOiJzY3JpcHQiLCJlY21hRmVhdHVyZXMiOnt9fSwicnVsZXMiOnsibm8tdW5zYWZlLW9wdGlvbmFsLWNoYWluaW5nIjpbImVycm9yIl0sIm5vLXVudXNlZC12YXJzIjpbImVycm9yIl19LCJlbnYiOnsiYnJvd3NlciI6dHJ1ZX19fQ== – VLAZ Oct 24 '22 at 13:50
  • 1
    Can’t reproduce that. It says `dateFormat` is unused or `formatDates` is unused. The exact code you provided is syntactically invalid. What is the minimal code that produces a warning like this? Note that the current ESLint version is 8.26.0. – Sebastian Simon Oct 24 '22 at 13:50
  • Can you post the entirety of `formatDates()`? It seems to be saying the argument doesn't get used in the function. – mykaf Oct 24 '22 at 14:02
  • 1
    Still no repro with the config. I created a new project, installed eslint@^6.7.2, babel-eslint, and eslint-plugin-vue then using the config here, I ran it against [this code](https://pastebin.com/Z07XDSeC) - it *uses* the `dateFormat` parameter inside the function. The `xyz` is added as control. There are only two errors - the empty block at line 8 and the unused variable `xyz` at line 13. There is no complaint that `prefDateFormat` is unused. If I remove the usage of `dateFormat`, then the error I get is that `dateFormat` is unused, but no error for `prefDateFormat` being unused. – VLAZ Oct 24 '22 at 14:09

1 Answers1

0

I solved the problem by adding ecmaVersion: 6 property to parserOptions, in my .eslintr.js file, like so:

parserOptions: {
  parser: 'babel-eslint',
  ecmaVersion: 6,
},