I have a function like below in which isformValid is decalred as a let and using it in if block and changing its value according to the condition.
validateForm(validationErrors, formData) {
let validationRules = this.state.dynamicJourneyData[this.state.currentStepName].validationRules;
let isFormValid = true;
let fullErrorList = [];
validationRules.rules.forEach((rule) => {
let errorList = this.evaluateRule(rule, formData);
if (errorList.length > 0) {
fullErrorList = fullErrorList.concat(errorList);
}
});
let finalErrorList = [];
let errorKeys = [];
fullErrorList.filter((error) => errorKeys.indexOf(error.id) < 0).forEach((error) => {
finalErrorList.push(error);
errorKeys.push(error.id);
});
if (finalErrorList.length > 0) {
isFormValid = false;
if (finalErrorList.length === 1) {
validationErrors.messageTitle = validationErrors.messageTitle
.replace('@count', finalErrorList.length)
.replace('were', 'was')
.replace('errors', 'error');
} else {
validationErrors.messageTitle = validationErrors.messageTitle.replace('@count', finalErrorList.length);
}
validationErrors.messageBody = finalErrorList; /*(fullErrorList.map(error=>error.label)).toString();*/
}
return finalErrorList;
}
I could see an eslint error as 'isFormValid' is assigned a value but never used ' eventhough i have used it in if block.
I have a function like below in which isformValid is decalred as a let and using it in if block and changing its value according to the condition.
validateForm(validationErrors, formData) {
let validationRules = this.state.dynamicJourneyData[this.state.currentStepName].validationRules;
let isFormValid = true;
let fullErrorList = [];
validationRules.rules.forEach((rule) => {
let errorList = this.evaluateRule(rule, formData);
if (errorList.length > 0) {
fullErrorList = fullErrorList.concat(errorList);
}
});
let finalErrorList = [];
let errorKeys = [];
fullErrorList.filter((error) => errorKeys.indexOf(error.id) < 0).forEach((error) => {
finalErrorList.push(error);
errorKeys.push(error.id);
});
if (finalErrorList.length > 0) {
isFormValid = false;
if (finalErrorList.length === 1) {
validationErrors.messageTitle = validationErrors.messageTitle
.replace('@count', finalErrorList.length)
.replace('were', 'was')
.replace('errors', 'error');
} else {
validationErrors.messageTitle = validationErrors.messageTitle.replace('@count', finalErrorList.length);
}
validationErrors.messageBody = finalErrorList; /*(fullErrorList.map(error=>error.label)).toString();*/
}
return finalErrorList;
}
I could see an eslint error as 'isFormValid' is assigned a value but never used ' eventhough i have used it in if block.
Share Improve this question asked Nov 3, 2018 at 8:09 vallivalli 1032 silver badges11 bronze badges 1- 1 It wasn't used but assigned another value. "Using" would mean doing something with it in a operation other than assignment or returning it. – Oluwafemi Sule Commented Nov 3, 2018 at 8:12
2 Answers
Reset to default 6The ESLint docs describes that pretty well (https://eslint/docs/rules/no-unused-vars):
// Write-only variables are not considered as used.
var y = 10;
y = 5;
You write to isFormValid
two times (during initialization and in the if block), but the value stored inside the variable is never read, thus triggering the linting error. A value stored in a local variable is discarded when the function returns. Since the value is never read nor returned, it doesn't change anything about the oute of the putation. It seems like you don't need this variable in your code at all.
From ESLint docs:
https://eslint/docs/rules/no-unused-vars
Variables that are declared and not used anywhere in the code are most likely an error due to inplete refactoring. Such variables take up space in the code and can lead to confusion by readers.
In your case, isFormValid
is simply assigned a value two times. Using it would mean to use it in something like a condition or loop.