Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

naming.js 3.5KB

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