From dc6cf5165daa3c05a6348d9675f0551efab7e3ba Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sat, 26 Aug 2023 12:19:32 -0700 Subject: [PATCH] issue: ts-macros appears to be generating temp variable clashes --- package.json5 | 4 ++-- src/generic/arithmetic.ts | 25 ++++++------------------- src/index.ts | 2 +- tsconfig.json | 7 ++++--- 4 files changed, 13 insertions(+), 25 deletions(-) diff --git a/package.json5 b/package.json5 index 8f26409..9caaad9 100644 --- a/package.json5 +++ b/package.json5 @@ -6,8 +6,8 @@ scripts: { test: 'echo "Error: no test specified" && exit 1', build: 'tsc && cp etc/package.json build', - exec: 'node build', - go: 'pnpm --sequential "/build|exec/"', + start: 'node build', + go: 'pnpm --sequential "/build|start/"', }, packageManager: 'pnpm', keywords: [ diff --git a/src/generic/arithmetic.ts b/src/generic/arithmetic.ts index a61946f..720fe57 100644 --- a/src/generic/arithmetic.ts +++ b/src/generic/arithmetic.ts @@ -1,5 +1,5 @@ import type {Dependencies, Signature} from '../interfaces/type.js' -import {$$typeToString, $$ts, $$define, $$raw, $$ident, $$escape} from 'ts-macros' +import {$$typeToString, $$ident, $$escape, $$define} from 'ts-macros' import * as ts from 'typescript' export const square = @@ -50,21 +50,8 @@ console.log("Or maybe even", $$typeToString!< >()) // Now try to wrap it up in a macro - -// From the creator of ts-macros; temporary until next release -function $export(name: string, value: unknown) { - $$raw!((ctx, name: ts.Expression, value: ts.Expression) => { - if (!ctx.ts.isStringLiteral(name)) throw ctx.error(name, "Expected a string literal."); - return [ctx.factory.createVariableStatement([ctx.factory.createToken(ctx.ts.SyntaxKind.ExportKeyword)], ctx.factory.createVariableDeclarationList([ - ctx.factory.createVariableDeclaration(name.text, undefined, undefined, value) - ], ctx.ts.NodeFlags.Const))]; - }); -} - -// Obviously we'd prefer the name before the expression, but that won't work -// until the next release -function $exportImpl(expr: Impl, name: string) { - $export!(name, expr); +function $exportImpl(name: string, expr: Impl) { + $$define!(name, expr, false, true); // Final `true` is export $$ident!(name).reflectedType = $$typeToString!< // (...args: DeepExpand>>) => DeepExpand>> // see comment in reflect below. Impl @@ -73,9 +60,9 @@ function $exportImpl(expr: Impl, name: string) { // works but then not visible at import with current ts-macros. // Author says he will be enhancing this "soon." -$exportImpl!((dep: Dependencies<'multiply', T>): Signature<'square', T> => - z => dep.multiply(z, z), - 'squire') +$exportImpl!('squire', + (dep: Dependencies<'multiply', T>): Signature<'square', T> => + z => dep.multiply(z, z)) function $reflect(expr: Impl) { return $$escape!(() => { diff --git a/src/index.ts b/src/index.ts index 79791d0..a3a5953 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,7 +27,7 @@ console.log('Type of square is', Specifications.generic.square.reflectedType) // Auto-generated export is invisible to TypeScript at the moment, author // says he will fix: console.log('Type of squire (auto-exported) is', - // @ts-ignore + // not ts-ignore Specifications.generic.squire.reflectedType) // Via a macro wrapper around the definition, two ways: diff --git a/tsconfig.json b/tsconfig.json index a5c4a3d..43f365a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,8 +4,9 @@ "rootDir": "./src", "outDir": "./build", "moduleResolution": "nodenext", - "plugins": [ - {"transform": "ts-macros" } - ] + "plugins": [ { + "transform": "ts-macros/dist/type-resolve", + "transformProgram": true + } ] } }