Skip to content

Commit ce273ad

Browse files
committed
Correctly escape escaped content in formatter configuration
The sketch code formatter configuration is passed to the ClangFormat tool as a string representing a JSON object via a command line argument. The quotes in the JSON syntax are escaped in order to make them compatible with this usage. Previously, consideration was not given to escaping of the content. For example, with the previous escaping code, this content: `\"` would be converted to `\\"`, whereas the correct escaping would look like `\\\"`. That did not result in problems only because the configuration didn't contain escaped content. This good fortune will not persist through updates to the configuration so the command must be properly processed. The content of the configuration will now be escaped in addition to the quotes of the JSON data format.
1 parent 0b33b51 commit ce273ad

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

arduino-ide-extension/src/node/clang-formatter.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ function toClangOptions(
123123

124124
// See: https://releases.llvm.org/11.0.1/tools/clang/docs/ClangFormatStyleOptions.html
125125
export function style({ TabWidth, UseTab }: ClangFormatOptions): string {
126-
return JSON.stringify(styleJson({ TabWidth, UseTab })).replace(/\"/g, '\\"');
126+
return JSON.stringify(styleJson({ TabWidth, UseTab })).replace(
127+
/[\\"]/g,
128+
'\\$&'
129+
);
127130
}
128131

129132
function styleJson({

0 commit comments

Comments
 (0)