![](/img/trans.png)
[英]unreachable statement, menuItem.setActionView(R.layout.theme_switch);
[英]Switch Statement default return “Unreachable” according to ESLint
我有一個功能,它可以根據內容創建者提供的某些URL和用戶設備的userAgent確定按鈕的URL(錨標記)。
在此功能中,我有兩個switch語句。 我首先檢查一下內容創建者為按鈕選擇的鏈接類型(Web URL或App Store鏈接)
如果內容創建者指定了App Store鏈接,則他們還將為多個平台提供一系列URL。 它們不一定為每個平台都提供鏈接,因此我們有一個備用網址,該網址也可以由創建者設置,也可以由后端提供(它基本上可以找到的第一個網址)
我遇到一個問題,其中內部switch
語句中的default
子句被ESLint標記為“ Unreachable code
。
ESLint錯誤嗎? 還是有什么我可以做得更好的?
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;
}
}
}
怎么樣:
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沒有為我顯示此警告。 確保錯誤不是由其他原因引起的,例如detectUserAgent()永遠不會返回非1、2、3或4的值?
無論如何,我都會重構代碼,以便外部條件使用其他內容,例如if-else,因為一眼看到嵌套的switch語句可能更難理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.