Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/57493 |
|
I have successfully managed to solve the problem, but I still need an opinion over modifying the |
| path.remove(); | ||
|
|
||
| case "a = 42": | ||
| expect(path.scope.getBinding("a")).toBeDefined(); |
There was a problem hiding this comment.
If this line never runs it will not fail. We should push a comment in the output saying "// a is defined? " + path.scope.getBinding("a"), so that if the comment is missing the test fails.
Also, could you use an if to make this slightly easier to follow?
const { value } = path.node.init;
if (value === 33) path.remove();
t.addComment(
path.getProgramParent().path.node,
"trailing",
`// a is defined when visiting a=${value}? ${path.scope.hasBinding("a")}`
);
``
packages/babel-traverse/test/fixtures/scope/remove-nested-let-path/plugin.js
Outdated
Show resolved
Hide resolved
…path/plugin.js Co-authored-by: Nicolò Ribaudo <hello@nicr.dev>
There was a problem hiding this comment.
I think this bug should be fixed in Scope#removeBinding:
babel/packages/babel-traverse/src/scope/index.ts
Lines 1369 to 1373 in de55414
The root cause of this bug is that currently the scope.removeBinding() method is not idempotent: When we call scope.removeBinding() on the same scope twice, we end up removing the binding of the outer scope, because the getBinding returns the binding info of the outer scope after the inner binding is removed.
To fix this, we can
- call
removeOwnBindingon current scope if the current scope path is a function parent (avarscope). Test case: calling inner scope.removeBinding() twice from an inner var scope should not result to deletion ofain the upper scope:
function outer() {
var a;
function inner() {
var a;
}
}- For let-scope, we can check the binding info. If the binding kind is
var, we call removeOwnBinding on the scope's function parent, we should not call the removal on binding scope because it may be the outer var scope. e.g. callingscope.removeBindingfrom inner block statement should not result to deletion ofain the upper scope:
function outer() {
var a;
function inner() {
{
var a;
}
}
}- For let-scope and other binding kinds, simply call
removeOwnBindingon current scope.
The new approach will only manipulate current scope or its closest var-scope. With the new approach, the path.remove() should work as intended with current logic.
|
As I understand this method, it removes a binding that is accessible in the current scope. I think this method is not expected to be idempotent because it takes a string parameter and when people call it twice, they will expect to remove two bindings named If it were made idempotent, it would be a breaking change and break some uses. Also, it is confusing that Rather than making this method idempotent, I would rather people call this method deterministically to avoid removing unexpected variables. |
Understood. The babel/packages/babel-traverse/src/path/removal.ts Lines 10 to 26 in beb3d69 How about moving the I personally don't like a boolean flag in a hot path such as the |
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [@babel/traverse](https://babel.dev/docs/en/next/babel-traverse) ([source](https://github.com/babel/babel/tree/HEAD/packages/babel-traverse)) | resolutions | minor | [`7.24.8` -> `7.25.6`](https://renovatebot.com/diffs/npm/@babel%2ftraverse/7.24.8/7.25.6) | --- ### Release Notes <details> <summary>babel/babel (@​babel/traverse)</summary> ### [`v7.25.6`](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7256-2024-08-29) [Compare Source](babel/babel@v7.25.4...v7.25.6) ##### 🐛 Bug Fix - `babel-generator` - [#​16783](babel/babel#16783) Properly print inner comments in TS array types ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - [#​16775](babel/babel#16775) fix: jsx whitespace is not properly preserved when retainLines ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-traverse` - [#​16727](babel/babel#16727) fix: `path.getAssignmentIdentifiers` may be `undefined` ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-parser` - [#​16761](babel/babel#16761) fix: improve static canFollowModifier checks ([@​JLHwung](https://github.com/JLHwung)) - `babel-helpers`, `babel-plugin-transform-optional-chaining`, `babel-runtime-corejs3` - [#​16769](babel/babel#16769) Only wrap functions in `superPropertyGet` helper ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ##### 💅 Polish - `babel-generator`, `babel-plugin-transform-async-to-generator`, `babel-plugin-transform-block-scoping`, `babel-plugin-transform-class-properties`, `babel-plugin-transform-classes`, `babel-plugin-transform-duplicate-named-capturing-groups-regex`, `babel-plugin-transform-named-capturing-groups-regex`, `babel-plugin-transform-react-jsx-development`, `babel-plugin-transform-react-jsx`, `babel-plugin-transform-react-pure-annotations`, `babel-plugin-transform-regenerator`, `babel-plugin-transform-runtime`, `babel-preset-env` - [#​16780](babel/babel#16780) Do not enforce printing space between `(` and comments ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-plugin-syntax-import-assertions`, `babel-plugin-syntax-import-attributes` - [#​16781](babel/babel#16781) Don't throw when enabling both syntax-import-{assertions,attributes} ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-generator` - [#​16782](babel/babel#16782) TS union/intersection nested in union does not need parens ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ##### 🏠 Internal - `babel-generator` - [#​16777](babel/babel#16777) Remove unused `parent` params in the generator ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ### [`v7.25.4`](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7254-2024-08-22) [Compare Source](babel/babel@v7.25.3...v7.25.4) ##### 🐛 Bug Fix - `babel-traverse` - [#​16756](babel/babel#16756) fix: Skip computed key when renaming ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-helper-create-class-features-plugin`, `babel-plugin-proposal-decorators` - [#​16755](babel/babel#16755) fix: Decorator 2018-09 may throw an exception ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-types` - [#​16710](babel/babel#16710) Visit AST fields nodes according to their syntactical order ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-generator` - [#​16709](babel/babel#16709) Print semicolon after TS `export namespace as A` ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ##### 💅 Polish - `babel-generator`, `babel-plugin-proposal-decorators`, `babel-plugin-proposal-destructuring-private`, `babel-plugin-proposal-pipeline-operator`, `babel-plugin-transform-class-properties`, `babel-plugin-transform-destructuring`, `babel-plugin-transform-optional-chaining`, `babel-plugin-transform-private-methods`, `babel-plugin-transform-private-property-in-object`, `babel-plugin-transform-typescript`, `babel-runtime-corejs2`, `babel-runtime`, `babel-traverse` - [#​16722](babel/babel#16722) Avoid unnecessary parens around sequence expressions ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-generator`, `babel-plugin-transform-class-properties` - [#​16714](babel/babel#16714) Avoid unnecessary parens around exported arrow functions ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-generator`, `babel-plugin-proposal-decorators`, `babel-plugin-proposal-destructuring-private`, `babel-plugin-transform-object-rest-spread` - [#​16712](babel/babel#16712) Avoid printing unnecessary parens around object destructuring ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ##### 🔬 Output optimization - `babel-generator` - [#​16740](babel/babel#16740) Avoid extra spaces between comments/regexps in compact mode ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ### [`v7.25.3`](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7253-2024-07-31) [Compare Source](babel/babel@v7.25.2...v7.25.3) ##### 🐛 Bug Fix - `babel-plugin-bugfix-firefox-class-in-computed-class-key`, `babel-traverse` - [#​16699](babel/babel#16699) Avoid validating visitors produced by `traverse.visitors.merge` ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ##### 🏠 Internal - `babel-parser` - [#​16688](babel/babel#16688) Add `@babel/types` as a dependency of `@babel/parser` ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ### [`v7.25.2`](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7252-2024-07-30) [Compare Source](babel/babel@v7.25.1...v7.25.2) ##### 🐛 Bug Fix - `babel-core`, `babel-traverse` - [#​16695](babel/babel#16695) Ensure that `requeueComputedKeyAndDecorators` is available ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ### [`v7.25.1`](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7251-2024-07-28) [Compare Source](babel/babel@v7.25.0...v7.25.1) ##### 🐛 Bug Fix - `babel-plugin-transform-function-name` - [#​16683](babel/babel#16683) fix: `ensureFunctionName` may be undefined ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-plugin-transform-react-constant-elements` - [#​16582](babel/babel#16582) fix plugin-transform-react-constant-elements transform JSXFrament but not add JSXExpressionContainer ([@​keiseiTi](https://github.com/keiseiTi)) - `babel-traverse` - [#​16587](babel/babel#16587) fix: fixed issue16583 + test ([@​nerodesu017](https://github.com/nerodesu017)) ##### 🏠 Internal - [#​16663](babel/babel#16663) Test eslint plugin against eslint 9 ([@​JLHwung](https://github.com/JLHwung)) ### [`v7.25.0`](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7250-2024-07-26) [Compare Source](babel/babel@v7.24.8...v7.25.0) ##### 👓 Spec Compliance - `babel-helpers`, `babel-plugin-proposal-explicit-resource-management`, `babel-runtime-corejs3` - [#​16537](babel/babel#16537) `await using` normative updates ([@​JLHwung](https://github.com/JLHwung)) - `babel-plugin-transform-typescript` - [#​16602](babel/babel#16602) Ensure enum members syntactically determinable to be strings do not get reverse mappings ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) ##### 🚀 New Feature - `babel-helper-create-class-features-plugin`, `babel-helper-function-name`, `babel-helper-plugin-utils`, `babel-helper-wrap-function`, `babel-plugin-bugfix-safari-class-field-initializer-scope`, `babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression`, `babel-plugin-transform-classes`, `babel-plugin-transform-function-name`, `babel-preset-env`, `babel-traverse`, `babel-types` - [#​16658](babel/babel#16658) Move `ensureFunctionName` to `NodePath.prototype` ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-helper-hoist-variables`, `babel-helper-plugin-utils`, `babel-plugin-proposal-async-do-expressions`, `babel-plugin-transform-modules-systemjs`, `babel-traverse` - [#​16644](babel/babel#16644) Move `hoistVariables` to `Scope.prototype` ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-helper-create-class-features-plugin`, `babel-helper-module-transforms`, `babel-helper-plugin-utils`, `babel-helper-split-export-declaration`, `babel-plugin-transform-classes`, `babel-traverse`, `babel-types` - [#​16645](babel/babel#16645) Move `splitExportDeclaration` to `NodePath.prototype` ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-helper-create-class-features-plugin`, `babel-helper-environment-visitor`, `babel-helper-module-transforms`, `babel-helper-plugin-utils`, `babel-helper-remap-async-to-generator`, `babel-helper-replace-supers`, `babel-plugin-bugfix-firefox-class-in-computed-class-key`, `babel-plugin-bugfix-v8-static-class-fields-redefine-readonly`, `babel-plugin-transform-async-generator-functions`, `babel-plugin-transform-classes`, `babel-traverse` - [#​16649](babel/babel#16649) Move `environment-visitor` helper into `@babel/traverse` ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-core`, `babel-parser` - [#​16480](babel/babel#16480) Expose wether a module has TLA or not as `.extra.async` ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-compat-data`, `babel-plugin-bugfix-safari-class-field-initializer-scope`, `babel-preset-env` - [#​16569](babel/babel#16569) Introduce `bugfix-safari-class-field-initializer-scope` ([@​davidtaylorhq](https://github.com/davidtaylorhq)) - `babel-plugin-transform-block-scoping`, `babel-traverse`, `babel-types` - [#​16551](babel/babel#16551) Add `NodePath#getAssignmentIdentifiers` ([@​JLHwung](https://github.com/JLHwung)) - `babel-helper-import-to-platform-api`, `babel-plugin-proposal-json-modules` - [#​16579](babel/babel#16579) Add `uncheckedRequire` option for JSON imports to CJS ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-helper-transform-fixture-test-runner`, `babel-node` - [#​16642](babel/babel#16642) Allow using custom config in `babel-node --eval` ([@​slatereax](https://github.com/slatereax)) - `babel-compat-data`, `babel-helper-create-regexp-features-plugin`, `babel-plugin-proposal-duplicate-named-capturing-groups-regex`, `babel-plugin-transform-duplicate-named-capturing-groups-regex`, `babel-preset-env`, `babel-standalone` - [#​16445](babel/babel#16445) Add `duplicate-named-capturing-groups-regex` to `preset-env` ([@​JLHwung](https://github.com/JLHwung)) ##### 🐛 Bug Fix - `babel-generator` - [#​16678](babel/babel#16678) Print parens around as expressions on the LHS ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-template`, `babel-types` - [#​15286](babel/babel#15286) fix: Props are lost when the template replaces the node ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) ##### 🏠 Internal - Other - [#​16674](babel/babel#16674) bump gulp to 5 ([@​JLHwung](https://github.com/JLHwung)) - `babel-generator` - [#​16651](babel/babel#16651) Simplify the printing logic for `(` before ambiguous tokens ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) - `babel-helper-function-name`, `babel-plugin-transform-arrow-functions`, `babel-plugin-transform-function-name`, `babel-preset-env`, `babel-traverse` - [#​16652](babel/babel#16652) Simplify `helper-function-name` logic ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ##### 🏃♀️ Performance - `babel-parser`, `babel-plugin-proposal-pipeline-operator` - [#​16461](babel/babel#16461) Some minor parser performance improvements for ts ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) ##### 🔬 Output optimization - `babel-plugin-transform-classes` - [#​16670](babel/babel#16670) Reduce redundant `assertThisInitialized` ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-helper-create-class-features-plugin`, `babel-helper-replace-supers`, `babel-helpers`, `babel-plugin-proposal-decorators`, `babel-plugin-transform-class-properties`, `babel-plugin-transform-classes`, `babel-plugin-transform-exponentiation-operator`, `babel-plugin-transform-object-super`, `babel-plugin-transform-private-methods`, `babel-runtime-corejs2`, `babel-runtime-corejs3`, `babel-runtime` - [#​16374](babel/babel#16374) Improve `super.x` output ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-plugin-transform-class-properties`, `babel-plugin-transform-classes` - [#​16656](babel/babel#16656) Simplify output for anonymous classes with no methods ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MzEuNCIsInVwZGF0ZWRJblZlciI6IjM3LjQzMS40IiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6WyJ0eXBlL2RlcGVuZGVuY2llcyJdfQ==--> Reviewed-on: https://git.vylpes.xyz/RabbitLabs/random-bunny/pulls/212 Reviewed-by: Vylpes <ethan@vylpes.com> Co-authored-by: Renovate Bot <renovate@vylpes.com> Co-committed-by: Renovate Bot <renovate@vylpes.com>



I have redone the solution for issue #16583.
The problem with it was that when removing a VariableDeclarator, the binding related to it was getting removed. If, after this, the VariableDeclaration parent was empty (check from removal hooks), the binding was removed a second time. If no binding with the same name was to be found in parent scopes, it wasn't problematic, but if a binding with the same name was in parent scopes, it was getting removed erroneously.