acture-build-tier 1.1.0
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.
- package/LICENSE +190 -0
- package/README.md +112 -0
- package/dist/ast.cjs +146 -0
- package/dist/ast.cjs.map +1 -0
- package/dist/ast.d.cts +65 -0
- package/dist/ast.d.ts +65 -0
- package/dist/ast.js +111 -0
- package/dist/ast.js.map +1 -0
- package/dist/chunk-SWJN6WEL.js +138 -0
- package/dist/chunk-SWJN6WEL.js.map +1 -0
- package/dist/esbuild.cjs +163 -0
- package/dist/esbuild.cjs.map +1 -0
- package/dist/esbuild.d.cts +44 -0
- package/dist/esbuild.d.ts +44 -0
- package/dist/esbuild.js +24 -0
- package/dist/esbuild.js.map +1 -0
- package/dist/index.cjs +141 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +62 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/vite.cjs +157 -0
- package/dist/vite.cjs.map +1 -0
- package/dist/vite.d.cts +37 -0
- package/dist/vite.d.ts +37 -0
- package/dist/vite.js +22 -0
- package/dist/vite.js.map +1 -0
- package/package.json +85 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding any notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Thor Whalen
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# acture-build-tier
|
|
2
|
+
|
|
3
|
+
> **acture is a development tool first.** This is dev/build-time tooling — it never becomes a runtime dependency of the apps it serves, and using it is entirely optional. See [`docs/positioning.md`](../../docs/positioning.md).
|
|
4
|
+
|
|
5
|
+
Build-step plugin. Scans `.ts` / `.tsx` source for JSDoc tier tags on `defineCommand` calls and mirrors them into the runtime command's `tier` metadata field.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add -D acture-build-tier
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## tsup / esbuild
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// tsup.config.ts
|
|
17
|
+
import { defineConfig } from 'tsup';
|
|
18
|
+
import { actureBuildTier } from 'acture-build-tier/esbuild';
|
|
19
|
+
|
|
20
|
+
export default defineConfig({
|
|
21
|
+
entry: ['src/index.ts'],
|
|
22
|
+
esbuildPlugins: [actureBuildTier()],
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Vite
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// vite.config.ts
|
|
30
|
+
import { defineConfig } from 'vite';
|
|
31
|
+
import { actureBuildTierVite } from 'acture-build-tier/vite';
|
|
32
|
+
|
|
33
|
+
export default defineConfig({
|
|
34
|
+
plugins: [actureBuildTierVite()],
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## What it does
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
/**
|
|
42
|
+
* Search users by email or display name.
|
|
43
|
+
* @stable
|
|
44
|
+
*/
|
|
45
|
+
export const searchUsers = defineCommand({
|
|
46
|
+
id: 'app.users.search',
|
|
47
|
+
title: 'Search Users',
|
|
48
|
+
execute: async (params) => { /* ... */ },
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
becomes (effectively):
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
export const searchUsers = defineCommand({
|
|
56
|
+
tier: "stable",
|
|
57
|
+
id: 'app.users.search',
|
|
58
|
+
title: 'Search Users',
|
|
59
|
+
execute: async (params) => { /* ... */ },
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Four tags recognized:
|
|
64
|
+
|
|
65
|
+
- `@stable` — `tier: 'stable'`
|
|
66
|
+
- `@experimental` — `tier: 'experimental'`
|
|
67
|
+
- `@deprecated [reason]` — `tier: 'deprecated'` plus `deprecationReason: '<reason>'`
|
|
68
|
+
- `@internal` — `tier: 'internal'` plus `internalToken: <module-scoped Symbol>`
|
|
69
|
+
|
|
70
|
+
Precedence (most-restrictive wins): `@internal` > `@deprecated` > `@experimental` > `@stable`.
|
|
71
|
+
|
|
72
|
+
The plugin is **idempotent**: if a spec already declares `tier:` explicitly, the JSDoc is ignored. So you can mix hand-written and tag-driven tiers without conflict.
|
|
73
|
+
|
|
74
|
+
## `@internal` enforcement
|
|
75
|
+
|
|
76
|
+
For each file containing at least one `@internal` command, the plugin injects a per-file module-scoped Symbol:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const __actureInternalToken__ = /* @__PURE__ */ Symbol('acture.internal');
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Each `@internal` command's `internalToken` field references this Symbol. The runtime registry rejects `dispatch` calls from outside the module (i.e., from any caller that didn't capture the Symbol at module load). See [`acture-tier-system`](../../.claude/skills/acture-tier-system/SKILL.md) §7.5.
|
|
83
|
+
|
|
84
|
+
## AST mode (v1.2)
|
|
85
|
+
|
|
86
|
+
The default transform is regex-based and intentionally conservative: when the surrounding source is exotic enough to outrun the regex's 4000-char lookahead, the spec falls through and keeps its hand-written `tier` (defaulting to `'stable'`).
|
|
87
|
+
|
|
88
|
+
For projects where AST-level certainty matters more than a fast regex pass, an AST mode is available behind the `/ast` entry point:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
// custom bundler plugin
|
|
92
|
+
import { transformSourceAst } from 'acture-build-tier/ast';
|
|
93
|
+
|
|
94
|
+
export function actureTierAstPlugin() {
|
|
95
|
+
return {
|
|
96
|
+
name: 'acture-tier-ast',
|
|
97
|
+
transform(code: string, id: string) {
|
|
98
|
+
if (!id.endsWith('.ts') && !id.endsWith('.tsx')) return null;
|
|
99
|
+
const { code: out, changed } = transformSourceAst(code);
|
|
100
|
+
return changed ? { code: out, map: null } : null;
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`ts-morph` is an **optional peer dependency** — pulled in only if you import the AST entry point. The output is interchangeable with the regex mode on every case the regex handles correctly; AST mode also handles 5000-char spec bodies and template-literal `${...}` substitutions the regex skips.
|
|
107
|
+
|
|
108
|
+
## Caveats
|
|
109
|
+
|
|
110
|
+
- Default mode is regex-based, not AST. The common case is recognized; exotic JSDoc-or-call shapes are silently ignored (the user's spec keeps its hand-written `tier`, defaulting to `'stable'`). Use `/ast` if your codebase trips this.
|
|
111
|
+
- 4000-char per-call lookahead window for the matching-brace scan (regex mode). Real specs are O(20 lines); this is comfortably generous.
|
|
112
|
+
- No `.d.ts` mirror — the JSDoc tag survives into `.d.ts` natively (IDE hover shows it), but the resolved `tier` value lives in the JS runtime output only.
|
package/dist/ast.cjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tsMorph = require('ts-morph');
|
|
4
|
+
|
|
5
|
+
// src/ast.ts
|
|
6
|
+
|
|
7
|
+
// src/transform.ts
|
|
8
|
+
var TIER_PRECEDENCE = /* @__PURE__ */ new Map([
|
|
9
|
+
["stable", 0],
|
|
10
|
+
["experimental", 1],
|
|
11
|
+
["deprecated", 2],
|
|
12
|
+
["internal", 3]
|
|
13
|
+
]);
|
|
14
|
+
function parseTierDirective(jsdocBody) {
|
|
15
|
+
let chosen;
|
|
16
|
+
const stripped = jsdocBody.replace(/^\s*\*\s?/gm, "");
|
|
17
|
+
const stableRe = /@stable\b/;
|
|
18
|
+
const experimentalRe = /@experimental\b/;
|
|
19
|
+
const internalRe = /@internal\b/;
|
|
20
|
+
const deprecatedRe = /@deprecated\b[ \t]*([^\n@]*)/;
|
|
21
|
+
if (stableRe.test(stripped)) chosen = take(chosen, { tier: "stable" });
|
|
22
|
+
if (experimentalRe.test(stripped)) chosen = take(chosen, { tier: "experimental" });
|
|
23
|
+
const depMatch = deprecatedRe.exec(stripped);
|
|
24
|
+
if (depMatch) {
|
|
25
|
+
const reason = (depMatch[1] ?? "").trim();
|
|
26
|
+
chosen = take(chosen, reason.length > 0 ? { tier: "deprecated", reason } : { tier: "deprecated" });
|
|
27
|
+
}
|
|
28
|
+
if (internalRe.test(stripped)) chosen = take(chosen, { tier: "internal" });
|
|
29
|
+
return chosen;
|
|
30
|
+
}
|
|
31
|
+
function take(current, candidate) {
|
|
32
|
+
if (!current) return candidate;
|
|
33
|
+
const cur = TIER_PRECEDENCE.get(current.tier) ?? 0;
|
|
34
|
+
const cand = TIER_PRECEDENCE.get(candidate.tier) ?? 0;
|
|
35
|
+
return cand > cur ? candidate : current;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/ast.ts
|
|
39
|
+
var INTERNAL_TOKEN_NAME = "__actureInternalToken__";
|
|
40
|
+
var INTERNAL_TOKEN_DECL = `const ${INTERNAL_TOKEN_NAME} = /* @__PURE__ */ Symbol('acture.internal');
|
|
41
|
+
`;
|
|
42
|
+
function transformSourceAst(source) {
|
|
43
|
+
if (!source.includes("defineCommand")) {
|
|
44
|
+
return { code: source, changed: false, applied: [] };
|
|
45
|
+
}
|
|
46
|
+
const project = new tsMorph.Project({
|
|
47
|
+
useInMemoryFileSystem: true,
|
|
48
|
+
compilerOptions: { target: tsMorph.ScriptTarget.ES2022, allowJs: false },
|
|
49
|
+
skipAddingFilesFromTsConfig: true,
|
|
50
|
+
skipFileDependencyResolution: true
|
|
51
|
+
});
|
|
52
|
+
const file = project.createSourceFile("__acture_build_tier__.ts", source);
|
|
53
|
+
const applied = [];
|
|
54
|
+
let internalCount = 0;
|
|
55
|
+
for (const call of findDefineCommandCalls(file)) {
|
|
56
|
+
const spec = firstObjectLiteralArg(call);
|
|
57
|
+
if (!spec) continue;
|
|
58
|
+
if (hasTierProperty(spec)) continue;
|
|
59
|
+
const jsdocBody = readLeadingJsdocBody(call);
|
|
60
|
+
if (!jsdocBody) continue;
|
|
61
|
+
const directive = parseTierDirective(jsdocBody);
|
|
62
|
+
if (!directive) continue;
|
|
63
|
+
const props = [];
|
|
64
|
+
props.push(`tier: ${JSON.stringify(directive.tier)}`);
|
|
65
|
+
if (directive.tier === "deprecated" && directive.reason !== void 0) {
|
|
66
|
+
props.push(`deprecationReason: ${JSON.stringify(directive.reason)}`);
|
|
67
|
+
}
|
|
68
|
+
if (directive.tier === "internal") {
|
|
69
|
+
props.push(`internalToken: ${INTERNAL_TOKEN_NAME}`);
|
|
70
|
+
internalCount++;
|
|
71
|
+
}
|
|
72
|
+
insertPropertiesAtStart(spec, props);
|
|
73
|
+
applied.push(
|
|
74
|
+
directive.reason !== void 0 ? { tier: directive.tier, reason: directive.reason } : { tier: directive.tier }
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
let code = file.getFullText();
|
|
78
|
+
if (internalCount > 0) {
|
|
79
|
+
code = INTERNAL_TOKEN_DECL + code;
|
|
80
|
+
}
|
|
81
|
+
return { code, changed: applied.length > 0, applied };
|
|
82
|
+
}
|
|
83
|
+
function findDefineCommandCalls(file) {
|
|
84
|
+
const calls = [];
|
|
85
|
+
file.forEachDescendant((node) => {
|
|
86
|
+
if (node.getKind() !== tsMorph.SyntaxKind.CallExpression) return;
|
|
87
|
+
const call = node;
|
|
88
|
+
if (call.getExpression().getText() !== "defineCommand") return;
|
|
89
|
+
calls.push(call);
|
|
90
|
+
});
|
|
91
|
+
return calls;
|
|
92
|
+
}
|
|
93
|
+
function firstObjectLiteralArg(call) {
|
|
94
|
+
const args = call.getArguments();
|
|
95
|
+
if (args.length === 0) return null;
|
|
96
|
+
const first = args[0];
|
|
97
|
+
if (first.getKind() !== tsMorph.SyntaxKind.ObjectLiteralExpression) return null;
|
|
98
|
+
return first;
|
|
99
|
+
}
|
|
100
|
+
function hasTierProperty(spec) {
|
|
101
|
+
return spec.getProperties().some((p) => {
|
|
102
|
+
if (p.getKind() !== tsMorph.SyntaxKind.PropertyAssignment) return false;
|
|
103
|
+
const name = p.getName?.();
|
|
104
|
+
return name === "tier";
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function readLeadingJsdocBody(node) {
|
|
108
|
+
let target = node;
|
|
109
|
+
while (true) {
|
|
110
|
+
const parent = target.getParent();
|
|
111
|
+
if (!parent) break;
|
|
112
|
+
const kind = parent.getKind();
|
|
113
|
+
if (kind === tsMorph.SyntaxKind.VariableDeclaration || kind === tsMorph.SyntaxKind.VariableDeclarationList || kind === tsMorph.SyntaxKind.VariableStatement || kind === tsMorph.SyntaxKind.ExpressionStatement) {
|
|
114
|
+
target = parent;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
const fullText = target.getSourceFile().getFullText();
|
|
120
|
+
const start = target.getFullStart();
|
|
121
|
+
const end = target.getStart();
|
|
122
|
+
const trivia = fullText.slice(start, end);
|
|
123
|
+
const match = /\/\*\*([\s\S]*?)\*\//g;
|
|
124
|
+
let last = null;
|
|
125
|
+
for (let m = match.exec(trivia); m !== null; m = match.exec(trivia)) {
|
|
126
|
+
last = m;
|
|
127
|
+
}
|
|
128
|
+
if (!last) return null;
|
|
129
|
+
return last[1] ?? "";
|
|
130
|
+
}
|
|
131
|
+
function insertPropertiesAtStart(spec, propTexts) {
|
|
132
|
+
const props = [...propTexts];
|
|
133
|
+
for (let i = props.length - 1; i >= 0; i--) {
|
|
134
|
+
spec.insertPropertyAssignment(0, parseStructure(props[i]));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function parseStructure(propText) {
|
|
138
|
+
const eq = propText.indexOf(":");
|
|
139
|
+
const name = propText.slice(0, eq).trim();
|
|
140
|
+
const initializer = propText.slice(eq + 1).trim();
|
|
141
|
+
return { name, initializer };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
exports.transformSourceAst = transformSourceAst;
|
|
145
|
+
//# sourceMappingURL=ast.cjs.map
|
|
146
|
+
//# sourceMappingURL=ast.cjs.map
|
package/dist/ast.cjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/transform.ts","../src/ast.ts"],"names":["Project","ScriptTarget","SyntaxKind"],"mappings":";;;;;;;AAkCA,IAAM,eAAA,uBAAiD,GAAA,CAAI;AAAA,EACzD,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Q,GAAG,CAAA;AAC/B,EAAA,MAAM,OAAO,QAAA,CAAS,KAAA,CAAM,CAAA,EAAG,EAAE,EAAE,IAAA,EAAK;AACxC,EAAA,MAAM,cAAc,QAAA,CAAS,KAAA,CAAM,EAAA,GAAK,CAAC,EAAE,IAAA,EAAK;AAChD,EAAA,OAAO,EAAE,MAAM,WAAA,EAAY;AAC7B","file":"ast.cjs","sourcesContent":["/**\n * Pure source-transform logic. The esbuild and Vite plugin wrappers\n * import this and call it from their respective transform hooks.\n *\n * Strategy: regex-based, intentionally conservative. We match a JSDoc\n * block immediately preceding a `defineCommand({ ... })` call, parse\n * the tier tag(s) from the JSDoc, and inject the corresponding\n * properties into the spec object literal.\n *\n * Why regex and not AST: the build step has to be fast (it runs on every\n * .ts file), and the patterns we accept are deliberately narrow — JSDoc\n * directly above the call site, no exotic syntax in between. If a user\n * writes `defineCommand` in a way our regex can't see, they fall back to\n * writing `tier: 'experimental'` explicitly in the spec — that path is\n * documented as the manual fallback.\n *\n * The four recognized tags:\n * @stable\n * @experimental\n * @internal\n * @deprecated [reason text...]\n *\n * Tag precedence: if multiple appear in the same JSDoc, we honour the\n * most-specific-restriction wins: internal > deprecated > experimental > stable.\n */\n\nexport type Tier = 'stable' | 'experimental' | 'internal' | 'deprecated';\n\nexport interface TierDirective {\n readonly tier: Tier;\n /** Non-empty only for `@deprecated <reason>`. */\n readonly reason?: string;\n}\n\nconst TIER_PRECEDENCE: ReadonlyMap<Tier, number> = new Map([\n ['stable', 0],\n ['experimental', 1],\n ['deprecated', 2],\n ['internal', 3],\n]);\n\n/**\n * Parse a JSDoc block body (the text between `/**` and `* /` ) for tier\n * tags. Returns the most-restrictive tag found, or `undefined` if none.\n */\nexport function parseTierDirective(jsdocBody: string): TierDirective | undefined {\n let chosen: TierDirective | undefined;\n // Strip the leading `*` on each line so multi-line JSDoc doesn't break\n // the tag scanner.\n const stripped = jsdocBody.replace(/^\\s*\\*\\s?/gm, '');\n\n const stableRe = /@stable\\b/;\n const experimentalRe = /@experimental\\b/;\n const internalRe = /@internal\\b/;\n // `@deprecated` may carry free-text reason on the same line.\n const deprecatedRe = /@deprecated\\b[ \\t]*([^\\n@]*)/;\n\n if (stableRe.test(stripped)) chosen = take(chosen, { tier: 'stable' });\n if (experimentalRe.test(stripped)) chosen = take(chosen, { tier: 'experimental' });\n const depMatch = deprecatedRe.exec(stripped);\n if (depMatch) {\n const reason = (depMatch[1] ?? '').trim();\n chosen = take(chosen, reason.length > 0 ? { tier: 'deprecated', reason } : { tier: 'deprecated' });\n }\n if (internalRe.test(stripped)) chosen = take(chosen, { tier: 'internal' });\n\n return chosen;\n}\n\nfunction take(\n current: TierDirective | undefined,\n candidate: TierDirective,\n): TierDirective {\n if (!current) return candidate;\n const cur = TIER_PRECEDENCE.get(current.tier) ?? 0;\n const cand = TIER_PRECEDENCE.get(candidate.tier) ?? 0;\n return cand > cur ? candidate : current;\n}\n\nexport interface TransformResult {\n readonly code: string;\n readonly changed: boolean;\n /** Tier directives applied, in order — exposed for testing. */\n readonly applied: ReadonlyArray<{ tier: Tier; reason?: string }>;\n}\n\n/**\n * Find every `/** ... * /` JSDoc block immediately followed by a\n * `defineCommand({ ... })` call, parse the tier tag(s), and inject the\n * tier (plus deprecationReason / internalToken when applicable) into\n * the spec object literal.\n *\n * - `tier` is injected at the head of the spec object.\n * - `deprecationReason` is injected after `tier`.\n * - `internalToken` references a module-scoped Symbol that is declared\n * once at the top of the file (only when at least one `@internal`\n * command is present in the file).\n *\n * Idempotent: if the spec already contains `tier:`, we leave it alone.\n */\nexport function transformSource(source: string): TransformResult {\n // Skip files that obviously do not call defineCommand.\n if (!source.includes('defineCommand')) {\n return { code: source, changed: false, applied: [] };\n }\n\n const applied: Array<{ tier: Tier; reason?: string }> = [];\n // Find every JSDoc block. We scan in order so insertion offsets remain\n // valid as we rebuild the source incrementally.\n const out: string[] = [];\n let i = 0;\n const len = source.length;\n let internalCount = 0;\n\n while (i < len) {\n const jsdocStart = source.indexOf('/**', i);\n if (jsdocStart < 0) {\n out.push(source.slice(i));\n break;\n }\n out.push(source.slice(i, jsdocStart));\n const jsdocEnd = source.indexOf('*/', jsdocStart + 3);\n if (jsdocEnd < 0) {\n out.push(source.slice(jsdocStart));\n break;\n }\n const jsdocBlock = source.slice(jsdocStart, jsdocEnd + 2);\n out.push(jsdocBlock);\n\n // Look ahead for `defineCommand(` after optional whitespace,\n // an optional `export ...`, an optional `const NAME = ` binding.\n const afterDocStart = jsdocEnd + 2;\n const lookahead = source.slice(afterDocStart, afterDocStart + 600);\n // The pattern captures the slice up to and including the opening\n // `{` of the spec object literal.\n // Optional declaration prefix (e.g. `export const NAME = `), then\n // `defineCommand({`. The declaration is optional so bare-call forms\n // are recognized too (`defineCommand({...})` on its own line).\n const dcRe = /^\\s*(?:(?:export\\s+)?(?:const|let|var)\\s+[A-Za-z_$][\\w$]*\\s*=\\s*)?defineCommand\\s*\\(\\s*\\{/;\n const m = dcRe.exec(lookahead);\n if (!m) {\n i = afterDocStart;\n continue;\n }\n const tier = parseTierDirective(jsdocBlock.slice(3, -2));\n if (!tier) {\n i = afterDocStart;\n continue;\n }\n // Compute the absolute position right after the opening `{`.\n // `m.index` is 0 (we anchored with `^`); `m[0].length` is the entire\n // match, ending at the `{` (inclusive).\n const openBraceAbs = afterDocStart + m[0].length;\n // Detect if the spec already declares `tier:` — if so, leave it.\n const restOfFile = source.slice(openBraceAbs, openBraceAbs + 4000);\n if (/\\btier\\s*:/.test(restOfFile.slice(0, indexOfMatchingBrace(restOfFile)))) {\n i = afterDocStart;\n continue;\n }\n\n // Emit everything between end-of-JSDoc and openBraceAbs.\n out.push(source.slice(afterDocStart, openBraceAbs));\n\n // Build the injected fields.\n const injected: string[] = [];\n injected.push(` tier: ${JSON.stringify(tier.tier)},`);\n if (tier.tier === 'deprecated' && tier.reason !== undefined) {\n injected.push(` deprecationReason: ${JSON.stringify(tier.reason)},`);\n }\n if (tier.tier === 'internal') {\n injected.push(` internalToken: __actureInternalToken__,`);\n internalCount++;\n }\n out.push(injected.join(''));\n\n applied.push(tier.reason !== undefined ? { tier: tier.tier, reason: tier.reason } : { tier: tier.tier });\n\n i = openBraceAbs;\n }\n\n let code = out.join('');\n if (internalCount > 0) {\n code = INTERNAL_TOKEN_DECL + code;\n }\n return { code, changed: applied.length > 0, applied };\n}\n\n/** Declared at module top so every `@internal` command in the file\n * shares it. Cross-module callers cannot see it. */\nconst INTERNAL_TOKEN_DECL =\n \"const __actureInternalToken__ = /* @__PURE__ */ Symbol('acture.internal');\\n\";\n\n/**\n * Find the offset of the matching closing brace for a string that\n * starts immediately after an opening `{` (the `{` is NOT in `text`).\n * Returns the index of the `}` (relative to `text`) on success, or\n * `text.length` if no balanced close is found.\n *\n * The scanner is intentionally minimal — it tracks nested `{}` only.\n * It treats string and template literals as opaque (skips matching\n * braces inside them) to a first approximation: handles single-quoted,\n * double-quoted, and backtick strings; does NOT handle template\n * substitutions perfectly (a `${` inside a backtick may foil it). The\n * defineCommand spec object is shallow enough that this is fine in\n * practice; users with exotic templates can write `tier: 'X'` manually.\n */\nfunction indexOfMatchingBrace(text: string): number {\n let depth = 1;\n let i = 0;\n const len = text.length;\n while (i < len) {\n const ch = text[i]!;\n if (ch === '\"' || ch === \"'\" || ch === '`') {\n const quote = ch;\n i++;\n while (i < len) {\n if (text[i] === '\\\\') { i += 2; continue; }\n if (text[i] === quote) { i++; break; }\n i++;\n }\n continue;\n }\n if (ch === '/' && text[i + 1] === '/') {\n // line comment\n while (i < len && text[i] !== '\\n') i++;\n continue;\n }\n if (ch === '/' && text[i + 1] === '*') {\n i += 2;\n while (i < len && !(text[i] === '*' && text[i + 1] === '/')) i++;\n i += 2;\n continue;\n }\n if (ch === '{') depth++;\n else if (ch === '}') {\n depth--;\n if (depth === 0) return i;\n }\n i++;\n }\n return len;\n}\n","/**\n * AST-mode tier mirror — the ts-morph companion to the regex\n * `transformSource` in `./transform.ts`.\n *\n * Why an AST mode exists (phase-4-reflection §1, caveat 1): the regex\n * transform handles the 95th-percentile form (JSDoc directly above a\n * `defineCommand({...})` call, with optional declaration prefix). It\n * caps its per-call lookahead at 4000 chars to stay fast, and treats\n * template-substitution braces conservatively. For projects with\n * heavily macro'd source — or where a CI gate wants *AST-level\n * certainty* rather than a fast regex pass — the AST mode is the\n * second entry point.\n *\n * The AST mode produces the same output as the regex transform on\n * every case the regex handles correctly. Where the regex falls\n * through (e.g. 5000-char spec body, template literals containing\n * `${`-delimited substitutions with braces), the AST mode still\n * applies the transformation. The two modes are interchangeable\n * outputs for normal input.\n *\n * Cost: ts-morph is a real dependency (~7 MB). It is declared as an\n * *optional peer* of `acture-build-tier` so consumers who only use\n * the regex mode don't pay for it. Importing this file pulls ts-morph\n * in lazily.\n *\n * Usage:\n *\n * // tsup, vite, etc. — point your plugin at the AST transform:\n * import { transformSourceAst } from 'acture-build-tier/ast';\n *\n * export function actureTierAstPlugin(): Plugin {\n * return {\n * name: 'acture-tier-ast',\n * transform(code, id) {\n * if (!id.endsWith('.ts') && !id.endsWith('.tsx')) return null;\n * const { code: out, changed } = transformSourceAst(code);\n * return changed ? { code: out, map: null } : null;\n * },\n * };\n * }\n *\n * The regex mode at `acture-build-tier/esbuild` and\n * `acture-build-tier/vite` is the documented default. Use AST mode\n * when the regex's lookahead window or string handling is insufficient.\n */\n\nimport {\n Project,\n ScriptTarget,\n SyntaxKind,\n type CallExpression,\n type Node,\n type ObjectLiteralExpression,\n type SourceFile,\n} from 'ts-morph';\nimport { parseTierDirective, type TransformResult } from './transform.js';\n\nconst INTERNAL_TOKEN_NAME = '__actureInternalToken__';\nconst INTERNAL_TOKEN_DECL = `const ${INTERNAL_TOKEN_NAME} = /* @__PURE__ */ Symbol('acture.internal');\\n`;\n\n/**\n * AST-mode counterpart of `transformSource`. Same input/output contract:\n * a `defineCommand({...})` call with a JSDoc tier tag gets `tier`,\n * `deprecationReason`, and/or `internalToken` injected into the spec.\n *\n * Idempotent: if the spec already declares `tier:`, the call is left\n * alone (matches the regex mode's behavior).\n *\n * Conservative: any `defineCommand` call that isn't a direct top-level\n * declaration or expression statement is processed the same way —\n * ts-morph walks ALL CallExpression nodes. The JSDoc lookup uses the\n * compiler's leading-comment-ranges API, which correctly attributes\n * docblocks regardless of how deeply nested the call is.\n */\nexport function transformSourceAst(source: string): TransformResult {\n if (!source.includes('defineCommand')) {\n return { code: source, changed: false, applied: [] };\n }\n\n const project = new Project({\n useInMemoryFileSystem: true,\n compilerOptions: { target: ScriptTarget.ES2022, allowJs: false },\n skipAddingFilesFromTsConfig: true,\n skipFileDependencyResolution: true,\n });\n const file = project.createSourceFile('__acture_build_tier__.ts', source);\n\n const applied: Array<{ tier: 'stable' | 'experimental' | 'internal' | 'deprecated'; reason?: string }> = [];\n let internalCount = 0;\n\n for (const call of findDefineCommandCalls(file)) {\n const spec = firstObjectLiteralArg(call);\n if (!spec) continue;\n if (hasTierProperty(spec)) continue; // idempotent\n\n const jsdocBody = readLeadingJsdocBody(call);\n if (!jsdocBody) continue;\n\n const directive = parseTierDirective(jsdocBody);\n if (!directive) continue;\n\n // Build the property text. We could go through ts-morph's\n // `insertPropertyAssignment` API, but for the small set of property\n // names we inject the text path is simpler and produces predictable\n // output that lines up with the regex mode's emission.\n const props: string[] = [];\n props.push(`tier: ${JSON.stringify(directive.tier)}`);\n if (directive.tier === 'deprecated' && directive.reason !== undefined) {\n props.push(`deprecationReason: ${JSON.stringify(directive.reason)}`);\n }\n if (directive.tier === 'internal') {\n props.push(`internalToken: ${INTERNAL_TOKEN_NAME}`);\n internalCount++;\n }\n\n insertPropertiesAtStart(spec, props);\n\n applied.push(\n directive.reason !== undefined\n ? { tier: directive.tier, reason: directive.reason }\n : { tier: directive.tier },\n );\n }\n\n let code = file.getFullText();\n if (internalCount > 0) {\n code = INTERNAL_TOKEN_DECL + code;\n }\n return { code, changed: applied.length > 0, applied };\n}\n\n/* ───────────────────────── internals ──────────────────────────────── */\n\nfunction findDefineCommandCalls(file: SourceFile): CallExpression[] {\n const calls: CallExpression[] = [];\n file.forEachDescendant((node) => {\n if (node.getKind() !== SyntaxKind.CallExpression) return;\n const call = node as CallExpression;\n if (call.getExpression().getText() !== 'defineCommand') return;\n calls.push(call);\n });\n return calls;\n}\n\nfunction firstObjectLiteralArg(call: CallExpression): ObjectLiteralExpression | null {\n const args = call.getArguments();\n if (args.length === 0) return null;\n const first = args[0]!;\n if (first.getKind() !== SyntaxKind.ObjectLiteralExpression) return null;\n return first as ObjectLiteralExpression;\n}\n\nfunction hasTierProperty(spec: ObjectLiteralExpression): boolean {\n return spec.getProperties().some((p) => {\n if (p.getKind() !== SyntaxKind.PropertyAssignment) return false;\n const name = (p as Node & { getName?: () => string }).getName?.();\n return name === 'tier';\n });\n}\n\n/**\n * Find the *attached* JSDoc block for a node, climbing through wrapping\n * `const x = …` / `export const x = …` declarations. ts-morph's\n * `getLeadingCommentRanges` would also work but doesn't follow these\n * wrappers cleanly.\n */\nfunction readLeadingJsdocBody(node: Node): string | null {\n let target: Node = node;\n // Walk up CallExpression → VariableDeclaration → VariableStatement\n // (or → ExpressionStatement). The JSDoc is attached to the\n // outermost declaration in the source.\n while (true) {\n const parent = target.getParent();\n if (!parent) break;\n const kind = parent.getKind();\n if (\n kind === SyntaxKind.VariableDeclaration ||\n kind === SyntaxKind.VariableDeclarationList ||\n kind === SyntaxKind.VariableStatement ||\n kind === SyntaxKind.ExpressionStatement\n ) {\n target = parent;\n continue;\n }\n break;\n }\n // Read leading trivia.\n const fullText = target.getSourceFile().getFullText();\n const start = target.getFullStart();\n const end = target.getStart();\n const trivia = fullText.slice(start, end);\n // Find LAST JSDoc block in the trivia.\n const match = /\\/\\*\\*([\\s\\S]*?)\\*\\//g;\n let last: RegExpExecArray | null = null;\n for (let m = match.exec(trivia); m !== null; m = match.exec(trivia)) {\n last = m;\n }\n if (!last) return null;\n return last[1] ?? '';\n}\n\nfunction insertPropertiesAtStart(\n spec: ObjectLiteralExpression,\n propTexts: readonly string[],\n): void {\n // We prepend in reverse so the visible order is `tier`, then\n // `deprecationReason`, then `internalToken`.\n const props = [...propTexts];\n for (let i = props.length - 1; i >= 0; i--) {\n spec.insertPropertyAssignment(0, parseStructure(props[i]!));\n }\n}\n\nfunction parseStructure(propText: string): { name: string; initializer: string } {\n const eq = propText.indexOf(':');\n const name = propText.slice(0, eq).trim();\n const initializer = propText.slice(eq + 1).trim();\n return { name, initializer };\n}\n"]}
|
package/dist/ast.d.cts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { TransformResult } from './index.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AST-mode tier mirror — the ts-morph companion to the regex
|
|
5
|
+
* `transformSource` in `./transform.ts`.
|
|
6
|
+
*
|
|
7
|
+
* Why an AST mode exists (phase-4-reflection §1, caveat 1): the regex
|
|
8
|
+
* transform handles the 95th-percentile form (JSDoc directly above a
|
|
9
|
+
* `defineCommand({...})` call, with optional declaration prefix). It
|
|
10
|
+
* caps its per-call lookahead at 4000 chars to stay fast, and treats
|
|
11
|
+
* template-substitution braces conservatively. For projects with
|
|
12
|
+
* heavily macro'd source — or where a CI gate wants *AST-level
|
|
13
|
+
* certainty* rather than a fast regex pass — the AST mode is the
|
|
14
|
+
* second entry point.
|
|
15
|
+
*
|
|
16
|
+
* The AST mode produces the same output as the regex transform on
|
|
17
|
+
* every case the regex handles correctly. Where the regex falls
|
|
18
|
+
* through (e.g. 5000-char spec body, template literals containing
|
|
19
|
+
* `${`-delimited substitutions with braces), the AST mode still
|
|
20
|
+
* applies the transformation. The two modes are interchangeable
|
|
21
|
+
* outputs for normal input.
|
|
22
|
+
*
|
|
23
|
+
* Cost: ts-morph is a real dependency (~7 MB). It is declared as an
|
|
24
|
+
* *optional peer* of `acture-build-tier` so consumers who only use
|
|
25
|
+
* the regex mode don't pay for it. Importing this file pulls ts-morph
|
|
26
|
+
* in lazily.
|
|
27
|
+
*
|
|
28
|
+
* Usage:
|
|
29
|
+
*
|
|
30
|
+
* // tsup, vite, etc. — point your plugin at the AST transform:
|
|
31
|
+
* import { transformSourceAst } from 'acture-build-tier/ast';
|
|
32
|
+
*
|
|
33
|
+
* export function actureTierAstPlugin(): Plugin {
|
|
34
|
+
* return {
|
|
35
|
+
* name: 'acture-tier-ast',
|
|
36
|
+
* transform(code, id) {
|
|
37
|
+
* if (!id.endsWith('.ts') && !id.endsWith('.tsx')) return null;
|
|
38
|
+
* const { code: out, changed } = transformSourceAst(code);
|
|
39
|
+
* return changed ? { code: out, map: null } : null;
|
|
40
|
+
* },
|
|
41
|
+
* };
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* The regex mode at `acture-build-tier/esbuild` and
|
|
45
|
+
* `acture-build-tier/vite` is the documented default. Use AST mode
|
|
46
|
+
* when the regex's lookahead window or string handling is insufficient.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* AST-mode counterpart of `transformSource`. Same input/output contract:
|
|
51
|
+
* a `defineCommand({...})` call with a JSDoc tier tag gets `tier`,
|
|
52
|
+
* `deprecationReason`, and/or `internalToken` injected into the spec.
|
|
53
|
+
*
|
|
54
|
+
* Idempotent: if the spec already declares `tier:`, the call is left
|
|
55
|
+
* alone (matches the regex mode's behavior).
|
|
56
|
+
*
|
|
57
|
+
* Conservative: any `defineCommand` call that isn't a direct top-level
|
|
58
|
+
* declaration or expression statement is processed the same way —
|
|
59
|
+
* ts-morph walks ALL CallExpression nodes. The JSDoc lookup uses the
|
|
60
|
+
* compiler's leading-comment-ranges API, which correctly attributes
|
|
61
|
+
* docblocks regardless of how deeply nested the call is.
|
|
62
|
+
*/
|
|
63
|
+
declare function transformSourceAst(source: string): TransformResult;
|
|
64
|
+
|
|
65
|
+
export { transformSourceAst };
|
package/dist/ast.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { TransformResult } from './index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AST-mode tier mirror — the ts-morph companion to the regex
|
|
5
|
+
* `transformSource` in `./transform.ts`.
|
|
6
|
+
*
|
|
7
|
+
* Why an AST mode exists (phase-4-reflection §1, caveat 1): the regex
|
|
8
|
+
* transform handles the 95th-percentile form (JSDoc directly above a
|
|
9
|
+
* `defineCommand({...})` call, with optional declaration prefix). It
|
|
10
|
+
* caps its per-call lookahead at 4000 chars to stay fast, and treats
|
|
11
|
+
* template-substitution braces conservatively. For projects with
|
|
12
|
+
* heavily macro'd source — or where a CI gate wants *AST-level
|
|
13
|
+
* certainty* rather than a fast regex pass — the AST mode is the
|
|
14
|
+
* second entry point.
|
|
15
|
+
*
|
|
16
|
+
* The AST mode produces the same output as the regex transform on
|
|
17
|
+
* every case the regex handles correctly. Where the regex falls
|
|
18
|
+
* through (e.g. 5000-char spec body, template literals containing
|
|
19
|
+
* `${`-delimited substitutions with braces), the AST mode still
|
|
20
|
+
* applies the transformation. The two modes are interchangeable
|
|
21
|
+
* outputs for normal input.
|
|
22
|
+
*
|
|
23
|
+
* Cost: ts-morph is a real dependency (~7 MB). It is declared as an
|
|
24
|
+
* *optional peer* of `acture-build-tier` so consumers who only use
|
|
25
|
+
* the regex mode don't pay for it. Importing this file pulls ts-morph
|
|
26
|
+
* in lazily.
|
|
27
|
+
*
|
|
28
|
+
* Usage:
|
|
29
|
+
*
|
|
30
|
+
* // tsup, vite, etc. — point your plugin at the AST transform:
|
|
31
|
+
* import { transformSourceAst } from 'acture-build-tier/ast';
|
|
32
|
+
*
|
|
33
|
+
* export function actureTierAstPlugin(): Plugin {
|
|
34
|
+
* return {
|
|
35
|
+
* name: 'acture-tier-ast',
|
|
36
|
+
* transform(code, id) {
|
|
37
|
+
* if (!id.endsWith('.ts') && !id.endsWith('.tsx')) return null;
|
|
38
|
+
* const { code: out, changed } = transformSourceAst(code);
|
|
39
|
+
* return changed ? { code: out, map: null } : null;
|
|
40
|
+
* },
|
|
41
|
+
* };
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* The regex mode at `acture-build-tier/esbuild` and
|
|
45
|
+
* `acture-build-tier/vite` is the documented default. Use AST mode
|
|
46
|
+
* when the regex's lookahead window or string handling is insufficient.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* AST-mode counterpart of `transformSource`. Same input/output contract:
|
|
51
|
+
* a `defineCommand({...})` call with a JSDoc tier tag gets `tier`,
|
|
52
|
+
* `deprecationReason`, and/or `internalToken` injected into the spec.
|
|
53
|
+
*
|
|
54
|
+
* Idempotent: if the spec already declares `tier:`, the call is left
|
|
55
|
+
* alone (matches the regex mode's behavior).
|
|
56
|
+
*
|
|
57
|
+
* Conservative: any `defineCommand` call that isn't a direct top-level
|
|
58
|
+
* declaration or expression statement is processed the same way —
|
|
59
|
+
* ts-morph walks ALL CallExpression nodes. The JSDoc lookup uses the
|
|
60
|
+
* compiler's leading-comment-ranges API, which correctly attributes
|
|
61
|
+
* docblocks regardless of how deeply nested the call is.
|
|
62
|
+
*/
|
|
63
|
+
declare function transformSourceAst(source: string): TransformResult;
|
|
64
|
+
|
|
65
|
+
export { transformSourceAst };
|