@ryuugg/stanza 0.1.2 → 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.2",
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;
@@ -6,6 +6,7 @@ const entries: Entry[] = [javascript];
6
6
  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()];
10
11
 
11
12
  export function entryFor(path: string): Entry | undefined {
@@ -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>();
@@ -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
 
@@ -251,18 +266,19 @@ export function facts(
251
266
  node: Statement | SwitchCase,
252
267
  width: Layout,
253
268
  ): Item<Statement | SwitchCase> {
269
+ const inner = exported(node);
254
270
  const finalizer = node.type === "TryStatement" ? node.finalizer : null;
255
271
  const joined = joinedColumns(doc, node, width);
256
272
  return {
257
273
  node,
258
274
  start: node.start,
259
275
  end: node.end,
260
- kind: kindOf(node),
261
- word: WORDS[node.type] ?? null,
262
- binds: bindsOf(doc, node),
276
+ kind: kindOf(inner),
277
+ word: WORDS[inner.type] ?? null,
278
+ binds: bindsOf(doc, inner),
263
279
  declaration:
264
- node.type === "VariableDeclaration"
265
- ? { keyword: node.kind, letLike: node.kind === "let" }
280
+ inner.type === "VariableDeclaration"
281
+ ? { keyword: inner.kind, letLike: inner.kind === "let" }
266
282
  : null,
267
283
  guard: guardOf(node),
268
284
  compact:
@@ -271,8 +287,8 @@ export function facts(
271
287
  joined,
272
288
  caseBody: node.type === "SwitchCase" && node.consequent.length > 0,
273
289
  operation: operation(doc, node),
274
- references: (names) => references(node, names),
275
- readsPath: (path) => readsPath(doc, node, path),
290
+ references: (names) => references(inner, names),
291
+ readsPath: (path) => readsPath(doc, inner, path),
276
292
  finallyRepeats: (expected) => finalizer !== null && repeatsOperation(doc, finalizer, expected),
277
293
  };
278
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;