Require variable declaration for pipe expressions
This commit is contained in:
		@ -2086,6 +2086,20 @@ const newVar = myVar + 1"#;
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[tokio::test(flavor = "multi_thread")]
 | 
			
		||||
    async fn test_execute_top_level_pipe_without_variable() {
 | 
			
		||||
        let ast = r#"startSketchOn('XY')
 | 
			
		||||
  |> startProfileAt([0, 0], %)
 | 
			
		||||
  |> lineTo([2, 2], %, $yo)
 | 
			
		||||
"#;
 | 
			
		||||
        let result = parse_execute(ast).await;
 | 
			
		||||
        assert!(result.is_err());
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            result.unwrap_err().to_string(),
 | 
			
		||||
            r#"syntax: KclErrorDetails { source_ranges: [SourceRange([0, 78])], message: "A top-level pipe expression must be assigned to a new variable declaration" }"#.to_owned()
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[tokio::test(flavor = "multi_thread")]
 | 
			
		||||
    async fn test_execute_angled_line_that_intersects() {
 | 
			
		||||
        let ast_fn = |offset: &str| -> String {
 | 
			
		||||
 | 
			
		||||
@ -50,6 +50,37 @@ fn program(i: TokenSlice) -> PResult<Program> {
 | 
			
		||||
    // Once this is merged and stable, consider changing this as I think it's more accurate
 | 
			
		||||
    // without the -1.
 | 
			
		||||
    out.end -= 1;
 | 
			
		||||
 | 
			
		||||
    // Prevent top-level pipe expressions without a variable declaration, giving
 | 
			
		||||
    // a good error message that will help users fix their code.  This is a
 | 
			
		||||
    // band-aid until we can use the artifact graph.
 | 
			
		||||
    let source_ranges = out
 | 
			
		||||
        .body
 | 
			
		||||
        .iter()
 | 
			
		||||
        .filter_map(|item| {
 | 
			
		||||
            if let BodyItem::ExpressionStatement(ExpressionStatement {
 | 
			
		||||
                expression: Value::PipeExpression(_),
 | 
			
		||||
                start,
 | 
			
		||||
                end,
 | 
			
		||||
                ..
 | 
			
		||||
            }) = item
 | 
			
		||||
            {
 | 
			
		||||
                Some(SourceRange([*start, *end]))
 | 
			
		||||
            } else {
 | 
			
		||||
                None
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
        .collect::<Vec<_>>();
 | 
			
		||||
    if !source_ranges.is_empty() {
 | 
			
		||||
        return Err(ErrMode::Cut(
 | 
			
		||||
            KclError::Syntax(KclErrorDetails {
 | 
			
		||||
                source_ranges,
 | 
			
		||||
                message: "A top-level pipe expression must be assigned to a new variable declaration".to_owned(),
 | 
			
		||||
            })
 | 
			
		||||
            .into(),
 | 
			
		||||
        ));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Ok(out)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user