Repository: babel/babel (TypeScript)

Issue:
Error when parsing `eval` in `.d.ts`
This should not throw an error.

```ts
// @filename: /a.d.ts
declare global {
    export namespace ns {
        export function eval(): void;
        export function arguments(): void;
    }
}

declare function eval(): void;
declare function arguments(): void;

export {}
```
https://astexplorer.net/#/gist/edd804e82dbf0359133afe706554573d/3b3bd2dc894b4b12efcf40ef96e35b0d6d174cbe

I discovered it while updating our typescript testing repo.


Hints:
Oh! This is not a bug, this is a change introduced in ts 5.5.

Patch:
diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts
--- a/packages/babel-parser/src/plugins/typescript/index.ts
+++ b/packages/babel-parser/src/plugins/typescript/index.ts
@@ -3905,12 +3905,15 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
     }
 
     tsInAmbientContext<T>(cb: () => T): T {
-      const oldIsAmbientContext = this.state.isAmbientContext;
+      const { isAmbientContext: oldIsAmbientContext, strict: oldStrict } =
+        this.state;
       this.state.isAmbientContext = true;
+      this.state.strict = false;
       try {
         return cb();
       } finally {
         this.state.isAmbientContext = oldIsAmbientContext;
+        this.state.strict = oldStrict;
       }
     }
 


