@ryuugg/stanza 0.1.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.
Files changed (50) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +163 -0
  3. package/package.json +63 -0
  4. package/src/cli.ts +820 -0
  5. package/src/compile/darwin-arm64.ts +3 -0
  6. package/src/compile/darwin-x64.ts +3 -0
  7. package/src/compile/linux-arm64-musl.ts +3 -0
  8. package/src/compile/linux-arm64.ts +3 -0
  9. package/src/compile/linux-x64-musl.ts +3 -0
  10. package/src/compile/linux-x64.ts +3 -0
  11. package/src/compiled.ts +6 -0
  12. package/src/engine/attach.ts +62 -0
  13. package/src/engine/directives.ts +154 -0
  14. package/src/engine/doc.ts +93 -0
  15. package/src/engine/edits.ts +79 -0
  16. package/src/engine/explain.ts +333 -0
  17. package/src/engine/gaps.ts +329 -0
  18. package/src/engine/hunks.ts +320 -0
  19. package/src/engine/index.ts +254 -0
  20. package/src/engine/model.ts +102 -0
  21. package/src/engine/rules.ts +103 -0
  22. package/src/engine/types.ts +37 -0
  23. package/src/files.ts +822 -0
  24. package/src/gitignore.ts +279 -0
  25. package/src/hook.ts +282 -0
  26. package/src/languages/index.ts +32 -0
  27. package/src/languages/javascript/ast.ts +199 -0
  28. package/src/languages/javascript/braces.ts +374 -0
  29. package/src/languages/javascript/config/biome.ts +112 -0
  30. package/src/languages/javascript/config/eslint.ts +128 -0
  31. package/src/languages/javascript/config/evaluate.ts +278 -0
  32. package/src/languages/javascript/config/find.ts +108 -0
  33. package/src/languages/javascript/config/glob.ts +150 -0
  34. package/src/languages/javascript/config/index.ts +48 -0
  35. package/src/languages/javascript/config/layers.ts +425 -0
  36. package/src/languages/javascript/config/module.ts +326 -0
  37. package/src/languages/javascript/config/oxlint.ts +170 -0
  38. package/src/languages/javascript/config/width.ts +715 -0
  39. package/src/languages/javascript/entry.ts +39 -0
  40. package/src/languages/javascript/facts.ts +268 -0
  41. package/src/languages/javascript/index.ts +20 -0
  42. package/src/languages/javascript/lists.ts +49 -0
  43. package/src/languages/javascript/oracle.ts +191 -0
  44. package/src/languages/javascript/parse.ts +58 -0
  45. package/src/languages/javascript/scan.ts +68 -0
  46. package/src/languages/language.ts +109 -0
  47. package/src/launcher +21 -0
  48. package/src/native-file.d.ts +4 -0
  49. package/src/step.ts +108 -0
  50. package/src/usage.ts +30 -0
