@abapify/abap-ast 0.0.0 → 0.3.4

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.
Files changed (2) hide show
  1. package/README.md +218 -1
  2. package/package.json +34 -3
package/README.md CHANGED
@@ -1,3 +1,220 @@
1
1
  # @abapify/abap-ast
2
2
 
3
- Placeholder. The real release is published via CI/CD.
3
+ **Zero-dependency typed AST and deterministic pretty-printer for ABAP source code.**
4
+
5
+ ## Overview
6
+
7
+ `@abapify/abap-ast` is the `ts.factory` of ABAP. It provides a typed node
8
+ model for ABAP classes, interfaces, types, statements, and expressions, plus
9
+ a single `print()` entry point that serialises any node to valid ABAP source.
10
+
11
+ There is **no parser (yet)** — this package is emit-only. A future `.aclass`
12
+ parser (e.g. Chevrotain-based) will produce the same AST, so code built on
13
+ top of this package stays forward-compatible.
14
+
15
+ Primary consumer: [`@abapify/openai-codegen`](../openai-codegen).
16
+
17
+ ### What it gives you
18
+
19
+ - Typed factory functions for every node kind (classes, interfaces,
20
+ methods, types, statements, expressions).
21
+ - A deterministic printer: identical input produces byte-identical output,
22
+ across runs, platforms, and Node versions.
23
+ - Configurable keyword case, indent, and line ending.
24
+ - No runtime dependencies.
25
+
26
+ ## Installation
27
+
28
+ ```bash
29
+ bun add @abapify/abap-ast
30
+ ```
31
+
32
+ ## Quick Start
33
+
34
+ ```typescript
35
+ import {
36
+ print,
37
+ classDef,
38
+ section,
39
+ typeDef,
40
+ methodDef,
41
+ methodImpl,
42
+ methodParam,
43
+ builtinType,
44
+ returnStmt,
45
+ } from '@abapify/abap-ast';
46
+
47
+ const cls = classDef({
48
+ name: 'zcl_greeter',
49
+ isFinal: true,
50
+ sections: [
51
+ section({
52
+ visibility: 'public',
53
+ members: [
54
+ typeDef({ name: 'ty_name', type: builtinType({ name: 'string' }) }),
55
+ methodDef({
56
+ name: 'greet',
57
+ visibility: 'public',
58
+ params: [
59
+ methodParam({
60
+ paramKind: 'importing',
61
+ name: 'iv_name',
62
+ typeRef: builtinType({ name: 'string' }),
63
+ }),
64
+ methodParam({
65
+ paramKind: 'returning',
66
+ name: 'rv_msg',
67
+ typeRef: builtinType({ name: 'string' }),
68
+ }),
69
+ ],
70
+ }),
71
+ ],
72
+ }),
73
+ ],
74
+ implementations: [methodImpl({ name: 'greet', body: [returnStmt()] })],
75
+ });
76
+
77
+ console.log(print(cls));
78
+ ```
79
+
80
+ emits:
81
+
82
+ ```abap
83
+ CLASS zcl_greeter DEFINITION PUBLIC FINAL CREATE PUBLIC.
84
+ PUBLIC SECTION.
85
+ TYPES ty_name TYPE string.
86
+ METHODS greet
87
+ IMPORTING iv_name TYPE string
88
+ RETURNING VALUE(rv_msg) TYPE string.
89
+ ENDCLASS.
90
+
91
+ CLASS zcl_greeter IMPLEMENTATION.
92
+ METHOD greet.
93
+ RETURN.
94
+ ENDMETHOD.
95
+ ENDCLASS.
96
+ ```
97
+
98
+ ## Node kinds
99
+
100
+ Every node has a `kind: NodeKind` PascalCase discriminant. Nodes are grouped
101
+ by topic under `src/nodes/`; each module exports interfaces plus matching
102
+ factory functions. You rarely need the raw interfaces — the factories are
103
+ the public surface.
104
+
105
+ | Module | Factories |
106
+ | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
107
+ | `base.ts` | `comment` (plus shared types: `NodeKind`, `Visibility`, `Identifier`) |
108
+ | `types.ts` | `builtinType`, `namedTypeRef`, `tableType`, `structureType`, `enumType`, `typeDef` |
109
+ | `data.ts` | `dataDecl`, `constantDecl`, `fieldSymbolDecl` |
110
+ | `expressions.ts` | `literal`, `identifierExpr`, `constructorExpr`, `methodCallExpr`, `binOp`, `stringTemplate`, `cast` |
111
+ | `statements.ts` | `assign`, `call`, `raise`, `ifStmt`, `loop`, `returnStmt`, `tryStmt`, `append`, `insert`, `read`, `clear`, `exit`, `continueStmt`, `raw` |
112
+ | `members.ts` | `methodParam`, `methodDef`, `methodImpl`, `eventDef`, `attributeDef` |
113
+ | `class.ts` | `section`, `classDef`, `localClassDef` |
114
+ | `interface.ts` | `interfaceDef` |
115
+
116
+ See [`src/nodes/`](src/nodes) for the full signatures — each factory is a
117
+ one-argument function taking a named-input object and returning a frozen
118
+ AST node.
119
+
120
+ `raw({ source })` is an escape hatch for ABAP fragments that are not yet
121
+ representable in the AST. Consumers should prefer structured nodes and
122
+ confine `raw` to known-safe cases.
123
+
124
+ ## ABAPDoc comments
125
+
126
+ Declaration nodes (`TypeDef`, `MethodDef`, `AttributeDef`, `InterfaceDef`,
127
+ `ClassDef`) accept an optional `abapDoc: readonly string[]` field. The
128
+ printer emits each line verbatim prefixed with `"!` at the declaration's
129
+ own indentation, immediately above it. This is the standard ABAP way to
130
+ attach structured metadata to a declaration (ABAPDoc / Knowledge Transfer
131
+ comments).
132
+
133
+ ```typescript
134
+ import { print, methodDef, methodParam, builtinType } from '@abapify/abap-ast';
135
+
136
+ const m = methodDef({
137
+ name: 'get_foo',
138
+ visibility: 'public',
139
+ abapDoc: [
140
+ '@openapi-operation getFoo',
141
+ '@openapi-path GET /foo/{id}',
142
+ 'Return a foo by id.',
143
+ ],
144
+ params: [
145
+ methodParam({
146
+ paramKind: 'importing',
147
+ name: 'id',
148
+ typeRef: builtinType({ name: 'string' }),
149
+ }),
150
+ ],
151
+ });
152
+
153
+ console.log(print(m));
154
+ ```
155
+
156
+ emits:
157
+
158
+ ```abap
159
+ "! @openapi-operation getFoo
160
+ "! @openapi-path GET /foo/{id}
161
+ "! Return a foo by id.
162
+ METHODS get_foo
163
+ IMPORTING id TYPE string.
164
+ ```
165
+
166
+ Lines are written verbatim: no automatic wrapping, no escaping, no tag
167
+ rewriting. Consumers own their tag conventions.
168
+
169
+ ## Printer
170
+
171
+ ```typescript
172
+ import { print, type PrintOptions } from '@abapify/abap-ast';
173
+
174
+ const source: string = print(node, options?);
175
+ ```
176
+
177
+ | Option | Type | Default | Notes |
178
+ | ------------- | -------------------- | --------- | ------------------------------------------- |
179
+ | `indent` | `number` | `2` | Spaces per indentation level. |
180
+ | `keywordCase` | `'upper' \| 'lower'` | `'upper'` | Case of ABAP keywords (`CLASS`, `METHODS`). |
181
+ | `eol` | `string` | `'\n'` | Line ending (`'\r\n'` for CRLF output). |
182
+
183
+ **Determinism guarantee.** For a given input tree and resolved options,
184
+ `print()` always returns the same string. The printer does no implicit
185
+ sorting, random id generation, or environment lookup. Snapshot-testing the
186
+ output is safe and is how this package is validated (see
187
+ `tests/printer.test.ts`).
188
+
189
+ The printer accepts most nodes at the top level, but a few internal nodes
190
+ (`Section`, `MethodParam`, and bare `TableType`/`StructureType` etc.) throw
191
+ if passed directly — wrap them in their enclosing node (`ClassDef`,
192
+ `MethodDef`, `TypeDef`) first.
193
+
194
+ ## Design
195
+
196
+ This package deliberately mirrors the `ts.factory` pattern from the
197
+ TypeScript compiler API:
198
+
199
+ - The AST is the single source of truth for ABAP structure.
200
+ - Factories validate required fields at construction time and freeze the
201
+ returned node.
202
+ - The printer is a pure function of the tree.
203
+ - No string concatenation leaks into consumer code — they always go through
204
+ a typed factory.
205
+
206
+ A future `.aclass` Chevrotain parser will populate the same AST, at which
207
+ point `print(parse(src)) ≈ src` becomes the round-trip contract.
208
+
209
+ ## Consumers
210
+
211
+ - [`@abapify/openai-codegen`](../openai-codegen) — OpenAPI → ABAP client
212
+ generator. Builds two `InterfaceDef`s, one `ClassDef` for the
213
+ implementation, and one `ClassDef` for the exception. Uses the
214
+ `abapDoc` field to attach round-trip `@openapi-*` markers on every
215
+ generated type and operation, and relies on the printer's determinism
216
+ guarantee for reproducible artefacts.
217
+
218
+ ## License
219
+
220
+ MIT
package/package.json CHANGED
@@ -1,5 +1,36 @@
1
1
  {
2
2
  "name": "@abapify/abap-ast",
3
- "version": "0.0.0",
4
- "description": "Placeholder — real package is published via CI/CD (trusted publishing)."
5
- }
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "0.3.4",
7
+ "description": "Zero-dependency typed AST and pretty-printer for ABAP source code (classes, interfaces, types, statements)",
8
+ "type": "module",
9
+ "types": "./dist/index.d.mts",
10
+ "exports": {
11
+ ".": "./dist/index.mjs",
12
+ "./package.json": "./package.json"
13
+ },
14
+ "dependencies": {},
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "keywords": [
20
+ "abap",
21
+ "ast",
22
+ "codegen",
23
+ "sap"
24
+ ],
25
+ "author": "abapify",
26
+ "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/abapify/adt-cli.git",
30
+ "directory": "packages/abap-ast"
31
+ },
32
+ "homepage": "https://github.com/abapify/adt-cli/tree/main/packages/abap-ast#readme",
33
+ "bugs": {
34
+ "url": "https://github.com/abapify/adt-cli/issues"
35
+ }
36
+ }