]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/shared/naming.js
first commit
[pve-eslint.git] / eslint / lib / shared / naming.js
1 /**
2 * @fileoverview Common helpers for naming of plugins, formatters and configs
3 */
4 "use strict";
5
6 const NAMESPACE_REGEX = /^@.*\//iu;
7
8 /**
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
13 * @private
14 */
15 function normalizePackageName(name, prefix) {
16 let normalizedName = name;
17
18 /**
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
22 */
23 if (normalizedName.includes("\\")) {
24 normalizedName = normalizedName.replace(/\\/gu, "/");
25 }
26
27 if (normalizedName.charAt(0) === "@") {
28
29 /**
30 * it's a scoped package
31 * package name is the prefix, or just a username
32 */
33 const scopedPackageShortcutRegex = new RegExp(`^(@[^/]+)(?:/(?:${prefix})?)?$`, "u"),
34 scopedPackageNameRegex = new RegExp(`^${prefix}(-|$)`, "u");
35
36 if (scopedPackageShortcutRegex.test(normalizedName)) {
37 normalizedName = normalizedName.replace(scopedPackageShortcutRegex, `$1/${prefix}`);
38 } else if (!scopedPackageNameRegex.test(normalizedName.split("/")[1])) {
39
40 /**
41 * for scoped packages, insert the prefix after the first / unless
42 * the path is already @scope/eslint or @scope/eslint-xxx-yyy
43 */
44 normalizedName = normalizedName.replace(/^@([^/]+)\/(.*)$/u, `@$1/${prefix}-$2`);
45 }
46 } else if (!normalizedName.startsWith(`${prefix}-`)) {
47 normalizedName = `${prefix}-${normalizedName}`;
48 }
49
50 return normalizedName;
51 }
52
53 /**
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.
58 */
59 function getShorthandName(fullname, prefix) {
60 if (fullname[0] === "@") {
61 let matchResult = new RegExp(`^(@[^/]+)/${prefix}$`, "u").exec(fullname);
62
63 if (matchResult) {
64 return matchResult[1];
65 }
66
67 matchResult = new RegExp(`^(@[^/]+)/${prefix}-(.+)$`, "u").exec(fullname);
68 if (matchResult) {
69 return `${matchResult[1]}/${matchResult[2]}`;
70 }
71 } else if (fullname.startsWith(`${prefix}-`)) {
72 return fullname.slice(prefix.length + 1);
73 }
74
75 return fullname;
76 }
77
78 /**
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.
82 */
83 function getNamespaceFromTerm(term) {
84 const match = term.match(NAMESPACE_REGEX);
85
86 return match ? match[0] : "";
87 }
88
89 //------------------------------------------------------------------------------
90 // Public Interface
91 //------------------------------------------------------------------------------
92
93 module.exports = {
94 normalizePackageName,
95 getShorthandName,
96 getNamespaceFromTerm
97 };