-
Notifications
You must be signed in to change notification settings - Fork 22.7k
Add reference for two optional chain SyntaxErrors #34676
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
files/en-us/web/javascript/reference/errors/bad_new_optional/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
title: "SyntaxError: new keyword cannot be used with an optional chain" | ||
slug: Web/JavaScript/Reference/Errors/Bad_new_optional | ||
page-type: javascript-error | ||
--- | ||
|
||
{{jsSidebar("Errors")}} | ||
|
||
The JavaScript exception "new keyword cannot be used with an optional chain" occurs when the constructor of a {{jsxref("Operators/new", "new")}} expression is an [optional chain](/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining), or if there's an optional chain between the constructor and the parenthesized list of arguments. | ||
|
||
## Message | ||
|
||
```plain | ||
SyntaxError: Invalid optional chain from new expression (V8-based) | ||
SyntaxError: new keyword cannot be used with an optional chain (Firefox) | ||
SyntaxError: Cannot call constructor in an optional chain. (Safari) | ||
``` | ||
|
||
## Error type | ||
|
||
{{jsxref("SyntaxError")}} | ||
|
||
## What went wrong? | ||
|
||
There are two ways to get this error. The first one is if the constructor expression is an optional chain expression, like this: | ||
|
||
```js-nolint example-bad | ||
new Intl?.DateTimeFormat(); | ||
Number?.[parseMethod]`Hello, world!`; | ||
``` | ||
|
||
The second one is if `?.` occurs between the constructor and the arguments list, like this: | ||
|
||
```js-nolint | ||
new Intl.DateTimeFormat?.(); | ||
``` | ||
|
||
Optional `new` is specifically forbidden because its syntax is complicated (`new` with and without arguments), and the result is unclear (it would be the only case where `new` does not evaluate to an object value). You need to translate the optional chaining to its underlying condition (see [optional chaining](/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) for more information). | ||
|
||
```js | ||
const result = | ||
Intl.DateTimeFormat === null || Intl.DateTimeFormat === undefined | ||
? undefined | ||
: new Intl.DateTimeFormat(); | ||
``` | ||
|
||
Remember that optional chaining only short-circuits within a parenthesized unit. If you parenthesize your constructor expression, the optional chaining will not cause an error, because now the constructor does not short-circuit and the result is clear (the constructor will produce `undefined` and then cause the `new` expression to throw). | ||
|
||
```js-nolint | ||
new (Intl?.DateTimeFormat)(); // Throws if Intl?.DateTimeFormat is undefined | ||
``` | ||
|
||
However this is a bit nonsensical anyway because optional chaining prevents errors inside the property access chain, but is then guaranteed to generate an error when calling `new`. You would probably still want to use a conditional check. | ||
|
||
Note that optional chaining is only forbidden as the constructor expression. You can use optional chaining inside the argument list, or use optional chaining on the `new` expression as a whole. | ||
|
||
```js example-good | ||
new Intl.DateTimeFormat(navigator?.languages); | ||
new Intl.DateTimeFormat().resolvedOptions?.(); | ||
``` | ||
|
||
Note that there's no needs to use `?.` on the `new` expression itself: `new a()?.b`, because `new` is guaranteed to produce a non-nullish object value. | ||
|
||
## See also | ||
|
||
- {{jsxref("Operators/new", "new")}} | ||
- [Optional chaining (`?.`)](/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) | ||
- [Original discussion on whether "optional new" should be allowed](https://github.com/tc39/proposal-optional-chaining/issues/22) |
67 changes: 67 additions & 0 deletions
67
files/en-us/web/javascript/reference/errors/bad_optional_template/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
title: "SyntaxError: tagged template cannot be used with optional chain" | ||
slug: Web/JavaScript/Reference/Errors/Bad_optional_template | ||
page-type: javascript-error | ||
--- | ||
|
||
{{jsSidebar("Errors")}} | ||
|
||
The JavaScript exception "tagged template cannot be used with optional chain" occurs when the tag expression of a [tagged template literal](/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) is an [optional chain](/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining), or if there's an optional chain between the tag and the template. | ||
|
||
## Message | ||
|
||
```plain | ||
SyntaxError: Invalid tagged template on optional chain (V8-based) | ||
SyntaxError: tagged template cannot be used with optional chain (Firefox) | ||
SyntaxError: Cannot use tagged templates in an optional chain. (Safari) | ||
``` | ||
|
||
## Error type | ||
|
||
{{jsxref("SyntaxError")}} | ||
|
||
## What went wrong? | ||
|
||
There are two ways to get this error. The first one is if the tag expression is an optional chain expression, like this: | ||
|
||
```js-nolint example-bad | ||
String?.raw`Hello, world!`; | ||
console.log?.()`Hello, world!`; | ||
Number?.[parseMethod]`Hello, world!`; | ||
``` | ||
|
||
The second one is if `?.` occurs between the tag and the template, like this: | ||
|
||
```js-nolint example-bad | ||
String.raw?.`Hello, world!`; | ||
``` | ||
|
||
Optional chaining in the tag is specifically forbidden because there's no great use case for it, and what the result is expected to be is unclear (should it be `undefined` or the template's value as if it's untagged?). You need to translate the optional chaining to its underlying condition (see [optional chaining](/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) for more information). | ||
|
||
```js example-good | ||
const result = | ||
String.raw === null || String.raw === undefined | ||
? undefined | ||
: String.raw`Hello, world!`; | ||
``` | ||
|
||
Remember that optional chaining only short-circuits within a parenthesized unit. If you parenthesize your tag expression, the optional chaining will not cause an error, because now the tag does not short-circuit and the result is clear (the tag will produce `undefined` and then cause the tagged template to throw). | ||
|
||
```js-nolint | ||
(console?.log)`Hello, world!`; // Throws if console?.log is undefined | ||
``` | ||
|
||
However this is a bit nonsensical anyway because optional chaining prevents errors inside the property access chain, but is then guaranteed to generate an error when calling the template tag. You would probably still want to use a conditional check. | ||
|
||
Note that optional chaining is only forbidden as the tag expression. You can use optional chaining inside the embedded expressions, or use optional chaining on the tagged template expression as a whole. | ||
|
||
```js example-good | ||
console.log`Hello, ${true.constructor?.name}!`; // ['Hello, ', '!', raw: Array(2)] 'Boolean' | ||
console.log`Hello`?.toString(); // undefined | ||
``` | ||
|
||
## See also | ||
|
||
- [Template literals](/en-US/docs/Web/JavaScript/Reference/Template_literals) | ||
- [Optional chaining (`?.`)](/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) | ||
- [Original discussion on whether optional chaining should be allowed in tagged template literals](https://github.com/tc39/proposal-optional-chaining/issues/54) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.