Skip to content

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

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

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

Conversation

@BhariGowda

Copy link
Copy Markdown

Fixes #63679

Problem

import.defer?.('x') was not producing an error in TypeScript,
even though all major parsers (Oxc, SWC, Babel, Acorn) reject it.

Fix

  • Added diagnostic TS18062: 'import.defer' does not support optional invocation
  • Added check in parser.ts for QuestionDotToken immediately
    after import.defer is parsed
  • Added conformance test with baselines

Test

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

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

)

- Add diagnostic TS18062: 'import.defer' does not support optional invocation
- Report error when '?.' follows 'import.defer' in parser
- Add conformance test importDeferOptionalCall.ts with baselines

Fixes microsoft#63679
Copilot AI review requested due to automatic review settings July 29, 2026 11:56
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Jul 29, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug labels Jul 29, 2026

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

This PR tightens the parser’s handling of the import.defer meta-property by rejecting optional chaining invocation syntax (import.defer?.(...)) and adds a conformance test/baselines to lock in the new diagnostic behavior.

Changes:

  • Add a new diagnostic (TS18062) for optional invocation on import.defer.
  • Update the parser to detect ?. immediately after parsing import.defer and report TS18062.
  • Add a conformance test and reference baselines for the new error.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/compiler/parser.ts Adds a parser check to error on QuestionDotToken following import.defer.
src/compiler/diagnosticMessages.json Introduces TS18062 message text and code assignment.
tests/cases/conformance/importDefer/importDeferOptionalCall.ts New conformance test for import.defer?.('x').
tests/baselines/reference/importDeferOptionalCall.errors.txt Baseline showing TS18062 (and current additional TS2307).
tests/baselines/reference/importDeferOptionalCall.types Type baseline for the test input.
tests/baselines/reference/importDeferOptionalCall.symbols Symbols baseline for the test input.
tests/baselines/reference/importDeferOptionalCall.js JS emit baseline for the test input.

// @target: es2015
// @module: esnext
// @filename: b.ts
import.defer?.('x');

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.

Fixed in the second commit added x.ts as a separate filename
section and used a relative specifier './x' to avoid TS2307 noise.

Comment thread src/compiler/parser.ts
Comment on lines 5947 to 5950

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.

Fixed in the second commit narrowed the check to only fire when
'?.' is followed by '(' or '<' (invocation only), so import.defer?.prop
and import.defer?.[0] are not affected.

@BhariGowda

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@MartinJohns

Copy link
Copy Markdown
Contributor

@BhariGowda You implemented the change in the wrong repository. See: https://github.com/microsoft/TypeScript/blob/main/CONTRIBUTING.md

@BhariGowda

Copy link
Copy Markdown
Author

Thanks for the heads up! I've filed microsoft/typescript-go#4789
and will port the fix there. Happy to keep this PR open or close
it whatever the team prefers.

@BhariGowda BhariGowda closed this Jul 29, 2026
@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Jul 29, 2026
@BhariGowda BhariGowda reopened this Jul 29, 2026
@github-project-automation github-project-automation Bot moved this from Done to Not started in PR Backlog Jul 29, 2026
@BhariGowda

Copy link
Copy Markdown
Author

Reopening microsoft/typescript-go#4789 was closed by a maintainer
who indicated the original issue #63679 is the
correct tracking location. Happy to proceed here or wait for
maintainer direction.

@MartinJohns

MartinJohns commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Issues are tracked here. Pull requests are done in the TypeScript-Go repository, because it contains the new codebase.

TypeScript 7.0 is no longer written in TypeScript / JavaScript.

@BhariGowda

Copy link
Copy Markdown
Author

Understood, closing this and will submit the fix to microsoft/typescript-go. Thanks for the clarification!

@BhariGowda BhariGowda closed this Jul 29, 2026
@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

3 participants