fix(napi-derive): correctly escape backslash in type definition doc comments (#1034)
This commit is contained in:
parent
b7ac6ee937
commit
cdcab888ee
5 changed files with 32 additions and 1 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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␊
|
||||
}␊
|
||||
|
|
Binary file not shown.
5
examples/napi/index.d.ts
vendored
5
examples/napi/index.d.ts
vendored
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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(),
|
||||
|
|
Loading…
Reference in a new issue