sindresorhus/eslint-plugin-unicorn
Rule proposal: Prefer `URL#href` to `URL#toString()`
クローズ
#1,655 opened on 2021/12/22
help wantednew rule
Repository metrics
- Stars
- (5,022 個のスター)
- PR merge metrics
- (平均マージ 4h 30m) (30d で 26 merged PRs)
説明
Description
Nitpick rule.
When building URLs via the URL constructor, casting them to string seems to be obviously done via .toString and String(), but there's actually a native property for this.
This is generally more commonly done in TypeScript since the string type is enforced, but many just pass the URL instance to fetch in JavaScript instead 😬
Fail
const url = new URL(location.origin);
console.log(url.toString())
console.log(String(url))
Pass
const url = new URL(location.origin);
console.log(url.href)