Skip to content

fix(parser): disallow optional chaining on import.defer - #4794

Open
BhariGowda wants to merge 2 commits into
microsoft:mainfrom
BhariGowda:fix/import-defer-optional-call
Open

fix(parser): disallow optional chaining on import.defer#4794
BhariGowda wants to merge 2 commits into
microsoft:mainfrom
BhariGowda:fix/import-defer-optional-call

Conversation

@BhariGowda

Copy link
Copy Markdown

Fixes microsoft/TypeScript#63679

Problem

import.defer?.('x') produces no error in TypeScript, but all
major parsers (Oxc, SWC, Babel, Acorn) reject it.

Fix

  • Added diagnostic TS18062: 'import.defer' does not support optional invocation
  • Added check in parser.go for QuestionDotToken followed by '(' or '<'
    immediately after import.defer is parsed

Test

Before:
import.defer?.('x'); // no error ❌

After:
import.defer?.('x');
// error TS18062: 'import.defer' does not support optional invocation ✅

Note

Fix was originally implemented in microsoft/TypeScript#63690
and ported here per maintainer guidance.

- Add diagnostic TS18062: 'import.defer' does not support optional invocation

- Report error when '?.' followed by '(' or '<' appears after 'import.defer'

Fixes microsoft#4789
Copilot AI review requested due to automatic review settings July 30, 2026 05:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds parser enforcement matching upstream TypeScript behavior by rejecting optional invocation of import.defer.

Changes:

  • Detects optional and generic optional invocation forms.
  • Adds diagnostic TS18062 and lookup mapping.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.

File Description
internal/parser/parser.go Reports invalid optional import.defer invocation.
internal/diagnostics/diagnostics_generated.go Defines and maps TS18062.
Files not reviewed (1)
  • internal/diagnostics/diagnostics_generated.go: Generated file

Comment thread internal/parser/parser.go
Comment on lines +5192 to +5195
if p.token == ast.KindQuestionDotToken && p.lookAhead(func(p *Parser) bool {
p.nextToken()
return p.token == ast.KindOpenParenToken || p.token == ast.KindLessThanToken
}) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added conformance test covering both ?.() and ?.() forms,
with a valid import.defer('./x') control case. Baselines committed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should not allow import.defer?.('x')

2 participants