diff --git a/README.md b/README.md index 3ee0bc4..701f683 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # typescript-type-introspection Reason with TypeScript code via -[type introspection](https://en.wikipedia.org/wiki/Type_introspection). +[_type introspection_](https://en.wikipedia.org/wiki/Type_introspection), +powered by [Tree-Sitter](https://tree-sitter.github.io/tree-sitter/). ## Introspection @@ -13,7 +14,7 @@ dynamic type handling in your TypeScript projects. ## Installation ```sh -# TODO: Publish on JSR.io +deno add @fartlabs/typescript-type-introspection ``` ## TypeBox @@ -38,9 +39,7 @@ dynamic type handling in your TypeScript projects. Instantiate a TypeBox schema at runtime using introspection via `@sinclair/typebox/compiler`. -```ts -// TODO: Add TypeBox example. -``` + ## Development diff --git a/deno.jsonc b/deno.jsonc index 1f75fc5..00a0dec 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,6 +1,12 @@ { "name": "@fartlabs/typescript-type-introspector", "version": "0.0.1", + "exports": { + ".": "./lib/introspector/mod.ts", + "./typebox": "./lib/typebox/mod.ts", + "./tree-sitter/tree-sitter": "./lib/tree-sitter/tree-sitter.ts", + "./tree-sitter/tree-sitter-introspector": "./lib/tree-sitter/tree-sitter-introspector.ts" + }, "lock": false, "tasks": { "dev": "deno test --watch -A" diff --git a/examples/tree-sitter-introspector.test.ts b/examples/tree-sitter-introspector.test.ts index c6e0430..19a48d1 100644 --- a/examples/tree-sitter-introspector.test.ts +++ b/examples/tree-sitter-introspector.test.ts @@ -1,6 +1,6 @@ import { assertEquals } from "@std/assert"; -import { TreeSitterIntrospector } from "#/lib/introspector/tree-sitter-introspector.ts"; -import { EXAMPLE_TYPESCRIPT_TREE_SITTER_PARSER } from "../lib/example/tree-sitter-parser.ts"; +import { TreeSitterIntrospector } from "#/lib/tree-sitter/tree-sitter-introspector.ts"; +import { EXAMPLE_TYPESCRIPT_TREE_SITTER_PARSER } from "#/lib/example/tree-sitter-parser.ts"; import { EXAMPLE_PERSON_INTROSPECTION, EXAMPLE_PERSON_SOURCE_CODE, diff --git a/lib/example/book.ts b/lib/example/book.ts index 0e8b202..1019397 100644 --- a/lib/example/book.ts +++ b/lib/example/book.ts @@ -1,10 +1,11 @@ import { queryRootNode } from "#/lib/tree-sitter/tree-sitter.ts"; -import { makeTreeSitterTypeScriptClassPattern } from "#/lib/introspector/tree-sitter-introspector.ts"; +import { makeTreeSitterTypeScriptClassPattern } from "#/lib/tree-sitter/tree-sitter-introspector.ts"; import { EXAMPLE_TYPESCRIPT_CLASS_TREE_SITTER_CAPTURE_NAME_MAP, EXAMPLE_TYPESCRIPT_TREE_SITTER_PARSER, } from "./tree-sitter-parser.ts"; -import { EXAMPLE_PERSON, Person } from "./person.ts"; +import type { Person } from "./person.ts"; +import { EXAMPLE_PERSON } from "./person.ts"; /** * Book is an example class that represents a book. diff --git a/lib/example/mod.ts b/lib/example/mod.ts new file mode 100644 index 0000000..ac540e4 --- /dev/null +++ b/lib/example/mod.ts @@ -0,0 +1,3 @@ +export * from "./book.ts"; +export * from "./person.ts"; +export * from "./tree-sitter-parser.ts"; diff --git a/lib/example/person.ts b/lib/example/person.ts index c10d7e2..81851aa 100644 --- a/lib/example/person.ts +++ b/lib/example/person.ts @@ -1,6 +1,6 @@ import { queryRootNode } from "#/lib/tree-sitter/tree-sitter.ts"; import type { Introspection } from "#/lib/introspector/introspector.ts"; -import { makeTreeSitterTypeScriptClassPattern } from "#/lib/introspector/tree-sitter-introspector.ts"; +import { makeTreeSitterTypeScriptClassPattern } from "#/lib/tree-sitter/tree-sitter-introspector.ts"; import { EXAMPLE_TYPESCRIPT_CLASS_TREE_SITTER_CAPTURE_NAME_MAP, EXAMPLE_TYPESCRIPT_TREE_SITTER_PARSER, diff --git a/lib/example/tree-sitter-parser.ts b/lib/example/tree-sitter-parser.ts index 9a3bfa1..a4d5c55 100644 --- a/lib/example/tree-sitter-parser.ts +++ b/lib/example/tree-sitter-parser.ts @@ -1,6 +1,6 @@ import { parserFromWasm } from "deno-tree-sitter/main.js"; import typescript from "common-tree-sitter-languages/typescript.js"; -import { defaultClassTreeSitterCaptureNameMap } from "#/lib/introspector/tree-sitter-introspector.ts"; +import { defaultClassTreeSitterCaptureNameMap } from "#/lib/tree-sitter/tree-sitter-introspector.ts"; /** * EXAMPLE_TYPESCRIPT_TREE_SITTER_PARSER is a tree-sitter parser for TypeScript. diff --git a/lib/introspector/mod.ts b/lib/introspector/mod.ts new file mode 100644 index 0000000..1e49f70 --- /dev/null +++ b/lib/introspector/mod.ts @@ -0,0 +1 @@ +export * from "./introspector.ts"; diff --git a/lib/introspector/tree-sitter-introspector.ts b/lib/tree-sitter/tree-sitter-introspector.ts similarity index 87% rename from lib/introspector/tree-sitter-introspector.ts rename to lib/tree-sitter/tree-sitter-introspector.ts index 9535a59..88ef726 100644 --- a/lib/introspector/tree-sitter-introspector.ts +++ b/lib/tree-sitter/tree-sitter-introspector.ts @@ -1,21 +1,14 @@ -import type { - NamedCapture, - Parser, - Tree, -} from "#/lib/tree-sitter/tree-sitter.ts"; -import { - findCaptureString, - queryRootNode, -} from "#/lib/tree-sitter/tree-sitter.ts"; import type { IntrospectedProperty, Introspection, Introspector, -} from "./introspector.ts"; +} from "#/lib/introspector/introspector.ts"; +import type { NamedCapture, Parser, Tree } from "./tree-sitter.ts"; +import { findCaptureString, queryRootNode } from "./tree-sitter.ts"; /** - * TreeSitterTypeScriptTypeIntrospector is a TypeScript type introspector that - * uses Tree Sitter to introspect TypeScript types. + * TreeSitterIntrospector is a TypeScript type introspector that uses Tree + * Sitter to introspect TypeScript types. */ export class TreeSitterIntrospector implements Introspector { public constructor( @@ -149,13 +142,14 @@ export const typeScriptTypeAnnotationPrefix = ": "; /** * defaultClassTreeSitterCaptureNameMap is the default class capture name map. */ -export const defaultClassTreeSitterCaptureNameMap = { - PROPERTY_IDENTIFIER: "property-identifier", - PUBLIC_FIELD_DEFINITION: "public-field-definition", - TYPE_ANNOTATION: "type-annotation", - TYPE_IDENTIFIER: "type-identifier", - VALUE: "value", -} as const satisfies ClassTreeSitterCaptureNameMap; +export const defaultClassTreeSitterCaptureNameMap: + ClassTreeSitterCaptureNameMap = { + PROPERTY_IDENTIFIER: "property-identifier", + PUBLIC_FIELD_DEFINITION: "public-field-definition", + TYPE_ANNOTATION: "type-annotation", + TYPE_IDENTIFIER: "type-identifier", + VALUE: "value", + } as const; /** * ClassTreeSitterCaptureNameMap is a map of class capture names to their diff --git a/lib/typebox/mod.ts b/lib/typebox/mod.ts new file mode 100644 index 0000000..0d47b0b --- /dev/null +++ b/lib/typebox/mod.ts @@ -0,0 +1 @@ +export * from "./schema-from-introspection.ts";