unicorn/prefer-url-can-parse
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-url-can-parse": "error",
},
},
];Prefer URL.canParse() over constructing a URL in a try/catch for validation.
function canParseUrl(value) {
try {
new URL(value);
return true;
} catch {
return false;
}
}let valid;
try {
new URL(value, base);
valid = true;
} catch {
valid = false;
}function canParseUrl(value) {
return URL.canParse(value);
}let valid;
valid = URL.canParse(value, base);Examples from the ESLint documentation.
unicorn/prefer-url-can-parse
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-url-can-parse": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Prefer Unicode code point escapes over legacy escape sequences.
Prefer using Element#toggleAttribute() to toggle attributes.
Prefer URLSearchParams over manually splitting query strings.
Prefer direct Temporal conversion methods.
Require CSS.escape() for interpolated values in CSS selectors.