Skip to content

Commit 54a651c

Browse files
authored
fix: escape special characters in string template literals (#603)
* fix: escape special characters in string template literals * fix: wrong quoting --------- Co-authored-by: Andreas Hochsteger <[email protected]>
1 parent 3426762 commit 54a651c

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

api.md

+18
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,24 @@ class MyObject {
14451445
```
14461446

14471447

1448+
## [string-template-literal](./test/programs/string-template-literal)
1449+
1450+
```ts
1451+
interface MyObject {
1452+
a: `@${string}`,
1453+
b: `@${number}`,
1454+
c: `@${bigint}`,
1455+
d: `@${boolean}`,
1456+
e: `@${undefined}`,
1457+
f: `@${null}`,
1458+
g: `${string}@`,
1459+
h: `${number}@`,
1460+
i: `${string}@${number}`,
1461+
j: `${string}.${string}`,
1462+
}
1463+
```
1464+
1465+
14481466
## [symbol](./test/programs/symbol)
14491467

14501468
```ts

test/programs/string-template-literal/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ interface MyObject {
88
g: `${string}@`,
99
h: `${number}@`,
1010
i: `${string}@${number}`,
11+
j: `${string}.${string}`,
1112
}

test/programs/string-template-literal/schema.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
"i": {
4040
"type": "string",
4141
"pattern": "^.*@[0-9]*$"
42+
},
43+
"j": {
44+
"type": "string",
45+
"pattern": "^.*\\..*$"
4246
}
4347
},
4448
"additionalProperties": false,
@@ -51,7 +55,8 @@
5155
"f",
5256
"g",
5357
"h",
54-
"i"
58+
"i",
59+
"j"
5560
],
5661
"$schema": "http://json-schema.org/draft-07/schema#"
5762
}

typescript-json-schema.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ export class JsonSchemaGenerator {
753753
const {texts, types} = propertyType;
754754
const pattern = [];
755755
for (let i = 0; i < texts.length; i++) {
756-
const text = texts[i];
756+
const text = texts[i].replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
757757
const type = types[i];
758758

759759
if (i === 0) {

0 commit comments

Comments
 (0)