GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid extra parens in TS `as`/`satisfies` by nicolo-ribaudo · Pull Request #16617 · babel/babel

Conversation

@nicolo-ribaudo

@nicolo-ribaudo nicolo-ribaudo commented Jul 5, 2024

Copy link
Copy Markdown
Member
Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

We were being extra conservative and always printing parens in our code generator. This PR avoids it, by considering as/satisfies as having the same precedence on the left as in. Extra care is needed for |/&, since they are different operators at the type and value levels.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nicolo-ribaudo nicolo-ribaudo added PR: Polish 💅 A type of pull request used for our changelog categories pkg: generator pkg: types labels Jul 5, 2024
@nicolo-ribaudo nicolo-ribaudo added this to the generator: preserveFormat milestone Jul 5, 2024
nicolo-ribaudo
nicolo-ribaudo commented Jul 5, 2024
View reviewed changes
Comment thread packages/babel-generator/test/fixtures/typescript/cast-need-parentheses-babel-7/output.js
a | b as c;
a as b | c;
(a as b) | c;
a as (b | c);

@nicolo-ribaudo nicolo-ribaudo Jul 5, 2024

Copy link
Copy Markdown
Member Author

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.

Here parens are not actually necessary, but our AST has a TSParenthesizedType node so they are preserved. Babel 8 removes the parens, because TSParenthesizedType is not enabled by default anymore.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

liuxingbaoyu reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@nicolo-ribaudo
nicolo-ribaudo force-pushed the ts-unnecessary-parens branch from c8c3390 to ceff568 Compare July 5, 2024 17:53
@babel-bot

babel-bot commented Jul 5, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/57279

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

liuxingbaoyu
liuxingbaoyu approved these changes Jul 6, 2024
View reviewed changes

@liuxingbaoyu liuxingbaoyu left a comment

Copy link
Copy Markdown
Member

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.

This is awesome!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/babel-generator/src/node/parentheses.ts Outdated
Comment on lines +48 to +59
function getBinaryPrecedence(node: t.Node) {
if (node.type === "BinaryExpression" || node.type === "LogicalExpression") {
return PRECEDENCE.get(node.operator);
}
if (node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression") {
return PRECEDENCE.get("in");
}
}

@liuxingbaoyu liuxingbaoyu Jul 6, 2024

Copy link
Copy Markdown
Member

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.

Can we add a parameter nodeType to use the parentType of the caller?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

nicolo-ribaudo reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@nicolo-ribaudo
Avoid extra parens in TS as/satisfies
f083dbe
@nicolo-ribaudo
nicolo-ribaudo force-pushed the ts-unnecessary-parens branch from 6ac192d to f083dbe Compare July 6, 2024 15:08
@liuxingbaoyu

liuxingbaoyu commented Jul 10, 2024

Copy link
Copy Markdown
Member

I noticed this PR was tagged pkg: types and there doesn't seem to be any relevant changes?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nicolo-ribaudo nicolo-ribaudo removed the pkg: types label Jul 10, 2024
@nicolo-ribaudo

nicolo-ribaudo commented Jul 10, 2024

Copy link
Copy Markdown
Member Author

by mistake :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nicolo-ribaudo nicolo-ribaudo added the area: typescript label Jul 10, 2024
@liuxingbaoyu
liuxingbaoyu requested a review from JLHwung July 12, 2024 17:27
JLHwung
JLHwung approved these changes Jul 12, 2024
View reviewed changes
@nicolo-ribaudo
nicolo-ribaudo merged commit 3e4723f into babel:main Jul 12, 2024
@nicolo-ribaudo
nicolo-ribaudo deleted the ts-unnecessary-parens branch July 12, 2024 19:37
@cpojer

cpojer commented Jul 16, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

The latest Babel version is causing breakage in Athena Crisis, and I believe this PR is related. Specifically it's breaking the common patterns below:

Spread is somehow broken due to the as const

