Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape special characters in string template literals #603

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading