index.js 3.53 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _helperPluginUtils = require("@babel/helper-plugin-utils");

var _core = require("@babel/core");

var _default = (0, _helperPluginUtils.declare)(api => {
  api.assertVersion(7);
  const surrogate = /[\ud800-\udfff]/g;
  const unicodeEscape = /(\\+)u\{([0-9a-fA-F]+)\}/g;

  function escape(code) {
    let str = code.toString(16);

    while (str.length < 4) str = "0" + str;

    return "\\u" + str;
  }

  function replacer(match, backslashes, code) {
    if (backslashes.length % 2 === 0) {
      return match;
    }

    const char = String.fromCodePoint(parseInt(code, 16));
    const escaped = backslashes.slice(0, -1) + escape(char.charCodeAt(0));
    return char.length === 1 ? escaped : escaped + escape(char.charCodeAt(1));
  }

  function replaceUnicodeEscapes(str) {
    return str.replace(unicodeEscape, replacer);
  }

  function getUnicodeEscape(str) {
    let match;

    while (match = unicodeEscape.exec(str)) {
      if (match[1].length % 2 === 0) continue;
      unicodeEscape.lastIndex = 0;
      return match[0];
    }

    return null;
  }

  return {
    name: "transform-unicode-escapes",

    manipulateOptions({
      generatorOpts
    }) {
      var _generatorOpts$jsescO, _generatorOpts$jsescO2;

      if (!generatorOpts.jsescOption) {
        generatorOpts.jsescOption = {};
      }

      (_generatorOpts$jsescO2 = (_generatorOpts$jsescO = generatorOpts.jsescOption).minimal) != null ? _generatorOpts$jsescO2 : _generatorOpts$jsescO.minimal = false;
    },

    visitor: {
      Identifier(path) {
        const {
          node,
          key
        } = path;
        const {
          name
        } = node;
        const replaced = name.replace(surrogate, c => {
          return `_u${c.charCodeAt(0).toString(16)}`;
        });
        if (name === replaced) return;

        const str = _core.types.inherits(_core.types.stringLiteral(name), node);

        if (key === "key") {
          path.replaceWith(str);
          return;
        }

        const {
          parentPath,
          scope
        } = path;

        if (parentPath.isMemberExpression({
          property: node
        }) || parentPath.isOptionalMemberExpression({
          property: node
        })) {
          parentPath.node.computed = true;
          path.replaceWith(str);
          return;
        }

        const binding = scope.getBinding(name);

        if (binding) {
          scope.rename(name, scope.generateUid(replaced));
          return;
        }

        throw path.buildCodeFrameError(`Can't reference '${name}' as a bare identifier`);
      },

      "StringLiteral|DirectiveLiteral"(path) {
        const {
          node
        } = path;
        const {
          extra
        } = node;
        if (extra != null && extra.raw) extra.raw = replaceUnicodeEscapes(extra.raw);
      },

      TemplateElement(path) {
        const {
          node,
          parentPath
        } = path;
        const {
          value
        } = node;
        const firstEscape = getUnicodeEscape(value.raw);
        if (!firstEscape) return;
        const grandParent = parentPath.parentPath;

        if (grandParent.isTaggedTemplateExpression()) {
          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.`);
        }

        value.raw = replaceUnicodeEscapes(value.raw);
      }

    }
  };
});

exports.default = _default;