@atscript/typescript 0.1.18 → 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,240 @@
1
+ # Annotations & Primitives — @atscript/typescript
2
+
3
+ > All built-in annotations, their arguments, and how to define custom annotations and primitives.
4
+
5
+ ## Built-in Annotations
6
+
7
+ ### `@meta.*` — Metadata Annotations
8
+
9
+ | Annotation | Arguments | Description |
10
+ |------------|-----------|-------------|
11
+ | `@meta.label` | `text: string` | Human-readable label for UI, logs, documentation |
12
+ | `@meta.id` | `name?: string` (optional) | Mark field as unique identifier; optional custom name |
13
+ | `@meta.description` | `text: string` | Detailed description of a field or entity |
14
+ | `@meta.documentation` | `text: string` | Multi-line docs (Markdown). Multiple allowed — each appends |
15
+ | `@meta.placeholder` | `text: string` | Placeholder for UI input fields (props/types only) |
16
+ | `@meta.sensitive` | *(none)* | Mark as sensitive (passwords, API keys). Strips from serialization |
17
+ | `@meta.readonly` | *(none)* | Mark as read-only |
18
+ | `@meta.required` | `message?: string` | Required field. Strings: non-whitespace. Booleans: must be `true` |
19
+ | `@meta.default` | `value: string` | Default value (strings as-is, others parsed as JSON) |
20
+ | `@meta.example` | `value: string` | Example value (strings as-is, others parsed as JSON) |
21
+ | `@meta.isKey` | *(none)* | Mark field as key inside array (string/number types only) |
22
+
23
+ ### `@expect.*` — Validation Constraints
24
+
25
+ | Annotation | Arguments | Applies To | Description |
26
+ |------------|-----------|-----------|-------------|
27
+ | `@expect.minLength` | `length: number`, `message?: string` | string, array | Minimum length |
28
+ | `@expect.maxLength` | `length: number`, `message?: string` | string, array | Maximum length |
29
+ | `@expect.min` | `minValue: number`, `message?: string` | number | Minimum value |
30
+ | `@expect.max` | `maxValue: number`, `message?: string` | number | Maximum value |
31
+ | `@expect.int` | *(none)* | number | Must be integer |
32
+ | `@expect.pattern` | `pattern: string`, `flags?: string`, `message?: string` | string | Regex validation. **Multiple allowed** (all must pass) |
33
+
34
+ ### `@emit.*` — Build-time Directives
35
+
36
+ | Annotation | Applies To | Description |
37
+ |------------|-----------|-------------|
38
+ | `@emit.jsonSchema` | interface | Pre-compute and embed JSON Schema at build time |
39
+
40
+ ## Custom Annotations
41
+
42
+ Define custom annotations in `atscript.config.ts` using `AnnotationSpec`:
43
+
44
+ ```ts
45
+ import { defineConfig, AnnotationSpec } from '@atscript/core'
46
+ import tsPlugin from '@atscript/typescript'
47
+
48
+ export default defineConfig({
49
+ plugins: [tsPlugin()],
50
+ annotations: {
51
+ // Namespaced annotations use nested objects
52
+ ui: {
53
+ // @ui.component "DatePicker"
54
+ component: new AnnotationSpec({
55
+ argument: { name: 'name', type: 'string' },
56
+ description: 'UI component to render this field',
57
+ }),
58
+
59
+ // @ui.hidden (no arguments — boolean flag)
60
+ hidden: new AnnotationSpec({
61
+ description: 'Hide this field in the UI',
62
+ }),
63
+
64
+ // @ui.order 5
65
+ order: new AnnotationSpec({
66
+ argument: { name: 'position', type: 'number' },
67
+ }),
68
+ },
69
+
70
+ // @tag "important" (multiple allowed, each appended)
71
+ tag: new AnnotationSpec({
72
+ multiple: true,
73
+ mergeStrategy: 'append',
74
+ argument: { name: 'value', type: 'string' },
75
+ }),
76
+
77
+ // Annotation with multiple named arguments
78
+ // @api.endpoint "/users" "GET"
79
+ api: {
80
+ endpoint: new AnnotationSpec({
81
+ argument: [
82
+ { name: 'path', type: 'string' },
83
+ { name: 'method', type: 'string', optional: true },
84
+ ],
85
+ }),
86
+ },
87
+ },
88
+ })
89
+ ```
90
+
91
+ ### `AnnotationSpec` Options
92
+
93
+ ```ts
94
+ new AnnotationSpec({
95
+ // Single argument
96
+ argument: { name: 'value', type: 'string' },
97
+
98
+ // Or multiple arguments
99
+ argument: [
100
+ { name: 'first', type: 'string' },
101
+ { name: 'second', type: 'number', optional: true },
102
+ ],
103
+
104
+ // Allow multiple instances on the same target
105
+ multiple: true, // default: false
106
+
107
+ // How duplicates merge: 'replace' (last wins) or 'append' (collect into array)
108
+ mergeStrategy: 'append', // default: 'replace'
109
+
110
+ // Human-readable description
111
+ description: 'What this annotation does',
112
+
113
+ // Restrict to specific node types
114
+ nodeType: ['interface', 'type', 'prop'],
115
+
116
+ // Custom validation function
117
+ validate: (mainToken, args, doc) => {
118
+ // Return array of diagnostic messages, or undefined
119
+ },
120
+ })
121
+ ```
122
+
123
+ ### Argument Types
124
+
125
+ Each argument accepts:
126
+
127
+ | Field | Type | Description |
128
+ |-------|------|-------------|
129
+ | `name` | `string` | Argument name (used in metadata object key) |
130
+ | `type` | `'string' \| 'number' \| 'boolean'` | Expected type |
131
+ | `optional` | `boolean` | Whether the argument can be omitted |
132
+ | `description` | `string` | Human-readable description |
133
+ | `values` | `string[]` | Allowed values (enum-like constraint) |
134
+
135
+ ### How Annotations Map to Runtime Metadata
136
+
137
+ - **Single argument** → metadata value is the argument value directly
138
+ - **Multiple named arguments** → metadata value is an object with argument names as keys
139
+ - **No arguments** → metadata value is `true`
140
+ - **`multiple: true`** → metadata value is an array
141
+
142
+ Example: `@api.endpoint "/users" "GET"` becomes:
143
+ ```ts
144
+ metadata.get('api.endpoint') // → { path: "/users", method: "GET" }
145
+ ```
146
+
147
+ ## Custom Primitives
148
+
149
+ Define custom primitive types in `atscript.config.ts`:
150
+
151
+ ```ts
152
+ export default defineConfig({
153
+ primitives: {
154
+ // Simple alias with built-in validation
155
+ currency: {
156
+ type: 'string',
157
+ tags: ['string'],
158
+ expect: {
159
+ pattern: /^\d+\.\d{2}$/,
160
+ message: 'Must be in format 0.00',
161
+ },
162
+ },
163
+
164
+ // Primitive with extensions (subtypes)
165
+ url: {
166
+ type: 'string',
167
+ tags: ['string'],
168
+ expect: {
169
+ pattern: /^https?:\/\/.+/,
170
+ },
171
+ extensions: {
172
+ // url.https — only HTTPS
173
+ https: {
174
+ expect: {
175
+ pattern: /^https:\/\/.+/,
176
+ },
177
+ },
178
+ // url.relative — relative URLs
179
+ relative: {
180
+ expect: {
181
+ pattern: /^\/.+/,
182
+ },
183
+ },
184
+ },
185
+ },
186
+
187
+ // Object-shaped primitive
188
+ point: {
189
+ type: {
190
+ kind: 'object',
191
+ props: {
192
+ x: 'number',
193
+ y: 'number',
194
+ },
195
+ },
196
+ },
197
+ },
198
+ })
199
+ ```
200
+
201
+ ### `TPrimitiveConfig` Options
202
+
203
+ | Field | Type | Description |
204
+ |-------|------|-------------|
205
+ | `type` | `TPrimitiveTypeDef` | Base type: `'string'`, `'number'`, `'boolean'`, `'void'`, `'null'`, `'phantom'`, or complex type |
206
+ | `tags` | `string[]` | Custom tags for categorization |
207
+ | `documentation` | `string` | Documentation string |
208
+ | `expect` | object | Built-in validation constraints |
209
+ | `extensions` | `Record<string, Partial<TPrimitiveConfig>>` | Sub-types accessible via dot notation |
210
+
211
+ ### `expect` Validation on Primitives
212
+
213
+ | Field | Applies To | Description |
214
+ |-------|-----------|-------------|
215
+ | `min` | number | Minimum value |
216
+ | `max` | number | Maximum value |
217
+ | `int` | number | Must be integer |
218
+ | `minLength` | string, array | Minimum length |
219
+ | `maxLength` | string, array | Maximum length |
220
+ | `pattern` | string | Regex pattern(s) |
221
+ | `required` | string, boolean | Non-empty / must be true |
222
+ | `message` | any | Custom error message for pattern |
223
+
224
+ ### Usage in `.as` Files
225
+
226
+ After defining custom primitives/annotations, use them directly:
227
+
228
+ ```as
229
+ interface Product {
230
+ @meta.label "Price"
231
+ price: currency
232
+
233
+ @ui.component "UrlInput"
234
+ website: url.https
235
+
236
+ @tag "featured"
237
+ @tag "new"
238
+ featured: boolean
239
+ }
240
+ ```
@@ -0,0 +1,120 @@
1
+ # Code Generation — @atscript/typescript
2
+
3
+ > How `.as` files are transformed into `.d.ts` type declarations and `.js` runtime modules.
4
+
5
+ ## Overview
6
+
7
+ Each `.as` file produces two outputs:
8
+
9
+ | Output | Generated By | Contains |
10
+ |--------|-------------|----------|
11
+ | `*.as.d.ts` | `npx asc -f dts` or build tool | TypeScript type declarations — interfaces become `declare class` with static type/metadata/validator |
12
+ | `*.as.js` | Build tool (unplugin-atscript) | Runtime module — classes with full type definitions, metadata maps, and validator factories |
13
+
14
+ ## `.d.ts` Output
15
+
16
+ The TypeScript declaration file makes `.as` types importable with full IntelliSense:
17
+
18
+ ```as
19
+ // user.as
20
+ @meta.label "User"
21
+ export interface User {
22
+ name: string
23
+ age: number
24
+ email?: string
25
+ }
26
+
27
+ export type Status = "active" | "inactive"
28
+ ```
29
+
30
+ Generates `user.as.d.ts`:
31
+
32
+ ```ts
33
+ import type {
34
+ TAtscriptTypeObject, TAtscriptAnnotatedType,
35
+ TMetadataMap, Validator, TValidatorOptions
36
+ } from "@atscript/typescript/utils"
37
+
38
+ export declare class User {
39
+ name: string
40
+ age: number
41
+ email?: string
42
+ static __is_atscript_annotated_type: true
43
+ static type: TAtscriptTypeObject<keyof User, User>
44
+ static metadata: TMetadataMap<AtscriptMetadata>
45
+ static validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof User>
46
+ static toJsonSchema: () => any
47
+ }
48
+
49
+ export type Status = "active" | "inactive"
50
+ declare namespace Status {
51
+ const __is_atscript_annotated_type: true
52
+ const type: TAtscriptTypeComplex<Status>
53
+ const metadata: TMetadataMap<AtscriptMetadata>
54
+ const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof Status>
55
+ const toJsonSchema: () => any
56
+ }
57
+ ```
58
+
59
+ Key points:
60
+ - **Interfaces** become `declare class` — so they work both as types and runtime values
61
+ - **Types** become a `type` alias + a companion `namespace` with runtime statics
62
+ - Each has `type`, `metadata`, `validator()`, and `toJsonSchema()` statics
63
+
64
+ ## `.js` Output
65
+
66
+ The JS module creates actual classes with runtime type definitions and metadata:
67
+
68
+ - Uses `defineAnnotatedType` (aliased as `$`) to build the type tree
69
+ - Populates metadata maps with all annotation values
70
+ - Wires up `validator()` and `toJsonSchema()` methods
71
+
72
+ You don't normally read or modify generated JS — the build tool handles it.
73
+
74
+ ## `atscript.d.ts` — Global Type Declarations
75
+
76
+ Running `npx asc -f dts` also generates `atscript.d.ts` in your project root:
77
+
78
+ ```ts
79
+ export {}
80
+
81
+ declare global {
82
+ interface AtscriptMetadata {
83
+ 'meta.label': string
84
+ 'meta.required': { message?: string } | true
85
+ 'expect.minLength': { length: number; message?: string }
86
+ // ... all annotations used in your project
87
+ }
88
+ type AtscriptPrimitiveTags = "string" | "number" | "boolean" | "null"
89
+ }
90
+ ```
91
+
92
+ This file is **auto-generated** based on the annotations actually used across all your `.as` files. It enables:
93
+
94
+ - Type-safe `metadata.get('meta.label')` calls — TypeScript knows the return type
95
+ - Autocompletion for annotation keys
96
+ - Correct types for primitive tags
97
+
98
+ **Important**: Re-run `npx asc -f dts` after adding new annotations to your config. The `atscript.d.ts` file should be committed to your repository.
99
+
100
+ ## Import Paths
101
+
102
+ Generated files use these import paths:
103
+
104
+ | Import | Source |
105
+ |--------|--------|
106
+ | `@atscript/typescript/utils` | Runtime utilities (used by generated `.js` files) |
107
+
108
+ When importing from `.as` files in your TypeScript code:
109
+
110
+ ```ts
111
+ // Import the generated interface — works as both a type and a runtime value
112
+ import { User } from './models/user.as'
113
+
114
+ // Use as a type
115
+ function greet(user: User) { ... }
116
+
117
+ // Use as a runtime value (has metadata, validator, etc.)
118
+ User.metadata.get('meta.label') // "User"
119
+ User.validator().validate(data)
120
+ ```
@@ -0,0 +1,153 @@
1
+ # Setup & Configuration — @atscript/typescript
2
+
3
+ > Installation, configuration file, TypeScript plugin options, and CLI usage.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @atscript/typescript @atscript/core
9
+ # or
10
+ npm install @atscript/typescript @atscript/core
11
+ ```
12
+
13
+ For build-tool integration (Vite, Webpack, Rollup, esbuild, Rspack):
14
+
15
+ ```bash
16
+ pnpm add unplugin-atscript
17
+ ```
18
+
19
+ ## Configuration File
20
+
21
+ Create `atscript.config.ts` (or `.js`, `.mjs`) in your project root:
22
+
23
+ ```ts
24
+ import { defineConfig } from '@atscript/core'
25
+ import tsPlugin from '@atscript/typescript'
26
+
27
+ export default defineConfig({
28
+ // Root directory for resolving .as files (defaults to cwd)
29
+ rootDir: '.',
30
+
31
+ // Entry points — glob patterns for .as files to compile
32
+ entries: ['src/**/*.as'],
33
+
34
+ // Include/exclude globs for dependency resolution
35
+ include: ['src/**/*.as'],
36
+ exclude: ['node_modules/**'],
37
+
38
+ // Plugins — tsPlugin is the TypeScript language extension
39
+ plugins: [tsPlugin()],
40
+
41
+ // How to handle unknown annotations: 'allow' | 'warn' | 'error'
42
+ unknownAnnotation: 'warn',
43
+
44
+ // Custom primitives (merged with built-in ones)
45
+ primitives: {},
46
+
47
+ // Custom annotations (merged with built-in ones)
48
+ annotations: {},
49
+ })
50
+ ```
51
+
52
+ ### `TAtscriptConfig` Fields
53
+
54
+ | Field | Type | Description |
55
+ |-------|------|-------------|
56
+ | `rootDir` | `string` | Root directory for resolving files |
57
+ | `entries` | `string[]` | Entry point globs for `.as` files |
58
+ | `include` | `string[]` | Include globs |
59
+ | `exclude` | `string[]` | Exclude globs |
60
+ | `plugins` | `TAtscriptPlugin[]` | Build plugins (e.g. `tsPlugin()`) |
61
+ | `primitives` | `Record<string, TPrimitiveConfig>` | Custom primitive type definitions |
62
+ | `annotations` | `TAnnotationsTree` | Custom annotation definitions |
63
+ | `unknownAnnotation` | `'allow' \| 'warn' \| 'error'` | Unknown annotation handling |
64
+ | `format` | `string` | Output format (set by CLI or build tool) |
65
+ | `outDir` | `string` | Output directory |
66
+
67
+ ## TypeScript Plugin Options
68
+
69
+ ```ts
70
+ tsPlugin({
71
+ jsonSchema: false // default — toJsonSchema() throws at runtime
72
+ // jsonSchema: 'lazy' — import buildJsonSchema, compute on demand, cache
73
+ // jsonSchema: 'bundle' — pre-compute at build time, embed in output
74
+ })
75
+ ```
76
+
77
+ Individual interfaces can override this with `@emit.jsonSchema` annotation to force build-time embedding regardless of plugin setting.
78
+
79
+ ## Build Tool Integration
80
+
81
+ ### Vite
82
+
83
+ ```ts
84
+ // vite.config.ts
85
+ import atscript from 'unplugin-atscript/vite'
86
+
87
+ export default {
88
+ plugins: [atscript()]
89
+ }
90
+ ```
91
+
92
+ ### Webpack
93
+
94
+ ```ts
95
+ // webpack.config.js
96
+ const atscript = require('unplugin-atscript/webpack')
97
+ module.exports = {
98
+ plugins: [atscript()]
99
+ }
100
+ ```
101
+
102
+ The unplugin automatically compiles `.as` files during development and build, generating `.as.js` modules that are importable.
103
+
104
+ ## CLI — `asc`
105
+
106
+ The `asc` CLI compiles `.as` files outside of build tools (e.g. for generating `.d.ts` files).
107
+
108
+ ```bash
109
+ # Generate .d.ts files (most common use case)
110
+ npx asc -f dts
111
+
112
+ # Generate .js files
113
+ npx asc -f js
114
+
115
+ # Use a specific config file
116
+ npx asc -c atscript.config.ts
117
+
118
+ # Only run diagnostics, no file output
119
+ npx asc --noEmit
120
+
121
+ # Skip diagnostics, always emit
122
+ npx asc --skipDiag
123
+ ```
124
+
125
+ ### Why run `npx asc -f dts`?
126
+
127
+ This generates two things:
128
+ 1. **`*.as.d.ts`** files next to each `.as` file — TypeScript type declarations for all interfaces/types
129
+ 2. **`atscript.d.ts`** in the project root — global type declarations for `AtscriptMetadata` and `AtscriptPrimitiveTags`
130
+
131
+ The `atscript.d.ts` file is **crucial for type safety** — it tells TypeScript about all annotations used in your project, enabling type-safe metadata access at runtime.
132
+
133
+ ### CLI Options
134
+
135
+ | Option | Short | Description |
136
+ |--------|-------|-------------|
137
+ | `--config <path>` | `-c` | Path to config file |
138
+ | `--format <fmt>` | `-f` | Output format: `dts`, `js` |
139
+ | `--noEmit` | | Only check for errors |
140
+ | `--skipDiag` | | Skip diagnostics, always emit |
141
+
142
+ ## Project Structure Example
143
+
144
+ ```
145
+ my-project/
146
+ atscript.config.ts ← config file
147
+ atscript.d.ts ← generated by `npx asc -f dts` (global types)
148
+ src/
149
+ models/
150
+ user.as ← source file
151
+ user.as.d.ts ← generated type declarations
152
+ user.as.js ← generated runtime module (by build tool)
153
+ ```