项目原始demo,不改动
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @fileoverview Common helpers for naming of plugins, formatters and configs
  3. */
  4. "use strict";
  5. //------------------------------------------------------------------------------
  6. // Requirements
  7. //------------------------------------------------------------------------------
  8. const pathUtil = require("../util/path-util");
  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 = pathUtil.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 term.
  53. * @param {string} prefix The prefix to remove.
  54. * @param {string} term The term which may have the prefix.
  55. * @returns {string} The term without prefix.
  56. */
  57. function removePrefixFromTerm(prefix, term) {
  58. return term.startsWith(prefix) ? term.slice(prefix.length) : term;
  59. }
  60. /**
  61. * Adds a prefix to a term.
  62. * @param {string} prefix The prefix to add.
  63. * @param {string} term The term which may not have the prefix.
  64. * @returns {string} The term with prefix.
  65. */
  66. function addPrefixToTerm(prefix, term) {
  67. return term.startsWith(prefix) ? term : `${prefix}${term}`;
  68. }
  69. /**
  70. * Gets the scope (namespace) of a term.
  71. * @param {string} term The term which may have the namespace.
  72. * @returns {string} The namepace of the term if it has one.
  73. */
  74. function getNamespaceFromTerm(term) {
  75. const match = term.match(NAMESPACE_REGEX);
  76. return match ? match[0] : "";
  77. }
  78. /**
  79. * Removes the namespace from a term.
  80. * @param {string} term The term which may have the namespace.
  81. * @returns {string} The name of the plugin without the namespace.
  82. */
  83. function removeNamespaceFromTerm(term) {
  84. return term.replace(NAMESPACE_REGEX, "");
  85. }
  86. //------------------------------------------------------------------------------
  87. // Public Interface
  88. //------------------------------------------------------------------------------
  89. module.exports = {
  90. normalizePackageName,
  91. removePrefixFromTerm,
  92. addPrefixToTerm,
  93. getNamespaceFromTerm,
  94. removeNamespaceFromTerm
  95. };