UNPKG

3.56 kBJavaScriptView Raw
1/**
2 * @fileoverview Common helpers for naming of plugins, formatters and configs
3 */
4"use strict";
5
6//------------------------------------------------------------------------------
7// Requirements
8//------------------------------------------------------------------------------
9
10const pathUtils = require("../util/path-utils");
11
12//------------------------------------------------------------------------------
13// Private
14//------------------------------------------------------------------------------
15
16const NAMESPACE_REGEX = /^@.*\//iu;
17
18/**
19 * Brings package name to correct format based on prefix
20 * @param {string} name The name of the package.
21 * @param {string} prefix Can be either "eslint-plugin", "eslint-config" or "eslint-formatter"
22 * @returns {string} Normalized name of the package
23 * @private
24 */
25function normalizePackageName(name, prefix) {
26 let normalizedName = name;
27
28 /**
29 * On Windows, name can come in with Windows slashes instead of Unix slashes.
30 * Normalize to Unix first to avoid errors later on.
31 * https://github.com/eslint/eslint/issues/5644
32 */
33 if (normalizedName.indexOf("\\") > -1) {
34 normalizedName = pathUtils.convertPathToPosix(normalizedName);
35 }
36
37 if (normalizedName.charAt(0) === "@") {
38
39 /**
40 * it's a scoped package
41 * package name is the prefix, or just a username
42 */
43 const scopedPackageShortcutRegex = new RegExp(`^(@[^/]+)(?:/(?:${prefix})?)?$`, "u"),
44 scopedPackageNameRegex = new RegExp(`^${prefix}(-|$)`, "u");
45
46 if (scopedPackageShortcutRegex.test(normalizedName)) {
47 normalizedName = normalizedName.replace(scopedPackageShortcutRegex, `$1/${prefix}`);
48 } else if (!scopedPackageNameRegex.test(normalizedName.split("/")[1])) {
49
50 /**
51 * for scoped packages, insert the prefix after the first / unless
52 * the path is already @scope/eslint or @scope/eslint-xxx-yyy
53 */
54 normalizedName = normalizedName.replace(/^@([^/]+)\/(.*)$/u, `@$1/${prefix}-$2`);
55 }
56 } else if (normalizedName.indexOf(`${prefix}-`) !== 0) {
57 normalizedName = `${prefix}-${normalizedName}`;
58 }
59
60 return normalizedName;
61}
62
63/**
64 * Removes the prefix from a fullname.
65 * @param {string} fullname The term which may have the prefix.
66 * @param {string} prefix The prefix to remove.
67 * @returns {string} The term without prefix.
68 */
69function getShorthandName(fullname, prefix) {
70 if (fullname[0] === "@") {
71 let matchResult = new RegExp(`^(@[^/]+)/${prefix}$`, "u").exec(fullname);
72
73 if (matchResult) {
74 return matchResult[1];
75 }
76
77 matchResult = new RegExp(`^(@[^/]+)/${prefix}-(.+)$`, "u").exec(fullname);
78 if (matchResult) {
79 return `${matchResult[1]}/${matchResult[2]}`;
80 }
81 } else if (fullname.startsWith(`${prefix}-`)) {
82 return fullname.slice(prefix.length + 1);
83 }
84
85 return fullname;
86}
87
88/**
89 * Gets the scope (namespace) of a term.
90 * @param {string} term The term which may have the namespace.
91 * @returns {string} The namepace of the term if it has one.
92 */
93function getNamespaceFromTerm(term) {
94 const match = term.match(NAMESPACE_REGEX);
95
96 return match ? match[0] : "";
97}
98
99//------------------------------------------------------------------------------
100// Public Interface
101//------------------------------------------------------------------------------
102
103module.exports = {
104 normalizePackageName,
105 getShorthandName,
106 getNamespaceFromTerm
107};