@@ -0,0 +1,109 @@
1
+ import type { Region } from "../engine/directives.ts";
2
+ import type { OffsetEdit } from "../engine/edits.ts";
3
+ import type { Comment, Doc, StatementList, Stmt } from "../engine/model.ts";
4
+ import type { Finding } from "../engine/types.ts";
5
+
6
+ export interface Rejection {
7
+ start: number;
8
+ message: string;
9
+ }
10
+
11
+ export interface Parsed {
12
+ comments: Comment[];
13
+ error: Rejection | undefined;
14
+ rejection(text: string): Rejection | undefined;
15
+ scan(doc: Doc, width: Layout): Scan;
16
+ }
17
+
18
+ export type WidthSource =
19
+ | { kind: "config"; file: string }
20
+ | { kind: "default"; formatter: "oxfmt" | "prettier" | "biome" }
21
+ | { kind: "fallback" };
22
+
23
+ export interface Width {
24
+ columns: number;
25
+ tab: number;
26
+ source: WidthSource;
27
+ unread: string[];
28
+ }
29
+
30
+ export type Layout = Pick<Width, "columns" | "tab">;
31
+ export const FALLBACK_WIDTH: Width = {
32
+ columns: 80,
33
+ tab: 2,
34
+ source: { kind: "fallback" },
35
+ unread: [],
36
+ };
37
+
38
+ export interface Scan {
39
+ lists: StatementList[];
40
+ frozen: Region[];
41
+ braces: BraceScan | undefined;
42
+ }
43
+
44
+ export type Touches = (first: number, last: number) => boolean;
45
+
46
+ export interface BracePass {
47
+ edits: OffsetEdit[];
48
+ findings: Finding[];
49
+ }
50
+
51
+ export interface Opening {
52
+ start: number;
53
+ open: number;
54
+ close: number;
55
+ inner: [number, number] | null;
56
+ }
57
+
58
+ export type Hold =
59
+ | { kind: "directive" }
60
+ | { kind: "inapplicable"; reason: string }
61
+ | { kind: "held"; reason: string | null };
62
+
63
+ export interface BraceScan {
64
+ pass(touches: Touches, touched: ReadonlySet<Stmt>, collapseChains: boolean): BracePass;
65
+ opening(line: number): Opening[];
66
+ hold(start: number, at: (offset: number) => number): Hold;
67
+ }
68
+
69
+ export interface ConfigArm {
70
+ width(path: string): Width;
71
+ setting(dir: string, extension: string): { enforced: boolean; unread: string[] };
72
+ decisions(
73
+ dir: string,
74
+ extension: string,
75
+ ): { setting: "on" | "off" | "unknown"; files: string[] }[];
76
+ }
77
+
78
+ export interface OracleStatement {
79
+ start: number;
80
+ clause: boolean;
81
+ skeleton(): string;
82
+ gaps(): [string | undefined, string | undefined];
83
+ }
84
+
85
+ export interface OracleSide {
86
+ comments: Comment[];
87
+ blocks: { start: number; end: number; single: boolean }[];
88
+ shape(): string | undefined;
89
+ container(comment: Comment): Region;
90
+ statementAfter(offset: number): OracleStatement | undefined;
91
+ }
92
+
93
+ export interface Oracle {
94
+ side(path: string, text: string): OracleSide;
95
+ }
96
+
97
+ export interface Language {
98
+ parse(path: string, text: string): Parsed;
99
+ config?: ConfigArm;
100
+ oracle(): Oracle;
101
+ }
102
+
103
+ export interface Entry {
104
+ id: string;
105
+ extensions: readonly string[];
106
+ directories: ReadonlySet<string>;
107
+ skipsName(name: string, keepGenerated: boolean): boolean;
108
+ load(): Language;
109
+ }
package/src/launcher ADDED
@@ -0,0 +1,21 @@
1
+ #!/bin/sh
2
+ ':' /*
3
+ # Runs as sh first and as JS under Bun.
4
+ if command -v bun >/dev/null 2>&1; then
5
+ exec bun "$0" "$@"
6
+ fi
7
+
8
+ echo 'stanza: needs Bun 1.4 or later (https://bun.sh) or a release binary (https://github.com/ryuudotgg/stanza/releases/latest)' >&2
9
+ [ "$1" = hook ] && exit 1
10
+ exit 2
11
+ */
12
+
13
+ if (!Bun.semver.satisfies(Bun.version, ">=1.4.0")) {
14
+ console.error(
15
+ `stanza: found Bun ${Bun.version}, needs Bun 1.4 or later (https://bun.sh) or a release binary (https://github.com/ryuudotgg/stanza/releases/latest)`,
16
+ );
17
+ process.exit(process.argv[2] === "hook" ? 1 : 2);
18
+ }
19
+
20
+ const { main, systemIo } = await import("./cli.ts");
21
+ process.exitCode = main(process.argv.slice(2), systemIo());
@@ -0,0 +1,4 @@
1
+ declare module "*.node" {
2
+ const path: string;
3
+ export default path;
4
+ }
package/src/step.ts ADDED
@@ -0,0 +1,108 @@
1
+ import { dirname, extname } from "node:path";
2
+ import { isGeneratedHeader } from "./files.ts";
3
+ import { processFile } from "./engine/index.ts";
4
+ import type { Changed } from "./engine/model.ts";
5
+ import { languageOf } from "./languages/index.ts";
6
+ import { FALLBACK_WIDTH, type Width } from "./languages/language.ts";
7
+ import type { Braces, FileResult, Finding, Mode, Options } from "./engine/types.ts";
8
+
9
+ export type Decoded = string | { message: string };
10
+
11
+ export interface StepOptions {
12
+ mode: Mode;
13
+ braces?: Braces | undefined;
14
+ changedLines?: Changed | undefined;
15
+ unread?: Set<string>;
16
+ unreadWidth?: Set<string>;
17
+ }
18
+
19
+ export interface StepResult {
20
+ findings: Finding[];
21
+ fixed: string | undefined;
22
+ parseError: boolean;
23
+ }
24
+
25
+ const utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
26
+ const bom = "";
27
+
28
+ export function decode(bytes: Uint8Array): Decoded {
29
+ try {
30
+ return utf8.decode(bytes);
31
+ } catch {
32
+ return { message: "not valid UTF-8, left untouched" };
33
+ }
34
+ }
35
+
36
+ export function withoutMark(text: string): string {
37
+ return text.startsWith(bom) ? text.slice(bom.length) : text;
38
+ }
39
+
40
+ export function keepBraces(path: string, braces?: Braces, unread?: Set<string>): boolean {
41
+ if (braces !== undefined) return braces === "off";
42
+
43
+ const setting = languageOf(path).config?.setting(dirname(path), extname(path)) ?? {
44
+ enforced: false,
45
+ unread: [],
46
+ };
47
+
48
+ for (const file of setting.unread) unread?.add(file);
49
+ return setting.enforced;
50
+ }
51
+
52
+ export function stepSettings(
53
+ path: string,
54
+ options: { braces?: Braces; unread?: Set<string>; unreadWidth?: Set<string> } = {},
55
+ ): { keepBraces: boolean; width: Width } {
56
+ const language = languageOf(path);
57
+ const width = language.config?.width(path) ?? FALLBACK_WIDTH;
58
+ for (const file of width.unread) options.unreadWidth?.add(file);
59
+
60
+ return { keepBraces: keepBraces(path, options.braces, options.unread), width };
61
+ }
62
+
63
+ export function fixText(path: string, text: string, mode: Mode, options: Options): FileResult {
64
+ const body = withoutMark(text);
65
+ const mark = text.slice(0, text.length - body.length);
66
+ const result = processFile(path, body, mode, options);
67
+ return { ...result, text: mark + result.text };
68
+ }
69
+
70
+ function failure(path: string, rule: "parse" | "error", message: string): StepResult {
71
+ return {
72
+ findings: [{ path, line: 1, col: 1, rule, message, fixable: false }],
73
+ fixed: undefined,
74
+ parseError: true,
75
+ };
76
+ }
77
+
78
+ export function formatText(path: string, text: Decoded, options: StepOptions): StepResult {
79
+ if (typeof text !== "string") return failure(path, "parse", text.message);
80
+
81
+ try {
82
+ if (isGeneratedHeader(text)) return { findings: [], fixed: undefined, parseError: false };
83
+
84
+ const result = fixText(path, text, options.mode, {
85
+ ...stepSettings(path, options),
86
+ ...(options.changedLines === undefined ? {} : { changedLines: options.changedLines }),
87
+ });
88
+
89
+ const changed = options.mode === "fix" && !result.parseError && result.text !== text;
90
+ if (changed) {
91
+ const error = languageOf(path).parse(path, withoutMark(result.text)).error;
92
+ if (error)
93
+ return failure(
94
+ path,
95
+ "error",
96
+ `fixed text does not parse (${error.message}), left untouched`,
97
+ );
98
+ }
99
+
100
+ return {
101
+ findings: result.findings,
102
+ fixed: changed ? result.text : undefined,
103
+ parseError: result.parseError,
104
+ };
105
+ } catch (error: unknown) {
106
+ return failure(path, "error", String(error));
107
+ }
108
+ }
package/src/usage.ts ADDED
@@ -0,0 +1,30 @@
1
+ export const usage =
2
+ "Usage: stanza (--fix | --check) [--changed [--hunks] | --stdin <path> | [--] <paths...>] [--json] [--braces | --no-braces]\n stanza --check --staged [--hunks] [--json] [--braces | --no-braces]\n stanza explain <file>:<line> [--no-braces]\n stanza hook [--braces | --no-braces] [--hunks]";
3
+
4
+ export const flags: (readonly [string, string])[] = [
5
+ ["--fix", "apply every deterministic rule in place"],
6
+ ["--check", "report only, change nothing"],
7
+ ["--changed", "files from `git diff --name-only HEAD` plus untracked files"],
8
+ ["--hunks", "with --changed or --staged, only gaps and blocks touching changed lines"],
9
+ ["--staged", "the staged content of staged files, for pre-commit"],
10
+ [
11
+ "--stdin <path>",
12
+ "source on stdin; --fix: fixed text on stdout, findings on stderr; --check: findings on stdout",
13
+ ],
14
+ ["--json", "findings as a JSON array, for hooks"],
15
+ ["--braces", "turn on the braces rule even when lint config turns it off or cannot be read"],
16
+ ["--no-braces", "turn off the braces rule, keep the blank line rules"],
17
+ ["explain <file>:<line>", "which rule decides the gap or braced body at that line, and why"],
18
+ ["hook", "the Stop hook and PreToolUse hook on Write, reads JSON on stdin"],
19
+ ["--help", "usage, flags and the rule catalog"],
20
+ ["--version", "the version, and for a built binary the commit it was built from"],
21
+ ];
22
+
23
+ export function columns(rows: readonly (readonly string[])[]): string[] {
24
+ const widths = rows[0]!.map((_, index) => Math.max(...rows.map((row) => row[index]!.length)));
25
+ return rows.map((row) =>
26
+ row
27
+ .map((cell, index) => (index < row.length - 1 ? cell.padEnd(widths[index]!) : cell))
28
+ .join(" "),
29
+ );
30
+ }