Skip to content

Commit

Permalink
fix: escape special characters in string template literals (#603)
Browse files Browse the repository at this point in the history
* fix: escape special characters in string template literals

* fix: wrong quoting

---------

Co-authored-by: Andreas Hochsteger <[email protected]>
  • Loading branch information
ahochsteger and ahochsteger authored May 14, 2024
1 parent 3426762 commit 54a651c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
18 changes: 18 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,24 @@ class MyObject {
```


## [string-template-literal](./test/programs/string-template-literal)

```ts
interface MyObject {
a: `@${string}`,
b: `@${number}`,
c: `@${bigint}`,
d: `@${boolean}`,
e: `@${undefined}`,
f: `@${null}`,
g: `${string}@`,
h: `${number}@`,
i: `${string}@${number}`,
j: `${string}.${string}`,
}
```


## [symbol](./test/programs/symbol)

```ts
Expand Down
1 change: 1 addition & 0 deletions test/programs/string-template-literal/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ interface MyObject {
g: `${string}@`,
h: `${number}@`,
i: `${string}@${number}`,
j: `${string}.${string}`,
}
7 changes: 6 additions & 1 deletion test/programs/string-template-literal/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
"i": {
"type": "string",
"pattern": "^.*@[0-9]*$"
},
"j": {
"type": "string",
"pattern": "^.*\\..*$"
}
},
"additionalProperties": false,
Expand All @@ -51,7 +55,8 @@
"f",
"g",
"h",
"i"
"i",
"j"
],
"$schema": "http://json-schema.org/draft-07/schema#"
}
2 changes: 1 addition & 1 deletion typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ export class JsonSchemaGenerator {
const {texts, types} = propertyType;
const pattern = [];
for (let i = 0; i < texts.length; i++) {
const text = texts[i];
const text = texts[i].replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
const type = types[i];

if (i === 0) {
Expand Down

0 comments on commit 54a651c

Please sign in to comment.