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.

190 lines
5.8 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getType = getType;
  6. exports.semverify = semverify;
  7. exports.intersection = intersection;
  8. exports.findSuggestion = findSuggestion;
  9. exports.prettifyVersion = prettifyVersion;
  10. exports.prettifyTargets = prettifyTargets;
  11. exports.isUnreleasedVersion = isUnreleasedVersion;
  12. exports.getLowestUnreleased = getLowestUnreleased;
  13. exports.filterStageFromList = filterStageFromList;
  14. exports.getImportSource = getImportSource;
  15. exports.getRequireSource = getRequireSource;
  16. exports.isPolyfillSource = isPolyfillSource;
  17. exports.getModulePath = getModulePath;
  18. exports.createImport = createImport;
  19. exports.isNamespaced = isNamespaced;
  20. exports.has = void 0;
  21. var t = _interopRequireWildcard(require("@babel/types"));
  22. var _invariant = _interopRequireDefault(require("invariant"));
  23. var _semver = _interopRequireDefault(require("semver"));
  24. var _jsLevenshtein = _interopRequireDefault(require("js-levenshtein"));
  25. var _helperModuleImports = require("@babel/helper-module-imports");
  26. var _unreleasedLabels = _interopRequireDefault(require("../data/unreleased-labels"));
  27. var _targetsParser = require("./targets-parser");
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  29. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  30. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  31. const has = Object.hasOwnProperty.call.bind(Object.hasOwnProperty);
  32. exports.has = has;
  33. function getType(target) {
  34. return Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
  35. }
  36. const versionRegExp = /^(\d+|\d+.\d+)$/;
  37. function semverify(version) {
  38. if (typeof version === "string" && _semver.default.valid(version)) {
  39. return version;
  40. }
  41. (0, _invariant.default)(typeof version === "number" || typeof version === "string" && versionRegExp.test(version), `'${version}' is not a valid version`);
  42. const split = version.toString().split(".");
  43. while (split.length < 3) {
  44. split.push("0");
  45. }
  46. return split.join(".");
  47. }
  48. function intersection(first, second, third) {
  49. const result = new Set();
  50. for (const el of first) {
  51. if (second.has(el) && third.has(el)) result.add(el);
  52. }
  53. return result;
  54. }
  55. function findSuggestion(options, option) {
  56. let levenshteinValue = Infinity;
  57. return options.reduce((suggestion, validOption) => {
  58. const value = (0, _jsLevenshtein.default)(validOption, option);
  59. if (value < levenshteinValue) {
  60. levenshteinValue = value;
  61. return validOption;
  62. }
  63. return suggestion;
  64. }, "");
  65. }
  66. function prettifyVersion(version) {
  67. if (typeof version !== "string") {
  68. return version;
  69. }
  70. const parts = [_semver.default.major(version)];
  71. const minor = _semver.default.minor(version);
  72. const patch = _semver.default.patch(version);
  73. if (minor || patch) {
  74. parts.push(minor);
  75. }
  76. if (patch) {
  77. parts.push(patch);
  78. }
  79. return parts.join(".");
  80. }
  81. function prettifyTargets(targets) {
  82. return Object.keys(targets).reduce((results, target) => {
  83. let value = targets[target];
  84. const unreleasedLabel = _unreleasedLabels.default[target];
  85. if (typeof value === "string" && unreleasedLabel !== value) {
  86. value = prettifyVersion(value);
  87. }
  88. results[target] = value;
  89. return results;
  90. }, {});
  91. }
  92. function isUnreleasedVersion(version, env) {
  93. const unreleasedLabel = _unreleasedLabels.default[env];
  94. return !!unreleasedLabel && unreleasedLabel === version.toString().toLowerCase();
  95. }
  96. function getLowestUnreleased(a, b, env) {
  97. const unreleasedLabel = _unreleasedLabels.default[env];
  98. const hasUnreleased = [a, b].some(item => item === unreleasedLabel);
  99. if (hasUnreleased) {
  100. return a === hasUnreleased ? b : a || b;
  101. }
  102. return (0, _targetsParser.semverMin)(a, b);
  103. }
  104. function filterStageFromList(list, stageList) {
  105. return Object.keys(list).reduce((result, item) => {
  106. if (!stageList[item]) {
  107. result[item] = list[item];
  108. }
  109. return result;
  110. }, {});
  111. }
  112. function getImportSource({
  113. node
  114. }) {
  115. if (node.specifiers.length === 0) return node.source.value;
  116. }
  117. function getRequireSource({
  118. node
  119. }) {
  120. if (!t.isExpressionStatement(node)) return;
  121. const {
  122. expression
  123. } = node;
  124. const isRequire = t.isCallExpression(expression) && t.isIdentifier(expression.callee) && expression.callee.name === "require" && expression.arguments.length === 1 && t.isStringLiteral(expression.arguments[0]);
  125. if (isRequire) return expression.arguments[0].value;
  126. }
  127. function isPolyfillSource(source) {
  128. return source === "@babel/polyfill" || source === "core-js";
  129. }
  130. const modulePathMap = {
  131. "regenerator-runtime": "regenerator-runtime/runtime"
  132. };
  133. function getModulePath(mod) {
  134. return modulePathMap[mod] || `core-js/modules/${mod}`;
  135. }
  136. function createImport(path, mod) {
  137. return (0, _helperModuleImports.addSideEffect)(path, getModulePath(mod));
  138. }
  139. function isNamespaced(path) {
  140. if (!path.node) return false;
  141. const binding = path.scope.getBinding(path.node.name);
  142. if (!binding) return false;
  143. return binding.path.isImportNamespaceSpecifier();
  144. }