-
-
Notifications
You must be signed in to change notification settings - Fork 485
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
traverse: insert new statements(If any) after calling each walk_statement
in walk_statements
#4767
Comments
One more complete example here |
I've come up with another solution that doesn't require any changes to the Save the statements you need to insert under the statement in a The implementation of fn exit_statements(&mut self, stmts: &mut Vec<'a, Statement<'a>>, ctx: &mut TraverseCtx<'a>) {
let new_stmts = ctx.ast.vec();
for statement in stmts.drain(..) {
new_stmts.push(statement);
// Check if there are any statements that need to be inserted below this statement.
if let Some(ss) = StmtsHashMap.get(statement.id) {
new_stmts.extend(ss)
}
}
} |
walk_statement
in walk_statements
walk_statement
in walk_statements
That solution sounds good. I propose that we implement #4807 to support that. Further down the line, switching |
I've made a PR for |
That's great. Should we implement this inside a specific plugin, transformer, or traverse? This functionality is useful in many plugins, such as the TypeScript enum and arrow-function plugin. |
Actually, in this case I don't think you need either |
Oh, yes this is a workable approach |
close: #3943 ## Further improvements There is a double visit here. We need to collect all react hooks calling in `Function` and `ArrowFunctionExpression`. If we want to remove this implementation, we may wait for #4188. https://github.com/oxc-project/oxc/blob/d797e595d286c613848b773c256bd43124ad1981/crates/oxc_transformer/src/react/refresh.rs#L744-L947 ## Tests All tests copy from https://github.com/facebook/react/blob/main/packages/react-refresh/src/__tests__/ReactFresh-test.js There are still 4 tests that have not been passed **1. refresh/can-handle-implicit-arrow-returns/input.jsx** Related to #4767. transform correct, just output doesn't match the expected output **2. refresh/registers-identifiers-used-in-jsx-at-definition-site/input.jsx** **3. refresh/registers-identifiers-used-in-react-create-element-at-definition-site/input.jsx** Blocked by #4746 **4. refresh/supports-typescript-namespace-syntax/input.tsx** oxc transforms ts to js first, so probably we can ignore this case. If we really want to pass this test, we also need to turn off `TypeScript` plugin. ## What's next? ### Options: 1. Support transform `refresh_reg` and `refresh_sig` options to `MemberExpression`. Currently `import.meta.xxxx` still is an `Identifier` 2. Support `emit_full_signatures` option ### Other NAPI, testing in `monitor-oxc`, etc..
close: #3943 ## Further improvements There is a double visit here. We need to collect all react hooks calling in `Function` and `ArrowFunctionExpression`. If we want to remove this implementation, we may wait for #4188. https://github.com/oxc-project/oxc/blob/d797e595d286c613848b773c256bd43124ad1981/crates/oxc_transformer/src/react/refresh.rs#L744-L947 ## Tests All tests copy from https://github.com/facebook/react/blob/main/packages/react-refresh/src/__tests__/ReactFresh-test.js There are still 4 tests that have not been passed **1. refresh/can-handle-implicit-arrow-returns/input.jsx** Related to #4767. transform correct, just output doesn't match the expected output **2. refresh/registers-identifiers-used-in-jsx-at-definition-site/input.jsx** **3. refresh/registers-identifiers-used-in-react-create-element-at-definition-site/input.jsx** Blocked by #4746 **4. refresh/supports-typescript-namespace-syntax/input.tsx** oxc transforms ts to js first, so probably we can ignore this case. If we really want to pass this test, we also need to turn off `TypeScript` plugin. ## What's next? ### Options: 1. Support transform `refresh_reg` and `refresh_sig` options to `MemberExpression`. Currently `import.meta.xxxx` still is an `Identifier` 2. Support `emit_full_signatures` option ### Other NAPI, testing in `monitor-oxc`, etc..
Background
Here's an example of why we need to support it
Looking at the example above, we can see that we need to add the
var _sX = functionName
statement after the function statement, and transform arrow function expression to_sX(() => {})
.For
FunctionDeclaration
, we can easily do it inexit_statements
, but forArrowFunctionExpression
, we need to transform it inexit_arrow_function_expression
. Due to these exit_* functions execution order is different. The output will look like:As you can see from the output,
exit_arrow_function_expression
is executed first, thenexit_statements
. We need to make sure that the transformation order is consistent, so we can’t use exit_statements to implement it.Possible Solution
TraverseCtx
provides an API where we can store statements that we want to insert afterexit_statement
.node.insertAfter(new)
.The text was updated successfully, but these errors were encountered: