Skip to content

fix(parser): reject generators in ambient contexts and overload signatures (TS1221/TS1222)#22848

Merged
graphite-app[bot] merged 1 commit into
mainfrom
fix/generators-in-ambient
May 31, 2026
Merged

fix(parser): reject generators in ambient contexts and overload signatures (TS1221/TS1222)#22848
graphite-app[bot] merged 1 commit into
mainfrom
fix/generators-in-ambient

Conversation

@Boshen

@Boshen Boshen commented May 30, 2026

Copy link
Copy Markdown
Member

A generator (function* / *method) is not allowed in an ambient context
(TS1221), and a generator overload signature with no body is not allowed
(TS1222). Mirrors tsc's checkGrammarForGenerator:

declare function* f(): any;              // now TS1221
declare namespace N { function* g(); }   // now TS1221
declare class C { *m(); }                // now TS1221
function* f(): any;                       // overload → now TS1222
function* f() { yield 1; }

function* f() { yield 1; }               // normal generator → still valid

The check lives in parse_function next to the TS1183 implementation-in-ambient
check and keys off ctx.has_ambient() / body.is_none(). Class methods are
covered too (class.rs does not check generators, so there is no duplicate
diagnostic).

Also fixes a latent bug: parse_ts_declare_function hard-coded generator: false
and never consumed the *, so declare function* f() silently dropped the
generator flag. It now eats the * and threads the flag through (which is what
lets TS1221 fire there).

parser_typescript +7 negative-pass (generatorInAmbientContext1-4, generatorOverloads1-3);
positive/AST stay 100%.

@github-actions github-actions Bot added the A-parser Area - Parser label May 30, 2026
@Boshen Boshen marked this pull request as ready for review May 30, 2026 05:59

Boshen commented May 30, 2026

Copy link
Copy Markdown
Member Author

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0f5040b36a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/oxc_parser/src/js/function.rs
@codspeed-hq

codspeed-hq Bot commented May 30, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 57 untouched benchmarks


Comparing fix/generators-in-ambient (93ac8fb) with main (cc60d8d)

Open in CodSpeed

@Boshen Boshen force-pushed the fix/generators-in-ambient branch from 0f5040b to f685a21 Compare May 30, 2026 07:30
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label May 30, 2026

Boshen commented May 30, 2026

Copy link
Copy Markdown
Member Author

