sindresorhus/eslint-plugin-unicorn

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

Chiusa

#1655 aperta il 22 dic 2021

 (17 commenti) (5 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 1g 16h) (399 PR mergiate in 30 g)

Descrizione

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)

Guida contributor