Skip to content

Conversation

@dnenov
Copy link
Collaborator

@dnenov dnenov commented Sep 16, 2025

Purpose

A follow up of https://github.com/DynamoDS/Dynamo/pull/16478/files#diff-d37d8c6fdae53b6093a8ac0f0e6198e21b99f65a1a60c9be8d39e6396444cd89.

This PR replaces the old ToLiteral implementation, which used CodeDom to generate C# string literals and then post-processed them.

The new implementation directly escapes only backslashes () and double quotes (") per DesignScript rules.

  • Removes brittleness caused by CodeDom formatting changes in .NET 10
  • Matches DesignScript documentation exactly
  • Full tests coverage

Resources used:

Declarations

Check these if you believe they are true

Release Notes

  • Replace CodeDom-based string escaping with proper DesignScript literal handling.
  • tests coverage added

Reviewers

@ luis.rivera@autodesk.com - not sure about Luis' handle
@QilongTang

FYIs

@saintentropy

Copy link
Contributor

@chubakueno chubakueno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks awesome!

@QilongTang QilongTang requested a review from Copilot September 16, 2025 23:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces the old CodeDom-based string escaping implementation with a new DesignScript-specific escaper. The change addresses brittleness issues with .NET 10 and aligns with DesignScript documentation by only escaping backslashes and double quotes while preserving all other characters including Unicode and newlines.

  • Replace CodeDom string literal generation with direct character-by-character escaping
  • Add comprehensive test coverage for the new escaping logic
  • Optimize performance with a fast path for strings that don't need escaping

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Engine/ProtoCore/Utils/CompilerUtils.cs Replaces CodeDom-based ToLiteral method with DesignScript-specific escaper
test/Engine/ProtoTest/UtilsTests/CompilerUtilsTest.cs Adds comprehensive test suite covering edge cases, Unicode, performance, and round-trip validation
Comments suppressed due to low confidence (1)

test/Engine/ProtoTest/UtilsTests/CompilerUtilsTest.cs:1

  • The explicit handling of \r and \n characters in the switch statement is unnecessary. These characters don't need escaping according to DesignScript rules, so they should be handled by the default case. The explicit cases add complexity without functional benefit.
using System;

/// </summary>
/// <param name="input">The string to escape</param>
/// <returns>The escaped string suitable for DesignScript string literals</returns>
internal static string ToLiteral(string input)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't you just use private string GetEscapedString(string s) from the Parser like you said?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @aparajit-pratap, thank you for the suggestion! The reason we can't directly use GetEscapedString is that it does the reverse of what we need - it unescapes strings (converts " to "), while ToLiteral needs to escape strings (converts " to ").
With that said, I've now reworked the ToLiteral method to closely follow what GetEscapedString does, but in reverse. Can you have a look and see if that's more what you had in mind?

- Ensure perfect round-trip compatibility with Parser
- change tests to align with aligning with the Parser.GetEscapedString inverse functionality
@dnenov
Copy link
Collaborator Author

dnenov commented Sep 23, 2025

@QilongTang QilongTang changed the title switching to a DS-specific escaper DYN-9196 switching to a DS-specific escaper Sep 23, 2025
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the ticket for this pull request: https://jira.autodesk.com/browse/DYN-9196

@dnenov
Copy link
Collaborator Author

dnenov commented Sep 23, 2025

@QilongTang QilongTang merged commit 9f318c0 into DynamoDS:master Sep 23, 2025
24 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants