Skip to content
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

Optimize the transformed output of JSXSpreadAttributes containing an ObjectLiteralExpression #49100

Merged
merged 4 commits into from
Mar 21, 2023

Conversation

Andarist
Copy link
Contributor

closes #49099

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label May 13, 2022
@Andarist Andarist force-pushed the optimize-jsx-spread-attribute branch from 1aa7f10 to 1e8ec38 Compare May 13, 2022 14:34
function transformJsxSpreadAttributeToSpreadAssignment(node: JsxSpreadAttribute) {
function transformJsxSpreadAttributeToProps(node: JsxSpreadAttribute) {
if (isObjectLiteralExpression(node.expression)) {
return node.expression.properties;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't like 100% correct when it comes to JS semantics, but TS never was 100% correct anyway (eg. TS doesn't emit 100% spec-compliant output for a case like this).

Copy link
Member

Choose a reason for hiding this comment

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

What isn't correct about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this should be OK for the "sane code" but probably once you start thinking about malicious getters, setters, and stuff then you could figure out some edge case that would change behavior with this change. But then it means that the code compiled with TS doesn't work exactly the same as the code compiled with Babel because Babel applies this optimization.

As mentioned though... TS isn't 100% correct even today so I'm not sure if a super subtle (and only for weird code) behavior change changes the "final balance" all that much.

TS is using Object.assign semantics for spreads (and that calls setters on the target object), whereas the correct semantic is to define properties (and that doesn't call setters). There are also cases like in the referenced Babel PR:

var p = { a: 1 }
var classicAssign = Object.assign({}, { get nefariousGetter() { (p = { evil: 'muahauhauha' }); return 42 } }, { p }) // {"nefariousGetter":42,"p":{"a":1}}
var correct = { ...{ get nefariousGetter() { (p = { evil: 'muahauhauha' }); return 42 } }, p } // {"nefariousGetter":42,"p":{"evil":"muahauhauha"}}

😅

Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

This needs tests (and to appropriately handle) the __proto__ and "__proto__" property names, like was mentioned in the original issue. Especially since babel patched the bug in their output, and now skips this inlining when such a property is present :)

@typescript-bot typescript-bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Mar 21, 2023
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #49099. If you can get it accepted, this PR will have a better chance of being reviewed.

@Andarist
Copy link
Contributor Author

@weswigham I fixed the issue with __proto__ by following the current Babel's behavior and the PR that fixed that there: https://github.com/babel/babel/pull/14759/files

@Andarist Andarist requested review from weswigham and removed request for rbuckton March 21, 2023 08:34
Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

I think this looks fine and meets what we set out to do now~

@weswigham weswigham merged commit 3f90887 into microsoft:main Mar 21, 2023
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
Archived in project
Development

Successfully merging this pull request may close these issues.

Optimize JSXSpreadAttribute JS emit when it contains ObjectLiteralExpression
4 participants