-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
fix(traverse): set the correct context when getting multiple NodePath
#12848
Conversation
fedeci
commented
Feb 22, 2021
•
edited by gitpod-io
bot
edited by gitpod-io
bot
Q | A |
---|---|
Fixed Issues? | Fixes #12570 |
Patch: Bug Fix? | Yes |
Major: Breaking Change? | |
Minor: New Feature? | |
Tests Added + Pass? | Yes |
Documentation PR Link | |
Any Dependency Changes? | |
License | MIT |
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/47769/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit c7db2a6:
|
Could you check if this also fixes #12631, which is another context-related bug? |
I applied the same changes to |
return NodePath.get({ | ||
listKey: key, | ||
parentPath: this, | ||
parent: node, | ||
container: container, | ||
key: i, | ||
}).setContext(context); | ||
}).setContext(v.context); |
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.
I think v.context
should be supplied as a fallback of context
.
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 the context defaults to true
, so if no specific context is passed it will be used the one of the current NodePath
(It may not be the right one).
Because of this we can't fallback since context is always defined when calling #get()
.
babel/packages/babel-traverse/src/path/family.ts
Lines 188 to 202 in 468a43c
function get<T extends t.Node>( | |
this: NodePath<T>, | |
key: string, | |
context: true | TraversalContext = true, | |
): NodePath | NodePath[] { | |
if (context === true) context = this.context; | |
const parts = key.split("."); | |
if (parts.length === 1) { | |
// "foo" | |
return this._getKey(key, context); | |
} else { | |
// "foo.bar" | |
return this._getPattern(parts, context); | |
} | |
} |
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.
In this case we can use v.context
when context
is true
, otherwise when a context
is indeed passed as TraversalContext
, we should respect the input, though.
468a43c
to
404539f
Compare
@@ -206,36 +205,39 @@ export { get }; | |||
export function _getKey<T extends t.Node>( | |||
this: NodePath<T>, | |||
key: string, | |||
context?: TraversalContext, | |||
context?: true | TraversalContext, |
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.
Just a couple questions.
Since this method is only called from #get()
this shouldn't be marked as optional because it will default to true when no value is provided. Right?
Also this is not expected to be called from third party plugins, then it shouldn't be exported?
} | ||
} | ||
|
||
export function _getPattern( | ||
this: NodePath, | ||
parts: string[], | ||
context?: TraversalContext, | ||
context?: true | TraversalContext, |
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.
The same for this.
404539f
to
c7db2a6
Compare
return NodePath.get({ | ||
listKey: key, | ||
parentPath: this, | ||
parent: node, | ||
container: container, | ||
key: i, | ||
}).setContext(context); | ||
}).setContext(isValidContext ? context : v.context); |
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.
v.context
is always undefined
because v
is a Node
not a NodePath
.