unicorn/relative-url-style

Enforce one style for relative URLs in new URL(): with or without ./

fixable

new URL("./logo.png", base) and new URL("logo.png", base) give the same URL. Pick one style so you write and search for the same file the same way everywhere.

The rule only checks new URL() with two arguments, and only when adding or removing ./ doesn't change the result. "../logo.png" and "/logo.png" are never reported.

--fix removes or adds the ./ in a string. For a template literal, the editor offers the change as a suggestion.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/relative-url-style" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"unicorn/relative-url-style" = ["error", "never"] # "never" or "always"

Examples#

example.js2 problems
const logo = new URL("./logo.png", import.meta.url);
const icon = new URL(`./${name}.svg`, import.meta.url);
example.jsNo problems
const logo = new URL("logo.png", import.meta.url);
const icon = new URL(`${name}.svg`, import.meta.url);
const shared = new URL("../shared/logo.png", import.meta.url);

Options#

OptionWhat it does
"never" (default)Report a string or a template literal that starts with ./.
"always"Report a string that doesn't start with ./. Template literals aren't checked.
example.jswith "always"1 problem
const logo = new URL("logo.png", import.meta.url);
example.jswith "always"No problems
const logo = new URL("./logo.png", import.meta.url);
const icon = new URL(`${name}.svg`, import.meta.url);
const shared = new URL("../shared/logo.png", import.meta.url);