fix(napi-derive): correctly escape backslash in type definition doc comments (#1034)

This commit is contained in:
Tim Fish 2022-01-17 04:38:16 +00:00 committed by GitHub
parent b7ac6ee937
commit cdcab888ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 1 deletions

View file

@ -51,7 +51,16 @@ fn escape_json(src: &str) -> String {
use std::fmt::Write;
let mut escaped = String::with_capacity(src.len());
let mut utf16_buf = [0u16; 2];
let mut pending_backslash = false;
for c in src.chars() {
if pending_backslash {
match c {
'b' | 'f' | 'n' | 'r' | 't' | 'u' | '"' => escaped += "\\",
_ => escaped += "\\\\",
}
pending_backslash = false;
}
match c {
'\x08' => escaped += "\\b",
'\x0c' => escaped += "\\f",
@ -59,7 +68,10 @@ fn escape_json(src: &str) -> String {
'\r' => escaped += "\\r",
'\t' => escaped += "\\t",
'"' => escaped += "\\\"",
'\\' => escaped += "\\",
'\\' => {
pending_backslash = true;
}
' ' => escaped += " ",
c if c.is_ascii_graphic() => escaped.push(c),
c => {
let encoded = c.encode_utf16(&mut utf16_buf);
@ -69,6 +81,12 @@ fn escape_json(src: &str) -> String {
}
}
}
// cater for trailing backslash
if pending_backslash {
escaped += "\\\\"
}
escaped
}

View file

@ -151,6 +151,11 @@ Generated by [AVA](https://avajs.dev).
whoami(): string␊
/** This is static... */␊
static getDogKind(): Kind␊
/**␊
* Here are some characters and character sequences␊
* that should be escaped correctly:␊
* \\[]{}/\\:""␊
*/␊
returnOtherClass(): Dog␊
returnOtherClassWithCustomConstructor(): Bird␊
}␊

View file

@ -141,6 +141,11 @@ export class Animal {
whoami(): string
/** This is static... */
static getDogKind(): Kind
/**
* Here are some characters and character sequences
* that should be escaped correctly:
* \[]{}/\:""
*/
returnOtherClass(): Dog
returnOtherClassWithCustomConstructor(): Bird
}

View file

@ -62,6 +62,9 @@ impl Animal {
}
#[napi]
/// Here are some characters and character sequences
/// that should be escaped correctly:
/// \[]{}/\:""
pub fn return_other_class(&self) -> Dog {
Dog {
name: "Doge".to_owned(),