【问题标题】:SonarLint : Code Smell : Local variables should not be declared and then immediately returned or thrownSonarLint:代码气味:不应声明局部变量,然后立即返回或抛出
【发布时间】:2022-01-18 16:53:16
【问题描述】:

使用 SonarLint 分析我的代码后,我得到以下异味代码:“不应声明局部变量然后立即返回或抛出”。

即使这没有阻塞并且组件运行良好。

我认为有更好的方法可以在函数中发布 return,但我不知道如何,如果有人知道诀窍的话。

这是我的组件:

const ColumnModalEvent = (currency: any) => {
  const columnsEventModal: Column[] = [
{
  Header: () => <I18nWrapper translateKey="movement.type.fieldName" />,
  accessor: 'type',
  disableSortBy: true,
  Cell: ({ value }) => (
    <I18nWrapper translateKey={value} prefix="movement.type" />
  ),
},
{
  Header: () => (
    <I18nWrapper translateKey="movement.uniqueReference.fieldNameShort" />
  ),
  accessor: 'uniqueRef',
},
{
  Header: () => <I18nWrapper translateKey="movement.documentDate" />,
  accessor: 'createdDate',
  className: 'text-end',
  headerClassName: 'text-end',
  Cell: ({ value }) => <DateFormater dateToFomat={value} />,
},
];

 return columnsEventModal;
 };

 export default ColumnModalEvent;

【问题讨论】:

    标签: reactjs sonarqube sonarlint


    【解决方案1】:

    只是为了代码简介,将第一行从

    const columnsEventModal=  Column[] = [...]
    return columnsEventModal;
    

    到线

    return [...]
    

    因为你在插入和返回语句之间没有做任何事情,Sonar 不明白你为什么要分配一个变量

    【讨论】:

    • 添加到这一点,为了保持你的类型检查,我还会在函数中添加一个返回类型 - const ColumnModalEvent = (currency: any): Column[] =&gt; {... 和/或在返回时使用as,即return [...] as Column[];
    • 我相信你是对的,我也会这样做,但我不知道反应,我只是喜欢声纳
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多