export const MoveAction = (
  from: Vector,
  to: Vector,
  path?: ReadonlyArray<Vector> | null,
  complete?: boolean,
) =>
  ({
    ...(complete ? { complete } : null),
    from,
    ...(path ? { path } : null),
    to,
    type: 'Move',
  }) as const;

Source: https://github.com/nkzw-tech/athena-crisis/blob/main/apollo/action-mutators/ActionMutators.tsx#L5-L17

Type casting a function isn't working

export default (function lazy(factory) {
  return _lazy(() => factory().catch(importErrorHandler));
} as typeof _lazy);

Source: https://github.com/nkzw-tech/athena-crisis/blob/main/ui/lib/lazy.tsx#L13-L15

The latter works with as (typeof _lazy), however prettier will strip the extraneous parenthesis.

To repro you can clone the linked repo, run pnpm up -r --latest and then run pnpm vitest tests/__tests__/CreateBuildingFog.test.tsx which should show you a number of "Pre-transform error" messages. I can't tell if this is because of the pipeline (React → Babel → Vite → esbuild/swc) or if this is also an issue with Babel only but the setup is fairly common.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nicolo-ribaudo

nicolo-ribaudo commented Jul 16, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Thanks for the report — that's an easy fix, I'll try to have it ready today.

Minimal repro: https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=FACglABAvAfBIG8C-ECGBnCBGMwg&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=false&presets=&prettier=false&targets=&version=7.24.9&externalPlugins=%40babel%2Fplugin-syntax-typescript%407.24.7&assumptions=%7B%7D

