@ryuugg/stanza 0.1.1 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ryuugg/stanza",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "An opinionated code formatter for TypeScript.",
5
5
  "keywords": [
6
6
  "formatter",
@@ -150,5 +150,8 @@ export function freeze(
150
150
  for (const list of lists)
151
151
  for (const stmt of list.stmts) if (within(frozen, stmt.start)) stmt.frozen = true;
152
152
 
153
- return { lists: lists.filter((list) => !within(frozen, list.start)), frozen };
153
+ return {
154
+ lists: lists.filter((list) => list.kind === "module" || !within(frozen, list.start)),
155
+ frozen,
156
+ };
154
157
  }
@@ -29,7 +29,6 @@ interface Traced extends Trace {
29
29
  }
30
30
 
31
31
  const LABEL = 11;
32
-
33
32
  function field(label: string, text: string): string {
34
33
  return `${label ? `${label}:` : ""}`.padEnd(LABEL) + text;
35
34
  }
@@ -51,7 +51,7 @@ function declarationJoin(prev: Stmt, next: Stmt): Ruled | null {
51
51
 
52
52
  function shortBody(list: StatementList): boolean {
53
53
  return (
54
- list.kind !== "switch" &&
54
+ list.kind === "block" &&
55
55
  list.stmts.length >= 2 &&
56
56
  list.stmts.length <= 3 &&
57
57
  list.stmts.every((stmt) => stmt.compact)
@@ -68,7 +68,10 @@ const LADDER: Step[] = [
68
68
  prev.guard && next.guard && prev.compact && next.compact
69
69
  ? { want: "none", rule: "guard-chain" }
70
70
  : null,
71
- (_list, prev) => (prev.multiline ? { want: "at-least-one", rule: "after-multiline" } : null),
71
+ (_list, prev, next) =>
72
+ prev.multiline && (prev.kind !== "import" || next.kind !== "import")
73
+ ? { want: "at-least-one", rule: "after-multiline" }
74
+ : null,
72
75
  (list, prev) =>
73
76
  list.kind === "switch" && prev.kind === "case" && prev.caseBody
74
77
  ? { want: "at-least-one", rule: "switch-clauses" }
@@ -271,7 +274,8 @@ function walls(
271
274
  (gap.decision.want === "keep" && gap.blank > 0)
272
275
  : gap.decision.want === "frozen" || gap.blank > 0);
273
276
 
274
- if (!stmt || stmt.multiline || separated) {
277
+ const counted = stmt && !stmt.multiline && stmt.kind !== "import";
278
+ if (!counted || separated) {
275
279
  if (runStart && length >= 6 && touches(runStart.startLine, list.stmts[index - 1]!.endLine))
276
280
  findings.push(finding(doc, runStart.start, "wall"));
277
281
 
@@ -279,7 +283,7 @@ function walls(
279
283
  length = 0;
280
284
  }
281
285
 
282
- if (!stmt || stmt.multiline) continue;
286
+ if (!counted) continue;
283
287
 
284
288
  runStart ??= stmt;
285
289
  length++;
@@ -51,7 +51,6 @@ function unbracedCode(line: string): string {
51
51
  }
52
52
 
53
53
  const editLimit = 2000;
54
-
55
54
  function shortestEdit(before: number[], after: number[]): [number, number][] | undefined {
56
55
  const offset = editLimit + 1;
57
56
  const reach = new Int32Array(2 * offset + 1);
@@ -148,7 +148,6 @@ export interface Trace {
148
148
  }
149
149
 
150
150
  const NO_BRACES: BracePass = { edits: [], findings: [] };
151
-
152
151
  export function traceFix(
153
152
  language: Language,
154
153
  original: Doc,
@@ -29,6 +29,7 @@ export type Kind =
29
29
  | "break"
30
30
  | "continue"
31
31
  | "expression"
32
+ | "import"
32
33
  | "other";
33
34
 
34
35
  export type Path = [string, ...string[]];
@@ -64,7 +65,7 @@ export interface Stmt<Node = unknown> extends Item<Node> {
64
65
  detached: boolean;
65
66
  }
66
67
 
67
- export type ListKind = "block" | "switch";
68
+ export type ListKind = "block" | "switch" | "module";
68
69
 
69
70
  export interface StatementList<Node = unknown> {
70
71
  kind: ListKind;
package/src/files.ts CHANGED
@@ -12,7 +12,6 @@ export interface Collected {
12
12
  }
13
13
 
14
14
  let gitAvailable: boolean | undefined;
15
-
16
15
  function hasGit(): boolean {
17
16
  return (gitAvailable ??= Bun.which("git") !== null);
18
17
  }
package/src/gitignore.ts CHANGED
@@ -4,6 +4,7 @@ type CharacterSet = {
4
4
  classes: string[];
5
5
  negated: boolean;
6
6
  };
7
+
7
8
  type Token =
8
9
  | { kind: "literal"; value: string }
9
10
  | { kind: "star" | "globstar" | "directories" | "any" }
package/src/hook.ts CHANGED
@@ -80,7 +80,6 @@ export function hookInput(
80
80
  const events = new Set(["Stop", "SubagentStop", "PreToolUse", "PostToolUse"]);
81
81
  const patchHeader = /^\*\*\* (Add File|Update File|Move to): (.+)$/;
82
82
  const patchApplied = /^Success\. Updated the following files:$/m;
83
-
84
83
  function editedPaths(input: Record<string, unknown>): string[] {
85
84
  const toolInput = input.tool_input;
86
85
  if (!isRecord(toolInput)) return [];
@@ -228,7 +227,6 @@ function writtenFilesInTranscript(text: string): Set<string> | undefined {
228
227
  }
229
228
 
230
229
  const editTools = new Set(["Write", "Edit", "MultiEdit"]);
231
-
232
230
  function editedPath(item: Record<string, unknown>): string | undefined {
233
231
  if (item.type !== "tool_use" || !editTools.has(String(item.name))) return undefined;
234
232
  if (!isRecord(item.input) || typeof item.input.file_path !== "string") return undefined;
@@ -7,6 +7,8 @@ const byExtension = new Map(
7
7
  entries.flatMap((entry) => entry.extensions.map((extension) => [extension, entry] as const)),
8
8
  );
9
9
 
10
+ export const extensions = [...byExtension.keys()];
11
+
10
12
  export function entryFor(path: string): Entry | undefined {
11
13
  return byExtension.get(extname(path));
12
14
  }
@@ -1,4 +1,5 @@
1
1
  import { visitorKeys, type Node } from "oxc-parser";
2
+ import type { Path } from "../../engine/model.ts";
2
3
 
3
4
  function isNode(value: unknown): value is Node {
4
5
  return (
@@ -149,6 +150,21 @@ export function declared(node: Node): string[] {
149
150
  }
150
151
  }
151
152
 
153
+ export function jsxElementPath(node: Node): Path | undefined {
154
+ if (node.type === "JSXIdentifier") return /^[a-z]|-/.test(node.name) ? undefined : [node.name];
155
+ if (node.type !== "JSXMemberExpression") return undefined;
156
+
157
+ const segments = [node.property.name];
158
+
159
+ let head = node.object;
160
+ while (head.type === "JSXMemberExpression") {
161
+ segments.push(head.property.name);
162
+ head = head.object;
163
+ }
164
+
165
+ return [head.name, ...segments.reverse()];
166
+ }
167
+
152
168
  export function* references(root: Node, names: Set<string>): Generator<string> {
153
169
  const stack = [{ node: root, names, binding: false }];
154
170
  while (stack.length > 0) {
@@ -168,6 +184,11 @@ export function* references(root: Node, names: Set<string>): Generator<string> {
168
184
 
169
185
  if (visible.size === 0) continue;
170
186
 
187
+ if (node.type === "JSXOpeningElement") {
188
+ const path = jsxElementPath(node.name);
189
+ if (!binding && path && visible.has(path[0])) yield path[0];
190
+ }
191
+
171
192
  const next = children(node);
172
193
  for (let index = next.length - 1; index >= 0; index--) {
173
194
  const [key, child] = next[index]!;
@@ -20,7 +20,6 @@ const identities = new WeakMap<string[], Map<boolean, PatternList>>();
20
20
  const allExtensions = [...entry.extensions];
21
21
 
22
22
  const expansionLimit = 256;
23
-
24
23
  function expand(pattern: string, limit = expansionLimit): string[] | undefined {
25
24
  const match = /\{([^{}]+)\}/.exec(pattern);
26
25
  if (!match) return [pattern];
@@ -327,7 +327,6 @@ export function fileLayers(file: string, context: Context): { layers: Layer[]; r
327
327
  }
328
328
 
329
329
  const manifestConfigs = new Map<string, Value>();
330
-
331
330
  export function eslintConfig(manifest: string): Value {
332
331
  if (manifestConfigs.has(manifest)) return manifestConfigs.get(manifest);
333
332
 
@@ -14,6 +14,7 @@ type Editor = {
14
14
  root: boolean;
15
15
  sections: { pattern: string | null; values: Record<string, string> }[];
16
16
  };
17
+
17
18
  type FormatKeys = {
18
19
  width: string;
19
20
  tab: string;
@@ -73,6 +74,7 @@ const parsed = new Map<string, Read>();
73
74
  const directories = new Map<string, Directory>();
74
75
  const dependencies = new Map<string, Formatter[]>();
75
76
  const globs = new Map<string, Bun.Glob>();
77
+
76
78
  const editorGlobs = new Map<string, { regex: RegExp; ranges: [bigint, bigint][] }>();
77
79
  const editors = new Map<string, Editor | null>();
78
80
  const widths = new Map<string, Width>();
@@ -1,5 +1,5 @@
1
1
  import type { Node, Statement, SwitchCase } from "oxc-parser";
2
- import { boundNames, children, declared, references } from "./ast.ts";
2
+ import { boundNames, children, declared, jsxElementPath, references } from "./ast.ts";
3
3
  import { lineAt, source } from "../../engine/doc.ts";
4
4
  import type { Binding, Doc, Item, Kind, Path } from "../../engine/model.ts";
5
5
  import type { Layout } from "../language.ts";
@@ -42,7 +42,22 @@ const JUMPS: Record<string, string> = {
42
42
  BreakStatement: "break",
43
43
  };
44
44
 
45
+ function exported(node: Statement | SwitchCase): Statement | SwitchCase {
46
+ if (node.type === "ExportNamedDeclaration" && node.declaration) return node.declaration;
47
+ if (node.type === "ExportDefaultDeclaration" && node.declaration.type === "FunctionDeclaration")
48
+ return node.declaration;
49
+
50
+ return node;
51
+ }
52
+
45
53
  function kindOf(node: Statement | SwitchCase): Kind {
54
+ if (
55
+ node.type === "ImportDeclaration" ||
56
+ node.type === "ExportAllDeclaration" ||
57
+ (node.type === "ExportNamedDeclaration" && node.source)
58
+ )
59
+ return "import";
60
+
46
61
  if (node.type === "ExpressionStatement")
47
62
  return node.expression.type === "AssignmentExpression" ? "assignment" : "expression";
48
63
 
@@ -120,6 +135,16 @@ function readsPath(doc: Doc, root: Node, path: Path): boolean {
120
135
  continue;
121
136
  }
122
137
 
138
+ if (node.type === "JSXOpeningElement") {
139
+ const elementPath = jsxElementPath(node.name);
140
+ if (
141
+ elementPath &&
142
+ elementPath.length >= path.length &&
143
+ path.every((segment, index) => elementPath[index] === segment)
144
+ )
145
+ return true;
146
+ }
147
+
123
148
  if (node.type !== "MemberExpression") {
124
149
  for (const [, child] of children(node).toReversed()) stack.push(child);
125
150
  continue;
@@ -241,18 +266,19 @@ export function facts(
241
266
  node: Statement | SwitchCase,
242
267
  width: Layout,
243
268
  ): Item<Statement | SwitchCase> {
269
+ const inner = exported(node);
244
270
  const finalizer = node.type === "TryStatement" ? node.finalizer : null;
245
271
  const joined = joinedColumns(doc, node, width);
246
272
  return {
247
273
  node,
248
274
  start: node.start,
249
275
  end: node.end,
250
- kind: kindOf(node),
251
- word: WORDS[node.type] ?? null,
252
- binds: bindsOf(doc, node),
276
+ kind: kindOf(inner),
277
+ word: WORDS[inner.type] ?? null,
278
+ binds: bindsOf(doc, inner),
253
279
  declaration:
254
- node.type === "VariableDeclaration"
255
- ? { keyword: node.kind, letLike: node.kind === "let" }
280
+ inner.type === "VariableDeclaration"
281
+ ? { keyword: inner.kind, letLike: inner.kind === "let" }
256
282
  : null,
257
283
  guard: guardOf(node),
258
284
  compact:
@@ -261,8 +287,8 @@ export function facts(
261
287
  joined,
262
288
  caseBody: node.type === "SwitchCase" && node.consequent.length > 0,
263
289
  operation: operation(doc, node),
264
- references: (names) => references(node, names),
265
- readsPath: (path) => readsPath(doc, node, path),
290
+ references: (names) => references(inner, names),
291
+ readsPath: (path) => readsPath(doc, inner, path),
266
292
  finallyRepeats: (expected) => finalizer !== null && repeatsOperation(doc, finalizer, expected),
267
293
  };
268
294
  }
@@ -8,6 +8,21 @@ import type { Layout } from "../language.ts";
8
8
  export type List = StatementList<Statement | SwitchCase>;
9
9
 
10
10
  export function listAt(doc: Doc, node: Node, width: Layout): List | undefined {
11
+ if (node.type === "Program")
12
+ return {
13
+ kind: "module",
14
+ start: node.start,
15
+ end: node.end,
16
+ openLine: null,
17
+ closeLine: null,
18
+ stmts: attach(
19
+ doc,
20
+ node.body.map((stmt) => facts(doc, stmt, width)),
21
+ node.start,
22
+ node.end,
23
+ ),
24
+ };
25
+
11
26
  if (node.type === "BlockStatement" || node.type === "StaticBlock") {
12
27
  const start =
13
28
  node.type === "StaticBlock" ? nextToken(doc, node.start + "static".length) : node.start;