@tinkered/cli 0.0.0-stage → 0.27.2

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,150 @@
1
+ import { existsSync, readFileSync, statSync } from "node:fs";
2
+ import { dirname, join, relative, resolve, sep } from "node:path";
3
+
4
+ /**
5
+ * Warn on `cell.controller`, `cell.atScope.controller` or `cell.atNamespace.controller` when
6
+ * `cell` is a `data(...)` cell that another capsule's folder declares (ADR 0123, ADR 0126). The way in from outside is the capsule's export (`talk.point`, read only)
7
+ * or an op it exports. A raw import still works; this rule only points at it.
8
+ *
9
+ * Options: `roots` are the folders whose first level is one capsule each (default
10
+ * `["tinker/components", "src/frontend"]`, the app layout); `aliases` maps an import prefix to a
11
+ * folder (default `{ "@/": "src/", "~/": "tinker/" }`). A file directly under a root belongs to
12
+ * no capsule.
13
+ *
14
+ * It sees `export const x = data(...)` (typed or not) and `export { x } from "..."`, followed
15
+ * through relative and aliased paths. It does not see `const x = data(...); export { x }` or a
16
+ * namespace import (`t.x.controller`).
17
+ */
18
+ const cellWrite = {
19
+ meta: {
20
+ type: "suggestion",
21
+ schema: [
22
+ {
23
+ type: "object",
24
+ properties: {
25
+ roots: { type: "array", items: { type: "string" } },
26
+ aliases: { type: "object", additionalProperties: { type: "string" } },
27
+ },
28
+ additionalProperties: false,
29
+ },
30
+ ],
31
+ messages: {
32
+ foreign:
33
+ "`{{name}}` is a cell of capsule `{{owner}}`. Read it through the capsule's export, " +
34
+ "or change it with an op that capsule exports. Read: @tinkered/core#core/data.",
35
+ },
36
+ },
37
+ create(context) {
38
+ const options = context.options?.[0] ?? {};
39
+ const roots = options.roots ?? ["tinker/components", "src/frontend"];
40
+ const aliases = options.aliases ?? { "@/": "src/", "~/": "tinker/" };
41
+ const cwd = context.cwd ?? process.cwd();
42
+ const file = context.filename ?? context.physicalFilename;
43
+ const here = capsuleOf(cwd, roots, file);
44
+ const imported = new Map();
45
+
46
+ return {
47
+ ImportDeclaration(node) {
48
+ const target = resolveImport(cwd, aliases, file, node.source.value);
49
+ if (target === undefined) return;
50
+
51
+ const owner = capsuleOf(cwd, roots, target);
52
+ if (owner === undefined || owner === here) return;
53
+
54
+ for (const spec of node.specifiers) {
55
+ if (spec.type !== "ImportSpecifier") continue;
56
+ const name = spec.imported.name ?? spec.imported.value;
57
+ if (cellsOf(cwd, aliases, target).has(name)) imported.set(spec.local.name, owner);
58
+ }
59
+ },
60
+ MemberExpression(node) {
61
+ if (node.computed || node.property.name !== "controller") return;
62
+ const cell = cellOf(node.object);
63
+ if (cell === undefined) return;
64
+
65
+ const owner = imported.get(cell.name);
66
+ if (owner === undefined) return;
67
+
68
+ context.report({ node, messageId: "foreign", data: { name: cell.name, owner } });
69
+ },
70
+ };
71
+ },
72
+ };
73
+
74
+ /** The cell before `.controller`: `x`, or `x` in a placed edge (`x.atScope`, `x.atNamespace`, ADR 0126). */
75
+ function cellOf(object) {
76
+ if (object.type === "Identifier") return object;
77
+ if (object.type !== "MemberExpression" || object.computed) return undefined;
78
+ if (!["atScope", "atNamespace"].includes(object.property.name)) return undefined;
79
+ return object.object.type === "Identifier" ? object.object : undefined;
80
+ }
81
+
82
+ /** The capsule folder a file sits in: the first folder under one of the roots. */
83
+ function capsuleOf(cwd, roots, file) {
84
+ const path = relative(cwd, file).split(sep).join("/");
85
+ for (const root of roots) {
86
+ if (!path.startsWith(`${root}/`)) continue;
87
+
88
+ const rest = path.slice(root.length + 1).split("/");
89
+ return rest.length > 1 ? `${root}/${rest[0]}` : undefined;
90
+ }
91
+
92
+ return undefined;
93
+ }
94
+
95
+ /** The source file an import names, for a relative or aliased path; undefined for a package. */
96
+ function resolveImport(cwd, aliases, file, spec) {
97
+ let base;
98
+ if (spec.startsWith(".")) base = resolve(dirname(file), spec);
99
+ else {
100
+ const prefix = Object.keys(aliases).find((key) => spec.startsWith(key));
101
+ if (prefix === undefined) return undefined;
102
+
103
+ base = resolve(cwd, aliases[prefix], spec.slice(prefix.length));
104
+ }
105
+
106
+ const stem = base.replace(/\.(m?[jt]sx?)$/, "");
107
+ const tries = [".ts", ".tsx", ".mts", ".js", ".mjs"].flatMap((ext) => [
108
+ `${stem}${ext}`,
109
+ join(stem, `index${ext}`),
110
+ ]);
111
+ return tries.find((path) => existsSync(path));
112
+ }
113
+
114
+ /** Per file, by its change time: the names it exports as `data(...)` cells, own or re-exported. */
115
+ const cellsByFile = new Map();
116
+
117
+ function cellsOf(cwd, aliases, file) {
118
+ const changed = statSync(file).mtimeMs;
119
+ const kept = cellsByFile.get(file);
120
+ if (kept !== undefined && kept.changed === changed) return kept.cells;
121
+
122
+ const cells = new Set();
123
+ cellsByFile.set(file, { changed, cells });
124
+ const text = readFileSync(file, "utf8");
125
+ for (const m of text.matchAll(/export const (\w+)\s*(?::[^=]+)?=\s*data\s*[<(]/g)) {
126
+ cells.add(m[1]);
127
+ }
128
+
129
+ for (const m of text.matchAll(/export\s*(\*|\{([^}]*)\})\s*from\s*["']([^"']+)["']/g)) {
130
+ const from = resolveImport(cwd, aliases, file, m[3]);
131
+ if (from !== undefined) addReexport(cells, cellsOf(cwd, aliases, from), m[2]);
132
+ }
133
+
134
+ return cells;
135
+ }
136
+
137
+ /** Add the cells one `export ... from` passes on: all of them for `*`, else the named ones. */
138
+ function addReexport(cells, inner, names) {
139
+ if (names === undefined) {
140
+ for (const cell of inner) cells.add(cell);
141
+ return;
142
+ }
143
+
144
+ for (const part of names.split(",")) {
145
+ const [orig, alias = orig] = part.trim().split(/\s+as\s+/);
146
+ if (inner.has(orig)) cells.add(alias);
147
+ }
148
+ }
149
+
150
+ export default cellWrite;
package/lint/index.mjs ADDED
@@ -0,0 +1,76 @@
1
+ import cellWrite from "./cell-write.mjs";
2
+
3
+ /**
4
+ * A blank line after the imports and around each multi-line top-level statement. One-line
5
+ * statements may sit together. Bodies are left alone. Oxfmt keeps blank lines but never adds
6
+ * them, so this rule adds them through `vp lint --fix`.
7
+ */
8
+ const blankLines = {
9
+ meta: {
10
+ type: "layout",
11
+ fixable: "whitespace",
12
+ messages: { blank: "Add a blank line before this statement." },
13
+ },
14
+ create(context) {
15
+ const code = context.sourceCode;
16
+ const oneLine = (node) => node.loc.start.line === node.loc.end.line;
17
+ const isImport = (node) => node.type === "ImportDeclaration";
18
+ const mayTouch = (prev, next) =>
19
+ (isImport(prev) && isImport(next)) || (!isImport(prev) && oneLine(prev) && oneLine(next));
20
+
21
+ const lineEnd = (node) =>
22
+ code
23
+ .getCommentsAfter(node)
24
+ .filter((comment) => comment.loc.start.line === node.loc.end.line)
25
+ .at(-1) ?? node;
26
+ return {
27
+ Program(program) {
28
+ program.body.forEach((next, index) => {
29
+ const prev = program.body[index - 1];
30
+ if (!prev || mayTouch(prev, next)) return;
31
+ const top = code.getCommentsBefore(next)[0] ?? next;
32
+ const end = lineEnd(prev);
33
+ if (top.loc.start.line - end.loc.end.line > 1) return;
34
+
35
+ context.report({
36
+ node: next,
37
+ messageId: "blank",
38
+ fix: (fixer) => fixer.insertTextAfter(end, "\n"),
39
+ });
40
+ });
41
+ },
42
+ };
43
+ },
44
+ };
45
+
46
+ /**
47
+ * A block of six or more statements reads as steps, with a blank line between them (coding
48
+ * convention rule 14). Which lines form a step is the writer's call, so there is no fix; the rule
49
+ * only flags a block that has no step break at all.
50
+ */
51
+ const bodySteps = {
52
+ meta: {
53
+ type: "layout",
54
+ messages: { steps: "Split this block into steps with a blank line between them." },
55
+ },
56
+ create(context) {
57
+ const code = context.sourceCode;
58
+ const gapAfter = (prev, next) => {
59
+ const top = code.getCommentsBefore(next)[0] ?? next;
60
+ return top.loc.start.line - prev.loc.end.line > 1;
61
+ };
62
+ return {
63
+ BlockStatement(block) {
64
+ const body = block.body;
65
+ if (body.length < 6) return;
66
+ if (body.some((next, index) => index > 0 && gapAfter(body[index - 1], next))) return;
67
+ context.report({ node: block, messageId: "steps" });
68
+ },
69
+ };
70
+ },
71
+ };
72
+
73
+ export default {
74
+ meta: { name: "tinker" },
75
+ rules: { "blank-lines": blankLines, "body-steps": bodySteps, "cell-write": cellWrite },
76
+ };
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@tinkered/cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.27.2",
4
+ "description": "The tinker command: one check that corrects the writer (types, then rules, each finding with its fix from the skill), and the skills.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/lagz0ne/tinkered.git",
9
+ "directory": "packages/cli"
10
+ },
11
+ "bin": {
12
+ "tinker": "./dist/cli.mjs"
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "lint",
17
+ "base"
18
+ ],
19
+ "type": "module",
20
+ "sideEffects": false,
21
+ "exports": {
22
+ ".": "./dist/index.mjs",
23
+ "./config": "./dist/config.mjs",
24
+ "./lint": "./lint/index.mjs",
25
+ "./tsconfig.json": "./base/tsconfig.json",
26
+ "./package.json": "./package.json"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "dependencies": {
32
+ "@tanstack/intent": "0.5.6",
33
+ "typescript-api": "npm:typescript@5.9.3"
34
+ },
35
+ "devDependencies": {
36
+ "@stryker-mutator/core": "^10.0.0",
37
+ "@stryker-mutator/vitest-runner": "^10.0.0",
38
+ "@types/node": "^26.1.1",
39
+ "vite-plus": "1.0.0",
40
+ "vitest": "5.0.1"
41
+ },
42
+ "engines": {
43
+ "node": ">=22.5"
44
+ },
45
+ "scripts": {
46
+ "build": "vp pack",
47
+ "dev": "vp pack --watch",
48
+ "test": "vp test",
49
+ "mutate": "stryker run",
50
+ "check": "vp check"
51
+ }
52
+ }