-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #1591 ## String templates [Playground examples](https://cadlplayground.z22.web.core.windows.net/prs/2630/?c=aW1wb3J0ICJAdHlwZXNwZWMvanNvbi1zY2hlbWEiOwoKdXNpbmcgVHlwZVNwZWMuSnNvblPFHTsKCkDELMYOCm5hbWVzcGFjZSDGEXM7CgphbGlhcyBteWNvbnN0ID0gImZvb2JhcsRXbW9kZWwgUGVyc29uIHsKICBzaW1wbGU6ICJTxQkgJHsxMjN9IGVuZCI7CiAgbXVsdGlsaW7EIiIiCiAgTcQRIAogxAHHLMUNJHt0cnVlfQogIMQuCiDELzsKCiAgcmVmOiAiUmVmIHRoaXMg5gCcJHvnAJ7KanRlbXBsYXRlOiBUxwo8ImN1c3RvbSI%2BOwp96ADWyR1UIGV4dGVuZHMgdmFsdWVvZiBzdHJpbmc%2B5ADxRm9vICR7VH0g5QD3&e=%40typespec%2Fjson-schema&options=%7B%7D) ``` import "@typespec/json-schema"; using TypeSpec.JsonSchema; @jsonschema namespace Schemas; alias myconst = "foobar"; model Person { simple: "Simple ${123} end"; multiline: """ Multi ${123} ${true} line """; ref: "Ref this alias ${myconst} end"; template: Template<"custom">; } alias Template<T extends valueof string> = "Foo ${T} bar"; ``` ## Other fixes Also fixes https://github.com/Azure/typespec-azure/issues/3399(Show invalid escape sequence char instead of the whole string) <img width="561" alt="image" src="https://github.com/microsoft/typespec/assets/1031227/7592a046-2c2c-4597-acfd-e45ebfb02cb7"> --------- Co-authored-by: Brian Terlson <[email protected]> Co-authored-by: Mark Cowlishaw <[email protected]>
- Loading branch information
1 parent
4e63cab
commit 360add2
Showing
40 changed files
with
1,777 additions
and
101 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
common/changes/@typespec/compiler/feature-string-template_2023-11-14-00-00.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@typespec/compiler", | ||
"comment": "**New language feature** **BREAKING** Added string template literal in typespec. Single and multi-line strings can be interpolated with `${` and `}`. Example `\\`Doc for url ${url} is here: ${location}\\``", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@typespec/compiler" | ||
} |
10 changes: 10 additions & 0 deletions
10
common/changes/@typespec/json-schema/feature-string-template_2023-11-14-00-00.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@typespec/json-schema", | ||
"comment": "Added support for string template literals", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@typespec/json-schema" | ||
} |
10 changes: 10 additions & 0 deletions
10
common/changes/@typespec/openapi3/feature-string-template_2023-11-16-22-33.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@typespec/openapi3", | ||
"comment": "Added support for string template literals", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@typespec/openapi3" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ model Operation {} | |
model Scalar {} | ||
model Union {} | ||
model UnionVariant {} | ||
model StringTemplate {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/compiler/src/core/helpers/string-template-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { createDiagnosticCollector } from "../diagnostics.js"; | ||
import { createDiagnostic } from "../messages.js"; | ||
import { Diagnostic, StringTemplate } from "../types.js"; | ||
import { getTypeName } from "./type-name-utils.js"; | ||
|
||
/** | ||
* Convert a string template to a string value. | ||
* Only literal interpolated can be converted to string. | ||
* Otherwise diagnostics will be reported. | ||
* | ||
* @param stringTemplate String template to convert. | ||
*/ | ||
export function stringTemplateToString( | ||
stringTemplate: StringTemplate | ||
): [string, readonly Diagnostic[]] { | ||
const diagnostics = createDiagnosticCollector(); | ||
const result = stringTemplate.spans | ||
.map((x) => { | ||
if (x.isInterpolated) { | ||
switch (x.type.kind) { | ||
case "String": | ||
case "Number": | ||
case "Boolean": | ||
return String(x.type.value); | ||
case "StringTemplate": | ||
return diagnostics.pipe(stringTemplateToString(x.type)); | ||
default: | ||
diagnostics.add( | ||
createDiagnostic({ | ||
code: "non-literal-string-template", | ||
target: x.node, | ||
}) | ||
); | ||
return getTypeName(x.type); | ||
} | ||
} else { | ||
return x.type.value; | ||
} | ||
}) | ||
.join(""); | ||
return diagnostics.wrap(result); | ||
} | ||
|
||
export function isStringTemplateSerializable( | ||
stringTemplate: StringTemplate | ||
): [boolean, readonly Diagnostic[]] { | ||
const diagnostics = createDiagnosticCollector(); | ||
for (const span of stringTemplate.spans) { | ||
if (span.isInterpolated) { | ||
switch (span.type.kind) { | ||
case "String": | ||
case "Number": | ||
case "Boolean": | ||
break; | ||
case "StringTemplate": | ||
diagnostics.pipe(isStringTemplateSerializable(span.type)); | ||
break; | ||
case "TemplateParameter": | ||
if (span.type.constraint && span.type.constraint.kind === "Value") { | ||
break; // Value types will be serializable in the template instance. | ||
} | ||
// eslint-disable-next-line no-fallthrough | ||
default: | ||
diagnostics.add( | ||
createDiagnostic({ | ||
code: "non-literal-string-template", | ||
target: span.node, | ||
}) | ||
); | ||
} | ||
} | ||
} | ||
return [diagnostics.diagnostics.length === 0, diagnostics.diagnostics]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.