Skip to content
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

Do not use an ident for literals #95

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/tests/string_method_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ mod tests {
let js_file = "test.js".to_string();
let rewritten = rewrite_js(original_code, js_file).map_err(|e| e.to_string())?;
assert_that(&rewritten.code)
.contains("let __datadog_test_0, __datadog_test_1, __datadog_test_2;
const a = (__datadog_test_0 = 1, __datadog_test_1 = String.prototype.concat, __datadog_test_2 = a(), _ddiast.stringConcat(__datadog_test_1.call(__datadog_test_0, __datadog_test_2, 3), __datadog_test_1, __datadog_test_0, __datadog_test_2, 3));");
.contains("let __datadog_test_0, __datadog_test_1;
const a = (__datadog_test_0 = String.prototype.concat, __datadog_test_1 = a(), _ddiast.stringConcat(__datadog_test_0.call(1, __datadog_test_1, 3), __datadog_test_0, 1, __datadog_test_1, 3));");
Ok(())
}

Expand Down
12 changes: 7 additions & 5 deletions src/transform/call_expr_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,11 @@ fn replace_call_expr_if_csi_method_with_member(
let mut call_replacement = call.clone();

// __datadog_token_$i = a
let ident_replacement =
let ident_replacement_option =
ident_provider.get_temporal_ident_used_in_assignation(expr, &mut assignations, &span);

let ident_replacement = ident_replacement_option.map_or_else(|| expr.clone(), Expr::Ident);

let ident_callee = match member_expr_opt {
Some(member_expr) => {
// __datadog_token_$i2 = member
Expand All @@ -272,7 +274,7 @@ fn replace_call_expr_if_csi_method_with_member(
// __datadog_token_$i.substring
let member_expr = MemberExpr {
span,
obj: Box::new(Expr::Ident(ident_replacement.clone())),
obj: Box::new(ident_replacement.clone()),
prop: MemberProp::Ident(ident_name.clone()),
};

Expand All @@ -286,12 +288,12 @@ fn replace_call_expr_if_csi_method_with_member(
}
};

arguments.push(Expr::Ident(ident_replacement.clone()));
arguments.push(ident_replacement.clone());

// change callee to __datadog_token_$i2.call
call_replacement.callee = Callee::Expr(Box::new(Expr::Member(MemberExpr {
span,
obj: Box::new(Expr::Ident(ident_callee)),
obj: Box::new(ident_callee.map_or_else(|| expr.clone(), Expr::Ident)),
prop: MemberProp::Ident(IdentName::new(JsWord::from("call"), span)),
})));

Expand All @@ -311,7 +313,7 @@ fn replace_call_expr_if_csi_method_with_member(
0,
ExprOrSpread {
spread: None,
expr: Box::new(Expr::Ident(ident_replacement)),
expr: Box::new(ident_replacement),
},
);

Expand Down
16 changes: 8 additions & 8 deletions src/transform/operand_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ pub trait OperandHandler {
Expr::Ident(_) => {
if ident_mode == IdentMode::Replace {
operand.map_with_mut(|op| {
Expr::Ident(ident_provider.get_ident_used_in_assignation(
let ident = ident_provider.get_ident_used_in_assignation(
&op,
assignations,
arguments,
span,
))
);

ident.map_or(op, Expr::Ident)
})
} else {
arguments.push(operand.clone())
Expand Down Expand Up @@ -70,12 +72,10 @@ pub trait OperandHandler {
ident_provider: &mut dyn IdentProvider,
) {
operand.map_with_mut(|op| {
Expr::Ident(ident_provider.get_ident_used_in_assignation(
&op,
assignations,
arguments,
span,
))
let ident =
ident_provider.get_ident_used_in_assignation(&op, assignations, arguments, span);

ident.map_or(op, Expr::Ident)
})
}

Expand Down
15 changes: 11 additions & 4 deletions src/visitor/ident_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ pub trait IdentProvider {
assignations: &mut Vec<Expr>,
arguments: &mut Vec<Expr>,
span: &Span,
) -> Ident {
) -> Option<Ident> {
let id = self.get_temporal_ident_used_in_assignation(operand, assignations, span);

// store ident as argument
arguments.push(Expr::Ident(id.clone()));
arguments.push(
id.as_ref()
.map_or_else(|| operand.clone(), |ident| Expr::Ident(ident.clone())),
);

id
}
Expand All @@ -32,7 +35,11 @@ pub trait IdentProvider {
operand: &Expr,
assignations: &mut Vec<Expr>,
span: &Span,
) -> Ident {
) -> Option<Ident> {
if operand.is_lit() {
return None;
}

let next_ident = self.next_ident();
let (assign, id) = self.create_assign_expression(next_ident, operand, span);

Expand All @@ -41,7 +48,7 @@ pub trait IdentProvider {

assignations.push(Expr::Assign(assign));

id
Some(id)
}

fn create_assign_expression(
Expand Down
22 changes: 11 additions & 11 deletions test/string_method.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ _ddiast.stringSubstring(__datadog_test_1.call(__datadog_test_0, 2), __datadog_te
rewriteAndExpect(
js,
`{
let __datadog_test_0, __datadog_test_1, __datadog_test_2;
(__datadog_test_0 = "hello", __datadog_test_1 = String.prototype.concat, __datadog_test_2 = a, _ddiast.concat(\
__datadog_test_1.call(__datadog_test_0, __datadog_test_2, "world"), __datadog_test_1, __datadog_test_0, \
__datadog_test_2, "world"));
let __datadog_test_0, __datadog_test_1;
(__datadog_test_0 = String.prototype.concat, __datadog_test_1 = a, _ddiast.concat(\
__datadog_test_0.call("hello", __datadog_test_1, "world"), __datadog_test_0, "hello", \
__datadog_test_1, "world"));
}`
)
})
Expand All @@ -241,10 +241,10 @@ __datadog_test_0, "world", null), __datadog_test_1, __datadog_test_0, "world", n
rewriteAndExpect(
js,
`{
let __datadog_test_0, __datadog_test_1, __datadog_test_2;
(__datadog_test_0 = "hello", __datadog_test_1 = String.prototype.concat, __datadog_test_2 = a, \
_ddiast.concat(__datadog_test_1.call(__datadog_test_0, "world", __datadog_test_2), __datadog_test_1, \
__datadog_test_0, "world", __datadog_test_2));
let __datadog_test_0, __datadog_test_1;
(__datadog_test_0 = String.prototype.concat, __datadog_test_1 = a, \
_ddiast.concat(__datadog_test_0.call("hello", "world", __datadog_test_1), __datadog_test_0, \
"hello", "world", __datadog_test_1));
}`
)
})
Expand Down Expand Up @@ -289,9 +289,9 @@ __datadog_test_0, "world", __datadog_test_2));
const js = builder.build(`return 'a'.${method}(${args});`)
rewriteAndExpectAndExpectEval(
js,
builder.build(`let __datadog_test_0, __datadog_test_1;
return (__datadog_test_0 = 'a', __datadog_test_1 = __datadog_test_0.${method}, _ddiast.${method}(\
__datadog_test_1.call(__datadog_test_0${argsWithComma}), __datadog_test_1, __datadog_test_0${argsWithComma}));`)
builder.build(`let __datadog_test_0;
return (__datadog_test_0 = 'a'.${method}, _ddiast.${method}(\
__datadog_test_0.call('a'${argsWithComma}), __datadog_test_0, 'a'${argsWithComma}));`)
)
})
} else {
Expand Down
Loading