issue: ts-macros appears to be generating temp variable clashes

This commit is contained in:
Glen Whitney 2023-08-26 12:19:32 -07:00
parent ae2303af7c
commit dc6cf5165d
4 changed files with 13 additions and 25 deletions

View File

@ -6,8 +6,8 @@
scripts: { scripts: {
test: 'echo "Error: no test specified" && exit 1', test: 'echo "Error: no test specified" && exit 1',
build: 'tsc && cp etc/package.json build', build: 'tsc && cp etc/package.json build',
exec: 'node build', start: 'node build',
go: 'pnpm --sequential "/build|exec/"', go: 'pnpm --sequential "/build|start/"',
}, },
packageManager: 'pnpm', packageManager: 'pnpm',
keywords: [ keywords: [

View File

@ -1,5 +1,5 @@
import type {Dependencies, Signature} from '../interfaces/type.js' 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' import * as ts from 'typescript'
export const square = export const square =
@ -50,21 +50,8 @@ console.log("Or maybe even", $$typeToString!<
>()) >())
// Now try to wrap it up in a macro // Now try to wrap it up in a macro
function $exportImpl<Impl>(name: string, expr: Impl) {
// From the creator of ts-macros; temporary until next release $$define!(name, expr, false, true); // Final `true` is export
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<Impl>(expr: Impl, name: string) {
$export!(name, expr);
$$ident!(name).reflectedType = $$typeToString!< $$ident!(name).reflectedType = $$typeToString!<
// <T>(...args: DeepExpand<Parameters<Impl<T>>>) => DeepExpand<ReturnType<Impl<T>>> // see comment in reflect below. // <T>(...args: DeepExpand<Parameters<Impl<T>>>) => DeepExpand<ReturnType<Impl<T>>> // see comment in reflect below.
Impl Impl
@ -73,9 +60,9 @@ function $exportImpl<Impl>(expr: Impl, name: string) {
// works but then not visible at import with current ts-macros. // works but then not visible at import with current ts-macros.
// Author says he will be enhancing this "soon." // Author says he will be enhancing this "soon."
$exportImpl!(<T>(dep: Dependencies<'multiply', T>): Signature<'square', T> => $exportImpl!('squire',
z => dep.multiply(z, z), <T>(dep: Dependencies<'multiply', T>): Signature<'square', T> =>
'squire') z => dep.multiply(z, z))
function $reflect<Impl>(expr: Impl) { function $reflect<Impl>(expr: Impl) {
return $$escape!(() => { return $$escape!(() => {

View File

@ -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 // Auto-generated export is invisible to TypeScript at the moment, author
// says he will fix: // says he will fix:
console.log('Type of squire (auto-exported) is', console.log('Type of squire (auto-exported) is',
// @ts-ignore // not ts-ignore
Specifications.generic.squire.reflectedType) Specifications.generic.squire.reflectedType)
// Via a macro wrapper around the definition, two ways: // Via a macro wrapper around the definition, two ways:

View File

@ -4,8 +4,9 @@
"rootDir": "./src", "rootDir": "./src",
"outDir": "./build", "outDir": "./build",
"moduleResolution": "nodenext", "moduleResolution": "nodenext",
"plugins": [ "plugins": [ {
{"transform": "ts-macros" } "transform": "ts-macros/dist/type-resolve",
] "transformProgram": true
} ]
} }
} }