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