-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse error on private identifier optional chain #35987
Parse error on private identifier optional chain #35987
Conversation
src/compiler/parser.ts
Outdated
propertyAccess.name = parseRightSideOfDot(/*allowIdentifierNames*/ true, /*allowPrivateIdentifiers*/ true); | ||
if (questionDotToken || expression.flags & NodeFlags.OptionalChain) { | ||
propertyAccess.flags |= NodeFlags.OptionalChain; | ||
if (isPrivateIdentifier(propertyAccess.name)) { | ||
parseErrorAtRange(propertyAccess.name, Diagnostics.An_optional_chain_cannot_contain_private_identifiers); | ||
propertyAccess.name = createMissingNode(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to just put the privatename identifier on the node so that the language service still works
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a grammar error in the binder @sandersn?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was looking around and it seems like diagnostics from the binder and checker are discarded for JS files that have checkJs off.
TypeScript/src/compiler/program.ts
Lines 1736 to 1737 in 18269c0
const includeBindAndCheckDiagnostics = !isTsNoCheck && (sourceFile.scriptKind === ScriptKind.TS || sourceFile.scriptKind === ScriptKind.TSX || | |
sourceFile.scriptKind === ScriptKind.External || isCheckJs || sourceFile.scriptKind === ScriptKind.Deferred); |
Previously, this error was reported in the checker, so JS files with checkJs: false were not erroring on this invalid syntax.
d6abc43
to
f3cdef3
Compare
@joeywatts where is this restriction documented? I searched in the proposal specs and didn't find anything useful. |
See the syntax for Optional Chaining in the ES2020 Specification. It only supports |
I think that the only reason that it's not allowed is because the two proposals developed separately. I would be happy to bring forward a proposal to remove the restriction (because why not I guess?) unless @littledan wants to add |
@DanielRosenwasser I didn't communicate or justify it well, but disallowing the combination was intentional on my part. However, now TC39 has revisited this and decided that the combination should be allowed, see tc39/proposal-class-fields#301. So, this change should be revisited (reverted?). |
Previously, this error was reported in the checker, so JS files with checkJs: false were not erroring on this invalid syntax.
Addresses the optional chaining part of #35962.