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 — Add `NodePath#getAssignmentIdentifiers` by JLHwung · Pull Request #16551 · babel/babel

Conversation

@JLHwung

@JLHwung JLHwung commented Jun 5, 2024 •
edited
Loading

Uh oh!

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

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

In this PR we introduce a new getAssignmentIdentifiers method to extract identifiers allowed in the left hand side. Currently this method is a subset of the getBindingIdentifiers, however in Babel 8 we will remove the assignment LHS support for getBindingIdentifiers, as those are not binding identifiers per spec. This goal will be addressed in a new PR.

In Babel 8, getBindingIdentifiers will only return identifiers that will introduce new bindings, while getAssignmentIdentifiers only returns identifiers that is reassigned.

Sorry, something went wrong.

Uh oh!

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

All reactions
@JLHwung JLHwung added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Jun 5, 2024
JLHwung
JLHwung commented Jun 5, 2024
View reviewed changes
Comment thread packages/babel-plugin-transform-block-scoping/src/validation.ts
const ids = path.getBindingIdentifiers();
const ids = process.env.BABEL_8_BREAKING
? path.getAssignmentIdentifiers()
: path.getBindingIdentifiers();

@JLHwung JLHwung Jun 5, 2024

Copy link
Copy Markdown
Contributor 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.

This change is deferred to Babel 8 because plugins might be used with an old Babel core that depends on a legacy Babel traverse.

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
@babel-bot

babel-bot commented Jun 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/57461

All reactions

Sorry, something went wrong.

Uh oh!

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

@nicolo-ribaudo nicolo-ribaudo added this to the v7.25.0 milestone Jun 6, 2024
nicolo-ribaudo
nicolo-ribaudo approved these changes Jun 6, 2024
View reviewed changes
@ehoogeveen-medweb

ehoogeveen-medweb commented Jun 6, 2024

Copy link
Copy Markdown

In this PR we introduce a new getAssignmentIdentifiers method to extract identifiers allowed in the left hand side.

In Babel 8, [...] getAssignmentIdentifiers only returns identifiers that is reassigned.

