Skip to content

Fix: disallow optional call chaining on import.defer expressions (#63679) - #64199

Open
Vaibhav Srivastava (vaibhavsrv) wants to merge 1 commit into
microsoft:mainfrom
vaibhavsrv:fix-import-defer-optional-chain
Open

Fix: disallow optional call chaining on import.defer expressions (#63679)#64199
Vaibhav Srivastava (vaibhavsrv) wants to merge 1 commit into
microsoft:mainfrom
vaibhavsrv:fix-import-defer-optional-chain

Conversation

@vaibhavsrv

Copy link
Copy Markdown

Fixes #63679.

Problem

import.defer (part of TC39 Stage 3 Deferred Import Evaluation proposal) is a syntactic call construct, not a function reference. Previously, optional call chaining syntax like import.defer?.('x') was accepted without grammar errors during parsing and type checking.

Solution

  • Updated checkGrammarImportCallExpression to check for non-nil QuestionDotToken (?.) and emit error TS1326 (This use of 'import' is invalid...).
  • Updated parser flag setting for import.defer when followed by ?. to ensure dynamic import statements with optional chaining are visited and grammar-checked.
  • Added test case importDeferOptionalChain.ts and reference baselines.

AI Disclosure: This patch was developed with AI assistance using Google Antigravity. I have reviewed, tested, and verified the implementation and baselines, and will shepherd this PR through review.

Copilot AI balanced review requested due to automatic review settings September 8, 2026 16:38
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Sep 8, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug labels Sep 8, 2026
return grammarErrorOnNode(node, Diagnostics.Dynamic_imports_are_only_supported_when_the_module_flag_is_set_to_es2020_es2022_esnext_commonjs_amd_system_umd_node16_node18_node20_or_nodenext);
}

if (node.typeArguments) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Your agent is very confused, these are test fixtures, not files to update

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.

🟢 Approval recommended

The implementation and regression coverage are consistent, with no unresolved issues.

Pull request overview

Disallows optional call chaining on import.defer expressions and adds regression coverage.

Changes:

  • Detects and rejects import.defer?.(...) with TS1326.
  • Updates parser and checker fixtures.
  • Adds conformance tests and expected baselines.
File summaries
File Description
tsc/testdata/tests/cases/conformance/importDefer/importDeferOptionalChain.ts Adds the regression test.
tsc/testdata/fixtures/compiler/parser.ts Updates parser fixture behavior.
tsc/testdata/fixtures/compiler/checker.ts Updates checker fixture behavior.
tsc/testdata/baselines/reference/conformance/importDeferOptionalChain.types Captures expected types.
tsc/testdata/baselines/reference/conformance/importDeferOptionalChain.symbols Captures expected symbols.
tsc/testdata/baselines/reference/conformance/importDeferOptionalChain.js Captures expected emit.
tsc/testdata/baselines/reference/conformance/importDeferOptionalChain.errors.txt Captures expected diagnostics.
tsc/internal/parser/parser.go Flags optional deferred imports for grammar checking.
tsc/internal/checker/grammarchecks.go Reports TS1326 for optional import calls.
Review details
  • Files reviewed: 7/7 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@vaibhavsrv

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@vaibhavsrv

Copy link
Copy Markdown
Author

Thanks Jake Bailey (@jakebailey)! Apologies for the confusion regarding the test fixtures — I have reverted all changes under tsc/testdata/fixtures/compiler/ and updated the PR branch. The patch now strictly contains the Go compiler source fixes (tsc/internal/), test case, and reference baselines.

Comment on lines +2171 to 2172
if nodeAsCall.TypeArguments != nil || nodeAsCall.QuestionDotToken != nil {
return c.grammarErrorOnNode(node, diagnostics.This_use_of_import_is_invalid_import_calls_can_be_written_but_they_must_have_parentheses_and_cannot_have_type_arguments)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should issue a different error message here.

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: Not started

Development

Successfully merging this pull request may close these issues.

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

4 participants