@murky-web/typebuddy 1.3.0 → 1.5.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.
@@ -0,0 +1,134 @@
1
+ type AstNode = {
2
+ type: string;
3
+ parent?: AstNode;
4
+ [key: string]: unknown;
5
+ };
6
+
7
+ const PACKAGE_NAME = "@murky-web/typebuddy";
8
+
9
+ function isNode(value: unknown): value is AstNode {
10
+ return typeof value === "object" && value !== null && "type" in value;
11
+ }
12
+
13
+ function isIdentifierNamed(node: unknown, name: string): boolean {
14
+ return isNode(node) && node.type === "Identifier" && node["name"] === name;
15
+ }
16
+
17
+ function isImportDeclaration(node: unknown): node is AstNode {
18
+ return isNode(node) && node.type === "ImportDeclaration";
19
+ }
20
+
21
+ function getStringLiteralValue(node: unknown): string | null {
22
+ if (!isNode(node)) {
23
+ return null;
24
+ }
25
+
26
+ if (
27
+ (node.type === "Literal" || node.type === "StringLiteral") &&
28
+ typeof node["value"] === "string"
29
+ ) {
30
+ return node["value"];
31
+ }
32
+
33
+ return null;
34
+ }
35
+
36
+ function getProgram(node: unknown): AstNode | null {
37
+ if (!isNode(node)) {
38
+ return null;
39
+ }
40
+
41
+ let current: AstNode | undefined = node;
42
+ while (current?.parent) {
43
+ current = current.parent;
44
+ }
45
+
46
+ return current?.type === "Program" ? current : null;
47
+ }
48
+
49
+ function getProgramBody(program: AstNode): AstNode[] {
50
+ if (!Array.isArray(program["body"])) {
51
+ return [];
52
+ }
53
+
54
+ return program["body"].filter(isNode);
55
+ }
56
+
57
+ function importsHelper(program: AstNode, helperName: string): boolean {
58
+ return getProgramBody(program).some((statement) => {
59
+ if (!isImportDeclaration(statement)) {
60
+ return false;
61
+ }
62
+
63
+ if (getStringLiteralValue(statement["source"]) !== PACKAGE_NAME) {
64
+ return false;
65
+ }
66
+
67
+ // A type-only import brings no runtime binding, so it does not satisfy a
68
+ // fix that is about to call the helper.
69
+ if (statement["importKind"] === "type") {
70
+ return false;
71
+ }
72
+
73
+ const specifiers = Array.isArray(statement["specifiers"])
74
+ ? statement["specifiers"]
75
+ : [];
76
+
77
+ return specifiers.some((specifier) => {
78
+ return (
79
+ isNode(specifier) &&
80
+ specifier.type === "ImportSpecifier" &&
81
+ isIdentifierNamed(specifier["local"], helperName)
82
+ );
83
+ });
84
+ });
85
+ }
86
+
87
+ /**
88
+ * Where and what to insert so `helperName` is imported from the package.
89
+ *
90
+ * Both async rules add `ok`/`err` imports, and both used to carry their own
91
+ * copy of this — including the same bug. Whether the import lands after an
92
+ * existing one was inferred from the anchor's offset (`range[0] !== 0`), which
93
+ * is only the same question in a file whose first statement starts at byte
94
+ * zero. A leading comment moved the first statement off zero, the wrong branch
95
+ * won, and the fix emitted a leading newline instead of a trailing one:
96
+ *
97
+ * import { err } from "@murky-web/typebuddy";export async function load()
98
+ *
99
+ * The list of imports answers it directly, so that is what decides now.
100
+ *
101
+ * @param {unknown} node - Any node in the file; the program is found from it.
102
+ * @param {string} helperName - The named export to import, e.g. `"err"`.
103
+ * @returns {{ range: [number, number]; text: string } | null} The zero-width
104
+ * insertion to apply, or null when the helper is already imported or no
105
+ * anchor carries a usable range.
106
+ */
107
+ function getTypeBuddyImportInsertion(
108
+ node: unknown,
109
+ helperName: string,
110
+ ): { range: [number, number]; text: string } | null {
111
+ const program = getProgram(node);
112
+ if (!program || importsHelper(program, helperName)) {
113
+ return null;
114
+ }
115
+
116
+ const body = getProgramBody(program);
117
+ const imports = body.filter(isImportDeclaration);
118
+ const anchor = imports.at(-1) ?? body[0] ?? program;
119
+ const range = anchor["range"];
120
+ if (!Array.isArray(range) || range.length < 2) {
121
+ return null;
122
+ }
123
+
124
+ const statement = `import { ${helperName} } from "${PACKAGE_NAME}";`;
125
+ if (imports.length > 0) {
126
+ const end: number = range[1];
127
+ return { range: [end, end], text: `\n${statement}` };
128
+ }
129
+
130
+ const start: number = range[0];
131
+ return { range: [start, start], text: `${statement}\n` };
132
+ }
133
+
134
+ export { getTypeBuddyImportInsertion };
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // program that imports this package, so the global helpers need no
3
3
  // tsconfig entry of their own. The empty brace form is the only way to say
4
4
  // "load this module for its ambient declarations" without importing a value,
5
- // and `globals.ts` sits at the package root because it is a published entry
5
+ // and `globals.d.ts` sits at the package root because it is a published entry
6
6
  // point (`@murky-web/typebuddy/globals`).
7
7
  // oxlint-disable-next-line no-empty-named-blocks, no-relative-parent-imports, require-module-specifiers
8
8
  import type {} from "../globals.js";
package/dist/globals.js DELETED
File without changes
File without changes