題:
是否有 ESLint 規則來強制使用*/indexon 進口。
例子:
檔案結構:
utils/
- index.js
- a.js
- b.js
main.js
主檔案
// WRONG
import utils from "./utils";
// OK
import utils from "./utils/index";
為什么
我需要這個,因為我的專案tscpaths在打字稿編譯后使用相對路徑替換絕對路徑,但是,由于某種原因,當*/index不存在時它不會替換。
選擇
如果規則不存在,是否有一個 vscode 設定會自動添加index自動匯入?
uj5u.com熱心網友回復:
選項 1,重組您的專案,以便無需index.js匯入。
選項 2,如果存在此問題的檔案夾不多,您可以為no-internal-imports規則撰寫配置,以便按名稱匯入檔案夾被視為“內部參考”。這可能是最糟糕的解決方案,因為沒有自動修復,添加新檔案夾需要更新 eslint 配置,但這可能有助于提及作為一個選項
選項 3,創建執行此操作的no-useless-path-segments規則的修改版本,它當前具有與您想要的相反的邏輯,因此您將修改第 95-108 行周圍的代碼以檢查匯入是否不是索引匯入而是決議為一個建議修復:
// assuming your rule has an option called enforceIndex
if (options && options.enforceIndex && !regexUnnecessaryIndex.test(importPath)) {
// if the import path is not pointing to an index file check if it resolves to a path that looks like an index file
const resolved_path = resolve(importPath, context);
if(regexUnnecessaryIndex.test(resolved_path)){
// if the resolved_path resolves to something that looks like an index then suggest adding /index
return reportWithProposedPath(`${importPath}/index`);
}
}
可能會聯系該 eslint 包的團隊并詢問他們是否愿意添加對該邏輯的支持,因為它可能會出現在其他人身上。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351498.html
標籤:javascript 打字稿 视觉工作室代码 eslint tsc
上一篇:打字稿嚴格布爾運算式錯誤
