Skip to content

Commit

Permalink
fix imports in default arguments in functions (vercel/turborepo#3292)
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra authored Jan 12, 2023
1 parent 5687c4a commit 19be87b
Show file tree
Hide file tree
Showing 33 changed files with 237,706 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { named } from "./module.js";

function Fun({ value = named }) {
return value;
}

it("support imports in default arguments", () => {
expect(Fun({})).toBe("named");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const named = "named";
3 changes: 2 additions & 1 deletion crates/turbopack-ecmascript/src/analyzer/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ impl VisitAstPath for Analyzer<'_> {
let value = self.current_value.take();
for (index, p) in n.iter().enumerate() {
self.current_value = Some(JsValue::Argument(index));
p.visit_children_with_path(self, ast_path);
ast_path.with_index(index, |ast_path| p.visit_children_with_path(self, ast_path));
}
self.current_value = value;
}
Expand Down Expand Up @@ -1102,6 +1102,7 @@ impl VisitAstPath for Analyzer<'_> {
match &value {
Some(current_value) => {
self.visit_pat_with_value(pat, obj, current_value, ast_path);

// We should not call visit_children_with
return;
}
Expand Down
4 changes: 4 additions & 0 deletions crates/turbopack-ecmascript/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,7 @@ mod tests {
crate::register();
let graph_snapshot_path = input.with_file_name("graph.snapshot");
let graph_explained_snapshot_path = input.with_file_name("graph-explained.snapshot");
let graph_effects_snapshot_path = input.with_file_name("graph-effects.snapshot");
let resolved_explained_snapshot_path = input.with_file_name("resolved-explained.snapshot");

run_test(false, |cm, handler| {
Expand Down Expand Up @@ -2065,6 +2066,9 @@ mod tests {
NormalizedOutput::from(explain_all(&named_values))
.compare_to_file(&graph_explained_snapshot_path)
.unwrap();
NormalizedOutput::from(format!("{:#?}", var_graph.effects))
.compare_to_file(&graph_effects_snapshot_path)
.unwrap();
}

{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[
Call {
func: FreeVar(
Require,
),
args: [
Variable(
(
Atom('x' type=static),
#1,
),
),
],
ast_path: [
Program(
Module,
),
Module(
Body(
2,
),
),
ModuleItem(
ModuleDecl,
),
ModuleDecl(
ExportDecl,
),
ExportDecl(
Decl,
),
Decl(
Fn,
),
FnDecl(
Function,
),
Function(
Body,
),
BlockStmt(
Stmts(
0,
),
),
Stmt(
Expr,
),
ExprStmt(
Expr,
),
Expr(
Call,
),
],
span: Span {
lo: BytePos(
54,
),
hi: BytePos(
64,
),
ctxt: #0,
},
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading

0 comments on commit 19be87b

Please sign in to comment.