简体   繁体   English

使用 ES lint 错误想要在不禁用它或更改规则的情况下解决

[英]With ES lint error want to reolve without disabling it or changing rule

While working with the react I have this below error but I dont want to use es-lint-disable在使用 react 时出现以下错误,但我不想使用 es-lint-disable

  1. With the below code I am getting the lint error of Do not nest ternary expressions.eslintno-nested-ternary How to resolve this?使用下面的代码,我得到了Do not nest ternary expressions.eslintno-nested-ternary的 lint 错误如何解决这个问题?

    getDaySuffix = day => (day === (1 || 21 || 31)? 'st': day === (2 || 22)? 'nd': day === (3 || 23)? 'rd': 'th');

  2. getting this error when I try to do following Assignment to property of function parameter 'data'.eslintno-param-reassign当我尝试执行以下Assignment to property of function parameter 'data'.eslintno-param-reassign时出现此错误

    if (recurrence.month) { delete data.date; if (recurrence.month) { 删除数据.date; } }

  3. With below code I am getting an error Assignment to property of function parameter 'carry'.eslintno-param-reassign使用下面的代码我得到一个错误Assignment to property of function parameter 'carry'.eslintno-param-reassign

     (carry, current) => { if (current.field === 'access_group_uuid') { carry[current.field] = (carry[current.field] || []).concat( current.value, ); } else { carry[current.field] = carry[current.field]? [carry[current.field], current.value]: current.value; } return carry; }, {}, );```
  4. With below code I am getting error of Unexpected dangling '_' in '_id'.eslintno-underscore-dangle .使用下面的代码,我得到了Unexpected dangling '_' in '_id'.eslintno-underscore-dangle错误。 here the data I am getting if from backend I cannot change it.这里是我从后端获取的数据,如果我无法更改它。 How can I resolve lint error?如何解决 lint 错误?

     const data = { _id: reporData._id, name: reporData.name }; const data = { _id: reporData._id, name: reporData.name, emails, recurrence: obj.recurrence, scheduled_report: true, // eslint-disable-next-line scheduled_report_id: obj._id, };```

Can anyone help me with this?谁能帮我这个? I trtied searching the error but I found // eslint-disable-next-line solution but I don't want that solution.我试图搜索错误,但我找到了// eslint-disable-next-line解决方案,但我不想要该解决方案。 Can please anyone help me with this?可以请任何人帮助我吗?

Thanks谢谢

  1. Nesting ternary expressions can make code more difficult to understand.嵌套三元表达式会使代码更难理解。 https://eslint.org/docs/rules/no-nested-ternary Define regual function instead. https://eslint.org/docs/rules/no-nested-ternary 改为定义常规 function。
const getDaySuffix = day => {
    if (day === 1 || day === 21 || day === 31) return 'st'
    if (day === 2 || day === 22) return 'nd'
    if (day === 3 || day === 23) return 'rd'
    return 'th'
  }
  1. This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.此规则旨在防止因修改或重新分配 function 参数而导致的意外行为。 https://eslint.org/docs/rules/no-param-reassign Clone data object and work with it. https://eslint.org/docs/rules/no-param-reassign克隆数据 object 并使用它。

  2. Same as #2.与#2 相同。 By updating carry you mutate this object in original structure.通过更新carry ,您可以改变原始结构中的 object。 Create new object, update it and return.创建新的 object,更新它并返回。

(carry, current) => {
    const carryClone = {...carry}
    if (current.field === 'access_group_uuid') {
      carryClone[current.field] = (carryClone[current.field] || []).concat(current.value)
    } else {
      carryClone[current.field] = carryClone[current.field] ? [carryClone[current.field], current.value] : current.value
    }
    return carryClone
  }
  1. This rule disallows dangling underscores in identifiers.此规则不允许在标识符中使用悬挂下划线。 https://eslint.org/docs/rules/no-underscore-dangle https://eslint.org/docs/rules/no-underscore-dangle

Object key cannot be _id . Object 密钥不能是_id Use id only instead.仅使用id

const data = { id: reporData._id, name: reporData.name };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM