index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _core = require("@babel/core");
  8. var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
  9. api.assertVersion(7);
  10. const surrogate = /[\ud800-\udfff]/g;
  11. const unicodeEscape = /(\\+)u\{([0-9a-fA-F]+)\}/g;
  12. function escape(code) {
  13. {
  14. let str = code.toString(16);
  15. while (str.length < 4) str = "0" + str;
  16. return "\\u" + str;
  17. }
  18. }
  19. function replacer(match, backslashes, code) {
  20. if (backslashes.length % 2 === 0) {
  21. return match;
  22. }
  23. const char = String.fromCodePoint(parseInt(code, 16));
  24. const escaped = backslashes.slice(0, -1) + escape(char.charCodeAt(0));
  25. return char.length === 1 ? escaped : escaped + escape(char.charCodeAt(1));
  26. }
  27. function replaceUnicodeEscapes(str) {
  28. return str.replace(unicodeEscape, replacer);
  29. }
  30. function getUnicodeEscape(str) {
  31. let match;
  32. while (match = unicodeEscape.exec(str)) {
  33. if (match[1].length % 2 === 0) continue;
  34. unicodeEscape.lastIndex = 0;
  35. return match[0];
  36. }
  37. return null;
  38. }
  39. return {
  40. name: "transform-unicode-escapes",
  41. manipulateOptions({
  42. generatorOpts
  43. }) {
  44. var _generatorOpts$jsescO, _generatorOpts$jsescO2;
  45. if (!generatorOpts.jsescOption) {
  46. generatorOpts.jsescOption = {};
  47. }
  48. (_generatorOpts$jsescO2 = (_generatorOpts$jsescO = generatorOpts.jsescOption).minimal) != null ? _generatorOpts$jsescO2 : _generatorOpts$jsescO.minimal = false;
  49. },
  50. visitor: {
  51. Identifier(path) {
  52. const {
  53. node,
  54. key
  55. } = path;
  56. const {
  57. name
  58. } = node;
  59. const replaced = name.replace(surrogate, c => {
  60. return `_u${c.charCodeAt(0).toString(16)}`;
  61. });
  62. if (name === replaced) return;
  63. const str = _core.types.inherits(_core.types.stringLiteral(name), node);
  64. if (key === "key") {
  65. path.replaceWith(str);
  66. return;
  67. }
  68. const {
  69. parentPath,
  70. scope
  71. } = path;
  72. if (parentPath.isMemberExpression({
  73. property: node
  74. }) || parentPath.isOptionalMemberExpression({
  75. property: node
  76. })) {
  77. parentPath.node.computed = true;
  78. path.replaceWith(str);
  79. return;
  80. }
  81. const binding = scope.getBinding(name);
  82. if (binding) {
  83. scope.rename(name, scope.generateUid(replaced));
  84. return;
  85. }
  86. throw path.buildCodeFrameError(`Can't reference '${name}' as a bare identifier`);
  87. },
  88. "StringLiteral|DirectiveLiteral"(path) {
  89. const {
  90. node
  91. } = path;
  92. const {
  93. extra
  94. } = node;
  95. if (extra != null && extra.raw) extra.raw = replaceUnicodeEscapes(extra.raw);
  96. },
  97. TemplateElement(path) {
  98. const {
  99. node,
  100. parentPath
  101. } = path;
  102. const {
  103. value
  104. } = node;
  105. const firstEscape = getUnicodeEscape(value.raw);
  106. if (!firstEscape) return;
  107. const grandParent = parentPath.parentPath;
  108. if (grandParent.isTaggedTemplateExpression()) {
  109. throw path.buildCodeFrameError(`Can't replace Unicode escape '${firstEscape}' inside tagged template literals. You can enable '@babel/plugin-transform-template-literals' to compile them to classic strings.`);
  110. }
  111. value.raw = replaceUnicodeEscapes(value.raw);
  112. }
  113. }
  114. };
  115. });
  116. //# sourceMappingURL=index.js.map