Regression: path.parentPath.get(path.listKey) can clobber a traversal's visitors with those from a separate traversal in the global path cache
See original GitHub issueBug Report
- I would like to work on a fix!
Input Code
const {parse} = require('@babel/core')
const generate = require('@babel/generator').default
const traverse = require('@babel/traverse').default
const code = `
import { Foo } from './Foo'
import { Bar } from './Bar'
`
const presets = [
['@babel/preset-env', { targets: { node: 'current' } }],
]
const ast = parse(code, {plugins, presets})
traverse(ast, {
Program(path) {
path.traverse({
ImportDeclaration: {
enter(path) {
console.log('ENTER', generate(path.node).code)
if (path.node.source.value === './Bar')
path.parentPath.get(path.listKey)
},
exit(path) {
console.log('EXIT ', generate(path.node).code)
}
}
})
}
})
Expected behavior
I haven’t bisected to find the version that introduced the regression yet, but with @babel/core, @babel/traverse, and @babel/preset-env 7.4.5, the exit visitor is called for both import statements:
ENTER import { Foo } from './Foo';
EXIT import { Foo } from './Foo';
ENTER import { Bar } from './Bar';
EXIT import { Bar } from './Bar';
Current behavior
With these versions: ├─ @babel/core@7.12.10 ├─ @babel/preset-env@7.12.11 └─ @babel/traverse@7.12.12
The exit visitor for the second import statement isn’t called because path.parentPath.get(path.listKey)
has the side effect of replacing path.opts
with those of the parent traversal (which only has a Program
visitor)
ENTER import { Foo } from './Foo';
EXIT import { Foo } from './Foo';
ENTER import { Bar } from './Bar';
(missing EXIT import { Bar } from './Bar'
)
Additional context
babel-plugin-flow-runtime
is no longer working after I upgraded @babel/*
, and I uncovered this issue debugging it.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (10 by maintainers)
Top Results From Across the Web
What is directory traversal, and how to prevent it? - PortSwigger
In this section, we'll explain what directory traversal is, describe how to carry out path traversal attacks and circumvent common obstacles, and spell...
Read more >Path Exploration in Google Analytics 4 (practical ... - YouTube
Learn how to use Path Exploration reports in Google Analytics 4 and see what your users do and how they behave on a...
Read more >The Path for Testing Path Traversal Vulnerabilities with Python
I have noticed an odd behavior in requests module in Python, which uses urllib3. I inspected the root cause via regression testing and...
Read more >Understanding directory symlinks traversals and the parent ...
bash "knows" about symlinks and tracks this info when you use a symlink to enter a directory. You can check this by doing...
Read more >repository-root.el - EmacsWiki
Download ;;; repository-root.el --- deduce the repository root directory for a given file ;; Copyright (C) 2008-2013 Avi Rozen ;; Author: ...
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 FreeTop 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
Top GitHub Comments
Huh I see. FWIW, I wonder if a different API design would be possible where the context information is passed around instead of being a property on the paths that gets mutated. This issue certainly wasn’t a showstopper for me but I did burn a lot of time in a painful debugging session on it.
@JLHwung oh interesting.
I noticed that
visitor
is already an argument topath.traverse
etc. And this issue involved traversal getting the visitors frompath.opts
in some cases, and that getting blown away by context changes. So aside from changes in that PR, I wonder if it would be possible for traversal to always use thevisitor
passed around via function arguments instead of anything related to context?