0

ESLint is giving me the following error message: "'semitoneInterval' is assigned a value but never used [no-unused-vars]". I guess it is pretty clear it isn't a unused var as I used it 30ish times. Using only one return semitoneInterval; in the end of the switch fixes the error, but I don't want to use this syntax due to the big amount of extra lines writing break;.

import React, { Component } from 'react';

export class Piano extends Component {
  getSemitoneIntervals = chordType => {
    let semitoneInterval;

    switch (chordType) {
      case '5': return semitoneInterval = [0, 7];
      case '': return semitoneInterval = [0, 4, 7];
      case 'm': return semitoneInterval = [0, 3, 7];
      case 'sus2': return semitoneInterval = [0, 5, 7];
      case 'sus4': return semitoneInterval = [0, 2, 7];
      case 'dim': return semitoneInterval = [0, 3, 6];
      case 'aug': return semitoneInterval = [0, 4, 8];
      case '7': return semitoneInterval = [0, 4, 7, 10];
      case 'm7': return semitoneInterval = [0, 3, 7, 10];
      case 'maj7': return semitoneInterval = [0, 4, 7, 11];
      case 'mM7': return semitoneInterval = [0, 3, 7, 11];
      case '6': return semitoneInterval = [0, 4, 7, 9];
      case 'm6': return semitoneInterval = [0, 3, 7, 9];
      case 'add2': return semitoneInterval = [0, 2, 4, 7];
      case 'add9': return semitoneInterval = [0, 4, 7, 14];
      case '7-5': return semitoneInterval = [0, 4, 6, 10];
      case '7+5': return semitoneInterval = [0, 4, 8, 10];
      case 'dim7': return semitoneInterval = [0, 3, 6, 9];
      case 'm7b5': return semitoneInterval = [0, 3, 6, 10];
      case 'aug7': return semitoneInterval = [0, 4, 8, 10];
      case '6/9': return semitoneInterval = [0, 4, 7, 9, 14];
      case '9': return semitoneInterval = [0, 4, 7, 10, 14];
      case 'm9': return semitoneInterval = [0, 3, 7, 10, 14];
      case 'maj9': return semitoneInterval = [0, 4, 7, 11, 14];
      case '11': return semitoneInterval = [0, 4, 7, 10, 14, 17];
      case 'm11': return semitoneInterval = [0, 3, 7, 10, 14, 17];
      case 'maj13': return semitoneInterval = [0, 4, 7, 11, 14, 21];
      case '13': return semitoneInterval = [0, 4, 7, 10, 14, 17, 21];
      case 'm13': return semitoneInterval = [0, 3, 7, 10, 14, 17, 21];
      default: return console.log('Not valid chord type');
    }
  }

  //[..]
}

export default Piano;

Nay Mello
  • 61
  • 4
  • 4
    It is useless, just return the array `case '5': return semitoneInterval = [0, 7];` should be `case '5': return [0, 7];` – epascarello May 13 '20 at 23:53
  • 2
    I'd suggest extracting all these cases in to a "map object," defined outside of your component, like `const intervals = { '5': [0, 7], 'm': [0, 3, 7], ... }`. That should allow you to do something more like `return intervals[chordType]`. That'd be less code noise than a switch statement. – Jacob May 14 '20 at 00:00
  • 2
    You might want to give that error message another read "**is assigned a value** but never used". I'd just create an object whose keys are the `case`s and values are the arrays. Then the function is just `return obj[chordType];`. – Heretic Monkey May 14 '20 at 00:04
  • Does this answer your question? [How can I use ESLint no-unused-vars for a block of code?](https://stackoverflow.com/questions/46284405/how-can-i-use-eslint-no-unused-vars-for-a-block-of-code) – StackSlave May 14 '20 at 00:19

2 Answers2

1

No, you are not use this variable, you just return it during the assignment, but you are not really use it.
you can just return the value without the assignment to the variable

Neriya Bar-lev
  • 564
  • 4
  • 4
1

you are assigning semitoneInterval to a value and right away returning its value.

Any return in your switch statement would cause the function to stop its execution and return a value. Therefore semitoneInterval is assigned a value in any of the case it goes through, but the value stored inside is never actually used (only the value returned by your function is).

Your switch statement could work equally like this:

switch (chordType) {
      case '5': return [0, 7];
      case '': return [0, 4, 7];
    ...

this other eslint rule(although a bit of a different topic) explains as well (in better words) why assigning a value this way has no real use in your code.

Kevin Amiranoff
  • 13,440
  • 11
  • 59
  • 90