zachbwh reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nicolo-ribaudo nicolo-ribaudo mentioned this pull request Jul 16, 2024
Fix parens detection for object&function in as/satisfies #16648
Merged
@ArnaudBarre ArnaudBarre mentioned this pull request Jul 16, 2024
Type cast in one line arrow function () => ({} as Type) cause vite:esbuild transform error vitejs/vite-plugin-react#348
Closed
7 tasks done
@X-oss-byte X-oss-byte mentioned this pull request Aug 5, 2024
[Snyk] Upgrade @babel/core from 7.11.6 to 7.24.9 X-oss-byte/gatsby-1#801
Open
@Exkaleburx Exkaleburx mentioned this pull request Aug 5, 2024
[Snyk] Upgrade @babel/core from 7.9.0 to 7.24.9 Exkaleburx/plug-and-klay#12
Open
@ashly1111111 ashly1111111 mentioned this pull request Aug 5, 2024
[Snyk] Upgrade: , , , , ashly1111111/node#117
Open
This was referenced Aug 5, 2024
[Snyk] Upgrade: , tiff-es/js-looping-and-iteration-looping-code-along-dc-web-091619#206
Open
[Snyk] Upgrade: , tiff-es/fewpjs-class-extensions-super-lab-online-web-sp-000#200
Open
[Snyk] Upgrade: , tiff-es/fewpjs-fns-as-first-class-data-do-behavior-dc-web-091619#195
Open
[Snyk] Upgrade: , tiff-es/fewpjs-adding-behavior-with-methods-dc-web-091619#213
Open
[Snyk] Upgrade: , tiff-es/fewpjs-document-onload-dc-web-091619#202
Open
@Sonic269 Sonic269 mentioned this pull request Oct 4, 2024
[Snyk] Upgrade @babel/core from 7.8.6 to 7.25.2 Sonic269/App6#140
Open
@anandgupta193 anandgupta193 mentioned this pull request Oct 4, 2024
[Snyk] Upgrade @babel/core from 7.10.4 to 7.25.2 anandgupta193/sortNsuffle-react#1138
Open
@DadonStyle DadonStyle mentioned this pull request Oct 4, 2024
[Snyk] Upgrade @babel/core from 7.17.8 to 7.25.2 DadonStyle/Crypto-Tower-FE#28
Open
@blackleadsu blackleadsu mentioned this pull request Oct 4, 2024
[Snyk] Upgrade @babel/core from 7.9.0 to 7.25.2 blackleadsu/PowerBI-visuals-tools#70
Open
@richyhoopd richyhoopd mentioned this pull request Oct 4, 2024
[Snyk] Upgrade @babel/core from 7.9.0 to 7.25.2 richyhoopd/Video-App#1230
Open
@AnushaSalian AnushaSalian mentioned this pull request Oct 4, 2024
[Snyk] Upgrade @babel/core from 7.20.12 to 7.25.2 virtual-labs/ph3-lab-mgmt#547
Open
This was referenced Oct 4, 2024
[Snyk] Upgrade @babel/core from 7.18.2 to 7.25.2 nejidevelops/phase-0-intro-to-js-2-looping-code-along#94
Open
[Snyk] Upgrade @babel/core from 7.18.2 to 7.25.2 nejidevelops/phase-0-javascript-events-acting-on-events-lab#95
Open
[Snyk] Upgrade @babel/core from 7.18.2 to 7.25.2 nejidevelops/phase-0-intro-to-js-2-objects-lab#91
Open
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 nejidevelops/phase-1-domcontentloaded#95
Open
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 nejidevelops/phase-1-destructuring-assignment#93
Open
This was referenced Oct 5, 2024
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 tiff-es/fewpjs-class-extensions-super-lab-online-web-sp-000#242
Open
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 tiff-es/js-data-structures-objects-lab-dc-web-091619#237
Open
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 tiff-es/fewpjs-adding-behavior-with-methods-dc-web-091619#245
Open
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 tiff-es/fewpjs-document-onload-dc-web-091619#247
Open
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 tiff-es/fewpjs-acting-on-events-dc-web-091619#240
Open
[Snyk] Upgrade @babel/core from 7.6.4 to 7.25.2 tiff-es/js-looping-and-iteration-looping-code-along-dc-web-091619#244
Open
@rish2497 rish2497 mentioned this pull request Oct 5, 2024
[Snyk] Upgrade @babel/core from 7.14.3 to 7.25.2 i3-Market-V3-Public-Repository/Support---Deployment-Tools#70
Open
@rawdaymohamed rawdaymohamed mentioned this pull request Oct 5, 2024
[Snyk] Upgrade @babel/core from 7.18.13 to 7.25.2 rawdaymohamed/Hot-Todo#401
Closed
This was referenced Oct 5, 2024
[Snyk] Upgrade @babel/generator from 7.1.3 to 7.25.6 NOUIY/node#490
Open
[Snyk] Upgrade @babel/types from 7.1.3 to 7.25.6 NOUIY/node#491
Open
@richyhoopd richyhoopd mentioned this pull request Oct 6, 2024
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 richyhoopd/RichyFlix#936
Open
@tiff-es tiff-es mentioned this pull request Oct 6, 2024
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 tiff-es/fewpjs-getter-and-setter-methods-dc-web-091619#261
Open
@nejidevelops nejidevelops mentioned this pull request Oct 8, 2024
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 nejidevelops/phase-1-domcontentloaded#97
Open
This was referenced Oct 8, 2024
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 tiff-es/fewpjs-class-extensions-super-lab-online-web-sp-000#244
Open
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 tiff-es/fewpjs-fns-as-first-class-data-array-o-functions-dc-web-091619#246
Open
[Snyk] Upgrade @babel/core from 7.4.5 to 7.25.2 tiff-es/fewpjs-document-onload-dc-web-091619#248
Open
@AnushaSalian AnushaSalian mentioned this pull request Oct 8, 2024
[Snyk] Upgrade @babel/core from 7.20.12 to 7.25.2 virtual-labs/ph3-lab-mgmt#549
Open
@github-actions github-actions Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 16, 2024
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@JLHwung JLHwung JLHwung approved these changes
@liuxingbaoyu liuxingbaoyu liuxingbaoyu approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator PR: Polish 💅 A type of pull request used for our changelog categories

Projects

None yet

Milestone

generator: preserveFormat

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@nicolo-ribaudo @babel-bot @liuxingbaoyu @cpojer @JLHwung
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.