有了ES皮棉错误,希望在不禁用它或更改规则的情况下解决它

问题描述

在使用React时,出现以下错误,但我不想使用es-lint-disable

  1. 使用以下代码,我得到Do not nest ternary expressions.eslintno-nested-ternary的棉绒错误,该如何解决

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

  2. 在尝试遵循Assignment to property of function parameter 'data'.eslintno-param-reassign

    时遇到此错误

    如果(重复发生的月份){ 删除data.date; }

  3. 使用以下代码,我得到一个错误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. 使用以下代码,我得到了Unexpected dangling '_' in '_id'.eslintno-underscore-dangle错误在这里,我从后端获取的数据无法更改。如何解决棉绒错误

    
       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,};```
    
    
    

有人可以帮我吗?我试图搜索错误,但找到了// eslint-disable-next-line解决方案,但我不想要该解决方案。可以请任何人帮我吗?

谢谢

解决方法

    嵌套三元表达式会使代码更难以理解。 https://eslint.org/docs/rules/no-nested-ternary改为定义常规功能。
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. 此规则旨在防止由于修改或重新分配功能参数而引起的意外行为。 https://eslint.org/docs/rules/no-param-reassign克隆数据对象并使用它。

  2. 与#2相同。通过更新carry,您可以将该对象更改为原始结构。创建新对象,对其进行更新并返回。

(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. 此规则禁止在标识符中悬挂下划线。 https://eslint.org/docs/rules/no-underscore-dangle

对象密钥不能为_id。只能使用id

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