From daeb5f72bcc6fe17565b896ad2426c4536e30645 Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Wed, 21 Aug 2024 12:01:35 -0500 Subject: [PATCH] Use unescaped template operator (#19) --- DESIGN.md | 8 +++---- template/package.json.ejs | 4 ++-- template/src/index.d.ts.ejs | 4 ++-- template/src/index.ts.ejs | 18 +++++++-------- template/src/main.ts.ejs | 12 +++++----- template/src/pdk.ts.ejs | 44 ++++++++++++++++++------------------- template/xtp.toml.ejs | 6 ++--- 7 files changed, 48 insertions(+), 48 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index 68d678b..c71a7a4 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -67,16 +67,16 @@ So `src/index.d.ts.ejs` will become `src/index.d.ts`. It might start off like th declare module 'main' { <% ctx.schema.exports.forEach(e => { %> - export function <% toCamelCase(e.name) %>(): I32; -<%- } %> + export function <%- toCamelCase(e.name) %>(): I32; +<% } %> } <% if (ctx.schema.imports.length > 0) { %> declare module 'extism:host' { interface user { <% ctx.schema.import.forEach(i => { %> - <% toCamelCase(i.name) %>(ptr: I64): I64; - <%- end %> + <%- toCamelCase(i.name) %>(ptr: I64): I64; + <% } %> } } <% } %> diff --git a/template/package.json.ejs b/template/package.json.ejs index 2dc057d..efdfda8 100644 --- a/template/package.json.ejs +++ b/template/package.json.ejs @@ -1,7 +1,7 @@ { - "name": "<%= project.name %>", + "name": "<%- project.name %>", "version": "1.0.0", - "description": "<%= project.description %>", + "description": "<%- project.description %>", "main": "src/index.ts", "scripts": { "build": "npx tsc --noEmit && node esbuild.js && extism-js dist/index.js -i src/index.d.ts -o dist/plugin.wasm", diff --git a/template/src/index.d.ts.ejs b/template/src/index.d.ts.ejs index edd5281..30433a3 100644 --- a/template/src/index.d.ts.ejs +++ b/template/src/index.d.ts.ejs @@ -1,6 +1,6 @@ declare module 'main' { <% schema.exports.forEach(ex => { -%> - export function <%= ex.name %>(): I32; + export function <%- ex.name %>(): I32; <% }) -%> } @@ -8,7 +8,7 @@ declare module 'main' { declare module 'extism:host' { interface user { <% schema.imports.forEach(im => { -%> - <%= im.name %>(ptr: I64): I64; + <%- im.name %>(ptr: I64): I64; <% }) -%> } } diff --git a/template/src/index.ts.ejs b/template/src/index.ts.ejs index 8b6fa21..cbbf878 100644 --- a/template/src/index.ts.ejs +++ b/template/src/index.ts.ejs @@ -2,37 +2,37 @@ import * as main from "./main" <% if (Object.values(schema.schemas).length > 0) { %> import { <% Object.values(schema.schemas).forEach(schema => { -%> - <%= schema.name %>, + <%- schema.name %>, <% }) -%> } from './pdk' <% } %> <% schema.exports.forEach(ex => { -%> -export function <%= ex.name %>(): number { +export function <%- ex.name %>(): number { <% if (ex.input) { -%> <% if (isJsonEncoded(ex.input)) { -%> <% if (isPrimitive(ex.input)) { -%> const input: <%- toTypeScriptType(ex.input) %> = JSON.parse(Host.inputString()) <% } else { -%> const untypedInput = JSON.parse(Host.inputString()) - const input = <%= toTypeScriptType(ex.input) %>.fromJson(untypedInput) + const input = <%- toTypeScriptType(ex.input) %>.fromJson(untypedInput) <% } -%> <% } else if (ex.input.type === 'string') { -%> - const input = Host.inputString() <%= (ex.input.$ref && ex.input.$ref.enum) ? `as ${ex.input.$ref.name}` : "" %> + const input = Host.inputString() <%- (ex.input.$ref && ex.input.$ref.enum) ? `as ${ex.input.$ref.name}` : "" %> <% } else { -%> const input: ArrayBufferLike = Host.inputBytes() <% } -%> <% if (ex.output) { -%> - const output = main.<%= ex.name %>Impl(input) + const output = main.<%- ex.name %>Impl(input) <% } else { -%> - main.<%= ex.name %>Impl(input) + main.<%- ex.name %>Impl(input) <% } -%> <% } else { -%> <% if (ex.output) { -%> - const output = main.<%= ex.name %>Impl() + const output = main.<%- ex.name %>Impl() <% } else { -%> - main.<%= ex.name %>Impl() + main.<%- ex.name %>Impl() <% } -%> <% } -%> @@ -41,7 +41,7 @@ export function <%= ex.name %>(): number { <% if (isPrimitive(ex.output)) { -%> Host.outputString(JSON.stringify(output)) <% } else { -%> - const untypedOutput = <%= toTypeScriptType(ex.output) %>.toJson(output) + const untypedOutput = <%- toTypeScriptType(ex.output) %>.toJson(output) Host.outputString(JSON.stringify(untypedOutput)) <% } -%> <% } else if (ex.output.type === 'string') { -%> diff --git a/template/src/main.ts.ejs b/template/src/main.ts.ejs index 86edce6..3b88b7d 100644 --- a/template/src/main.ts.ejs +++ b/template/src/main.ts.ejs @@ -1,14 +1,14 @@ <% if (Object.values(schema.schemas).length > 0) { %> import { <% Object.values(schema.schemas).forEach(schema => { -%> - <%= schema.name %>, + <%- schema.name %>, <% }) -%> } from './pdk' <% } %> <% if (schema.imports.length > 0) { %> import { <% schema.imports.forEach(imp => { -%> - <%= imp.name %>, + <%- imp.name %>, <% }) -%> } from './pdk' <% } %> @@ -17,17 +17,17 @@ import { <% schema.exports.forEach(ex => { %> <% if (hasComment(ex)) -%> /** - * <%= formatCommentBlock(ex.description) %> + * <%- formatCommentBlock(ex.description) %> * <% if (hasComment(ex.input)) { -%> - * @param input {<%- toTypeScriptType(ex.input) %>} <%= formatCommentLine(ex.input.description) %> + * @param input {<%- toTypeScriptType(ex.input) %>} <%- formatCommentLine(ex.input.description) %> <% } -%> <% if (hasComment(ex.output)) { -%> - * @returns {<%- toTypeScriptType(ex.output) %>} <%= formatCommentLine(ex.output.description) %> + * @returns {<%- toTypeScriptType(ex.output) %>} <%- formatCommentLine(ex.output.description) %> <% } -%> */ <% -%> -export function <%= ex.name %>Impl(<%- ex.input ? `input: ${toTypeScriptType(ex.input)}`: null %>)<%- ex.output ? `:${toTypeScriptType(ex.output)}` : null %> { +export function <%- ex.name %>Impl(<%- ex.input ? `input: ${toTypeScriptType(ex.input)}`: null %>)<%- ex.output ? `:${toTypeScriptType(ex.output)}` : null %> { <% if (featureFlags['stub-with-code-samples'] && codeSamples(ex, 'typescript').length > 0) { -%> <%- codeSamples(ex, 'typescript')[0].source %> <% } else { -%> diff --git a/template/src/pdk.ts.ejs b/template/src/pdk.ts.ejs index 6fa0760..cb5163d 100644 --- a/template/src/pdk.ts.ejs +++ b/template/src/pdk.ts.ejs @@ -6,44 +6,44 @@ const hostFunctions = Host.getFunctions() <% if (schema.properties.length > 0) { %> /** - * <%= formatCommentBlock(schema.description) %> + * <%- formatCommentBlock(schema.description) %> */ -export class <%= schema.name %> { +export class <%- schema.name %> { <% schema.properties.forEach(p => { %> <% if (p.description) { %> /** - * <%= formatCommentBlock(p.description) %> + * <%- formatCommentBlock(p.description) %> */ <% } -%> - <%= (p.nullable || toTypeScriptType(p) === 'any') ? null : '// @ts-expect-error TS2564\n' -%> - <%= p.name %><%= p.nullable ? '?' : null %>: <%- toTypeScriptType(p) %>; + <%- (p.nullable || toTypeScriptType(p) === 'any') ? null : '// @ts-expect-error TS2564\n' -%> + <%- p.name %><%- p.nullable ? '?' : null %>: <%- toTypeScriptType(p) %>; <% }) %> - static fromJson(obj: any): <%= schema.name %> { + static fromJson(obj: any): <%- schema.name %> { return { ...obj, <% schema.properties.forEach(p => { -%> <% if (needsCasting(p)) { -%> <% if (isDateTime(p)) { -%> - <%= p.name -%>: obj.<%= p.name -%> ? new Date(obj.<%= p.name -%>) : null, + <%- p.name -%>: obj.<%- p.name -%> ? new Date(obj.<%- p.name -%>) : null, <% } else if (!isPrimitive(p)) {-%> - <%= p.name -%>: obj.<%= p.name -%> ? <%= p.$ref.name %>.fromJson(obj.<%= p.name %>) : null, + <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.fromJson(obj.<%- p.name %>) : null, <% } -%> <% } -%> <% }) -%> } } - static toJson(obj: <%= schema.name %>): any{ + static toJson(obj: <%- schema.name %>): any{ return { ...obj, <% schema.properties.forEach(p => { -%> <% if (needsCasting(p)) { -%> <% if (p.type === "string" && p.format === "date-time") { -%> - <%= p.name -%>: obj.<%= p.name -%> ? obj.<%= p.name %>.toISOString() : null, + <%- p.name -%>: obj.<%- p.name -%> ? obj.<%- p.name %>.toISOString() : null, <% } else if (p.$ref && !p.$ref.enum) {-%> - <%= p.name -%>: obj.<%= p.name -%> ? <%= p.$ref.name %>.toJson(obj.<%= p.name %>) : null, + <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.toJson(obj.<%- p.name %>) : null, <% } -%> <% } -%> <% }) -%> @@ -53,11 +53,11 @@ export class <%= schema.name %> { <% } else if (schema.enum) { %> /** - * <%= formatCommentLine(schema.description) %> + * <%- formatCommentLine(schema.description) %> */ -export enum <%= schema.name %> { +export enum <%- schema.name %> { <% schema.enum.forEach(variant => { -%> - <%= variant %> = "<%= variant %>", + <%- variant %> = "<%- variant %>", <% }) -%> } @@ -68,22 +68,22 @@ export enum <%= schema.name %> { <% schema.imports.forEach(imp => { %> <% if (hasComment(imp)) -%> /** - * <%= formatCommentBlock(imp.description) %> + * <%- formatCommentBlock(imp.description) %> * <% if (hasComment(imp.input)) { -%> - * @param input {<%- toTypeScriptType(imp.input) %>} <%= formatCommentLine(imp.input.description) %> + * @param input {<%- toTypeScriptType(imp.input) %>} <%- formatCommentLine(imp.input.description) %> <% } -%> <% if (hasComment(imp.output)) { -%> - * @returns {<%- toTypeScriptType(imp.output) %>} <%= formatCommentLine(imp.output.description) %> + * @returns {<%- toTypeScriptType(imp.output) %>} <%- formatCommentLine(imp.output.description) %> <% } -%> */ -export function <%= imp.name %>(input:<%- imp.input ? toTypeScriptType(imp.input) : 'any' %>) <%- imp.output ? `:${toTypeScriptType(imp.output)}` : null %> { +export function <%- imp.name %>(input:<%- imp.input ? toTypeScriptType(imp.input) : 'any' %>) <%- imp.output ? `:${toTypeScriptType(imp.output)}` : null %> { <% if (imp.input) { -%> <% if (isJsonEncoded(imp.input)) { -%> <% if (isPrimitive(imp.input)) { %> const mem = Memory.fromJsonObject(input as any) <% } else { %> - const casted = <%= toTypeScriptType(imp.input) %>.toJson(input) + const casted = <%- toTypeScriptType(imp.input) %>.toJson(input) const mem = Memory.fromJsonObject(casted) <% } %> <% } else if (isUtf8Encoded(imp.input)) { -%> @@ -94,9 +94,9 @@ export function <%= imp.name %>(input:<%- imp.input ? toTypeScriptType(imp.input const mem = Memory.fromBuffer(input) <% } -%> - <% if (imp.output) { -%>const ptr =<% } -%> hostFunctions.<%= imp.name %>(mem.offset) + <% if (imp.output) { -%>const ptr =<% } -%> hostFunctions.<%- imp.name %>(mem.offset) <% } else { -%> - <% if (imp.output) { -%>const ptr =<% } -%> hostFunctions.<%= imp.name %>() + <% if (imp.output) { -%>const ptr =<% } -%> hostFunctions.<%- imp.name %>() <% } -%> <% if (imp.output) { -%> @@ -105,7 +105,7 @@ export function <%= imp.name %>(input:<%- imp.input ? toTypeScriptType(imp.input return Memory.find(ptr).readJsonObject(); <% } else { -%> const output = Memory.find(ptr).readJsonObject(); - return <%= toTypeScriptType(imp.output) %>.fromJson(output) + return <%- toTypeScriptType(imp.output) %>.fromJson(output) <% } -%> <% } else if (isUtf8Encoded(imp.output)) { -%> return Memory.find(ptr).readString(); diff --git a/template/xtp.toml.ejs b/template/xtp.toml.ejs index aba3a30..93a6755 100644 --- a/template/xtp.toml.ejs +++ b/template/xtp.toml.ejs @@ -1,11 +1,11 @@ -app_id = "<%= project.appId %>" +app_id = "<%- project.appId %>" # This is where 'xtp plugin push' expects to find the wasm file after the build script has run. bin = "dist/plugin.wasm" -extension_point_id = "<%= project.extensionPointId %>" +extension_point_id = "<%- project.extensionPointId %>" # This is the 'binding' name used for the plugin. -name = "<%= project.name %>" +name = "<%- project.name %>" [scripts] # xtp plugin build runs this script to generate the wasm file