2 * @fileoverview Common helpers for naming of plugins, formatters and configs
6 const NAMESPACE_REGEX = /^@.*\//iu;
9 * Brings package name to correct format based on prefix
10 * @param {string} name The name of the package.
11 * @param {string} prefix Can be either "eslint-plugin", "eslint-config" or "eslint-formatter"
12 * @returns {string} Normalized name of the package
15 function normalizePackageName(name, prefix) {
16 let normalizedName = name;
19 * On Windows, name can come in with Windows slashes instead of Unix slashes.
20 * Normalize to Unix first to avoid errors later on.
21 * https://github.com/eslint/eslint/issues/5644
23 if (normalizedName.includes("\\")) {
24 normalizedName = normalizedName.replace(/\\/gu, "/");
27 if (normalizedName.charAt(0) === "@") {
30 * it's a scoped package
31 * package name is the prefix, or just a username
33 const scopedPackageShortcutRegex = new RegExp(`^(@[^/]+)(?:/(?:${prefix})?)?$`, "u"),
34 scopedPackageNameRegex = new RegExp(`^${prefix}(-|$)`, "u");
36 if (scopedPackageShortcutRegex.test(normalizedName)) {
37 normalizedName = normalizedName.replace(scopedPackageShortcutRegex, `$1/${prefix}`);
38 } else if (!scopedPackageNameRegex.test(normalizedName.split("/")[1])) {
41 * for scoped packages, insert the prefix after the first / unless
42 * the path is already @scope/eslint or @scope/eslint-xxx-yyy
44 normalizedName = normalizedName.replace(/^@([^/]+)\/(.*)$/u, `@$1/${prefix}-$2`);
46 } else if (!normalizedName.startsWith(`${prefix}-`)) {
47 normalizedName = `${prefix}-${normalizedName}`;
50 return normalizedName;
54 * Removes the prefix from a fullname.
55 * @param {string} fullname The term which may have the prefix.
56 * @param {string} prefix The prefix to remove.
57 * @returns {string} The term without prefix.
59 function getShorthandName(fullname, prefix) {
60 if (fullname[0] === "@") {
61 let matchResult = new RegExp(`^(@[^/]+)/${prefix}$`, "u").exec(fullname);
64 return matchResult[1];
67 matchResult = new RegExp(`^(@[^/]+)/${prefix}-(.+)$`, "u").exec(fullname);
69 return `${matchResult[1]}/${matchResult[2]}`;
71 } else if (fullname.startsWith(`${prefix}-`)) {
72 return fullname.slice(prefix.length + 1);
79 * Gets the scope (namespace) of a term.
80 * @param {string} term The term which may have the namespace.
81 * @returns {string} The namespace of the term if it has one.
83 function getNamespaceFromTerm(term) {
84 const match = term.match(NAMESPACE_REGEX);
86 return match ? match[0] : "";
89 //------------------------------------------------------------------------------
91 //------------------------------------------------------------------------------