Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/CommandCompletion/CompletionHelpers.cs (C#)

Code before:
        /// <param name="completionText">
        /// The text to be quoted and potentially escaped.
        /// </param>
        /// <param name="quote">
        /// The quote character to use for enclosing the text. Defaults to a single quote ('') if not provided.
        /// </param>
        /// <param name="escapeSingleQuoteChars">
        /// Indicates whether single quote characters in the text should be escaped. Defaults to <c>true</c>.
        /// </param>
        /// <param name="escapeDoubleQuoteChars">
        /// Indicates whether special characters (e.g., backticks and dollar signs) in the text
        /// should be escaped when using double quotes. Defaults to <c>false</c>.
        /// </param>
        /// <param name="escapeGlobbingPathChars">
        /// Indicates whether globbing path characters (e.g., square brackets) in the text should be escaped.
        /// Defaults to <c>false</c>.
        /// </param>
        /// <returns>
        /// The quoted and optionally escaped version of the <paramref name="completionText"/>.
        /// </returns>
        internal static string QuoteCompletionText(
            string completionText,
            string quote,
            bool escapeSingleQuoteChars = true,
            bool escapeDoubleQuoteChars = false,
            bool escapeGlobbingPathChars = false)
        {
            if (!CompletionRequiresQuotes(completionText, escapeGlobbingPathChars))
            {
                return quote + completionText + quote;
            }

            string quoteInUse = string.IsNullOrEmpty(quote) ? "'" : quote;

            completionText = quoteInUse switch
            {
                "'" when escapeSingleQuoteChars => completionText.Replace("'", "''"),
                "\"" when escapeDoubleQuoteChars => completionText.Replace("`", "``").Replace("$", "`$"),
                _ => completionText
            };

            if (escapeGlobbingPathChars)
            {
                string leftBracket = quoteInUse == "'" ? "`[" : "``[";
                string rightBracket = quoteInUse == "'" ? "`]" : "``]";
                completionText = completionText.Replace("[", leftBracket).Replace("]", rightBracket);
            }

            return quoteInUse + completionText + quoteInUse;
        }
    }

Review comment (bug):
It is not used anywhere. Rather, it should be removed if there is no clear understanding that it should be used, i.e. to fix existing bugs. 
I don't have a clear understanding in which cases and which characters should be disabled. It would be nice to have a description of all such scenarios.

Code after:
            }

            return string.Empty;
        }

        internal static bool CompletionRequiresQuotes(string completion)
        {
            // If the tokenizer sees the completion as more than two tokens, or if there is some error, then
            // some form of quoting is necessary (if it's a variable, we'd need ${}, filenames would need [], etc.)

            Parser.ParseInput(completion, out Token[] tokens, out ParseError[] errors);

            // Expect no errors and 2 tokens (1 is for our completion, the other is eof)
            // Or if the completion is a keyword, we ignore the errors
            bool requireQuote = !(errors.Length == 0 && tokens.Length == 2);
            if ((!requireQuote && tokens[0] is StringToken) ||
                (tokens.Length == 2 && (tokens[0].TokenFlags & TokenFlags.Keyword) != 0))
            {
                requireQuote = ContainsCharsToCheck(tokens[0].Text);
            }

            return requireQuote;
        }

        private static bool ContainsCharsToCheck(ReadOnlySpan<char> text)
            => text.ContainsAny(s_defaultCharsToCheck);

        /// <summary>
        /// Quotes a given completion text.
        /// </summary>
        /// <param name="completionText">
        /// The text to be quoted.
        /// </param>
        /// <param name="quote">
        /// The quote character to use for enclosing the text. Defaults to a single quote if not provided.
        /// </param>
        /// <returns>
        /// The quoted <paramref name="completionText"/>.
        /// </returns>
        internal static string QuoteCompletionText(string completionText, string quote)
        {
            if (!CompletionRequiresQuotes(completionText))
            {
                return quote + completionText + quote;
            }

            string quoteInUse = string.IsNullOrEmpty(quote) ? "'" : quote;

            if (quoteInUse == "'")
            {
                completionText = CodeGeneration.EscapeSingleQuotedStringContent(completionText);