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 pathUtil = require("../util/path-util");
11
12//------------------------------------------------------------------------------
13// Private
14//------------------------------------------------------------------------------
15
16const NAMESPACE_REGEX = /^@.*\//i;
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
27 /**
28 * On Windows, name can come in with Windows slashes instead of Unix slashes.
29 * Normalize to Unix first to avoid errors later on.
30 * https://github.com/eslint/eslint/issues/5644
31 */
32 if (name.indexOf("\\") > -1) {
33 name = pathUtil.convertPathToPosix(name);
34 }
35
36 if (name.charAt(0) === "@") {
37
38 /**
39 * it's a scoped package
40 * package name is the prefix, or just a username
41 */
42 const scopedPackageShortcutRegex = new RegExp(`^(@[^/]+)(?:/(?:${prefix})?)?$`),
43 scopedPackageNameRegex = new RegExp(`^${prefix}(-|$)`);
44
45 if (scopedPackageShortcutRegex.test(name)) {
46 name = name.replace(scopedPackageShortcutRegex, `$1/${prefix}`);
47 } else if (!scopedPackageNameRegex.test(name.split("/")[1])) {
48
49 /**
50 * for scoped packages, insert the prefix after the first / unless
51 * the path is already @scope/eslint or @scope/eslint-xxx-yyy
52 */
53 name = name.replace(/^@([^/]+)\/(.*)$/, `@$1/${prefix}-$2`);
54 }
55 } else if (name.indexOf(`${prefix}-`) !== 0) {
56 name = `${prefix}-${name}`;
57 }
58
59 return name;
60}
61
62/**
63 * Removes the prefix from a term.
64 * @param {string} prefix The prefix to remove.
65 * @param {string} term The term which may have the prefix.
66 * @returns {string} The term without prefix.
67 */
68function removePrefixFromTerm(prefix, term) {
69 return term.startsWith(prefix) ? term.slice(prefix.length) : term;
70}
71
72/**
73 * Adds a prefix to a term.
74 * @param {string} prefix The prefix to add.
75 * @param {string} term The term which may not have the prefix.
76 * @returns {string} The term with prefix.
77 */
78function addPrefixToTerm(prefix, term) {
79 return term.startsWith(prefix) ? term : `${prefix}${term}`;
80}
81
82/**
83 * Gets the scope (namespace) of a term.
84 * @param {string} term The term which may have the namespace.
85 * @returns {string} The namepace of the term if it has one.
86 */
87function getNamespaceFromTerm(term) {
88 const match = term.match(NAMESPACE_REGEX);
89
90 return match ? match[0] : "";
91}
92
93/**
94 * Removes the namespace from a term.
95 * @param {string} term The term which may have the namespace.
96 * @returns {string} The name of the plugin without the namespace.
97 */
98function removeNamespaceFromTerm(term) {
99 return term.replace(NAMESPACE_REGEX, "");
100}
101
102//------------------------------------------------------------------------------
103// Public Interface
104//------------------------------------------------------------------------------
105
106module.exports = {
107 normalizePackageName,
108 removePrefixFromTerm,
109 addPrefixToTerm,
110 getNamespaceFromTerm,
111 removeNamespaceFromTerm
112};