简体   繁体   English

根据ESLint,Switch语句默认返回“ Unreachable”

[英]Switch Statement default return “Unreachable” according to ESLint

I have a function which determines the url of a button (an anchor tag) based on some provided urls by a content creator, and the userAgent of the user's device. 我有一个功能,它可以根据内容创建者提供的某些URL和用户设备的userAgent确定按钮的URL(锚标记)。

In this function, I have two switch statements. 在此功能中,我有两个switch语句。 I first check to see what the linktype the content creator has chosen to use for a button (either a web url, or an App Store link) 我首先检查一下内容创建者为按钮选择的链接类型(Web URL或App Store链接)

If the content creator specifies an App Store link, they also provide a range of urls for multiple platforms. 如果内容创建者指定了App Store链接,则他们还将为多个平台提供一系列URL。 They don't necessarily give a link for every platform, so we have a fallback to a web url, which is also either set by the creator, or given by the back-end (the first url it can find basically) 它们不一定为每个平台都提供链接,因此我们有一个备用网址,该网址也可以由创建者设置,也可以由后端提供(它基本上可以找到的第一个网址)

I have a problem where the default clause within the inner switch statement is flagged as Unreachable code by ESLint. 我遇到一个问题,其中内部switch语句中的default子句被ESLint标记为“ Unreachable code

Is ESLint wrong? ESLint错误吗? or is there something I could do better? 还是有什么我可以做得更好的?

function getButtonLink() {
    switch(this.props.linkType) {
        case 0: {  // appStore link, get the best-fit appstore link for device
            switch(this.detectUserAgent()) {
                case 1: {
                    return this.setButtonUrlProp('windows');
                }
                case 2: {
                    return this.setButtonUrlProp('android'); 
                }
                case 3: {
                    return this.setButtonUrlProp('ios');
                }
                case 4: {
                    return this.setButtonUrlProp('amazon'); 
                }
                default: {
                    return this.setButtonUrlProp('web');
                }
            }
        }
        case 1:   // web link
        default: {
            return this.props.button.urls.web;
        }
    }
}

How about: 怎么样:

    function getButtonLink() {
        switch(this.props.linkType) {
            case 0: // appStore link, get the best-fit appstore link for device
                var agents = ["web", "windows", "android", "ios", "amazon"];
                return this.setButtonUrlProp(
                    agents[this.detectUserAgent()] || agents[0]
                );
            }
            case 1: // web link
            default: {
                return this.props.button.urls.web;
            }
        }
    }

ESLint is not showing this warning for me. ESLint没有为我显示此警告。 Make sure sure that the error is not caused by something else, such as detectUserAgent() never returning a value that is not 1, 2, 3 or 4? 确保错误不是由其他原因引起的,例如detectUserAgent()永远不会返回非1、2、3或4的值?

Whatever the case, I would refactor the code so that the outer condition uses something else, such as if - else as nested switch statements can be harder to read at a glance. 无论如何,我都会重构代码,以便外部条件使用其他内容,例如if-else,因为一眼看到嵌套的switch语句可能更难理解。

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

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