val*_*lli 1 javascript let ecmascript-6 reactjs eslint
我有一个像下面这样的函数,其中 isformValid 被标记为一个 let 并在 if 块中使用它并根据条件更改它的值。
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;
}
Run Code Online (Sandbox Code Playgroud)
我可以看到一个 eslint 错误,因为 'isFormValid' 被分配了一个值但从未使用过 ' 即使我在 if 块中使用过它。
ESLint 文档很好地描述了这一点(https://eslint.org/docs/rules/no-unused-vars):
// Write-only variables are not considered as used.
var y = 10;
y = 5;
Run Code Online (Sandbox Code Playgroud)
您写入isFormValid两次(在初始化期间和 if 块中),但永远不会读取存储在变量中的值,从而触发 linting 错误。当函数返回时,存储在局部变量中的值将被丢弃。由于从未读取或返回该值,因此它不会改变计算结果的任何内容。看起来您的代码中根本不需要这个变量。
| 归档时间: |
|
| 查看次数: |
4895 次 |
| 最近记录: |