Fix module name mangling to avoid double escaping.

PiperOrigin-RevId: 264290453
diff --git a/internal/tsc_wrapped/compiler_host.ts b/internal/tsc_wrapped/compiler_host.ts
index 9cae018..4aac685 100644
--- a/internal/tsc_wrapped/compiler_host.ts
+++ b/internal/tsc_wrapped/compiler_host.ts
@@ -292,8 +292,7 @@
     const escape = (c: string) => {
       return '$' + c.charCodeAt(0).toString(16);
     };
-    const moduleName = importPath.replace(/^[^a-zA-Z_/]/, escape)
-                           .replace(/[^a-zA-Z_0-9_/]/g, escape)
+    const moduleName = importPath.replace(/^[0-9]|[^a-zA-Z_0-9_/]/g, escape)
                            .replace(/\//g, '.');
     return moduleName;
   }
diff --git a/internal/tsc_wrapped/compiler_host_test.ts b/internal/tsc_wrapped/compiler_host_test.ts
index eaa0ac4..6eab46e 100644
--- a/internal/tsc_wrapped/compiler_host_test.ts
+++ b/internal/tsc_wrapped/compiler_host_test.ts
@@ -58,5 +58,24 @@
                     createTsModule('path/to/package/root_dir/index.ts')))
              .toBe('my_lib');
        });
+
+    describe('#pathToModuleName', () => {
+      it('should escape non-identifier characters', () => {
+        expect(defaultHost.pathToModuleName('context', '$-!@'))
+            .toBe('$24$2d$21$40');
+      });
+
+      it('should escape leading numbers', () => {
+        expect(defaultHost.pathToModuleName('context', '1234')).toBe('$31234');
+      });
+
+      it('should transform slashes to dots', () => {
+        expect(defaultHost.pathToModuleName('context', 'a/b')).toBe('a.b');
+      });
+
+      it('should not escape valid identifers', () => {
+        expect(defaultHost.pathToModuleName('context', 'a1/b2')).toBe('a1.b2');
+      });
+    });
   });
 });
diff --git a/internal/tsc_wrapped/tsc_wrapped_test.ts b/internal/tsc_wrapped/tsc_wrapped_test.ts
index 3f77c07..59d725b 100644
--- a/internal/tsc_wrapped/tsc_wrapped_test.ts
+++ b/internal/tsc_wrapped/tsc_wrapped_test.ts
@@ -190,6 +190,7 @@
         expectPath('', 'some|123').toBe('google3.some$7c123');
         expectPath('', '1some|').toBe('google3.1some$7c');
         expectPath('', 'bar/foo.bam.ts').toBe('google3.bar.foo$2ebam');
+        expectPath('', '-foo-').toBe('google3.$2dfoo$2d');
         // Underscore is unmodified, because it is common in google3 paths.
         expectPath('', 'foo_bar').toBe('google3.foo_bar');
       });