Test patch:
diff --git a/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/input.ts b/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/input.ts
new file mode 100644
--- /dev/null
+++ b/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/input.ts
@@ -0,0 +1,12 @@
+// @filename: /a.d.ts
+declare global {
+    export namespace ns {
+        export function eval(): void;
+        export function arguments(): void;
+    }
+}
+
+declare function eval(): void;
+declare function arguments(): void;
+
+export {}
diff --git a/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/options.json b/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/options.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/options.json
@@ -0,0 +1,4 @@
+{
+  "sourceType": "module",
+  "plugins": [["typescript", { "dts": true }]]
+}
diff --git a/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/output.json b/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/output.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/output.json
@@ -0,0 +1,170 @@
+{
+  "type": "File",
+  "start":0,"end":232,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":12,"column":9,"index":232}},
+  "program": {
+    "type": "Program",
+    "start":0,"end":232,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":12,"column":9,"index":232}},
+    "sourceType": "module",
+    "interpreter": null,
+    "body": [
+      {
+        "type": "TSModuleDeclaration",
+        "start":22,"end":153,"loc":{"start":{"line":2,"column":0,"index":22},"end":{"line":7,"column":1,"index":153}},
+        "global": true,
+        "id": {
+          "type": "Identifier",
+          "start":30,"end":36,"loc":{"start":{"line":2,"column":8,"index":30},"end":{"line":2,"column":14,"index":36},"identifierName":"global"},
+          "name": "global"
+        },
+        "body": {
+          "type": "TSModuleBlock",
+          "start":37,"end":153,"loc":{"start":{"line":2,"column":15,"index":37},"end":{"line":7,"column":1,"index":153}},
+          "body": [
+            {
+              "type": "ExportNamedDeclaration",
+              "start":43,"end":151,"loc":{"start":{"line":3,"column":4,"index":43},"end":{"line":6,"column":5,"index":151}},
+              "exportKind": "value",
+              "specifiers": [],
+              "source": null,
+              "declaration": {
+                "type": "TSModuleDeclaration",
+                "start":50,"end":151,"loc":{"start":{"line":3,"column":11,"index":50},"end":{"line":6,"column":5,"index":151}},
+                "id": {
+                  "type": "Identifier",
+                  "start":60,"end":62,"loc":{"start":{"line":3,"column":21,"index":60},"end":{"line":3,"column":23,"index":62},"identifierName":"ns"},
+                  "name": "ns"
+                },
+                "body": {
+                  "type": "TSModuleBlock",
+                  "start":63,"end":151,"loc":{"start":{"line":3,"column":24,"index":63},"end":{"line":6,"column":5,"index":151}},
+                  "body": [
+                    {
+                      "type": "ExportNamedDeclaration",
+                      "start":73,"end":102,"loc":{"start":{"line":4,"column":8,"index":73},"end":{"line":4,"column":37,"index":102}},
+                      "exportKind": "value",
+                      "specifiers": [],
+                      "source": null,
+                      "declaration": {
+                        "type": "TSDeclareFunction",
+                        "start":80,"end":102,"loc":{"start":{"line":4,"column":15,"index":80},"end":{"line":4,"column":37,"index":102}},
+                        "id": {
+                          "type": "Identifier",
+                          "start":89,"end":93,"loc":{"start":{"line":4,"column":24,"index":89},"end":{"line":4,"column":28,"index":93},"identifierName":"eval"},
+                          "name": "eval"
+                        },
+                        "generator": false,
+                        "async": false,
+                        "params": [],
+                        "returnType": {
+                          "type": "TSTypeAnnotation",
+                          "start":95,"end":101,"loc":{"start":{"line":4,"column":30,"index":95},"end":{"line":4,"column":36,"index":101}},
+                          "typeAnnotation": {
+                            "type": "TSVoidKeyword",
+                            "start":97,"end":101,"loc":{"start":{"line":4,"column":32,"index":97},"end":{"line":4,"column":36,"index":101}}
+                          }
+                        }
+                      }
+                    },
+                    {
+                      "type": "ExportNamedDeclaration",
+                      "start":111,"end":145,"loc":{"start":{"line":5,"column":8,"index":111},"end":{"line":5,"column":42,"index":145}},
+                      "exportKind": "value",
+                      "specifiers": [],
+                      "source": null,
+                      "declaration": {
+                        "type": "TSDeclareFunction",
+                        "start":118,"end":145,"loc":{"start":{"line":5,"column":15,"index":118},"end":{"line":5,"column":42,"index":145}},
+                        "id": {
+                          "type": "Identifier",
+                          "start":127,"end":136,"loc":{"start":{"line":5,"column":24,"index":127},"end":{"line":5,"column":33,"index":136},"identifierName":"arguments"},
+                          "name": "arguments"
+                        },
+                        "generator": false,
+                        "async": false,
+                        "params": [],
+                        "returnType": {
+                          "type": "TSTypeAnnotation",
+                          "start":138,"end":144,"loc":{"start":{"line":5,"column":35,"index":138},"end":{"line":5,"column":41,"index":144}},
+                          "typeAnnotation": {
+                            "type": "TSVoidKeyword",
+                            "start":140,"end":144,"loc":{"start":{"line":5,"column":37,"index":140},"end":{"line":5,"column":41,"index":144}}
+                          }
+                        }
+                      }
+                    }
+                  ]
+                }
+              }
+            }
+          ]
+        },
+        "declare": true,
+        "leadingComments": [
+          {
+            "type": "CommentLine",
+            "value": " @filename: /a.d.ts",
+            "start":0,"end":21,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":21,"index":21}}
+          }
+        ]
+      },
+      {
+        "type": "TSDeclareFunction",
+        "start":155,"end":185,"loc":{"start":{"line":9,"column":0,"index":155},"end":{"line":9,"column":30,"index":185}},
+        "declare": true,
+        "id": {
+          "type": "Identifier",
+          "start":172,"end":176,"loc":{"start":{"line":9,"column":17,"index":172},"end":{"line":9,"column":21,"index":176},"identifierName":"eval"},
+          "name": "eval"
+        },
+        "generator": false,
+        "async": false,
+        "params": [],
+        "returnType": {
+          "type": "TSTypeAnnotation",
+          "start":178,"end":184,"loc":{"start":{"line":9,"column":23,"index":178},"end":{"line":9,"column":29,"index":184}},
+          "typeAnnotation": {
+            "type": "TSVoidKeyword",
+            "start":180,"end":184,"loc":{"start":{"line":9,"column":25,"index":180},"end":{"line":9,"column":29,"index":184}}
+          }
+        }
+      },
+      {
+        "type": "TSDeclareFunction",
+        "start":186,"end":221,"loc":{"start":{"line":10,"column":0,"index":186},"end":{"line":10,"column":35,"index":221}},
+        "declare": true,
+        "id": {
+          "type": "Identifier",
+          "start":203,"end":212,"loc":{"start":{"line":10,"column":17,"index":203},"end":{"line":10,"column":26,"index":212},"identifierName":"arguments"},
+          "name": "arguments"
+        },
+        "generator": false,
+        "async": false,
+        "params": [],
+        "returnType": {
+          "type": "TSTypeAnnotation",
+          "start":214,"end":220,"loc":{"start":{"line":10,"column":28,"index":214},"end":{"line":10,"column":34,"index":220}},
+          "typeAnnotation": {
+            "type": "TSVoidKeyword",
+            "start":216,"end":220,"loc":{"start":{"line":10,"column":30,"index":216},"end":{"line":10,"column":34,"index":220}}
+          }
+        }
+      },
+      {
+        "type": "ExportNamedDeclaration",
+        "start":223,"end":232,"loc":{"start":{"line":12,"column":0,"index":223},"end":{"line":12,"column":9,"index":232}},
+        "exportKind": "value",
+        "specifiers": [],
+        "source": null,
+        "declaration": null
+      }
+    ],
+    "directives": []
+  },
+  "comments": [
+    {
+      "type": "CommentLine",
+      "value": " @filename: /a.d.ts",
+      "start":0,"end":21,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":21,"index":21}}
+    }
+  ]
+}
diff --git a/packages/babel-parser/test/fixtures/typescript/declare/eval/input.ts b/packages/babel-parser/test/fixtures/typescript/declare/eval/input.ts
new file mode 100644
--- /dev/null
+++ b/packages/babel-parser/test/fixtures/typescript/declare/eval/input.ts
@@ -0,0 +1,11 @@
+declare global {
+    export namespace ns {
+        export function eval(): void;
+        export function arguments(): void;
+    }
+}
+
+declare function eval(): void;
+declare function arguments(): void;
+
+export {}
diff --git a/packages/babel-parser/test/fixtures/typescript/declare/eval/output.json b/packages/babel-parser/test/fixtures/typescript/declare/eval/output.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-parser/test/fixtures/typescript/declare/eval/output.json
@@ -0,0 +1,156 @@
+{
+  "type": "File",
+  "start":0,"end":210,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":11,"column":9,"index":210}},
+  "program": {
+    "type": "Program",
+    "start":0,"end":210,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":11,"column":9,"index":210}},
+    "sourceType": "module",
+    "interpreter": null,
+    "body": [
+      {
+        "type": "TSModuleDeclaration",
+        "start":0,"end":131,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":1,"index":131}},
+        "global": true,
+        "id": {
+          "type": "Identifier",
+          "start":8,"end":14,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":14,"index":14},"identifierName":"global"},
+          "name": "global"
+        },
+        "body": {
+          "type": "TSModuleBlock",
+          "start":15,"end":131,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":6,"column":1,"index":131}},
+          "body": [
+            {
+              "type": "ExportNamedDeclaration",
+              "start":21,"end":129,"loc":{"start":{"line":2,"column":4,"index":21},"end":{"line":5,"column":5,"index":129}},
+              "exportKind": "value",
+              "specifiers": [],
+              "source": null,
+              "declaration": {
+                "type": "TSModuleDeclaration",
+                "start":28,"end":129,"loc":{"start":{"line":2,"column":11,"index":28},"end":{"line":5,"column":5,"index":129}},
+                "id": {
+                  "type": "Identifier",
+                  "start":38,"end":40,"loc":{"start":{"line":2,"column":21,"index":38},"end":{"line":2,"column":23,"index":40},"identifierName":"ns"},
+                  "name": "ns"
+                },
+                "body": {
+                  "type": "TSModuleBlock",
+                  "start":41,"end":129,"loc":{"start":{"line":2,"column":24,"index":41},"end":{"line":5,"column":5,"index":129}},
+                  "body": [
+                    {
+                      "type": "ExportNamedDeclaration",
+                      "start":51,"end":80,"loc":{"start":{"line":3,"column":8,"index":51},"end":{"line":3,"column":37,"index":80}},
+                      "exportKind": "value",
+                      "specifiers": [],
+                      "source": null,
+                      "declaration": {
+                        "type": "TSDeclareFunction",
+                        "start":58,"end":80,"loc":{"start":{"line":3,"column":15,"index":58},"end":{"line":3,"column":37,"index":80}},
+                        "id": {
+                          "type": "Identifier",
+                          "start":67,"end":71,"loc":{"start":{"line":3,"column":24,"index":67},"end":{"line":3,"column":28,"index":71},"identifierName":"eval"},
+                          "name": "eval"
+                        },
+                        "generator": false,
+                        "async": false,
+                        "params": [],
+                        "returnType": {
+                          "type": "TSTypeAnnotation",
+                          "start":73,"end":79,"loc":{"start":{"line":3,"column":30,"index":73},"end":{"line":3,"column":36,"index":79}},
+                          "typeAnnotation": {
+                            "type": "TSVoidKeyword",
+                            "start":75,"end":79,"loc":{"start":{"line":3,"column":32,"index":75},"end":{"line":3,"column":36,"index":79}}
+                          }
+                        }
+                      }
+                    },
+                    {
+                      "type": "ExportNamedDeclaration",
+                      "start":89,"end":123,"loc":{"start":{"line":4,"column":8,"index":89},"end":{"line":4,"column":42,"index":123}},
+                      "exportKind": "value",
+                      "specifiers": [],
+                      "source": null,
+                      "declaration": {
+                        "type": "TSDeclareFunction",
+                        "start":96,"end":123,"loc":{"start":{"line":4,"column":15,"index":96},"end":{"line":4,"column":42,"index":123}},
+                        "id": {
+                          "type": "Identifier",
+                          "start":105,"end":114,"loc":{"start":{"line":4,"column":24,"index":105},"end":{"line":4,"column":33,"index":114},"identifierName":"arguments"},
+                          "name": "arguments"
+                        },
+                        "generator": false,
+                        "async": false,
+                        "params": [],
+                        "returnType": {
+                          "type": "TSTypeAnnotation",
+                          "start":116,"end":122,"loc":{"start":{"line":4,"column":35,"index":116},"end":{"line":4,"column":41,"index":122}},
+                          "typeAnnotation": {
+                            "type": "TSVoidKeyword",
+                            "start":118,"end":122,"loc":{"start":{"line":4,"column":37,"index":118},"end":{"line":4,"column":41,"index":122}}
+                          }
+                        }
+                      }
+                    }
+                  ]
+                }
+              }
+            }
+          ]
+        },
+        "declare": true
+      },
+      {
+        "type": "TSDeclareFunction",
+        "start":133,"end":163,"loc":{"start":{"line":8,"column":0,"index":133},"end":{"line":8,"column":30,"index":163}},
+        "declare": true,
+        "id": {
+          "type": "Identifier",
+          "start":150,"end":154,"loc":{"start":{"line":8,"column":17,"index":150},"end":{"line":8,"column":21,"index":154},"identifierName":"eval"},
+          "name": "eval"
+        },
+        "generator": false,
+        "async": false,
+        "params": [],
+        "returnType": {
+          "type": "TSTypeAnnotation",
+          "start":156,"end":162,"loc":{"start":{"line":8,"column":23,"index":156},"end":{"line":8,"column":29,"index":162}},
+          "typeAnnotation": {
+            "type": "TSVoidKeyword",
+            "start":158,"end":162,"loc":{"start":{"line":8,"column":25,"index":158},"end":{"line":8,"column":29,"index":162}}
+          }
+        }
+      },
+      {
+        "type": "TSDeclareFunction",
+        "start":164,"end":199,"loc":{"start":{"line":9,"column":0,"index":164},"end":{"line":9,"column":35,"index":199}},
+        "declare": true,
+        "id": {
+          "type": "Identifier",
+          "start":181,"end":190,"loc":{"start":{"line":9,"column":17,"index":181},"end":{"line":9,"column":26,"index":190},"identifierName":"arguments"},
+          "name": "arguments"
+        },
+        "generator": false,
+        "async": false,
+        "params": [],
+        "returnType": {
+          "type": "TSTypeAnnotation",
+          "start":192,"end":198,"loc":{"start":{"line":9,"column":28,"index":192},"end":{"line":9,"column":34,"index":198}},
+          "typeAnnotation": {
+            "type": "TSVoidKeyword",
+            "start":194,"end":198,"loc":{"start":{"line":9,"column":30,"index":194},"end":{"line":9,"column":34,"index":198}}
+          }
+        }
+      },
+      {
+        "type": "ExportNamedDeclaration",
+        "start":201,"end":210,"loc":{"start":{"line":11,"column":0,"index":201},"end":{"line":11,"column":9,"index":210}},
+        "exportKind": "value",
+        "specifiers": [],
+        "source": null,
+        "declaration": null
+      }
+    ],
+    "directives": []
+  }
+}