sindresorhus/eslint-plugin-unicorn

Rule proposal: Prefer `URL#href` to `URL#toString()`

Closed

#1,655 opened on Dec 22, 2021

 (17 comments) (5 reactions) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

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)

Contributor guide