Commit 2743ebe
committed
fix(parser): reject function implementations in ambient contexts (TS1183)
A function declaration with a body is an implementation, which is not allowed in
an ambient context. oxc already emitted TS1183 ("An implementation cannot be
declared in ambient contexts") when the function carried its own `declare`
modifier, but missed functions that inherit the ambient context from an enclosing
`declare module` / `declare namespace` or a `.d.ts` file:
declare module "m" { function f() {} } // now TS1183
declare namespace N { function f() {} } // now TS1183
// foo.d.ts
function foo(): any {} // now TS1183
Bodyless signatures stay valid (`declare module "m" { function f(): void; }`,
`export declare function f(): void;`).
The check now keys solely off `ctx.has_ambient()` instead of the function's own
`declare` modifier, scoped to function declarations. Class methods are excluded
because they are already validated in `check_method_definition`, which avoids a
duplicate diagnostic. This matches `typescript-go`'s
`checkGrammarStatementInAmbientContext` / function-like body check, and babel.
The fast path is unchanged: `has_ambient()` is a cheap bitflag test that
short-circuits for all non-ambient code.
Two linter rule fixtures relied on the old leniency and are updated to valid TS:
`no_invalid_void_type` moves a function implementation out of a `declare module`
into a non-ambient `module`, and `check_tag_names` replaces a `.d.ts` function
body with a `declare namespace` scope.
parser_babel +3 and parser_typescript +1 negative-pass; positive/AST stay 100%.1 parent c645615 commit 2743ebe
5 files changed
Lines changed: 53 additions & 14 deletions
File tree
- crates
- oxc_linter/src/rules
- jsdoc
- typescript
- oxc_parser/src/js
- tasks/coverage/snapshots
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1182 | 1182 | | |
1183 | 1183 | | |
1184 | 1184 | | |
1185 | | - | |
| 1185 | + | |
1186 | 1186 | | |
1187 | | - | |
| 1187 | + | |
1188 | 1188 | | |
1189 | 1189 | | |
1190 | 1190 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
693 | 693 | | |
694 | 694 | | |
695 | 695 | | |
696 | | - | |
| 696 | + | |
697 | 697 | | |
698 | 698 | | |
699 | 699 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
286 | 286 | | |
287 | 287 | | |
288 | 288 | | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
289 | 294 | | |
290 | | - | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
291 | 301 | | |
292 | 302 | | |
293 | 303 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | 17 | | |
22 | 18 | | |
23 | | - | |
24 | | - | |
25 | 19 | | |
26 | 20 | | |
27 | 21 | | |
| |||
13492 | 13486 | | |
13493 | 13487 | | |
13494 | 13488 | | |
| 13489 | + | |
| 13490 | + | |
| 13491 | + | |
| 13492 | + | |
| 13493 | + | |
| 13494 | + | |
| 13495 | + | |
| 13496 | + | |
13495 | 13497 | | |
13496 | 13498 | | |
13497 | 13499 | | |
| |||
13500 | 13502 | | |
13501 | 13503 | | |
13502 | 13504 | | |
| 13505 | + | |
| 13506 | + | |
| 13507 | + | |
| 13508 | + | |
| 13509 | + | |
| 13510 | + | |
| 13511 | + | |
| 13512 | + | |
| 13513 | + | |
| 13514 | + | |
| 13515 | + | |
| 13516 | + | |
| 13517 | + | |
| 13518 | + | |
13503 | 13519 | | |
13504 | 13520 | | |
13505 | 13521 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
1782 | 1782 | | |
1783 | 1783 | | |
1784 | 1784 | | |
1785 | | - | |
1786 | | - | |
1787 | 1785 | | |
1788 | 1786 | | |
1789 | 1787 | | |
| |||
13846 | 13844 | | |
13847 | 13845 | | |
13848 | 13846 | | |
| 13847 | + | |
| 13848 | + | |
| 13849 | + | |
| 13850 | + | |
| 13851 | + | |
| 13852 | + | |
| 13853 | + | |
| 13854 | + | |
13849 | 13855 | | |
13850 | 13856 | | |
13851 | 13857 | | |
| |||
25260 | 25266 | | |
25261 | 25267 | | |
25262 | 25268 | | |
| 25269 | + | |
| 25270 | + | |
| 25271 | + | |
| 25272 | + | |
| 25273 | + | |
| 25274 | + | |
| 25275 | + | |
25263 | 25276 | | |
25264 | 25277 | | |
25265 | 25278 | | |
| |||
0 commit comments