Repository: babel/babel (TypeScript)

Issue:
[Bug]: Missing registerDeclaration in plugin-transform-typescript
### 💻

- [X] Would you like to work on a fix?

### How are you using Babel?

Programmatic API (`babel.transform`, `babel.parse`)

### Input code

```
import foo = bar
```

### Configuration file name

_No response_

### Configuration

_No response_

### Current and expected behavior

plugin-transform-typescript fails to register the declaration at

```
        path.replaceWith(
          t.variableDeclaration("var", [
            t.variableDeclarator(
              path.node.id,
              entityNameToExpr(path.node.moduleReference),
            ),
          ]),
        );
```
in packages/babel-plugin-transform-typescript/src/index.ts.

### Environment

@babel/plugin-transform-typescript: 7.20.13


### Possible solution

Insert
```
path.scope.registerDeclaration(path);
```
after this line:
https://github.com/babel/babel/blob/6e8ce9da64dbb2a9a572bef9ef9450cbd038aba3/packages/babel-plugin-transform-typescript/src/index.ts#L594

### Additional context

_No response_


Hints:
Hey @amoeller! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant [Slack community](https://babeljs.slack.com) that typically always has someone willing to help. You can sign-up [here](https://slack.babeljs.io/) for an invite.
Thank you! I'm assigning this issue to you since you selected "Would you like to work on a fix?"

If it is the first time that you contribute to Babel, follow these steps: (you need to have `make` and `yarn` available on your machine)
1. Fork the repo
1. Run `git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel`
1. Run `yarn && make bootstrap`
1. Wait :hourglass_flowing_sand: 
1. Run `make watch` (or `make build` whenever you change a file)
1. Add a test in https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-typescript/test. Testing this is a bit hard, maybe we could create a test with a custom `plugin.js` (like https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-typescript/test/fixtures/regression/10162) which logs all the registered declaration on `Program:exit`, and with the `  "validateLogs": true` option enabled.
1. Update the code!
1. `yarn jest transform-typescript` to run the tests
    - If some test outputs don't match but the new results are correct, you can delete the bad `output.js` files and run the tests again
    - If you prefer, you can run `OVERWRITE=true yarn jest [name-of-the-package-to-test]` and they will be automatically updated.
1. If it is working, run `make test` to run all the tests
1. Run `git push` and open a PR!

Feel free to ask here if you need any other help :)

Patch:
diff --git a/packages/babel-plugin-transform-typescript/src/index.ts b/packages/babel-plugin-transform-typescript/src/index.ts
--- a/packages/babel-plugin-transform-typescript/src/index.ts
+++ b/packages/babel-plugin-transform-typescript/src/index.ts
@@ -592,6 +592,7 @@ export default declare((api, opts: Options) => {
             ),
           ]),
         );
+        path.scope.registerDeclaration(path);
       },
 
       TSExportAssignment(path) {


Test patch:
diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/input.ts
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/input.ts
@@ -0,0 +1 @@
+import foo = bar
diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/options.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/options.json
@@ -0,0 +1,3 @@
+{
+  "plugins": ["transform-typescript", "./plugin"]
+}
diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/output.mjs
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/output.mjs
@@ -0,0 +1 @@
+var foo = bar;
diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/plugin.js b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/plugin.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=-declaration/plugin.js
@@ -0,0 +1,16 @@
+module.exports = function({ types: t }) {
+  return {
+    visitor: {
+      Identifier(path) {
+        if (path.node.name === "foo") {
+          expect(path.scope.hasBinding(path.node.name)).toBe(true);
+        }
+      },
+      Program: {
+        exit() {
+          expect.hasAssertions();
+        }
+      }
+    }
+  };
+};