Debian Codegraph @

packages/babel-plugin-transform-unicode-escapes/src/index.ts

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