Extract chokes on "obj?.function" (optional-chaining + "function" attribute)
See original GitHub issueDescribe the bug Cannot parse source code containing a optional chaining followed by an attribute name “function”.
To Reproduce
export default function App() {
return news.author?.function // fails
// news.author?.['function'] is OK
// news.author.function is also OK
}
SyntaxError: src\containers\news\NewsDetail.js: Unexpected token (68:45)
66 | authorAvatarUrl={null}
67 | authorFullName={news.author?.fullName}
> 68 | authorFunction={news.author?.function}
| ^
69 | categoryName={news.category?.name}
70 | categoryColorObject={news.category?.color}
71 | content={news.content}
at _class.raise (node_modules\@babel\core\node_modules\@babel\parser\lib\index.js:4051:15)
at _class.unexpected (node_modules\@babel\core\node_modules\@babel\parser\lib\index.js:5382:16)
at _class.jsxParseIdentifier (node_modules\@babel\core\node_modules\@babel\parser\lib\index.js:3527:14)
at _class.jsxParseNamespacedName (node_modules\@babel\core\node_modules\@babel\parser\lib\index.js:3537:23)
at _class.jsxParseAttribute (node_modules\@babel\core\node_modules\@babel\parser\lib\index.js:3620:24)
at _class.jsxParseOpeningElementAfterName (node_modules\@babel\core\node_modules\@babel\parser\lib\index.js:3641:30)
at _class.jsxParseOpeningElementAt (node_modules\@babel\core\node_modules\@babel\parser\lib\index.js:3634:19)
at _class.jsxParseElementAt (node_modules\@babel\core\node_modules\@babel\parser\lib\index.js:3666:33)
at _class.jsxParseElement (node_modules\@babel\core\node_modules\@babel\parser\lib\index.js:3735:19)
at _class.parseExprAtom (node_modules\@babel\core\node_modules\@babel\parser\lib\index.js:3742:21)
Expected behavior To work
Additional context js-lingui 2.7.2 @babel/plugin-proposal-optional-chaining (plugin included in extractBabelOptions)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The optional chaining ( ?. ) operator accesses an object's property or calls a function. If the object accessed or function called is ......
Read more >Optional chaining '?.' - The Modern JavaScript Tutorial
The optional chaining ?. is a safe way to access nested object properties, even if an intermediate property doesn't exist.
Read more >JavaScript Optional Chaining Operator (?.)
In this tutorial, you'll learn about the optional chaining operator (?.) that simplifies the way to access values through connected objects.
Read more >LOM - The Live Object Model - Max 8 Documentation
Displays the device chain and the browser and activates Hot-Swap Mode for the selected device. Calling this function again deactivates Hot-Swap Mode. zoom_view....
Read more >How to Use Optional Chaining in JavaScript - freeCodeCamp
Optional chaining is a safe and concise way to perform access checks for nested object properties. The optional chaining operator ?. takes ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry for the spam, I found the reason for this issue and it’s likely unrelated. I’m using typescript and lingui-cli is using typescript for parsing typescript files (not babel, which would be possible as well nowadays).
But lingui is using it’s own typescript dependency which is Typescript 2.x and doesn’t support optional chaining. I would have expected it to use typescript as a peer dependency instead. To workaround this issue I forced typescript to be resolved with by intended version in package.json:
Now lingui extracts uses the same Typescript version as my project and extraction works fine.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.