Merge activity

  • May 30, 7:33 AM UTC: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • May 30, 7:49 AM UTC: Boshen added this pull request to the Graphite merge queue.
  • May 30, 7:55 AM UTC: The Graphite merge queue couldn't merge this PR because it was not satisfying all requirements (Failed CI: 'Test NAPI Compiler').
  • May 31, 8:02 AM UTC: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • May 31, 8:02 AM UTC: Boshen added this pull request to the Graphite merge queue.
  • May 31, 8:07 AM UTC: Merged by the Graphite merge queue.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f685a21eec

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/oxc_parser/src/ts/statement.rs
graphite-app Bot pushed a commit that referenced this pull request May 30, 2026
…tures (TS1221/TS1222) (#22848)

A generator (`function*` / `*method`) is not allowed in an ambient context
(TS1221), and a generator overload signature with no body is not allowed
(TS1222). Mirrors tsc's `checkGrammarForGenerator`:

    declare function* f(): any;              // now TS1221
    declare namespace N { function* g(); }   // now TS1221
    declare class C { *m(); }                // now TS1221
    function* f(): any;                       // overload → now TS1222
    function* f() { yield 1; }

    function* f() { yield 1; }               // normal generator → still valid

The check lives in `parse_function` next to the TS1183 implementation-in-ambient
check and keys off `ctx.has_ambient()` / `body.is_none()`. Class methods are
covered too (`class.rs` does not check generators, so there is no duplicate
diagnostic).

Also fixes a latent bug: `parse_ts_declare_function` hard-coded `generator: false`
and never consumed the `*`, so `declare function* f()` silently dropped the
generator flag. It now eats the `*` and threads the flag through (which is what
lets TS1221 fire there).

parser_typescript +7 negative-pass (generatorInAmbientContext1-4, generatorOverloads1-3);
positive/AST stay 100%.
@graphite-app graphite-app Bot force-pushed the fix/generators-in-ambient branch from f685a21 to 5d7fa1a Compare May 30, 2026 07:49
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label May 30, 2026
@Boshen Boshen force-pushed the fix/generators-in-ambient branch 2 times, most recently from 8ee95fc to 93ac8fb Compare May 31, 2026 07:40
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label May 31, 2026
…tures (TS1221/TS1222) (#22848)

A generator (`function*` / `*method`) is not allowed in an ambient context
(TS1221), and a generator overload signature with no body is not allowed
(TS1222). Mirrors tsc's `checkGrammarForGenerator`:

    declare function* f(): any;              // now TS1221
    declare namespace N { function* g(); }   // now TS1221
    declare class C { *m(); }                // now TS1221
    function* f(): any;                       // overload → now TS1222
    function* f() { yield 1; }

    function* f() { yield 1; }               // normal generator → still valid

The check lives in `parse_function` next to the TS1183 implementation-in-ambient
check and keys off `ctx.has_ambient()` / `body.is_none()`. Class methods are
covered too (`class.rs` does not check generators, so there is no duplicate
diagnostic).

Also fixes a latent bug: `parse_ts_declare_function` hard-coded `generator: false`
and never consumed the `*`, so `declare function* f()` silently dropped the
generator flag. It now eats the `*` and threads the flag through (which is what
lets TS1221 fire there).

parser_typescript +7 negative-pass (generatorInAmbientContext1-4, generatorOverloads1-3);
positive/AST stay 100%.
@graphite-app graphite-app Bot force-pushed the fix/generators-in-ambient branch from 93ac8fb to c360fb6 Compare May 31, 2026 08:02
@graphite-app graphite-app Bot merged commit c360fb6 into main May 31, 2026
30 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label May 31, 2026
@graphite-app graphite-app Bot deleted the fix/generators-in-ambient branch May 31, 2026 08:07
camc314 pushed a commit that referenced this pull request Jun 1, 2026
### 🚀 Features

- 9c71f2e ast, codegen, formatter: Add `WithClauseKeyword::as_str`
helper and use it (#22791) (camc314)

### 🐛 Bug Fixes

- cf5769c parser: Reject TypeScript declarations in single-statement
context (#22827) (Boshen)
- c360fb6 parser: Reject generators in ambient contexts and overload
signatures (TS1221/TS1222) (#22848) (Boshen)
- cc60d8d parser: Reject invalid index signature parameter types
(TS1268/TS1337) (#22852) (Boshen)
- 3d13e29 parser: Reject `declare` in an already-ambient context
(TS1038) (#22850) (Boshen)
- 5152854 parser: Reject statements in ambient contexts (TS1036)
(#22849) (Boshen)
- 4f9afc5 parser: Reject `export as namespace` inside a namespace body
(TS1316) (#22846) (Boshen)
- 2eafea6 parser: Reject function implementations in ambient contexts
(TS1183) (#22845) (Boshen)
- c645615 parser: Reject incompatible class member modifiers (#22843)
(Boshen)
- 276b78b parser: Reject module-referencing imports/exports in a
namespace body (#22836) (Boshen)
- 842ed1c parser: Parse `class implements` with `implements` as the
class name (#22801) (Boshen)

### ⚡ Performance

- 7e3a567 parser: Reuse cached token kind in delimited-list loops
(#22841) (Boshen)
- 9e741c2 parser: Use peek_token instead of lookahead on the modifier
path (#22842) (Boshen)
- 9e496a7 semantic: Defer declare lookup for empty accessors (#22810)
(camc314)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-parser Area - Parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant