@tailor-platform/sdk-codemod 0.3.0-next.2 → 0.3.0-next.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.
- package/CHANGELOG.md +137 -7
- package/dist/codemods/ast-grep-helpers-D3FXAKNz.js +171 -0
- package/dist/codemods/v2/apply-to-deploy/scripts/transform.js +1 -1
- package/dist/codemods/v2/auth-attributes-rename/scripts/transform.js +213 -0
- package/dist/codemods/v2/auth-connection-token-helper/scripts/transform.js +243 -0
- package/dist/codemods/v2/auth-invoker-call-unwrap/scripts/transform.js +7 -0
- package/dist/codemods/v2/auth-invoker-unwrap/scripts/transform.js +13 -6
- package/dist/codemods/v2/db-type-to-table/scripts/transform.js +383 -0
- package/dist/codemods/v2/define-generators-to-plugins/scripts/transform.js +2 -1
- package/dist/codemods/v2/env-var-rename/scripts/transform.js +88 -0
- package/dist/codemods/v2/principal-unify/scripts/transform.js +428 -5
- package/dist/codemods/v2/rename-bin/scripts/transform.js +1087 -0
- package/dist/codemods/v2/runtime-globals-opt-in/scripts/transform.js +103 -0
- package/dist/codemods/v2/runtime-subpath-namespace/scripts/transform.js +792 -0
- package/dist/codemods/v2/sdk-skills-shim/scripts/transform.js +3 -3
- package/dist/codemods/v2/tailor-output-ignore-dir/scripts/transform.js +14 -0
- package/dist/codemods/v2/wait-point-rename/scripts/transform.js +126 -0
- package/dist/index.js +868 -60
- package/package.json +8 -7
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { c as localDeclarationNames, i as importBindings, r as findImportStatements, t as buildAddNamedImportEdit } from "../../../ast-grep-helpers-D3FXAKNz.js";
|
|
2
|
+
import { Lang, parse } from "@ast-grep/napi";
|
|
3
|
+
//#region codemods/v2/runtime-globals-opt-in/scripts/transform.ts
|
|
4
|
+
const RUNTIME_MODULE = "@tailor-platform/sdk/runtime";
|
|
5
|
+
const TAILOR_IDP_CLIENT = "tailor.idp.Client";
|
|
6
|
+
const NON_ARGUMENT_KINDS = /* @__PURE__ */ new Set([
|
|
7
|
+
"(",
|
|
8
|
+
")",
|
|
9
|
+
",",
|
|
10
|
+
"comment"
|
|
11
|
+
]);
|
|
12
|
+
function quickFilter(source) {
|
|
13
|
+
return source.includes(TAILOR_IDP_CLIENT);
|
|
14
|
+
}
|
|
15
|
+
function sourceLang(filePath, source) {
|
|
16
|
+
return filePath.endsWith(".tsx") || filePath.endsWith(".jsx") || source.includes("</") ? Lang.Tsx : Lang.TypeScript;
|
|
17
|
+
}
|
|
18
|
+
function runtimeIdpLocalName(imports) {
|
|
19
|
+
for (const importStmt of imports) for (const binding of importBindings(importStmt)) if (binding.source === RUNTIME_MODULE && binding.importedName === "idp" && !binding.typeOnly) return binding.localName;
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
function isRuntimeIdpBinding(binding) {
|
|
23
|
+
return binding.source === RUNTIME_MODULE && binding.importedName === "idp";
|
|
24
|
+
}
|
|
25
|
+
function hasCollision(imports, localNames, idpLocal, injectingNewIdpName) {
|
|
26
|
+
if (localNames.has("tailor") || injectingNewIdpName && localNames.has("idp") || localNames.has(idpLocal)) return true;
|
|
27
|
+
for (const importStmt of imports) for (const binding of importBindings(importStmt)) {
|
|
28
|
+
if (binding.localName === "tailor") return true;
|
|
29
|
+
if (binding.localName !== "idp") continue;
|
|
30
|
+
if (isRuntimeIdpBinding(binding)) continue;
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
function isDirectiveStatement(node) {
|
|
36
|
+
return node.kind() === "expression_statement" && node.children()[0]?.kind() === "string";
|
|
37
|
+
}
|
|
38
|
+
function importInsertionIndex(root, imports, source) {
|
|
39
|
+
const lastImport = imports.at(-1);
|
|
40
|
+
if (lastImport) return lastImport.range().end.index;
|
|
41
|
+
let pos = 0;
|
|
42
|
+
if (source.startsWith("#!")) {
|
|
43
|
+
const newlineIndex = source.indexOf("\n");
|
|
44
|
+
pos = newlineIndex === -1 ? source.length : newlineIndex + 1;
|
|
45
|
+
}
|
|
46
|
+
for (const child of root.children()) {
|
|
47
|
+
if (child.range().start.index < pos) continue;
|
|
48
|
+
if (child.kind() === "comment") {
|
|
49
|
+
pos = child.range().end.index;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (!isDirectiveStatement(child)) break;
|
|
53
|
+
pos = child.range().end.index;
|
|
54
|
+
}
|
|
55
|
+
return pos;
|
|
56
|
+
}
|
|
57
|
+
function buildAddRuntimeImportEdit(root, source, imports) {
|
|
58
|
+
return buildAddNamedImportEdit({
|
|
59
|
+
importName: "idp",
|
|
60
|
+
imports,
|
|
61
|
+
insertionIndex: importInsertionIndex,
|
|
62
|
+
moduleName: RUNTIME_MODULE,
|
|
63
|
+
root,
|
|
64
|
+
source
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function argumentExpressions(args) {
|
|
68
|
+
return args.children().filter((child) => !NON_ARGUMENT_KINDS.has(child.kind()));
|
|
69
|
+
}
|
|
70
|
+
function hasConstructorArguments(newExpression) {
|
|
71
|
+
const args = newExpression.field("arguments");
|
|
72
|
+
return args ? argumentExpressions(args).length > 0 : false;
|
|
73
|
+
}
|
|
74
|
+
function findTailorIdpClientConstructors(root) {
|
|
75
|
+
return root.findAll({ rule: { kind: "new_expression" } }).filter(hasConstructorArguments).map((node) => node.field("constructor")).filter((node) => node?.text() === TAILOR_IDP_CLIENT);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Rewrite direct `new tailor.idp.Client(...)` calls to the typed runtime
|
|
79
|
+
* wrapper. Files with local `tailor` or conflicting `idp` bindings are left
|
|
80
|
+
* unchanged for the runtime-globals review prompt.
|
|
81
|
+
* @param source - File contents
|
|
82
|
+
* @param filePath - Absolute path to the file
|
|
83
|
+
* @returns Transformed source or null when nothing matched.
|
|
84
|
+
*/
|
|
85
|
+
function transform(source, filePath) {
|
|
86
|
+
if (!quickFilter(source)) return null;
|
|
87
|
+
const root = parse(sourceLang(filePath, source), source).root();
|
|
88
|
+
const constructors = findTailorIdpClientConstructors(root);
|
|
89
|
+
if (constructors.length === 0) return null;
|
|
90
|
+
const imports = findImportStatements(root);
|
|
91
|
+
const existingIdpLocal = runtimeIdpLocalName(imports);
|
|
92
|
+
const idpLocal = existingIdpLocal ?? "idp";
|
|
93
|
+
if (hasCollision(imports, localDeclarationNames(root), idpLocal, existingIdpLocal === null)) return null;
|
|
94
|
+
const edits = constructors.map((constructor) => constructor.replace(`${idpLocal}.Client`));
|
|
95
|
+
if (!existingIdpLocal) {
|
|
96
|
+
if (filePath.endsWith(".cts")) return null;
|
|
97
|
+
edits.push(buildAddRuntimeImportEdit(root, source, imports));
|
|
98
|
+
}
|
|
99
|
+
const result = root.commitEdits(edits);
|
|
100
|
+
return result === source ? null : result;
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
export { transform as default };
|