Remove CallExpression support (#6639)

Users MUST use keyword call syntax now.

Closes https://github.com/KittyCAD/modeling-app/issues/4600
This commit is contained in:
Adam Chalmers
2025-05-02 16:08:12 -05:00
committed by GitHub
parent 75916d4300
commit 4fe8741ea7
309 changed files with 51419 additions and 66399 deletions

View File

@ -19,9 +19,9 @@ use crate::{
modules::{ModuleId, ModulePath, ModuleRepr},
parsing::ast::types::{
Annotation, ArrayExpression, ArrayRangeExpression, BinaryExpression, BinaryOperator, BinaryPart, BodyItem,
CallExpression, CallExpressionKw, Expr, FunctionExpression, IfExpression, ImportPath, ImportSelector,
ItemVisibility, LiteralIdentifier, LiteralValue, MemberExpression, MemberObject, Name, Node, NodeRef,
ObjectExpression, PipeExpression, Program, TagDeclarator, Type, UnaryExpression, UnaryOperator,
CallExpressionKw, Expr, FunctionExpression, IfExpression, ImportPath, ImportSelector, ItemVisibility,
LiteralIdentifier, LiteralValue, MemberExpression, MemberObject, Name, Node, NodeRef, ObjectExpression,
PipeExpression, Program, TagDeclarator, Type, UnaryExpression, UnaryOperator,
},
source_range::SourceRange,
std::{
@ -668,7 +668,6 @@ impl ExecutorContext {
}
}
}
Expr::CallExpression(call_expression) => call_expression.execute(exec_state, self).await?,
Expr::CallExpressionKw(call_expression) => call_expression.execute(exec_state, self).await?,
Expr::PipeExpression(pipe_expression) => pipe_expression.get_result(exec_state, self).await?,
Expr::PipeSubstitution(pipe_substitution) => match statement_kind {
@ -755,7 +754,6 @@ impl BinaryPart {
BinaryPart::Literal(literal) => Ok(KclValue::from_literal((**literal).clone(), exec_state)),
BinaryPart::Name(name) => name.get_result(exec_state, ctx).await.cloned(),
BinaryPart::BinaryExpression(binary_expression) => binary_expression.get_result(exec_state, ctx).await,
BinaryPart::CallExpression(call_expression) => call_expression.execute(exec_state, ctx).await,
BinaryPart::CallExpressionKw(call_expression) => call_expression.execute(exec_state, ctx).await,
BinaryPart::UnaryExpression(unary_expression) => unary_expression.get_result(exec_state, ctx).await,
BinaryPart::MemberExpression(member_expression) => member_expression.get_result(exec_state),
@ -1426,12 +1424,6 @@ impl Node<CallExpressionKw> {
e.add_source_ranges(vec![callsite])
})?;
#[cfg(feature = "artifact-graph")]
if matches!(fn_src, FunctionSource::User { .. }) {
// Track return operation.
exec_state.global.operations.push(Operation::GroupEnd);
}
let result = return_value.ok_or_else(move || {
let mut source_ranges: Vec<SourceRange> = vec![callsite];
// We want to send the source range of the original function.
@ -1450,145 +1442,6 @@ impl Node<CallExpressionKw> {
}
}
impl Node<CallExpression> {
#[async_recursion]
pub async fn execute(&self, exec_state: &mut ExecState, ctx: &ExecutorContext) -> Result<KclValue, KclError> {
let fn_name = &self.callee;
let callsite = SourceRange::from(self);
let mut fn_args: Vec<Arg> = Vec::with_capacity(self.arguments.len());
for arg_expr in &self.arguments {
let metadata = Metadata {
source_range: SourceRange::from(arg_expr),
};
let value = ctx
.execute_expr(arg_expr, exec_state, &metadata, &[], StatementKind::Expression)
.await?;
let arg = Arg::new(value, SourceRange::from(arg_expr));
fn_args.push(arg);
}
let fn_args = fn_args; // remove mutability
match ctx.stdlib.get_either(fn_name) {
FunctionKind::Core(func) => {
if func.deprecated() {
exec_state.warn(CompilationError::err(
self.callee.as_source_range(),
format!("`{fn_name}` is deprecated, see the docs for a recommended replacement"),
));
}
#[cfg(feature = "artifact-graph")]
let op = if func.feature_tree_operation() {
let op_labeled_args = func
.args(false)
.iter()
.zip(&fn_args)
.map(|(k, arg)| {
(
k.name.clone(),
OpArg::new(OpKclValue::from(&arg.value), arg.source_range),
)
})
.collect();
Some(Operation::StdLibCall {
std_lib_fn: (&func).into(),
unlabeled_arg: None,
labeled_args: op_labeled_args,
source_range: callsite,
is_error: false,
})
} else {
None
};
// Attempt to call the function.
let args = crate::std::Args::new(
fn_args,
self.into(),
ctx.clone(),
exec_state.mod_local.pipe_value.clone().map(|v| Arg::new(v, callsite)),
);
let mut return_value = {
// Don't early-return in this block.
exec_state.mut_stack().push_new_env_for_rust_call();
let result = func.std_lib_fn()(exec_state, args).await;
exec_state.mut_stack().pop_env();
#[cfg(feature = "artifact-graph")]
if let Some(mut op) = op {
op.set_std_lib_call_is_error(result.is_err());
// Track call operation. We do this after the call
// since things like patternTransform may call user code
// before running, and we will likely want to use the
// return value. The call takes ownership of the args,
// so we need to build the op before the call.
exec_state.global.operations.push(op);
}
result
}?;
update_memory_for_tags_of_geometry(&mut return_value, exec_state)?;
Ok(return_value)
}
FunctionKind::UserDefined => {
let source_range = SourceRange::from(self);
// Clone the function so that we can use a mutable reference to
// exec_state.
let func = fn_name.get_result(exec_state, ctx).await?.clone();
// Track call operation.
#[cfg(feature = "artifact-graph")]
exec_state.global.operations.push(Operation::GroupBegin {
group: Group::FunctionCall {
name: Some(fn_name.to_string()),
function_source_range: func.function_def_source_range().unwrap_or_default(),
unlabeled_arg: None,
// TODO: Add the arguments for legacy positional parameters.
labeled_args: Default::default(),
},
source_range: callsite,
});
let Some(fn_src) = func.as_fn() else {
return Err(KclError::Semantic(KclErrorDetails {
message: "cannot call this because it isn't a function".to_string(),
source_ranges: vec![source_range],
}));
};
let return_value = fn_src
.call(Some(fn_name.to_string()), exec_state, ctx, fn_args, source_range)
.await
.map_err(|e| {
// Add the call expression to the source ranges.
// TODO currently ignored by the frontend
e.add_source_ranges(vec![source_range])
})?;
let result = return_value.ok_or_else(move || {
let mut source_ranges: Vec<SourceRange> = vec![source_range];
// We want to send the source range of the original function.
if let KclValue::Function { meta, .. } = func {
source_ranges = meta.iter().map(|m| m.source_range).collect();
};
KclError::UndefinedValue(KclErrorDetails {
message: format!("Result of function {} is undefined", fn_name),
source_ranges,
})
})?;
// Track return operation.
#[cfg(feature = "artifact-graph")]
exec_state.global.operations.push(Operation::GroupEnd);
Ok(result)
}
}
}
}
fn update_memory_for_tags_of_geometry(result: &mut KclValue, exec_state: &mut ExecState) -> Result<(), KclError> {
// If the return result is a sketch or solid, we want to update the
// memory for the tags of the group.
@ -2474,7 +2327,15 @@ impl FunctionSource {
});
}
call_user_defined_function_kw(fn_name.as_deref(), args.kw_args, *memory, ast, exec_state, ctx).await
let result =
call_user_defined_function_kw(fn_name.as_deref(), args.kw_args, *memory, ast, exec_state, ctx)
.await;
// Track return operation.
#[cfg(feature = "artifact-graph")]
exec_state.global.operations.push(Operation::GroupEnd);
result
}
FunctionSource::None => unreachable!(),
}