To be clear, will this method return identifiers that are assigned to, or identifiers that can be assigned to (but don't create a new binding)? If it's the latter, is this affected by non-strict mode (where assigning to an undeclared variable creates a property on the global object)?

All reactions

Sorry, something went wrong.

Uh oh!

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

@JLHwung

JLHwung commented Jun 7, 2024

Copy link
Copy Markdown
Contributor Author

To be clear, will this method return identifiers that are assigned to, or identifiers that can be assigned to (but don't create a new binding)? If it's the latter, is this affected by non-strict mode (where assigning to an undeclared variable creates a property on the global object)?

Good catch. This method is supposed to return those that are assigned and it does not take into account non-strict mode. I overlooked the non-strict mode behaviour of assignment expressions. I believe this is why getBindingIdentifiers currently handles assignment expressions as well.

However, getBindingIdentifiers also covers update expressions and unary expressions, e.g. delete x, which mutates the value referenced by the identifiers but never introduce new bindings.

The map getBindingIdentifiers.keys also determines the virtual type BindingIdentifier, and here is known mismatch between Babel's type BindingIdentifier / ReferencedIdentifier and the spec production BindingIdentifier / IdentifierReference. Note: Babel's BindingIdentifier and ReferencedIdentifier also cover some TS/Flow types, which are omitted for clarity.

Babel ECMA262 (without Annex B)
BindingIdentifier BindingIdentifier + LabelIdentifier + IdentifierReference from AssignmentPattern from LeftHandSideExpression (LHS) + IdentifierReference from LHS from UpdateExpression and UnaryExpression
ReferencedIdentifier IdentifierReference - IdentifierReference from AssignmentPattern from LeftHandSideExpression (LHS)

As we can see,

  • the Babel BindingIdentifier covers binding identifier as well as most mutated identifier references, however, it does not cover x in for (x of []);
  • the Babel ReferencedIdentifier covers mostly immutable references, however, it does cover x in x++
  • the Babel BindingIdentifier set intersects with ReferencedIdentifier, e.g. x in x++ can be matched by both BindingIdentifier and ReferencedIdentifier
  • The union of BindingIdentifier and ReferencedIdentifier is BindingIdentifier + LabelIdentifier + IdentifierReference

If we introduce getAssignmentIdentifiers without updating these virtual types, it might be even more confusing as a Babel "BindingIdentifier", e.g. IdentifierReference in an assignment LHS, will not be returned from getBindingIdentifiers.

ehoogeveen-medweb reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@JLHwung

JLHwung commented Jun 7, 2024

Copy link
Copy Markdown
Contributor Author

Here are some use cases for Babel's "BindingIdentifier" and "ReferencedIdentifier":

  1. transform-parameters deoptimize ...args when args is shadowed or mutated using BindingIdentifier

    babel/packages/babel-plugin-transform-parameters/src/rest.ts

    Lines 196 to 207 in 55a2050

    /**
    * Deopt on use of a binding identifier with the same name as our rest param.
    *
    * See https://github.com/babel/babel/issues/2091
    */
    BindingIdentifier(path, state) {
    if (referencesRest(path, state)) {
    state.deopted = true;
    }
    },
    };
  2. transform-reserved-words renames invalid ES3 identifiers using the union BindingIdentifier | ReferencedIdentifier

    babel/packages/babel-plugin-transform-reserved-words/src/index.ts

    Lines 11 to 14 in 55a2050

    "BindingIdentifier|ReferencedIdentifier"(path: NodePath<t.Identifier>) {
    if (!t.isValidES3Identifier(path.node.name)) {
    path.scope.rename(path.node.name);
    }
  3. transform-class-properties renames the class binding references by the new class name using ReferencedIdentifier

    babel/packages/babel-helper-create-class-features-plugin/src/fields.ts

    Lines 1440 to 1449 in 55a2050

    const innerReferencesVisitor: Visitor<ReplaceInnerBindingReferenceState> = {
    ReferencedIdentifier(path, state) {
    if (
    path.scope.bindingIdentifierEquals(path.node.name, state.innerBinding)
    ) {
    state.needsClassRef = true;
    path.node.name = state.thisRef.name;
    }
    },
    };
  4. traverse collects whether arguments are referenced using isReferencedIdentifier:

    babel/packages/babel-traverse/src/path/conversion.ts

    Lines 778 to 779 in 55a2050

    Identifier(child, { argumentsPaths }) {
    if (!child.isReferencedIdentifier({ name: "arguments" })) return;
  5. traverse remove bindings from the scope

    babel/packages/babel-traverse/src/path/removal.ts

    Lines 27 to 30 in 55a2050

    export function _removeFromScope(this: NodePath) {
    const bindings = getBindingIdentifiers(this.node, false, false, true);
    Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
    }

If we are to align current Babel definition to the spec notation, we will have to adjust case 1 and 4 since they now cover different AST structures. The case 2 will not be affected because the union set is not changed unless we want to separate LabelIdentifier from the BindingIdentifier. The case 3 is probably fine, too as the inner class binding is immutable. The case 5 need no changes and we can further remove the newBindingsOnly option.

I think the case 1 justifies the requirement of getAssignmentIdentifiers, although we can query similar information from binding.constantViolations, the AST query will return the up-to-date information, otherwise we will have to do a scope crawl to ensure that the constantViolations is correct.

One of technical challenges that may block alignment is identifiers within a pattern. To determine whether such an identifier is a reference, we have to walk up to the parent of the toplevel object/array pattern, but the current isBindingIdentifier only have access to up to 2 levels of ancestors. We could do the walk before calling the query and pass down something like patternParent, but then the babel-types isBindingIdentifier and isReferecedIdentifier check will be more expensive and no longer context-free.

ehoogeveen-medweb and liuxingbaoyu reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

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

@JLHwung JLHwung mentioned this pull request Jun 11, 2024
improve getBindingIdentifiers.keys typing #16570
Merged
liuxingbaoyu
liuxingbaoyu approved these changes Jun 28, 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.

Thank you for your detailed explanation!

Sorry, something went wrong.

Uh oh!

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

All reactions
@nicolo-ribaudo nicolo-ribaudo added the PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release label Jun 29, 2024
@nicolo-ribaudo
nicolo-ribaudo force-pushed the add-get-assignment-identifiers branch from 09bff4f to 2a5a022 Compare July 25, 2024 18:44
@nicolo-ribaudo nicolo-ribaudo added the pkg: traverse label Jul 25, 2024
JLHwung and others added 5 commits July 26, 2024 11:17
@JLHwung @nicolo-ribaudo
feat: add getAssignmentIdentifiers
dd396e7
@JLHwung @nicolo-ribaudo
feat: add NodePath#getAssignmentIdentifiers
9cf3588
@JLHwung @nicolo-ribaudo
fix: use path.getAssignmentIdentifiers for Babel 8
988a7c0
@JLHwung @nicolo-ribaudo
update test snapshot
8a38b99
@nicolo-ribaudo
Update snapshot
f0a5e6f
@nicolo-ribaudo
nicolo-ribaudo force-pushed the add-get-assignment-identifiers branch from 2a5a022 to f0a5e6f Compare July 26, 2024 09:22
@nicolo-ribaudo nicolo-ribaudo changed the title Add NodePath#getAssignmentIdentifiers Add NodePath#getAssignmentIdentifiers Jul 26, 2024
@nicolo-ribaudo
nicolo-ribaudo merged commit 47d8259 into babel:main Jul 26, 2024
@nicolo-ribaudo
nicolo-ribaudo deleted the add-get-assignment-identifiers branch July 26, 2024 09:46
This was referenced Aug 16, 2024
[Snyk] Upgrade: , nejidevelops/phase-0-javascript-events-acting-on-events-lab#50
Open
[Snyk] Upgrade: , nejidevelops/phase-0-intro-to-js-2-looping-code-along#50
Open
@ashly1111111 ashly1111111 mentioned this pull request Aug 16, 2024
[Snyk] Upgrade: , , , , ashly1111111/node#119
Open
@nejidevelops nejidevelops mentioned this pull request Aug 16, 2024
[Snyk] Upgrade: , nejidevelops/phase-0-intro-to-js-2-objects-lab#52
Open
@DevangPatelUK DevangPatelUK mentioned this pull request Aug 16, 2024
[Snyk] Upgrade @babel/runtime from 7.19.0 to 7.25.0 DevangPatelUK/BotFramework-WebChat#144
Open
@nejidevelops nejidevelops mentioned this pull request Aug 16, 2024
[Snyk] Upgrade: , nejidevelops/phase-1-destructuring-assignment#52
Open
@YinghanFeng YinghanFeng mentioned this pull request Aug 16, 2024
[Snyk] Upgrade @babel/runtime from 7.13.17 to 7.25.0 YinghanFeng/redux-orm#3
Open
@DevangPatelUK DevangPatelUK mentioned this pull request Aug 16, 2024
[Snyk] Upgrade @babel/runtime from 7.19.0 to 7.25.0 DevangPatelUK/BotFramework-WebChat#145
Open
This was referenced Oct 5, 2024
[Snyk] Upgrade @babel/parser from 7.1.3 to 7.25.6 NOUIY/node#488
Open
[Snyk] Upgrade @babel/traverse from 7.1.4 to 7.25.6 NOUIY/node#489
Open
[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
[Snyk] Upgrade @babel/template from 7.1.2 to 7.25.0 NOUIY/node#492
Open
@richyhoopd richyhoopd mentioned this pull request Oct 6, 2024
[Snyk] Upgrade @babel/preset-env from 7.4.5 to 7.25.4 richyhoopd/RichyFlix#937
Open
This was referenced Oct 6, 2024
[Snyk] Upgrade @babel/preset-env from 7.4.5 to 7.25.4 tiff-es/fewpjs-getter-and-setter-methods-dc-web-091619#262
Open
[Snyk] Upgrade @babel/preset-env from 7.10.4 to 7.25.4 tiff-es/tiffany-portfolio-responsive#536
Open
@mdfkbtc mdfkbtc mentioned this pull request Oct 6, 2024
[Snyk] Upgrade @babel/preset-env from 7.5.0 to 7.25.4 mdfkbtc/veles-mobile-wallet-mdfkbtc#1868
Open
@nejidevelops nejidevelops mentioned this pull request Oct 8, 2024
[Snyk] Upgrade @babel/preset-env from 7.4.5 to 7.25.4 nejidevelops/phase-1-domcontentloaded#98
Open
This was referenced Oct 8, 2024
[Snyk] Upgrade @babel/preset-env from 7.4.5 to 7.25.4 tiff-es/fewpjs-class-extensions-super-lab-online-web-sp-000#245
Open
[Snyk] Upgrade @babel/preset-env from 7.4.5 to 7.25.4 tiff-es/fewpjs-fns-as-first-class-data-array-o-functions-dc-web-091619#247
Open
[Snyk] Upgrade @babel/preset-env from 7.4.5 to 7.25.4 tiff-es/fewpjs-document-onload-dc-web-091619#249
Open
@paulineribeyre paulineribeyre mentioned this pull request Oct 17, 2024
[Snyk] Upgrade @babel/preset-env from 7.20.2 to 7.25.4 uc-cdis/Atlas#65
Closed
This was referenced Oct 24, 2024
[Snyk] Upgrade @babel/parser from 7.1.3 to 7.25.7 nerdy-tech-com-gitub/node#10
Open
[Snyk] Upgrade @babel/traverse from 7.1.4 to 7.25.7 nerdy-tech-com-gitub/node#11
Open
This was referenced Oct 24, 2024
[Snyk] Upgrade @babel/parser from 7.1.3 to 7.25.7 ashly1111111/node#142
Open
[Snyk] Upgrade @babel/traverse from 7.1.4 to 7.25.7 ashly1111111/node#143
Open
[Snyk] Upgrade @babel/generator from 7.1.3 to 7.25.7 ashly1111111/node#144
Open
[Snyk] Upgrade @babel/types from 7.1.3 to 7.25.7 ashly1111111/node#145
Open
[Snyk] Upgrade @babel/template from 7.1.2 to 7.25.7 ashly1111111/node#146
Open
@nerdy-tech-com-gitub nerdy-tech-com-gitub mentioned this pull request Oct 24, 2024
[Snyk] Upgrade @babel/preset-env from 7.23.7 to 7.25.7 nerdy-tech-com-gitub/joplin#7
Open
This was referenced Oct 25, 2024
[Snyk] Upgrade @babel/preset-env from 7.8.4 to 7.25.7 YoYBaBy/docs#870
Open
[Snyk] Upgrade @babel/runtime from 7.11.2 to 7.25.7 YoYBaBy/docs#871
Open
@DevangPatelUK DevangPatelUK mentioned this pull request Oct 25, 2024
[Snyk] Upgrade @babel/runtime from 7.19.0 to 7.25.7 DevangPatelUK/BotFramework-WebChat#464
Open
@snyk-io snyk-io Bot mentioned this pull request Oct 26, 2024
[Snyk] Upgrade @babel/runtime from 7.4.3 to 7.25.7 Playgirlkaybraz11/td#16
Open
@nejidevelops nejidevelops mentioned this pull request Oct 26, 2024
[Snyk] Upgrade @babel/preset-env from 7.4.5 to 7.25.7 nejidevelops/phase-1-destructuring-assignment#100
Open
@richyhoopd richyhoopd mentioned this pull request Oct 26, 2024
[Snyk] Upgrade @babel/preset-env from 7.9.0 to 7.25.7 richyhoopd/Video-App#1237
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 26, 2024
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@nicolo-ribaudo nicolo-ribaudo nicolo-ribaudo 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

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: traverse PR: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release

Projects

None yet

Milestone

v7.25.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

5 participants

@JLHwung @babel-bot @ehoogeveen-medweb @nicolo-ribaudo @liuxingbaoyu
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.