unicorn/prefer-url-href
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-url-href": "error",
},
},
];new URL('https://example.com').toString();const url = new URL('https://example.com');
String(url);function stringify(url: URL) {
return url.toString();
}new URL('https://example.com').href;const url = new URL('https://example.com');
url.href;function stringify(url: URL) {
return url.href;
}Examples from the ESLint documentation.
unicorn/prefer-url-href
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-url-href": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Prefer URL.canParse() over constructing a URL in a try/catch for validation.
Prefer URLSearchParams over manually splitting query strings.
Prefer Unicode code point escapes over legacy escape sequences.
Require CSS.escape() for interpolated values in CSS selectors.
Prefer using Element#toggleAttribute() to toggle attributes.
Require passive event listeners for high-frequency events.