View File

@ -263,20 +263,6 @@ impl KclValue {
}
}
#[cfg(feature = "artifact-graph")]
pub(crate) fn function_def_source_range(&self) -> Option<SourceRange> {
let KclValue::Function {
value: FunctionSource::User { ast, .. },
..
} = self
else {
return None;
};
// TODO: It would be nice if we could extract the source range starting
// at the fn, but that's the variable declaration.
Some(ast.as_source_range())
}
#[allow(unused)]
pub(crate) fn none() -> Self {
Self::KclNone {

View File

@ -6,8 +6,7 @@ use crate::{
execution::{types::UnitLen, PlaneInfo, Point3d},
lint::rule::{def_finding, Discovered, Finding},
parsing::ast::types::{
BinaryPart, CallExpression, CallExpressionKw, Expr, LiteralValue, Node as AstNode, ObjectExpression, Program,
UnaryOperator,
BinaryPart, CallExpressionKw, Expr, LiteralValue, Node as AstNode, ObjectExpression, Program, UnaryOperator,
},
walk::Node,
SourceRange,
@ -128,37 +127,11 @@ fn get_offset(info: &PlaneInfo) -> Option<f64> {
pub fn start_sketch_on_check_specific_plane(node: Node) -> Result<Option<(SourceRange, PlaneName, f64)>> {
match node {
Node::CallExpression(node) => start_sketch_on_check_specific_plane_pos(node),
Node::CallExpressionKw(node) => start_sketch_on_check_specific_plane_kw(node),
_ => Ok(None),
}
}
pub fn start_sketch_on_check_specific_plane_pos(
call: &AstNode<CallExpression>,
) -> Result<Option<(SourceRange, PlaneName, f64)>> {
if call.inner.callee.inner.name.name != "startSketchOn" {
return Ok(None);
}
if call.arguments.len() != 1 {
// we only look for single-argument object patterns, if there's more
// than that we don't have a plane decl
return Ok(None);
}
let call_source_range = SourceRange::new(
call.arguments[0].start(),
call.arguments[0].end(),
call.arguments[0].module_id(),
);
let Expr::ObjectExpression(arg) = &call.arguments[0] else {
return Ok(None);
};
common(arg, call_source_range)
}
pub fn start_sketch_on_check_specific_plane_kw(
call: &AstNode<CallExpressionKw>,
) -> Result<Option<(SourceRange, PlaneName, f64)>> {

View File

@ -89,7 +89,6 @@ impl Expr {
Expr::FunctionExpression(function_expression) => {
function_expression.get_hover_value_for_position(pos, code, opts)
}
Expr::CallExpression(call_expression) => call_expression.get_hover_value_for_position(pos, code, opts),
Expr::CallExpressionKw(call_expression) => call_expression.get_hover_value_for_position(pos, code, opts),
Expr::PipeExpression(pipe_expression) => pipe_expression.get_hover_value_for_position(pos, code, opts),
Expr::ArrayExpression(array_expression) => array_expression.get_hover_value_for_position(pos, code, opts),
@ -144,9 +143,6 @@ impl BinaryPart {
BinaryPart::BinaryExpression(binary_expression) => {
binary_expression.get_hover_value_for_position(pos, code, opts)
}
BinaryPart::CallExpression(call_expression) => {
call_expression.get_hover_value_for_position(pos, code, opts)
}
BinaryPart::CallExpressionKw(call_expression) => {
call_expression.get_hover_value_for_position(pos, code, opts)
}
@ -161,35 +157,6 @@ impl BinaryPart {
}
}
impl CallExpression {
fn get_hover_value_for_position(&self, pos: usize, code: &str, opts: &HoverOpts) -> Option<Hover> {
let callee_source_range: SourceRange = self.callee.clone().into();
if callee_source_range.contains(pos) {
return Some(Hover::Function {
name: self.callee.to_string(),
range: callee_source_range.to_lsp_range(code),
});
}
for (index, arg) in self.arguments.iter().enumerate() {
let source_range: SourceRange = arg.into();
if source_range.contains(pos) {
return if opts.prefer_sig {
Some(Hover::Signature {
name: self.callee.to_string(),
parameter_index: index as u32,
range: source_range.to_lsp_range(code),
})
} else {
arg.get_hover_value_for_position(pos, code, opts)
};
}
}
None
}
}
impl CallExpressionKw {
fn get_hover_value_for_position(&self, pos: usize, code: &str, opts: &HoverOpts) -> Option<Hover> {
let callee_source_range: SourceRange = self.callee.clone().into();

View File

@ -516,23 +516,6 @@ impl Backend {
}
return get_modifier(vec![SemanticTokenModifier::DECLARATION]);
}
crate::walk::Node::CallExpression(call_expr) => {
let sr: SourceRange = (&call_expr.callee).into();
if sr.contains(source_range.start()) {
let mut ti = token_index.lock().map_err(|_| anyhow::anyhow!("mutex"))?;
*ti = match self.get_semantic_token_type_index(&SemanticTokenType::FUNCTION) {
Some(index) => index,
None => token_type_index,
};
if self.stdlib_completions.contains_key(&call_expr.callee.name.name) {
// This is a stdlib function.
return get_modifier(vec![SemanticTokenModifier::DEFAULT_LIBRARY]);
}
return Ok(false);
}
}
crate::walk::Node::CallExpressionKw(call_expr) => {
let sr: SourceRange = (&call_expr.callee).into();
if sr.contains(source_range.start()) {

View File

@ -2,11 +2,11 @@ use sha2::{Digest as DigestTrait, Sha256};
use crate::parsing::ast::types::{
Annotation, ArrayExpression, ArrayRangeExpression, AscribedExpression, BinaryExpression, BinaryPart, BodyItem,
CallExpression, CallExpressionKw, DefaultParamVal, ElseIf, Expr, ExpressionStatement, FunctionExpression,
Identifier, IfExpression, ImportItem, ImportSelector, ImportStatement, ItemVisibility, KclNone, LabelledExpression,
Literal, LiteralIdentifier, LiteralValue, MemberExpression, MemberObject, Name, ObjectExpression, ObjectProperty,
Parameter, PipeExpression, PipeSubstitution, PrimitiveType, Program, ReturnStatement, TagDeclarator, Type,
TypeDeclaration, UnaryExpression, VariableDeclaration, VariableDeclarator, VariableKind,
CallExpressionKw, DefaultParamVal, ElseIf, Expr, ExpressionStatement, FunctionExpression, Identifier, IfExpression,
ImportItem, ImportSelector, ImportStatement, ItemVisibility, KclNone, LabelledExpression, Literal,
LiteralIdentifier, LiteralValue, MemberExpression, MemberObject, Name, ObjectExpression, ObjectProperty, Parameter,
PipeExpression, PipeSubstitution, PrimitiveType, Program, ReturnStatement, TagDeclarator, Type, TypeDeclaration,
UnaryExpression, VariableDeclaration, VariableDeclarator, VariableKind,
};
/// Position-independent digest of the AST node.
@ -132,7 +132,6 @@ impl Expr {
Expr::TagDeclarator(tag) => tag.compute_digest(),
Expr::BinaryExpression(be) => be.compute_digest(),
Expr::FunctionExpression(fe) => fe.compute_digest(),
Expr::CallExpression(ce) => ce.compute_digest(),
Expr::CallExpressionKw(ce) => ce.compute_digest(),
Expr::PipeExpression(pe) => pe.compute_digest(),
Expr::PipeSubstitution(ps) => ps.compute_digest(),
@ -159,7 +158,6 @@ impl BinaryPart {
BinaryPart::Literal(lit) => lit.compute_digest(),
BinaryPart::Name(id) => id.compute_digest(),
BinaryPart::BinaryExpression(be) => be.compute_digest(),
BinaryPart::CallExpression(ce) => ce.compute_digest(),
BinaryPart::CallExpressionKw(ce) => ce.compute_digest(),
BinaryPart::UnaryExpression(ue) => ue.compute_digest(),
BinaryPart::MemberExpression(me) => me.compute_digest(),
@ -480,16 +478,6 @@ impl PipeExpression {
});
}
impl CallExpression {
compute_digest!(|slf, hasher| {
hasher.update(slf.callee.compute_digest());
hasher.update(slf.arguments.len().to_ne_bytes());
for argument in slf.arguments.iter_mut() {
hasher.update(argument.compute_digest());
}
});
}
impl CallExpressionKw {
compute_digest!(|slf, hasher| {
hasher.update(slf.callee.compute_digest());

View File

@ -26,7 +26,6 @@ impl Expr {
Expr::TagDeclarator(tag) => tag.module_id,
Expr::BinaryExpression(binary_expression) => binary_expression.module_id,
Expr::FunctionExpression(function_expression) => function_expression.module_id,
Expr::CallExpression(call_expression) => call_expression.module_id,
Expr::CallExpressionKw(call_expression) => call_expression.module_id,
Expr::PipeExpression(pipe_expression) => pipe_expression.module_id,
Expr::PipeSubstitution(pipe_substitution) => pipe_substitution.module_id,
@ -49,7 +48,6 @@ impl BinaryPart {
BinaryPart::Literal(literal) => literal.module_id,
BinaryPart::Name(identifier) => identifier.module_id,
BinaryPart::BinaryExpression(binary_expression) => binary_expression.module_id,
BinaryPart::CallExpression(call_expression) => call_expression.module_id,
BinaryPart::CallExpressionKw(call_expression) => call_expression.module_id,
BinaryPart::UnaryExpression(unary_expression) => unary_expression.module_id,
BinaryPart::MemberExpression(member_expression) => member_expression.module_id,

View File

@ -932,7 +932,6 @@ pub enum Expr {
TagDeclarator(BoxNode<TagDeclarator>),
BinaryExpression(BoxNode<BinaryExpression>),
FunctionExpression(BoxNode<FunctionExpression>),
CallExpression(BoxNode<CallExpression>),
CallExpressionKw(BoxNode<CallExpressionKw>),
PipeExpression(BoxNode<PipeExpression>),
PipeSubstitution(BoxNode<PipeSubstitution>),
@ -978,7 +977,6 @@ impl Expr {
Expr::MemberExpression(_mem_exp) => None,
Expr::Literal(_literal) => None,
Expr::FunctionExpression(_func_exp) => None,
Expr::CallExpression(_call_exp) => None,
Expr::CallExpressionKw(_call_exp) => None,
Expr::Name(_ident) => None,
Expr::TagDeclarator(_tag) => None,
@ -1006,7 +1004,6 @@ impl Expr {
Expr::MemberExpression(_) => {}
Expr::Literal(_) => {}
Expr::FunctionExpression(ref mut func_exp) => func_exp.replace_value(source_range, new_value),
Expr::CallExpression(ref mut call_exp) => call_exp.replace_value(source_range, new_value),
Expr::CallExpressionKw(ref mut call_exp) => call_exp.replace_value(source_range, new_value),
Expr::Name(_) => {}
Expr::TagDeclarator(_) => {}
@ -1027,7 +1024,6 @@ impl Expr {
Expr::TagDeclarator(tag) => tag.start,
Expr::BinaryExpression(binary_expression) => binary_expression.start,
Expr::FunctionExpression(function_expression) => function_expression.start,
Expr::CallExpression(call_expression) => call_expression.start,
Expr::CallExpressionKw(call_expression) => call_expression.start,
Expr::PipeExpression(pipe_expression) => pipe_expression.start,
Expr::PipeSubstitution(pipe_substitution) => pipe_substitution.start,
@ -1050,7 +1046,6 @@ impl Expr {
Expr::TagDeclarator(tag) => tag.end,
Expr::BinaryExpression(binary_expression) => binary_expression.end,
Expr::FunctionExpression(function_expression) => function_expression.end,
Expr::CallExpression(call_expression) => call_expression.end,
Expr::CallExpressionKw(call_expression) => call_expression.end,
Expr::PipeExpression(pipe_expression) => pipe_expression.end,
Expr::PipeSubstitution(pipe_substitution) => pipe_substitution.end,
@ -1081,7 +1076,6 @@ impl Expr {
binary_expression.rename_identifiers(old_name, new_name)
}
Expr::FunctionExpression(_function_identifier) => {}
Expr::CallExpression(ref mut call_expression) => call_expression.rename_identifiers(old_name, new_name),
Expr::CallExpressionKw(ref mut call_expression) => call_expression.rename_identifiers(old_name, new_name),
Expr::PipeExpression(ref mut pipe_expression) => pipe_expression.rename_identifiers(old_name, new_name),
Expr::PipeSubstitution(_) => {}
@ -1110,7 +1104,6 @@ impl Expr {
Expr::BinaryExpression(binary_expression) => binary_expression.get_constraint_level(),
Expr::FunctionExpression(function_identifier) => function_identifier.get_constraint_level(),
Expr::CallExpression(call_expression) => call_expression.get_constraint_level(),
Expr::CallExpressionKw(call_expression) => call_expression.get_constraint_level(),
Expr::PipeExpression(pipe_expression) => pipe_expression.get_constraint_level(),
Expr::PipeSubstitution(pipe_substitution) => ConstraintLevel::Ignore {
@ -1128,16 +1121,6 @@ impl Expr {
}
}
pub fn has_substitution_arg(&self) -> bool {
match self {
Expr::CallExpression(call_expression) => call_expression.has_substitution_arg(),
Expr::CallExpressionKw(call_expression) => call_expression.has_substitution_arg(),
Expr::LabelledExpression(expr) => expr.expr.has_substitution_arg(),
Expr::AscribedExpression(expr) => expr.expr.has_substitution_arg(),
_ => false,
}
}
pub fn literal_bool(&self) -> Option<bool> {
match self {
Expr::Literal(lit) => match lit.value {
@ -1194,7 +1177,6 @@ impl From<&BinaryPart> for Expr {
BinaryPart::Literal(literal) => Expr::Literal(literal.clone()),
BinaryPart::Name(name) => Expr::Name(name.clone()),
BinaryPart::BinaryExpression(binary_expression) => Expr::BinaryExpression(binary_expression.clone()),
BinaryPart::CallExpression(call_expression) => Expr::CallExpression(call_expression.clone()),
BinaryPart::CallExpressionKw(call_expression) => Expr::CallExpressionKw(call_expression.clone()),
BinaryPart::UnaryExpression(unary_expression) => Expr::UnaryExpression(unary_expression.clone()),
BinaryPart::MemberExpression(member_expression) => Expr::MemberExpression(member_expression.clone()),
@ -1261,7 +1243,6 @@ pub enum BinaryPart {
Literal(BoxNode<Literal>),
Name(BoxNode<Name>),
BinaryExpression(BoxNode<BinaryExpression>),
CallExpression(BoxNode<CallExpression>),
CallExpressionKw(BoxNode<CallExpressionKw>),
UnaryExpression(BoxNode<UnaryExpression>),
MemberExpression(BoxNode<MemberExpression>),
@ -1287,7 +1268,6 @@ impl BinaryPart {
BinaryPart::Literal(literal) => literal.get_constraint_level(),
BinaryPart::Name(identifier) => identifier.get_constraint_level(),
BinaryPart::BinaryExpression(binary_expression) => binary_expression.get_constraint_level(),
BinaryPart::CallExpression(call_expression) => call_expression.get_constraint_level(),
BinaryPart::CallExpressionKw(call_expression) => call_expression.get_constraint_level(),
BinaryPart::UnaryExpression(unary_expression) => unary_expression.get_constraint_level(),
BinaryPart::MemberExpression(member_expression) => member_expression.get_constraint_level(),
@ -1302,9 +1282,6 @@ impl BinaryPart {
BinaryPart::BinaryExpression(ref mut binary_expression) => {
binary_expression.replace_value(source_range, new_value)
}
BinaryPart::CallExpression(ref mut call_expression) => {
call_expression.replace_value(source_range, new_value)
}
BinaryPart::CallExpressionKw(ref mut call_expression) => {
call_expression.replace_value(source_range, new_value)
}
@ -1321,7 +1298,6 @@ impl BinaryPart {
BinaryPart::Literal(literal) => literal.start,
BinaryPart::Name(identifier) => identifier.start,
BinaryPart::BinaryExpression(binary_expression) => binary_expression.start,
BinaryPart::CallExpression(call_expression) => call_expression.start,
BinaryPart::CallExpressionKw(call_expression) => call_expression.start,
BinaryPart::UnaryExpression(unary_expression) => unary_expression.start,
BinaryPart::MemberExpression(member_expression) => member_expression.start,
@ -1334,7 +1310,6 @@ impl BinaryPart {
BinaryPart::Literal(literal) => literal.end,
BinaryPart::Name(identifier) => identifier.end,
BinaryPart::BinaryExpression(binary_expression) => binary_expression.end,
BinaryPart::CallExpression(call_expression) => call_expression.end,
BinaryPart::CallExpressionKw(call_expression) => call_expression.end,
BinaryPart::UnaryExpression(unary_expression) => unary_expression.end,
BinaryPart::MemberExpression(member_expression) => member_expression.end,
@ -1350,9 +1325,6 @@ impl BinaryPart {
BinaryPart::BinaryExpression(ref mut binary_expression) => {
binary_expression.rename_identifiers(old_name, new_name)
}
BinaryPart::CallExpression(ref mut call_expression) => {
call_expression.rename_identifiers(old_name, new_name)
}
BinaryPart::CallExpressionKw(ref mut call_expression) => {
call_expression.rename_identifiers(old_name, new_name)
}
@ -1844,18 +1816,6 @@ pub struct ExpressionStatement {
pub digest: Option<Digest>,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(tag = "type")]
pub struct CallExpression {
pub callee: Node<Name>,
pub arguments: Vec<Expr>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub digest: Option<Digest>,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase", tag = "type")]
@ -1881,37 +1841,12 @@ pub struct LabeledArg {
pub arg: Expr,
}
impl From<Node<CallExpression>> for Expr {
fn from(call_expression: Node<CallExpression>) -> Self {
Expr::CallExpression(Box::new(call_expression))
}
}
impl From<Node<CallExpressionKw>> for Expr {
fn from(call_expression: Node<CallExpressionKw>) -> Self {
Expr::CallExpressionKw(Box::new(call_expression))
}
}
impl Node<CallExpression> {
/// Return the constraint level for this call expression.
pub fn get_constraint_level(&self) -> ConstraintLevel {
if self.arguments.is_empty() {
return ConstraintLevel::Ignore {
source_ranges: vec![self.into()],
};
}
// Iterate over the arguments and get the constraint level for each one.
let mut constraint_levels = ConstraintLevels::new();
for arg in &self.arguments {
constraint_levels.push(arg.get_constraint_level());
}
constraint_levels.get_constraint_level(self.into())
}
}
impl Node<CallExpressionKw> {
/// Return the constraint level for this call expression.
pub fn get_constraint_level(&self) -> ConstraintLevel {
@ -1931,38 +1866,6 @@ impl Node<CallExpressionKw> {
}
}
impl CallExpression {
pub fn new(name: &str, arguments: Vec<Expr>) -> Result<Node<Self>, KclError> {
Ok(Node::no_src(Self {
callee: Name::new(name),
arguments,
digest: None,
}))
}
/// Is at least one argument the '%' i.e. the substitution operator?
pub fn has_substitution_arg(&self) -> bool {
self.arguments
.iter()
.any(|arg| matches!(arg, Expr::PipeSubstitution(_)))
}
pub fn replace_value(&mut self, source_range: SourceRange, new_value: Expr) {
for arg in &mut self.arguments {
arg.replace_value(source_range, new_value.clone());
}
}
/// Rename all identifiers that have the old name to the new given name.
fn rename_identifiers(&mut self, old_name: &str, new_name: &str) {
self.callee.rename(old_name, new_name);
for arg in &mut self.arguments {
arg.rename_identifiers(old_name, new_name);
}
}
}
impl CallExpressionKw {
pub fn new(name: &str, unlabeled: Option<Expr>, arguments: Vec<LabeledArg>) -> Result<Node<Self>, KclError> {
Ok(Node::no_src(Self {
@ -1982,13 +1885,6 @@ impl CallExpressionKw {
.chain(self.arguments.iter().map(|arg| (Some(&arg.label), &arg.arg)))
}
/// Is at least one argument the '%' i.e. the substitution operator?
pub fn has_substitution_arg(&self) -> bool {
self.arguments
.iter()
.any(|arg| matches!(arg.arg, Expr::PipeSubstitution(_)))
}
pub fn replace_value(&mut self, source_range: SourceRange, new_value: Expr) {
for arg in &mut self.arguments {
arg.arg.replace_value(source_range, new_value.clone());
@ -4188,13 +4084,8 @@ cylinder = startSketchOn(-XZ)
};
oe
}
Expr::CallExpression(ce) => {
let Expr::ObjectExpression(ref oe) = (ce.arguments).first().unwrap() else {
panic!("expected an object!");
};
oe
}
other => panic!("expected a Call or CallKw, found {other:?}"),
other => panic!("expected a CallKw, found {other:?}"),
};
assert_eq!(oe.properties.len(), 2);

View File

@ -177,18 +177,6 @@ impl NodePath {
}
}
}
Expr::CallExpression(node) => {
if node.callee.contains_range(&range) {
path.push(Step::CallCallee);
return Some(path);
}
for (i, arg) in node.arguments.iter().enumerate() {
if arg.contains_range(&range) {
path.push(Step::CallArg { index: i });
return Self::from_expr(arg, range, path);
}
}
}
Expr::CallExpressionKw(node) => {
if node.callee.contains_range(&range) {
path.push(Step::CallKwCallee);

View File

@ -14,7 +14,7 @@ use winnow::{
};
use super::{
ast::types::{AscribedExpression, CallExpression, ImportPath, LabelledExpression},
ast::types::{AscribedExpression, ImportPath, LabelledExpression},
token::{NumericSuffix, RESERVED_WORDS},
DeprecationKind,
};
@ -630,7 +630,6 @@ fn operand(i: &mut TokenSlice) -> PResult<BinaryPart> {
Expr::Literal(x) => BinaryPart::Literal(x),
Expr::Name(x) => BinaryPart::Name(x),
Expr::BinaryExpression(x) => BinaryPart::BinaryExpression(x),
Expr::CallExpression(x) => BinaryPart::CallExpression(x),
Expr::CallExpressionKw(x) => BinaryPart::CallExpressionKw(x),
Expr::MemberExpression(x) => BinaryPart::MemberExpression(x),
Expr::IfExpression(x) => BinaryPart::IfExpression(x),
@ -2030,7 +2029,6 @@ fn expr_allowed_in_pipe_expr(i: &mut TokenSlice) -> PResult<Expr> {
bool_value.map(Expr::Literal),
tag.map(Box::new).map(Expr::TagDeclarator),
literal.map(Expr::Literal),
fn_call.map(Box::new).map(Expr::CallExpression),
fn_call_kw.map(Box::new).map(Expr::CallExpressionKw),
name.map(Box::new).map(Expr::Name),
array,
@ -2050,7 +2048,6 @@ fn possible_operands(i: &mut TokenSlice) -> PResult<Expr> {
bool_value.map(Expr::Literal),
member_expression.map(Box::new).map(Expr::MemberExpression),
literal.map(Expr::Literal),
fn_call.map(Box::new).map(Expr::CallExpression),
fn_call_kw.map(Box::new).map(Expr::CallExpressionKw),
name.map(Box::new).map(Expr::Name),
binary_expr_in_parens.map(Box::new).map(Expr::BinaryExpression),
@ -2711,13 +2708,6 @@ fn pipe_sep(i: &mut TokenSlice) -> PResult<()> {
Ok(())
}
/// Arguments are passed into a function.
fn arguments(i: &mut TokenSlice) -> PResult<Vec<Expr>> {
separated(0.., expression, comma_sep)
.context(expected("function arguments"))
.parse_next(i)
}
fn labeled_argument(i: &mut TokenSlice) -> PResult<LabeledArg> {
separated_pair(
terminated(nameable_identifier, opt(whitespace)),
@ -2986,11 +2976,7 @@ fn binding_name(i: &mut TokenSlice) -> PResult<Node<Identifier>> {
/// Either a positional or keyword function call.
fn fn_call_pos_or_kw(i: &mut TokenSlice) -> PResult<Expr> {
alt((
fn_call.map(Box::new).map(Expr::CallExpression),
fn_call_kw.map(Box::new).map(Expr::CallExpressionKw),
))
.parse_next(i)
alt((fn_call_kw.map(Box::new).map(Expr::CallExpressionKw),)).parse_next(i)
}
fn labelled_fn_call(i: &mut TokenSlice) -> PResult<Expr> {
@ -3003,43 +2989,6 @@ fn labelled_fn_call(i: &mut TokenSlice) -> PResult<Expr> {
}
}
fn fn_call(i: &mut TokenSlice) -> PResult<Node<CallExpression>> {
let fn_name = name(i)?;
opt(whitespace).parse_next(i)?;
let _ = terminated(open_paren, opt(whitespace)).parse_next(i)?;
let args = arguments(i)?;
let end = preceded(opt(whitespace), close_paren).parse_next(i)?.end;
let result = Node::new_node(
fn_name.start,
end,
fn_name.module_id,
CallExpression {
callee: fn_name,
arguments: args,
digest: None,
},
);
let callee_str = result.callee.name.name.to_string();
if let Some(suggestion) = super::deprecation(&callee_str, DeprecationKind::Function) {
ParseContext::warn(
CompilationError::err(
result.as_source_range(),
format!("Calling `{}` is deprecated, prefer using `{}`.", callee_str, suggestion),
)
.with_suggestion(
format!("Replace `{}` with `{}`", callee_str, suggestion),
suggestion,
None,
Tag::Deprecated,
),
);
}
Ok(result)
}
fn fn_call_kw(i: &mut TokenSlice) -> PResult<Node<CallExpressionKw>> {
let fn_name = name(i)?;
opt(whitespace).parse_next(i)?;
@ -3231,18 +3180,6 @@ mod tests {
assert_reserved("import");
}
#[test]
fn parse_args() {
for (i, (test, expected_len)) in [("someVar", 1), ("5, 3", 2), (r#""a""#, 1)].into_iter().enumerate() {
let tokens = crate::parsing::token::lex(test, ModuleId::default()).unwrap();
let actual = match arguments.parse(tokens.as_slice()) {
Ok(x) => x,
Err(e) => panic!("Failed test {i}, could not parse function arguments from \"{test}\": {e:?}"),
};
assert_eq!(actual.len(), expected_len, "failed test {i}");
}
}
#[test]
fn parse_names() {
for (test, expected_len) in [("someVar", 0), ("::foo", 0), ("foo::bar::baz", 2)] {

View File

@ -19,24 +19,6 @@ expression: actual
"init": {
"body": [
{
"arguments": [
{
"abs_path": false,
"commentStart": 26,
"end": 28,
"name": {
"commentStart": 26,
"end": 28,
"name": "XY",
"start": 26,
"type": "Identifier"
},
"path": [],
"start": 26,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 12,
@ -55,8 +37,24 @@ expression: actual
"commentStart": 12,
"end": 29,
"start": 12,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 26,
"end": 28,
"name": {
"commentStart": 26,
"end": 28,
"name": "XY",
"start": 26,
"type": "Identifier"
},
"path": [],
"start": 26,
"type": "Name",
"type": "Name"
}
},
{
"arguments": [

View File

@ -63,7 +63,6 @@ expression: actual
"commentStart": 56,
"end": 74,
"expression": {
"arguments": [],
"callee": {
"abs_path": false,
"commentStart": 56,
@ -82,8 +81,9 @@ expression: actual
"commentStart": 56,
"end": 74,
"start": 56,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": null
},
"start": 56,
"type": "ExpressionStatement",

View File

@ -71,17 +71,6 @@ expression: actual
"commentStart": 48,
"end": 60,
"expression": {
"arguments": [
{
"commentStart": 54,
"end": 59,
"raw": "false",
"start": 54,
"type": "Literal",
"type": "Literal",
"value": false
}
],
"callee": {
"abs_path": false,
"commentStart": 48,
@ -100,8 +89,17 @@ expression: actual
"commentStart": 48,
"end": 60,
"start": 48,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"commentStart": 54,
"end": 59,
"raw": "false",
"start": 54,
"type": "Literal",
"type": "Literal",
"value": false
}
},
"start": 48,
"type": "ExpressionStatement",

View File

@ -19,24 +19,6 @@ expression: actual
"init": {
"body": [
{
"arguments": [
{
"abs_path": false,
"commentStart": 25,
"end": 27,
"name": {
"commentStart": 25,
"end": 27,
"name": "XY",
"start": 25,
"type": "Identifier"
},
"path": [],
"start": 25,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 11,
@ -55,8 +37,24 @@ expression: actual
"commentStart": 11,
"end": 28,
"start": 11,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 25,
"end": 27,
"name": {
"commentStart": 25,
"end": 27,
"name": "XY",
"start": 25,
"type": "Identifier"
},
"path": [],
"start": 25,
"type": "Name",
"type": "Name"
}
},
{
"arguments": [
@ -367,7 +365,6 @@ expression: actual
"unlabeled": null
},
{
"arguments": [],
"callee": {
"abs_path": false,
"commentStart": 223,
@ -386,8 +383,9 @@ expression: actual
"commentStart": 223,
"end": 230,
"start": 223,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": null
}
],
"commentStart": 11,

View File

@ -19,24 +19,6 @@ expression: actual
"init": {
"body": [
{
"arguments": [
{
"abs_path": false,
"commentStart": 25,
"end": 27,
"name": {
"commentStart": 25,
"end": 27,
"name": "XY",
"start": 25,
"type": "Identifier"
},
"path": [],
"start": 25,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 11,
@ -55,8 +37,24 @@ expression: actual
"commentStart": 11,
"end": 28,
"start": 11,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 25,
"end": 27,
"name": {
"commentStart": 25,
"end": 27,
"name": "XY",
"start": 25,
"type": "Identifier"
},
"path": [],
"start": 25,
"type": "Name",
"type": "Name"
}
},
{
"arguments": [
@ -195,7 +193,6 @@ expression: actual
"unlabeled": null
},
{
"arguments": [],
"callee": {
"abs_path": false,
"commentStart": 90,
@ -214,8 +211,9 @@ expression: actual
"commentStart": 90,
"end": 97,
"start": 90,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": null
}
],
"commentStart": 11,

View File

@ -19,24 +19,6 @@ expression: actual
"init": {
"body": [
{
"arguments": [
{
"abs_path": false,
"commentStart": 22,
"end": 24,
"name": {
"commentStart": 22,
"end": 24,
"name": "XY",
"start": 22,
"type": "Identifier"
},
"path": [],
"start": 22,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 8,
@ -55,8 +37,24 @@ expression: actual
"commentStart": 8,
"end": 25,
"start": 8,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 22,
"end": 24,
"name": {
"commentStart": 22,
"end": 24,
"name": "XY",
"start": 22,
"type": "Identifier"
},
"path": [],
"start": 22,
"type": "Name",
"type": "Name"
}
},
{
"arguments": [

View File

@ -19,20 +19,6 @@ expression: actual
"init": {
"body": [
{
"arguments": [
{
"commentStart": 10,
"end": 11,
"raw": "1",
"start": 10,
"type": "Literal",
"type": "Literal",
"value": {
"value": 1.0,
"suffix": "None"
}
}
],
"callee": {
"abs_path": false,
"commentStart": 8,
@ -51,24 +37,22 @@ expression: actual
"commentStart": 8,
"end": 12,
"start": 8,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"commentStart": 10,
"end": 11,
"raw": "1",
"start": 10,
"type": "Literal",
"type": "Literal",
"value": {
"value": 1.0,
"suffix": "None"
}
}
},
{
"arguments": [
{
"commentStart": 18,
"end": 19,
"raw": "2",
"start": 18,
"type": "Literal",
"type": "Literal",
"value": {
"value": 2.0,
"suffix": "None"
}
}
],
"callee": {
"abs_path": false,
"commentStart": 16,
@ -87,8 +71,20 @@ expression: actual
"commentStart": 16,
"end": 20,
"start": 16,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"commentStart": 18,
"end": 19,
"raw": "2",
"start": 18,
"type": "Literal",
"type": "Literal",
"value": {
"value": 2.0,
"suffix": "None"
}
}
}
],
"commentStart": 8,

View File

@ -19,24 +19,6 @@ expression: actual
"init": {
"body": [
{
"arguments": [
{
"abs_path": false,
"commentStart": 22,
"end": 24,
"name": {
"commentStart": 22,
"end": 24,
"name": "XY",
"start": 22,
"type": "Identifier"
},
"path": [],
"start": 22,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 8,
@ -55,8 +37,24 @@ expression: actual
"commentStart": 8,
"end": 25,
"start": 8,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 22,
"end": 24,
"name": {
"commentStart": 22,
"end": 24,
"name": "XY",
"start": 22,
"type": "Identifier"
},
"path": [],
"start": 22,
"type": "Name",
"type": "Name"
}
},
{
"arguments": [

View File

@ -19,24 +19,6 @@ expression: actual
"init": {
"body": [
{
"arguments": [
{
"abs_path": false,
"commentStart": 25,
"end": 27,
"name": {
"commentStart": 25,
"end": 27,
"name": "XY",
"start": 25,
"type": "Identifier"
},
"path": [],
"start": 25,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 11,
@ -55,8 +37,24 @@ expression: actual
"commentStart": 11,
"end": 28,
"start": 11,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 25,
"end": 27,
"name": {
"commentStart": 25,
"end": 27,
"name": "XY",
"start": 25,
"type": "Identifier"
},
"path": [],
"start": 25,
"type": "Name",
"type": "Name"
}
},
{
"arguments": [

View File

@ -8,45 +8,6 @@ expression: actual
"commentStart": 0,
"end": 12,
"expression": {
"arguments": [
{
"commentStart": 5,
"elements": [
{
"commentStart": 6,
"end": 7,
"raw": "0",
"start": 6,
"type": "Literal",
"type": "Literal",
"value": {
"value": 0.0,
"suffix": "None"
}
},
{
"abs_path": false,
"commentStart": 9,
"end": 10,
"name": {
"commentStart": 9,
"end": 10,
"name": "l",
"start": 9,
"type": "Identifier"
},
"path": [],
"start": 9,
"type": "Name",
"type": "Name"
}
],
"end": 11,
"start": 5,
"type": "ArrayExpression",
"type": "ArrayExpression"
}
],
"callee": {
"abs_path": false,
"commentStart": 0,
@ -65,8 +26,45 @@ expression: actual
"commentStart": 0,
"end": 12,
"start": 0,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"commentStart": 5,
"elements": [
{
"commentStart": 6,
"end": 7,
"raw": "0",
"start": 6,
"type": "Literal",
"type": "Literal",
"value": {
"value": 0.0,
"suffix": "None"
}
},
{
"abs_path": false,
"commentStart": 9,
"end": 10,
"name": {
"commentStart": 9,
"end": 10,
"name": "l",
"start": 9,
"type": "Identifier"
},
"path": [],
"start": 9,
"type": "Name",
"type": "Name"
}
],
"end": 11,
"start": 5,
"type": "ArrayExpression",
"type": "ArrayExpression"
}
},
"start": 0,
"type": "ExpressionStatement",

View File

@ -19,24 +19,6 @@ expression: actual
"init": {
"body": [
{
"arguments": [
{
"abs_path": false,
"commentStart": 25,
"end": 27,
"name": {
"commentStart": 25,
"end": 27,
"name": "XY",
"start": 25,
"type": "Identifier"
},
"path": [],
"start": 25,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 11,
@ -55,8 +37,24 @@ expression: actual
"commentStart": 11,
"end": 28,
"start": 11,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 25,
"end": 27,
"name": {
"commentStart": 25,
"end": 27,
"name": "XY",
"start": 25,
"type": "Identifier"
},
"path": [],
"start": 25,
"type": "Name",
"type": "Name"
}
},
{
"arguments": [

View File

@ -32,24 +32,6 @@ expression: actual
{
"commentStart": 38,
"cond": {
"arguments": [
{
"abs_path": false,
"commentStart": 51,
"end": 57,
"name": {
"commentStart": 51,
"end": 57,
"name": "radius",
"start": 51,
"type": "Identifier"
},
"path": [],
"start": 51,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 46,
@ -68,8 +50,24 @@ expression: actual
"commentStart": 46,
"end": 58,
"start": 46,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 51,
"end": 57,
"name": {
"commentStart": 51,
"end": 57,
"name": "radius",
"start": 51,
"type": "Identifier"
},
"path": [],
"start": 51,
"type": "Name",
"type": "Name"
}
},
"digest": null,
"end": 84,

View File

@ -51,20 +51,6 @@ expression: actual
"type": "BinaryExpression"
},
{
"arguments": [
{
"commentStart": 24,
"end": 26,
"raw": "45",
"start": 24,
"type": "Literal",
"type": "Literal",
"value": {
"value": 45.0,
"suffix": "None"
}
}
],
"callee": {
"abs_path": false,
"commentStart": 17,
@ -83,8 +69,20 @@ expression: actual
"commentStart": 17,
"end": 27,
"start": 17,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"commentStart": 24,
"end": 26,
"raw": "45",
"start": 24,
"type": "Literal",
"type": "Literal",
"value": {
"value": 45.0,
"suffix": "None"
}
}
}
],
"commentStart": 8,

View File

@ -19,24 +19,6 @@ expression: actual
"init": {
"body": [
{
"arguments": [
{
"abs_path": false,
"commentStart": 19,
"end": 21,
"name": {
"commentStart": 19,
"end": 21,
"name": "XY",
"start": 19,
"type": "Identifier"
},
"path": [],
"start": 19,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 5,
@ -55,28 +37,26 @@ expression: actual
"commentStart": 5,
"end": 22,
"start": 5,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 19,
"end": 21,
"name": {
"commentStart": 19,
"end": 21,
"name": "XY",
"start": 19,
"type": "Identifier"
},
"path": [],
"start": 19,
"type": "Name",
"type": "Name"
}
},
{
"arguments": [
{
"abs_path": false,
"commentStart": 39,
"end": 42,
"name": {
"commentStart": 39,
"end": 42,
"name": "pos",
"start": 39,
"type": "Identifier"
},
"path": [],
"start": 39,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 26,
@ -95,8 +75,24 @@ expression: actual
"commentStart": 26,
"end": 43,
"start": 26,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 39,
"end": 42,
"name": {
"commentStart": 39,
"end": 42,
"name": "pos",
"start": 39,
"type": "Identifier"
},
"path": [],
"start": 39,
"type": "Name",
"type": "Name"
}
}
],
"commentStart": 5,

View File

@ -19,24 +19,6 @@ expression: actual
"init": {
"body": [
{
"arguments": [
{
"abs_path": false,
"commentStart": 19,
"end": 21,
"name": {
"commentStart": 19,
"end": 21,
"name": "XY",
"start": 19,
"type": "Identifier"
},
"path": [],
"start": 19,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 5,
@ -55,28 +37,26 @@ expression: actual
"commentStart": 5,
"end": 22,
"start": 5,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 19,
"end": 21,
"name": {
"commentStart": 19,
"end": 21,
"name": "XY",
"start": 19,
"type": "Identifier"
},
"path": [],
"start": 19,
"type": "Name",
"type": "Name"
}
},
{
"arguments": [
{
"abs_path": false,
"commentStart": 43,
"end": 46,
"name": {
"commentStart": 43,
"end": 46,
"name": "pos",
"start": 43,
"type": "Identifier"
},
"path": [],
"start": 43,
"type": "Name",
"type": "Name"
}
],
"callee": {
"abs_path": false,
"commentStart": 30,
@ -95,57 +75,26 @@ expression: actual
"commentStart": 30,
"end": 47,
"start": 30,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"abs_path": false,
"commentStart": 43,
"end": 46,
"name": {
"commentStart": 43,
"end": 46,
"name": "pos",
"start": 43,
"type": "Identifier"
},
"path": [],
"start": 43,
"type": "Name",
"type": "Name"
}
},
{
"arguments": [
{
"commentStart": 56,
"elements": [
{
"commentStart": 57,
"end": 58,
"raw": "0",
"start": 57,
"type": "Literal",
"type": "Literal",
"value": {
"value": 0.0,
"suffix": "None"
}
},
{
"argument": {
"abs_path": false,
"commentStart": 61,
"end": 66,
"name": {
"commentStart": 61,
"end": 66,
"name": "scale",
"start": 61,
"type": "Identifier"
},
"path": [],
"start": 61,
"type": "Name",
"type": "Name"
},
"commentStart": 60,
"end": 66,
"operator": "-",
"start": 60,
"type": "UnaryExpression",
"type": "UnaryExpression"
}
],
"end": 67,
"start": 56,
"type": "ArrayExpression",
"type": "ArrayExpression"
}
],
"callee": {
"abs_path": false,
"commentStart": 51,
@ -164,8 +113,53 @@ expression: actual
"commentStart": 51,
"end": 68,
"start": 51,
"type": "CallExpression",
"type": "CallExpression"
"type": "CallExpressionKw",
"type": "CallExpressionKw",
"unlabeled": {
"commentStart": 56,
"elements": [
{
"commentStart": 57,
"end": 58,
"raw": "0",
"start": 57,
"type": "Literal",
"type": "Literal",
"value": {
"value": 0.0,
"suffix": "None"
}
},
{
"argument": {
"abs_path": false,
"commentStart": 61,
"end": 66,
"name": {
"commentStart": 61,
"end": 66,
"name": "scale",
"start": 61,
"type": "Identifier"
},
"path": [],
"start": 61,
"type": "Name",
"type": "Name"
},
"commentStart": 60,
"end": 66,
"operator": "-",
"start": 60,
"type": "UnaryExpression",
"type": "UnaryExpression"
}
],
"end": 67,
"start": 56,
"type": "ArrayExpression",
"type": "ArrayExpression"
}
}
],
"commentStart": 5,

View File

@ -3,10 +3,10 @@ use std::fmt::Write;
use crate::parsing::{
ast::types::{
Annotation, ArrayExpression, ArrayRangeExpression, BinaryExpression, BinaryOperator, BinaryPart, BodyItem,
CallExpression, CallExpressionKw, CommentStyle, DefaultParamVal, Expr, FormatOptions, FunctionExpression,
IfExpression, ImportSelector, ImportStatement, ItemVisibility, LabeledArg, Literal, LiteralIdentifier,
LiteralValue, MemberExpression, MemberObject, Node, NonCodeNode, NonCodeValue, ObjectExpression, Parameter,
PipeExpression, Program, TagDeclarator, TypeDeclaration, UnaryExpression, VariableDeclaration, VariableKind,
CallExpressionKw, CommentStyle, DefaultParamVal, Expr, FormatOptions, FunctionExpression, IfExpression,
ImportSelector, ImportStatement, ItemVisibility, LabeledArg, Literal, LiteralIdentifier, LiteralValue,
MemberExpression, MemberObject, Node, NonCodeNode, NonCodeValue, ObjectExpression, Parameter, PipeExpression,
Program, TagDeclarator, TypeDeclaration, UnaryExpression, VariableDeclaration, VariableKind,
},
deprecation,
token::NumericSuffix,
@ -291,7 +291,6 @@ impl Expr {
result += &func_exp.recast(options, indentation_level);
result
}
Expr::CallExpression(call_exp) => call_exp.recast(options, indentation_level, ctxt),
Expr::CallExpressionKw(call_exp) => call_exp.recast(options, indentation_level, ctxt),
Expr::Name(name) => {
let result = name.to_string();
@ -342,9 +341,6 @@ impl BinaryPart {
}
}
BinaryPart::BinaryExpression(binary_expression) => binary_expression.recast(options),
BinaryPart::CallExpression(call_expression) => {
call_expression.recast(options, indentation_level, ExprContext::Other)
}
BinaryPart::CallExpressionKw(call_expression) => {
call_expression.recast(options, indentation_level, ExprContext::Other)
}
@ -355,25 +351,6 @@ impl BinaryPart {
}
}
impl CallExpression {
fn recast(&self, options: &FormatOptions, indentation_level: usize, ctxt: ExprContext) -> String {
format!(
"{}{}({})",
if ctxt == ExprContext::Pipe {
"".to_string()
} else {
options.get_indentation(indentation_level)
},
self.callee,
self.arguments
.iter()
.map(|arg| arg.recast(options, indentation_level, ctxt))
.collect::<Vec<String>>()
.join(", ")
)
}
}
impl CallExpressionKw {
fn recast_args(&self, options: &FormatOptions, indentation_level: usize, ctxt: ExprContext) -> Vec<String> {
let mut arg_list = if let Some(first_arg) = &self.unlabeled {
@ -748,8 +725,7 @@ impl UnaryExpression {
| BinaryPart::Name(_)
| BinaryPart::MemberExpression(_)
| BinaryPart::IfExpression(_)
| BinaryPart::CallExpressionKw(_)
| BinaryPart::CallExpression(_) => {
| BinaryPart::CallExpressionKw(_) => {
format!("{}{}", &self.operator, self.argument.recast(options, 0))
}
BinaryPart::BinaryExpression(_) | BinaryPart::UnaryExpression(_) => {

View File

@ -23,7 +23,6 @@ pub enum Node<'a> {
Name(NodeRef<'a, types::Name>),
BinaryExpression(NodeRef<'a, types::BinaryExpression>),
FunctionExpression(NodeRef<'a, types::FunctionExpression>),
CallExpression(NodeRef<'a, types::CallExpression>),
CallExpressionKw(NodeRef<'a, types::CallExpressionKw>),
PipeExpression(NodeRef<'a, types::PipeExpression>),
PipeSubstitution(NodeRef<'a, types::PipeSubstitution>),
@ -64,7 +63,6 @@ impl Node<'_> {
Node::Name(n) => n.digest,
Node::BinaryExpression(n) => n.digest,
Node::FunctionExpression(n) => n.digest,
Node::CallExpression(n) => n.digest,
Node::CallExpressionKw(n) => n.digest,
Node::PipeExpression(n) => n.digest,
Node::PipeSubstitution(n) => n.digest,
@ -109,7 +107,6 @@ impl Node<'_> {
Node::Name(n) => *n as *const _ as *const (),
Node::BinaryExpression(n) => *n as *const _ as *const (),
Node::FunctionExpression(n) => *n as *const _ as *const (),
Node::CallExpression(n) => *n as *const _ as *const (),
Node::CallExpressionKw(n) => *n as *const _ as *const (),
Node::PipeExpression(n) => *n as *const _ as *const (),
Node::PipeSubstitution(n) => *n as *const _ as *const (),
@ -154,7 +151,6 @@ impl TryFrom<&Node<'_>> for SourceRange {
Node::Name(n) => SourceRange::from(*n),
Node::BinaryExpression(n) => SourceRange::from(*n),
Node::FunctionExpression(n) => SourceRange::from(*n),
Node::CallExpression(n) => SourceRange::from(*n),
Node::CallExpressionKw(n) => SourceRange::from(*n),
Node::PipeExpression(n) => SourceRange::from(*n),
Node::PipeSubstitution(n) => SourceRange::from(*n),
@ -199,7 +195,6 @@ impl<'tree> From<&'tree types::Expr> for Node<'tree> {
types::Expr::Name(id) => id.as_ref().into(),
types::Expr::BinaryExpression(be) => be.as_ref().into(),
types::Expr::FunctionExpression(fe) => fe.as_ref().into(),
types::Expr::CallExpression(ce) => ce.as_ref().into(),
types::Expr::CallExpressionKw(ce) => ce.as_ref().into(),
types::Expr::PipeExpression(pe) => pe.as_ref().into(),
types::Expr::PipeSubstitution(ps) => ps.as_ref().into(),
@ -222,7 +217,6 @@ impl<'tree> From<&'tree types::BinaryPart> for Node<'tree> {
types::BinaryPart::Literal(lit) => lit.as_ref().into(),
types::BinaryPart::Name(id) => id.as_ref().into(),
types::BinaryPart::BinaryExpression(be) => be.as_ref().into(),
types::BinaryPart::CallExpression(ce) => ce.as_ref().into(),
types::BinaryPart::CallExpressionKw(ce) => ce.as_ref().into(),
types::BinaryPart::UnaryExpression(ue) => ue.as_ref().into(),
types::BinaryPart::MemberExpression(me) => me.as_ref().into(),
@ -282,7 +276,6 @@ impl_from!(Node, Identifier);
impl_from!(Node, Name);
impl_from!(Node, BinaryExpression);
impl_from!(Node, FunctionExpression);
impl_from!(Node, CallExpression);
impl_from!(Node, CallExpressionKw);
impl_from!(Node, PipeExpression);
impl_from!(Node, PipeSubstitution);

View File

@ -78,19 +78,17 @@ impl<'tree> Visitable<'tree> for Node<'tree> {
children.push((&n.body).into());
children
}
Node::CallExpression(n) => {
let mut children = n.arguments.iter().map(|v| v.into()).collect::<Vec<Node>>();
children.insert(0, (&n.callee).into());
children
}
Node::CallExpressionKw(n) => {
let mut children = n.unlabeled.iter().map(|v| v.into()).collect::<Vec<Node>>();
let mut children: Vec<Node<'_>> =
Vec::with_capacity(1 + if n.unlabeled.is_some() { 1 } else { 0 } + n.arguments.len());
children.push((&n.callee).into());
children.extend(n.unlabeled.iter().map(Node::from));
// TODO: this is wrong but it's what the old walk code was doing.
// We likely need a real LabeledArg AST node, but I don't
// want to tango with it since it's a lot deeper than
// adding it to the enum.
children.extend(n.arguments.iter().map(|v| (&v.arg).into()).collect::<Vec<Node>>());
children.extend(n.arguments.iter().map(|v| Node::from(&v.arg)));
children
}
Node::PipeExpression(n) => n.body.iter().map(|v| v.into()).collect(),