source: trip-planner-front/node_modules/@babel/plugin-transform-unicode-escapes/lib/index.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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