@plur-ai/migrate 0.16.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 +60 -0
- package/dist/index.d.ts +96 -0
- package/dist/index.js +464 -0
- package/package.json +18 -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 PLUR (plur.ai)
|
|
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,60 @@
|
|
|
1
|
+
# @plur-ai/migrate
|
|
2
|
+
|
|
3
|
+
Find PLUR calls left un-awaited by the 0.16 async migration.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npx @plur-ai/migrate # report (default — changes nothing)
|
|
7
|
+
npx @plur-ai/migrate --write # apply the unambiguous fixes
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Why you need this
|
|
11
|
+
|
|
12
|
+
As of 0.16 the engine's read and write methods return promises, so a store can
|
|
13
|
+
live across a network. A call left un-awaited **does not throw** — it yields a
|
|
14
|
+
`Promise`, and most things you do with a Promise succeed:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
plur.recall(q).length // undefined, not an error
|
|
18
|
+
{...plur.status()} // {}
|
|
19
|
+
plur.learn(x) // resolves later, or never
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
TypeScript catches every one of these. JavaScript catches none of them, which
|
|
23
|
+
is what this tool is for.
|
|
24
|
+
|
|
25
|
+
## What it will and won't rewrite
|
|
26
|
+
|
|
27
|
+
It rewrites only where adding `await` is unambiguous, and **parenthesises when
|
|
28
|
+
the result is consumed**:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
const n = plur.list().length → const n = (await plur.list()).length
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`await plur.list().length` would parse as `await (plur.list().length)` — it
|
|
35
|
+
awaits `undefined` and yields `undefined`. Getting that wrong produces code
|
|
36
|
+
that runs and returns a plausible value.
|
|
37
|
+
|
|
38
|
+
It reports, but refuses to rewrite:
|
|
39
|
+
|
|
40
|
+
- **calls inside `Promise.race` / `Promise.all` arrays** — awaiting there
|
|
41
|
+
settles the call *before* the combinator sees it. This silently disabled a 5s
|
|
42
|
+
timeout guard in PLUR's own CLI, and the test suite stayed green.
|
|
43
|
+
- **concise arrow bodies** — the enclosing function has to become `async` first.
|
|
44
|
+
- **calls whose result is consumed across multiple lines.**
|
|
45
|
+
|
|
46
|
+
It never touches string literals or comments. An early version of the codemod
|
|
47
|
+
that migrated PLUR itself rewrote a CLI help string into `hook-await inject`
|
|
48
|
+
and a user-facing message into "Add a remote with `await plur.sync(...)`" —
|
|
49
|
+
both valid TypeScript, both shipped.
|
|
50
|
+
|
|
51
|
+
## Exit codes
|
|
52
|
+
|
|
53
|
+
`0` clean, or all findings fixed. `2` when something needs a human — so it
|
|
54
|
+
composes in CI.
|
|
55
|
+
|
|
56
|
+
## Scope
|
|
57
|
+
|
|
58
|
+
Only methods that became async **in 0.16**. `recallHybrid` and friends were
|
|
59
|
+
always async — an un-awaited call there was already a bug. `capture` and
|
|
60
|
+
`timeline` are still synchronous.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find PLUR calls that stopped being synchronous in 0.16 and were never awaited.
|
|
3
|
+
*
|
|
4
|
+
* ## Why this reports rather than rewrites, by default
|
|
5
|
+
*
|
|
6
|
+
* The 0.16 migration inside this repo was done with codemods over ~1,100 call
|
|
7
|
+
* sites, and they were wrong in instructive ways before they were right. Every
|
|
8
|
+
* class of mistake they made is guarded for here, because a tool that silently
|
|
9
|
+
* corrupts a user's source is worse than no tool:
|
|
10
|
+
*
|
|
11
|
+
* - **String literals.** An early pass rewrote text inside quotes, turning a
|
|
12
|
+
* CLI's help output into `hook-await inject` and a user-facing message into
|
|
13
|
+
* "Add a remote with await plur.sync(...)". Both were valid TypeScript and
|
|
14
|
+
* both shipped through a green test suite.
|
|
15
|
+
* - **`Promise.race` / `Promise.all` arrays.** Inserting `await` into an
|
|
16
|
+
* element resolves that call BEFORE the array is constructed, which
|
|
17
|
+
* silently disabled a 5s timeout guard — the racing promise was already
|
|
18
|
+
* settled. Still valid, still green.
|
|
19
|
+
* - **ASI hazards.** A line beginning `(await ...)` after a line with no
|
|
20
|
+
* semicolon parses as a call on the previous expression.
|
|
21
|
+
*
|
|
22
|
+
* The compiler catches an un-awaited promise in TypeScript. It cannot catch any
|
|
23
|
+
* of the above, and neither can a test suite. So: report everything, and only
|
|
24
|
+
* rewrite the sites where the transformation is unambiguous.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Methods on `Plur` that return a promise as of 0.16 and did not before.
|
|
28
|
+
*
|
|
29
|
+
* Deliberately NOT a list of "every async method" — `recallHybrid`,
|
|
30
|
+
* `injectHybrid` and friends were always async, so a call to them without
|
|
31
|
+
* `await` was already a bug and is not this migration's business.
|
|
32
|
+
*
|
|
33
|
+
* `capture` and `timeline` are absent because they are still synchronous: they
|
|
34
|
+
* are backed by `episodes.yaml`, not the engram store. `suggestScope`,
|
|
35
|
+
* `dismissScope`, `reofferScopes` and `listImportSources` are absent because
|
|
36
|
+
* they were briefly async before release and were reverted.
|
|
37
|
+
*/
|
|
38
|
+
declare const NEWLY_ASYNC: readonly ["compact", "episodeToEngram", "getById", "ingest", "inject", "installPack", "learn", "list", "listPinned", "listStores", "outboxCount", "purgeTensions", "recall", "receipt", "recordTensions", "reindex", "rerankerEvalStatus", "resolveTension", "saveMetaEngrams", "setPinned", "status", "sync", "updateEngram", "feedback", "flushOutbox", "forget", "learnBatch", "learnRouted"];
|
|
39
|
+
interface Finding {
|
|
40
|
+
file: string;
|
|
41
|
+
line: number;
|
|
42
|
+
column: number;
|
|
43
|
+
method: string;
|
|
44
|
+
/** The source line, trimmed. */
|
|
45
|
+
text: string;
|
|
46
|
+
/** True when this site can be rewritten mechanically. */
|
|
47
|
+
fixable: boolean;
|
|
48
|
+
/** Why it is not fixable, when it is not. */
|
|
49
|
+
reason?: string;
|
|
50
|
+
/**
|
|
51
|
+
* End column (1-based, exclusive) of the call expression, when the result is
|
|
52
|
+
* immediately consumed and the insertion therefore needs parentheses.
|
|
53
|
+
*
|
|
54
|
+
* `await plur.list().length` parses as `await (plur.list().length)` — it
|
|
55
|
+
* awaits `undefined` and yields `undefined`. The correct rewrite is
|
|
56
|
+
* `(await plur.list()).length`. Getting this wrong produces code that runs,
|
|
57
|
+
* returns a plausible value, and is silently incorrect — the same failure
|
|
58
|
+
* class this whole tool exists to catch.
|
|
59
|
+
*/
|
|
60
|
+
wrapTo?: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Scan one file's source for un-awaited calls to newly-async methods.
|
|
64
|
+
*
|
|
65
|
+
* Matches `<receiver>.<method>(` where the receiver is any identifier or member
|
|
66
|
+
* expression — this cannot know that the receiver is a `Plur`, so it reports
|
|
67
|
+
* `db.list()` too. That is the correct trade for a text tool: a false positive
|
|
68
|
+
* costs a glance, a false negative ships a silent bug. The report says which
|
|
69
|
+
* receiver it saw so the reader can dismiss it instantly.
|
|
70
|
+
*
|
|
71
|
+
* Receiver chains may include single-level bracket indexes — `stores[0]`,
|
|
72
|
+
* `byName['team']`, `pool[i + 1]`, `arr?.[0]` — because arrays/maps of stores
|
|
73
|
+
* are ordinary shapes for this API and a receiver the regex cannot see is a
|
|
74
|
+
* call the user is never told about (the 0.16.0 audit found `arr[0].learn(x)`
|
|
75
|
+
* scanned clean, #752; its second pass found `arr?.[0].learn(x)` still did).
|
|
76
|
+
* Two index shapes remain documented misses: a NESTED index
|
|
77
|
+
* (`m[a[0]].learn(x)`) and an index whose string key contains `]`
|
|
78
|
+
* (`m["a]b"].learn(x)`) — `[^\]]` cannot span the inner `]`, and every
|
|
79
|
+
* partial interpretation fails to reach the method dot, so both are missed —
|
|
80
|
+
* not misreported, and never rewritten at the wrong offset.
|
|
81
|
+
*/
|
|
82
|
+
declare function scanSource(file: string, src: string, methods?: readonly string[]): Finding[];
|
|
83
|
+
/**
|
|
84
|
+
* Add `await` at the fixable sites. Returns the new source and a count.
|
|
85
|
+
*
|
|
86
|
+
* Applied right-to-left so earlier offsets stay valid, and only to sites the
|
|
87
|
+
* scanner marked fixable.
|
|
88
|
+
*/
|
|
89
|
+
declare function applyFixes(src: string, findings: Finding[]): {
|
|
90
|
+
src: string;
|
|
91
|
+
applied: number;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
declare function run(argv: string[]): number;
|
|
95
|
+
|
|
96
|
+
export { NEWLY_ASYNC, applyFixes, run, scanSource };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { readFileSync, writeFileSync, readdirSync, statSync } from "fs";
|
|
5
|
+
import { join, relative, extname } from "path";
|
|
6
|
+
|
|
7
|
+
// src/scan.ts
|
|
8
|
+
var NEWLY_ASYNC = [
|
|
9
|
+
// Derived from git, not from memory: these are the methods whose signature
|
|
10
|
+
// went sync -> async in 0.16. `packages/migrate/test/method-list.test.ts`
|
|
11
|
+
// re-derives the same set from `Plur` and fails if this drifts — the list was
|
|
12
|
+
// hand-written once and was wrong in both directions (it advised `await` on a
|
|
13
|
+
// synchronous `addStore`, and missed eight methods entirely).
|
|
14
|
+
"compact",
|
|
15
|
+
"episodeToEngram",
|
|
16
|
+
"getById",
|
|
17
|
+
"ingest",
|
|
18
|
+
"inject",
|
|
19
|
+
"installPack",
|
|
20
|
+
"learn",
|
|
21
|
+
"list",
|
|
22
|
+
"listPinned",
|
|
23
|
+
"listStores",
|
|
24
|
+
"outboxCount",
|
|
25
|
+
"purgeTensions",
|
|
26
|
+
"recall",
|
|
27
|
+
"receipt",
|
|
28
|
+
"recordTensions",
|
|
29
|
+
"reindex",
|
|
30
|
+
"rerankerEvalStatus",
|
|
31
|
+
"resolveTension",
|
|
32
|
+
"saveMetaEngrams",
|
|
33
|
+
"setPinned",
|
|
34
|
+
"status",
|
|
35
|
+
"sync",
|
|
36
|
+
"updateEngram",
|
|
37
|
+
// Already async before 0.16, so an un-awaited call was always a bug rather
|
|
38
|
+
// than migration fallout — but reporting it costs nothing and helps, and
|
|
39
|
+
// these were in the original list. `addStore` was too and is REMOVED: it is
|
|
40
|
+
// synchronous, so the tool was telling users to add an `await` that does not
|
|
41
|
+
// belong.
|
|
42
|
+
"feedback",
|
|
43
|
+
"flushOutbox",
|
|
44
|
+
"forget",
|
|
45
|
+
"learnBatch",
|
|
46
|
+
"learnRouted"
|
|
47
|
+
];
|
|
48
|
+
function literalSpans(src) {
|
|
49
|
+
const spans = [];
|
|
50
|
+
let i = 0;
|
|
51
|
+
while (i < src.length) {
|
|
52
|
+
const c = src[i];
|
|
53
|
+
if (c === "/" && src[i + 1] === "/") {
|
|
54
|
+
const end = src.indexOf("\n", i);
|
|
55
|
+
spans.push([i, end === -1 ? src.length : end]);
|
|
56
|
+
i = end === -1 ? src.length : end;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (c === "/" && src[i + 1] === "*") {
|
|
60
|
+
const end = src.indexOf("*/", i + 2);
|
|
61
|
+
spans.push([i, end === -1 ? src.length : end + 2]);
|
|
62
|
+
i = end === -1 ? src.length : end + 2;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (c === '"' || c === "'") {
|
|
66
|
+
const quote = c;
|
|
67
|
+
let j = i + 1;
|
|
68
|
+
while (j < src.length) {
|
|
69
|
+
if (src[j] === "\\") {
|
|
70
|
+
j += 2;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (src[j] === quote) break;
|
|
74
|
+
j++;
|
|
75
|
+
}
|
|
76
|
+
spans.push([i, Math.min(j + 1, src.length)]);
|
|
77
|
+
i = j + 1;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (c === "`") {
|
|
81
|
+
let j = i + 1;
|
|
82
|
+
let chunkStart = i;
|
|
83
|
+
while (j < src.length) {
|
|
84
|
+
if (src[j] === "\\") {
|
|
85
|
+
j += 2;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (src[j] === "`") break;
|
|
89
|
+
if (src[j] === "$" && src[j + 1] === "{") {
|
|
90
|
+
spans.push([chunkStart, j]);
|
|
91
|
+
let depth = 1;
|
|
92
|
+
let k = j + 2;
|
|
93
|
+
while (k < src.length && depth > 0) {
|
|
94
|
+
if (src[k] === "{") depth++;
|
|
95
|
+
else if (src[k] === "}") depth--;
|
|
96
|
+
k++;
|
|
97
|
+
}
|
|
98
|
+
j = k;
|
|
99
|
+
chunkStart = k;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
j++;
|
|
103
|
+
}
|
|
104
|
+
spans.push([chunkStart, Math.min(j + 1, src.length)]);
|
|
105
|
+
i = j + 1;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
i++;
|
|
109
|
+
}
|
|
110
|
+
return spans;
|
|
111
|
+
}
|
|
112
|
+
function inSpans(spans, idx) {
|
|
113
|
+
for (const [a, b] of spans) if (idx >= a && idx < b) return true;
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
var ASI_SAFE_BEFORE_PAREN = /* @__PURE__ */ new Set([
|
|
117
|
+
";",
|
|
118
|
+
"{",
|
|
119
|
+
",",
|
|
120
|
+
"(",
|
|
121
|
+
"[",
|
|
122
|
+
":",
|
|
123
|
+
"=",
|
|
124
|
+
">",
|
|
125
|
+
"&",
|
|
126
|
+
"|",
|
|
127
|
+
"+",
|
|
128
|
+
"-",
|
|
129
|
+
"*",
|
|
130
|
+
"%",
|
|
131
|
+
"<",
|
|
132
|
+
"~",
|
|
133
|
+
"^",
|
|
134
|
+
"?"
|
|
135
|
+
]);
|
|
136
|
+
function spanAt(spans, idx) {
|
|
137
|
+
for (const s of spans) if (idx >= s[0] && idx < s[1]) return s;
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
function lastSignificantChar(src, from, spans) {
|
|
141
|
+
for (let j = from; j >= 0; j--) {
|
|
142
|
+
const span = spanAt(spans, j);
|
|
143
|
+
if (span) {
|
|
144
|
+
if (src[span[0]] === "/") {
|
|
145
|
+
j = span[0];
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
return src[j];
|
|
149
|
+
}
|
|
150
|
+
if (/\s/.test(src[j])) continue;
|
|
151
|
+
return src[j];
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
function positionOf(src, idx) {
|
|
156
|
+
const before = src.slice(0, idx);
|
|
157
|
+
const line = before.split("\n").length;
|
|
158
|
+
const lineStart = before.lastIndexOf("\n") + 1;
|
|
159
|
+
const lineEnd = src.indexOf("\n", idx);
|
|
160
|
+
return {
|
|
161
|
+
line,
|
|
162
|
+
column: idx - lineStart + 1,
|
|
163
|
+
text: src.slice(lineStart, lineEnd === -1 ? src.length : lineEnd).trim()
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
var CONTROL_KEYWORDS = /* @__PURE__ */ new Set(["if", "for", "while", "switch", "catch", "with", "do"]);
|
|
167
|
+
function insideCombinatorArray(src, idx, spans) {
|
|
168
|
+
let square = 0, round = 0, curly = 0;
|
|
169
|
+
for (let i = idx - 1; i >= 0; i--) {
|
|
170
|
+
if (inSpans(spans, i)) continue;
|
|
171
|
+
const c = src[i];
|
|
172
|
+
if (c === "]") {
|
|
173
|
+
square++;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (c === ")") {
|
|
177
|
+
round++;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (c === "}") {
|
|
181
|
+
curly++;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
if (c === ")" || c === "}") continue;
|
|
185
|
+
if (c === "[") {
|
|
186
|
+
if (square > 0) {
|
|
187
|
+
square--;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
let before = "";
|
|
191
|
+
for (let j = i - 1; j >= 0 && before.length < 60; j--) {
|
|
192
|
+
if (inSpans(spans, j)) continue;
|
|
193
|
+
before = src[j] + before;
|
|
194
|
+
}
|
|
195
|
+
return /Promise\s*\.\s*(all|race|allSettled|any)\s*\(\s*$/.test(before);
|
|
196
|
+
}
|
|
197
|
+
if (c === "(") {
|
|
198
|
+
if (round > 0) {
|
|
199
|
+
round--;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
if (c === "{") {
|
|
205
|
+
if (curly > 0) {
|
|
206
|
+
curly--;
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
function enclosingFunctionKind(src, idx, spans) {
|
|
215
|
+
let depth = 0;
|
|
216
|
+
for (let i = idx - 1; i >= 0; i--) {
|
|
217
|
+
if (inSpans(spans, i)) continue;
|
|
218
|
+
const c = src[i];
|
|
219
|
+
if (c === "}") {
|
|
220
|
+
depth++;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (c !== "{") continue;
|
|
224
|
+
if (depth > 0) {
|
|
225
|
+
depth--;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const header = src.slice(Math.max(0, i - 220), i);
|
|
229
|
+
const fn = /(?:^|[^\w$])(async\s+)?function\s*\*?\s*[\w$]*\s*\([^()]*\)\s*$/.exec(header);
|
|
230
|
+
if (fn) return fn[1] ? "async" : "sync";
|
|
231
|
+
const arrow = /(?:^|[^\w$])(async\s*)?\([^()]*\)\s*=>\s*$/.exec(header);
|
|
232
|
+
if (arrow) return arrow[1] ? "async" : "sync";
|
|
233
|
+
const bareArrow = /(?:^|[^\w$])(async\s+)?[\w$]+\s*=>\s*$/.exec(header);
|
|
234
|
+
if (bareArrow) return bareArrow[1] ? "async" : "sync";
|
|
235
|
+
const method = /(?:^|[^\w$])(async\s+)?([\w$]+)\s*\([^()]*\)\s*$/.exec(header);
|
|
236
|
+
if (method && !CONTROL_KEYWORDS.has(method[2])) return method[1] ? "async" : "sync";
|
|
237
|
+
depth = 0;
|
|
238
|
+
}
|
|
239
|
+
return "top-level";
|
|
240
|
+
}
|
|
241
|
+
function isEsm(file, src) {
|
|
242
|
+
if (/\.(mjs|mts|ts|tsx)$/.test(file)) return true;
|
|
243
|
+
if (/\.(cjs|cts)$/.test(file)) return false;
|
|
244
|
+
return /^\s*(import\s|export\s|import\()/m.test(src);
|
|
245
|
+
}
|
|
246
|
+
function scanSource(file, src, methods = NEWLY_ASYNC) {
|
|
247
|
+
const spans = literalSpans(src);
|
|
248
|
+
const out = [];
|
|
249
|
+
const alt = methods.join("|");
|
|
250
|
+
const re = new RegExp(String.raw`([A-Za-z_$][\w$]*(?:\??\.[A-Za-z_$][\w$]*|(?:\?\.)?\[[^\]\n]*\])*)\??\.(${alt})\s*(?:\?\.)?\s*\(`, "g");
|
|
251
|
+
let m;
|
|
252
|
+
while (m = re.exec(src)) {
|
|
253
|
+
const idx = m.index;
|
|
254
|
+
if (inSpans(spans, idx)) continue;
|
|
255
|
+
const before = src.slice(Math.max(0, idx - 80), idx);
|
|
256
|
+
if (/\b(await|yield)\s*$/.test(before)) continue;
|
|
257
|
+
if (/\breturn\s*$/.test(before)) continue;
|
|
258
|
+
if (/\bvoid\s*$/.test(before)) continue;
|
|
259
|
+
const after = src.slice(idx);
|
|
260
|
+
const callEnd = matchParen(after, m[0].length - 1, spans, idx);
|
|
261
|
+
if (callEnd > 0 && /^\s*\.(then|catch|finally)\s*\(/.test(after.slice(callEnd))) continue;
|
|
262
|
+
const pos = positionOf(src, idx);
|
|
263
|
+
let fixable = true;
|
|
264
|
+
let reason;
|
|
265
|
+
if (insideCombinatorArray(src, idx, spans)) {
|
|
266
|
+
fixable = false;
|
|
267
|
+
reason = "inside a Promise combinator array \u2014 awaiting here settles the call before the combinator sees it";
|
|
268
|
+
} else if (/=>\s*$/.test(before)) {
|
|
269
|
+
fixable = false;
|
|
270
|
+
reason = "concise arrow body \u2014 the enclosing function must become async first";
|
|
271
|
+
} else {
|
|
272
|
+
const kind = enclosingFunctionKind(src, idx, spans);
|
|
273
|
+
if (kind === "sync") {
|
|
274
|
+
fixable = false;
|
|
275
|
+
reason = "the enclosing function is not `async` \u2014 make it async first, then re-run";
|
|
276
|
+
} else if (kind === "top-level" && !isEsm(file, src)) {
|
|
277
|
+
fixable = false;
|
|
278
|
+
reason = "top-level await needs an ES module \u2014 convert the file, or wrap the call in an async function";
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
let wrapTo;
|
|
282
|
+
if (callEnd < 0) {
|
|
283
|
+
fixable = false;
|
|
284
|
+
reason = "could not determine where the call ends \u2014 add `await` by hand";
|
|
285
|
+
} else if (callEnd > 0 && /^\s*[.[]/.test(after.slice(callEnd))) {
|
|
286
|
+
const endIdx = idx + callEnd;
|
|
287
|
+
const endPos = positionOf(src, endIdx);
|
|
288
|
+
if (endPos.line === pos.line) wrapTo = endPos.column;
|
|
289
|
+
else {
|
|
290
|
+
fixable = false;
|
|
291
|
+
reason = "result is consumed by a multi-line call \u2014 needs `(await ...)` by hand";
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (fixable && wrapTo !== void 0) {
|
|
295
|
+
const lineStart = idx - (pos.column - 1);
|
|
296
|
+
if (/^\s*$/.test(src.slice(lineStart, idx))) {
|
|
297
|
+
const prev = lastSignificantChar(src, lineStart - 1, spans);
|
|
298
|
+
if (prev !== null && !ASI_SAFE_BEFORE_PAREN.has(prev)) {
|
|
299
|
+
fixable = false;
|
|
300
|
+
reason = "previous line has no terminator \u2014 a leading `(await ...)` splices onto it (ASI); end it with `;` or add `(await ...)` by hand";
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
out.push({ file, line: pos.line, column: pos.column, method: m[2], text: pos.text, fixable, reason, wrapTo });
|
|
305
|
+
}
|
|
306
|
+
return out;
|
|
307
|
+
}
|
|
308
|
+
function matchParen(s, open, spans, base) {
|
|
309
|
+
if (open < 0) return -1;
|
|
310
|
+
let d = 0;
|
|
311
|
+
for (let i = open; i < s.length; i++) {
|
|
312
|
+
if (inSpans(spans, base + i)) continue;
|
|
313
|
+
if (s[i] === "(") d++;
|
|
314
|
+
else if (s[i] === ")") {
|
|
315
|
+
d--;
|
|
316
|
+
if (d === 0) return i + 1;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return -1;
|
|
320
|
+
}
|
|
321
|
+
function applyFixes(src, findings) {
|
|
322
|
+
const lines = src.split("\n");
|
|
323
|
+
const fixable = findings.filter((f) => f.fixable).sort((a, b) => b.line - a.line || b.column - a.column);
|
|
324
|
+
let applied = 0;
|
|
325
|
+
for (const f of fixable) {
|
|
326
|
+
const li = f.line - 1;
|
|
327
|
+
const line = lines[li];
|
|
328
|
+
if (line === void 0) continue;
|
|
329
|
+
const at = f.column - 1;
|
|
330
|
+
if (at < 0 || at > line.length) continue;
|
|
331
|
+
if (f.wrapTo !== void 0) {
|
|
332
|
+
const end = f.wrapTo - 1;
|
|
333
|
+
if (end <= at || end > line.length) continue;
|
|
334
|
+
lines[li] = line.slice(0, at) + "(await " + line.slice(at, end) + ")" + line.slice(end);
|
|
335
|
+
} else {
|
|
336
|
+
lines[li] = line.slice(0, at) + "await " + line.slice(at);
|
|
337
|
+
}
|
|
338
|
+
applied++;
|
|
339
|
+
}
|
|
340
|
+
return { src: lines.join("\n"), applied };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// src/index.ts
|
|
344
|
+
var HELP = `plur-migrate \u2014 find PLUR calls left un-awaited by the 0.16 async migration
|
|
345
|
+
|
|
346
|
+
USAGE
|
|
347
|
+
npx @plur-ai/migrate [path] report un-awaited calls (default: .)
|
|
348
|
+
npx @plur-ai/migrate [path] --write also apply the unambiguous fixes
|
|
349
|
+
|
|
350
|
+
WHY
|
|
351
|
+
As of 0.16 the PLUR engine's read and write methods return promises, so a
|
|
352
|
+
store can live across a network. A call left un-awaited does not throw \u2014 it
|
|
353
|
+
yields a Promise, and most property reads on a Promise succeed:
|
|
354
|
+
|
|
355
|
+
plur.recall(q).length -> undefined, not an error
|
|
356
|
+
{...plur.status()} -> {}
|
|
357
|
+
|
|
358
|
+
TypeScript catches all of it. JavaScript does not, which is what this is for.
|
|
359
|
+
|
|
360
|
+
OPTIONS
|
|
361
|
+
--write apply fixes (default: report only)
|
|
362
|
+
--ext .ts,.js file extensions to scan (default: .ts,.tsx,.js,.mjs,.cjs)
|
|
363
|
+
--help this
|
|
364
|
+
`;
|
|
365
|
+
var SKIP_DIRS = /* @__PURE__ */ new Set(["node_modules", "dist", "build", ".git", "coverage", ".next"]);
|
|
366
|
+
var PREFILTER = new RegExp(String.raw`\.(?:${NEWLY_ASYNC.join("|")})\s*\(`);
|
|
367
|
+
function walk(dir, exts, out = []) {
|
|
368
|
+
let entries;
|
|
369
|
+
try {
|
|
370
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
371
|
+
} catch {
|
|
372
|
+
return out;
|
|
373
|
+
}
|
|
374
|
+
for (const e of entries) {
|
|
375
|
+
const p = join(dir, e.name);
|
|
376
|
+
if (e.isDirectory()) {
|
|
377
|
+
if (SKIP_DIRS.has(e.name)) continue;
|
|
378
|
+
walk(p, exts, out);
|
|
379
|
+
} else if (exts.has(extname(e.name))) {
|
|
380
|
+
out.push(p);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return out;
|
|
384
|
+
}
|
|
385
|
+
function run(argv) {
|
|
386
|
+
if (argv.includes("--help") || argv.includes("-h")) {
|
|
387
|
+
process.stdout.write(HELP);
|
|
388
|
+
return 0;
|
|
389
|
+
}
|
|
390
|
+
const write = argv.includes("--write");
|
|
391
|
+
const extIdx = argv.indexOf("--ext");
|
|
392
|
+
const exts = new Set(
|
|
393
|
+
extIdx >= 0 && argv[extIdx + 1] ? argv[extIdx + 1].split(",").map((s) => s.startsWith(".") ? s : `.${s}`) : [".ts", ".tsx", ".js", ".mjs", ".cjs"]
|
|
394
|
+
);
|
|
395
|
+
const root = argv.find((a, i) => !a.startsWith("-") && argv[i - 1] !== "--ext") ?? ".";
|
|
396
|
+
let files;
|
|
397
|
+
try {
|
|
398
|
+
files = statSync(root).isDirectory() ? walk(root, exts) : [root];
|
|
399
|
+
} catch {
|
|
400
|
+
process.stderr.write(`plur-migrate: cannot read ${root}
|
|
401
|
+
`);
|
|
402
|
+
return 1;
|
|
403
|
+
}
|
|
404
|
+
const all = [];
|
|
405
|
+
let changed = 0;
|
|
406
|
+
for (const f of files) {
|
|
407
|
+
let src;
|
|
408
|
+
try {
|
|
409
|
+
src = readFileSync(f, "utf8");
|
|
410
|
+
} catch {
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
if (!PREFILTER.test(src)) continue;
|
|
414
|
+
const findings = scanSource(relative(process.cwd(), f) || f, src);
|
|
415
|
+
if (findings.length === 0) continue;
|
|
416
|
+
all.push(...findings);
|
|
417
|
+
if (write) {
|
|
418
|
+
const { src: next, applied } = applyFixes(src, findings);
|
|
419
|
+
if (applied > 0) {
|
|
420
|
+
writeFileSync(f, next);
|
|
421
|
+
changed++;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
if (all.length === 0) {
|
|
426
|
+
process.stdout.write("plur-migrate: no un-awaited PLUR calls found.\n");
|
|
427
|
+
return 0;
|
|
428
|
+
}
|
|
429
|
+
const fixable = all.filter((f) => f.fixable);
|
|
430
|
+
const manual = all.filter((f) => !f.fixable);
|
|
431
|
+
for (const f of all) {
|
|
432
|
+
const mark = f.fixable ? write ? "fixed " : "fixable" : "MANUAL ";
|
|
433
|
+
process.stdout.write(`${mark} ${f.file}:${f.line}:${f.column} .${f.method}()
|
|
434
|
+
${f.text}
|
|
435
|
+
`);
|
|
436
|
+
if (f.reason) process.stdout.write(` ^ ${f.reason}
|
|
437
|
+
`);
|
|
438
|
+
}
|
|
439
|
+
process.stdout.write("\n");
|
|
440
|
+
if (write) {
|
|
441
|
+
process.stdout.write(`plur-migrate: applied ${fixable.length} fix(es) across ${changed} file(s).
|
|
442
|
+
`);
|
|
443
|
+
} else {
|
|
444
|
+
process.stdout.write(`plur-migrate: ${fixable.length} fixable, ${manual.length} need a human. Re-run with --write to apply the fixable ones.
|
|
445
|
+
`);
|
|
446
|
+
}
|
|
447
|
+
if (manual.length > 0) {
|
|
448
|
+
process.stdout.write(
|
|
449
|
+
`
|
|
450
|
+
${manual.length} site(s) are NOT rewritten on purpose. Inserting \`await\` there changes
|
|
451
|
+
program meaning rather than just adding a wait \u2014 the notes above say how.
|
|
452
|
+
`
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
return manual.length > 0 ? 2 : 0;
|
|
456
|
+
}
|
|
457
|
+
var invokedDirectly = process.argv[1] && /plur-migrate|migrate[/\\]dist[/\\]index\.js$/.test(process.argv[1]);
|
|
458
|
+
if (invokedDirectly) process.exit(run(process.argv.slice(2)));
|
|
459
|
+
export {
|
|
460
|
+
NEWLY_ASYNC,
|
|
461
|
+
applyFixes,
|
|
462
|
+
run,
|
|
463
|
+
scanSource
|
|
464
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@plur-ai/migrate",
|
|
3
|
+
"version": "0.16.0",
|
|
4
|
+
"description": "Find and fix un-awaited PLUR calls after the 0.16 async migration",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"plur-migrate": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsup",
|
|
16
|
+
"test": "vitest run"
|
|
17
|
+
}
|
|
18
|
+
}
|