Skip to content

Fix missing syntax errors for optional property access in plain JS - #64744

Open
MaheshDesu (mahesh-desu) wants to merge 1 commit into
microsoft:mainfrom
mahesh-desu:fix/plain-js-optional-access-diagnostics
Open

MaheshDesu (mahesh-desu) wants to merge 1 commit into
microsoft:mainfrom
mahesh-desu:fix/plain-js-optional-access-diagnostics

Conversation

@mahesh-desu

@mahesh-desu MaheshDesu (mahesh-desu) commented Oct 12, 2026 •

Copy link
Copy Markdown

Fixes #57640.

In plain JS files (no checkJs), TypeScript drops most checker diagnostics through the plainJSErrors allowlist. Five grammar diagnostics that are runtime SyntaxErrors for optional property access in assignment positions were not in that allowlist, so tsc reported nothing for code that immediately fails in an engine:

  • TS2777 obj?.a++
  • TS2778 ({ ...obj?.a } = {})
  • TS2779 obj?.a = 1
  • TS2780 for (obj?.a in {})
  • TS2781 for (obj?.a of [])

Change: adds those five codes to plainJSErrors in tsc/internal/compiler/program.go (5 lines), following the approach analyzed by Emanuel Hoogeveen (@ehoogeveen-medweb) in the issue.

Tests: extends conformance/salsa/plainJSGrammarErrors to cover all five codes plus negative controls.

Known limitations, inherited from how the checker produces these diagnostics (same behavior as .ts files and classic TypeScript):

  • TS2780 is reported only when the left-hand side has type string or any; other types produce TS2405, a type error that remains filtered in plain JS. The test covers both the string and any cases.
  • TS2777 is produced only when the arithmetic operand check passes; obj?.a++ on a nonnumeric property produces TS2356 instead, which likewise remains filtered. The test uses a numeric property.

Fully closing these gaps requires grammar checking in JS files, which is a larger work item. The same class of gap exists for other invalid update targets in plain JS (e.g. 1++ is silent because TS2357 is not allowlisted).

Verified locally: hereby build, full hereby test/test:all (no new failures; only pre-existing symlink-privilege failures in internal/fswatch on this Windows box), hereby lint, hereby format, hereby check:format, hereby generate (no diff), and go mod tidy -diff for both modules.

Plain JS files filter checker diagnostics through an allowlist, so the
grammar errors for optional property access in assignment positions
(TS2777-TS2781) were silently dropped even though they are runtime
SyntaxErrors. Add the five codes to the allowlist and extend the
plainJSGrammarErrors conformance test to cover them.

Fixes microsoft#57640.

Signed-off-by: MaheshDesu <mahesh.desu3@gmail.com>
Copilot AI balanced review requested due to automatic review settings October 12, 2026 05:20
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Oct 12, 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.

🟡 Changes recommended

TS2777 remains suppressed for optional-chain increments on nonnumeric properties.

1 open finding
What changed in this PR

Allows optional-chain assignment grammar diagnostics through plain-JavaScript filtering and adds regression baselines.

Changes:

  • Adds five diagnostics to plainJSErrors.
  • Tests invalid optional-chain assignment positions and valid controls.
  • Updates generated diagnostic, emit, type, and symbol baselines.
File Description
tsc/​internal/​compiler/​program.go Allows optional-chain assignment diagnostics in plain JS.
tsc/​testdata/​tests/​cases/​conformance/​salsa/​plainJSGrammarErrors.ts Adds regression cases and controls.
tsc/​testdata/​baselines/​reference/​conformance/​plainJSGrammarErrors.errors.txt Records expected diagnostics.
tsc/​testdata/​baselines/​reference/​conformance/​plainJSGrammarErrors.js Updates emit baseline.
tsc/​testdata/​baselines/​reference/​conformance/​plainJSGrammarErrors.types Updates type baseline.
tsc/​testdata/​baselines/​reference/​conformance/​plainJSGrammarErrors.symbols Updates symbol baseline.

🧠 Review effort: Balanced

Comment thread tsc/internal/compiler/program.go

This branch has not been deployed

No deployments
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.

JS | Optional chaining left hand assignment not detected as syntax error.

2 participants