Skip to content

Commit 17b386c

Browse files
Make formatter options optional
1 parent e98b6f3 commit 17b386c

10 files changed

Lines changed: 39 additions & 59 deletions

src/lib/talonFormatter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import type { Node } from "web-tree-sitter";
2-
import type { Options } from "../types.js";
32
import { getColumnWidth } from "../util/getColumnWidth.js";
43
import { getIndentation } from "../util/getIndentation.js";
54

5+
interface Options {
6+
readonly indentTabs?: boolean;
7+
readonly indentWidth?: number;
8+
readonly columnWidth?: number;
9+
}
10+
611
export function talonFormatter(node: Node, options: Options): string {
712
const columnWidth = getColumnWidth(node.text) ?? options.columnWidth;
8-
const indentation = getIndentation(options);
13+
const indentation = getIndentation(options.indentTabs, options.indentWidth);
914
const formatter = new TalonFormatter(indentation, columnWidth);
1015
return formatter.getText(node);
1116
}

src/lib/talonListFormatter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import type { Options } from "../types.js";
21
import { getColumnWidth } from "../util/getColumnWidth.js";
32
import { parseTalonList } from "./parseTalonList.js";
43

4+
interface Options {
5+
readonly columnWidth?: number;
6+
}
7+
58
export function talonListFormatter(text: string, options: Options): string {
69
const columnWidth = getColumnWidth(text) ?? options.columnWidth;
710
const talonList = parseTalonList(text);

src/lib/treeSitterFormatter.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import type { Node } from "web-tree-sitter";
2-
import type { Options } from "../types.js";
32
import { getIndentation } from "../util/getIndentation.js";
43

4+
interface Options {
5+
readonly indentTabs?: boolean;
6+
readonly indentWidth?: number;
7+
}
8+
59
export function treeSitterFormatter(node: Node, options: Options): string {
6-
const indentation = getIndentation(options);
10+
const indentation = getIndentation(options.indentTabs, options.indentWidth);
711
const formatter = new TreeSitterFormatter(indentation);
812
return formatter.getText(node);
913
}

src/test/cli.test.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import { PassThrough } from "node:stream";
66
import { formatFile, formatFiles, mainFormatStdin } from "../cli/cli.js";
77
import type { CLI, Options, ParsedArgs } from "../types.js";
88
import { EXIT_FAIL, EXIT_OK } from "../util/constants.js";
9-
import {
10-
getDefaultArguments,
11-
getDefaultOptions,
12-
} from "../util/getDefaultArguments.js";
9+
import { getDefaultArguments } from "../util/getDefaultArguments.js";
1310
import { parseArgs } from "../util/parseArgs.js";
1411
import { printHelp } from "../util/printHelp.js";
1512

@@ -23,12 +20,7 @@ suite("CLI", () => {
2320
const cli = createCLI((text) => `${text} updated`);
2421

2522
try {
26-
const didChange = await formatFile(
27-
cli,
28-
false,
29-
getDefaultOptions(),
30-
fileName,
31-
);
23+
const didChange = await formatFile(cli, false, {}, fileName);
3224
const actual = await fs.readFile(fileName, "utf8");
3325

3426
assert.equal(didChange, true);
@@ -47,12 +39,7 @@ suite("CLI", () => {
4739
const cli = createCLI((text) => `${text} updated`);
4840

4941
try {
50-
const didChange = await formatFile(
51-
cli,
52-
true,
53-
getDefaultOptions(),
54-
fileName,
55-
);
42+
const didChange = await formatFile(cli, true, {}, fileName);
5643
const actual = await fs.readFile(fileName, "utf8");
5744

5845
assert.equal(didChange, true);
@@ -78,7 +65,7 @@ suite("CLI", () => {
7865
cli,
7966
[unchangedFileName, changedFileName],
8067
false,
81-
getDefaultOptions(),
68+
{},
8269
);
8370
const unchangedContent = await fs.readFile(
8471
unchangedFileName,
@@ -98,12 +85,7 @@ suite("CLI", () => {
9885
const fileName = path.join(os.tmpdir(), "talonfmt-missing.txt");
9986
const cli = createCLI((text) => `${text} updated`);
10087

101-
const didChange = await formatFile(
102-
cli,
103-
false,
104-
getDefaultOptions(),
105-
fileName,
106-
);
88+
const didChange = await formatFile(cli, false, {}, fileName);
10789

10890
assert.equal(didChange, false);
10991
});
@@ -120,7 +102,7 @@ suite("CLI", () => {
120102

121103
try {
122104
await assert.rejects(
123-
formatFile(cli, false, getDefaultOptions(), fileName),
105+
formatFile(cli, false, {}, fileName),
124106
/Failed to format '.*example\.txt': boom/,
125107
);
126108
} finally {
@@ -170,7 +152,6 @@ suite("CLI", () => {
170152
"content",
171153
);
172154
const options = {
173-
...getDefaultOptions(),
174155
indentTabs: true,
175156
indentWidth: 2,
176157
columnWidth: 24,
@@ -204,7 +185,6 @@ suite("CLI", () => {
204185

205186
test("passes options and stdin file name to stdin formatter", async () => {
206187
const options = {
207-
...getDefaultOptions(),
208188
indentTabs: true,
209189
indentWidth: 2,
210190
};
@@ -266,7 +246,7 @@ suite("CLI", () => {
266246
check: false,
267247
indentTabs: true,
268248
indentWidth: 2,
269-
lineWidth: 80,
249+
lineWidth: undefined,
270250
columnWidth: 24,
271251
});
272252
const actual = parseArgs(
@@ -428,7 +408,7 @@ async function readAndFormatStdin(
428408
cli: CLI,
429409
input: string,
430410
check: boolean = false,
431-
options: Options = getDefaultOptions(),
411+
options: Options = {},
432412
): Promise<number> {
433413
const stdin = new PassThrough();
434414
Object.defineProperty(stdin, "isTTY", { value: false });

src/test/talonFormatter.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as assert from "node:assert";
22
import { talonFormatter } from "../lib/talonFormatter.js";
33
import { parseText } from "../util/parseText.js";
4-
import { getDefaultOptions } from "../util/getDefaultArguments.js";
54

65
type Content = string | string[];
76

@@ -180,7 +179,6 @@ suite("Talon formatter", () => {
180179
const content = getContentString(fixture.pre);
181180
const rootNode = await parseText(content, "tree-sitter-talon");
182181
const actual = talonFormatter(rootNode, {
183-
...getDefaultOptions(),
184182
columnWidth: 28,
185183
});
186184
const expected = getContentString(fixture.post);
@@ -195,7 +193,6 @@ suite("Talon formatter", () => {
195193
);
196194

197195
const actual = talonFormatter(rootNode, {
198-
...getDefaultOptions(),
199196
indentTabs: true,
200197
});
201198

src/test/talonListFormatter.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as assert from "node:assert";
22
import { talonListFormatter } from "../lib/talonListFormatter.js";
3-
import { getDefaultOptions } from "../util/getDefaultArguments.js";
43

54
const fixtures: {
65
title: string;
@@ -60,7 +59,6 @@ suite("Talon list formatter", () => {
6059
for (const fixture of fixtures) {
6160
test(fixture.title, () => {
6261
const actual = talonListFormatter(fixture.pre, {
63-
...getDefaultOptions(),
6462
columnWidth: 10,
6563
});
6664
assert.equal(actual, fixture.post);

src/test/treeSitterFormatter.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as assert from "node:assert";
22
import { treeSitterFormatter } from "../lib/treeSitterFormatter.js";
33
import { parseText } from "../util/parseText.js";
4-
import { getDefaultOptions } from "../util/getDefaultArguments.js";
54

65
type Content = string | string[];
76

@@ -103,7 +102,7 @@ suite("Tree-sitter formatter", () => {
103102
test(fixture.title, async () => {
104103
const content = getContentString(fixture.pre);
105104
const rootNode = await parseText(content, "tree-sitter-query");
106-
const actual = treeSitterFormatter(rootNode, getDefaultOptions());
105+
const actual = treeSitterFormatter(rootNode, {});
107106
const expected = getContentString(fixture.post);
108107
assert.equal(actual, expected);
109108
});
@@ -113,7 +112,6 @@ suite("Tree-sitter formatter", () => {
113112
const rootNode = await parseText("(aaa (bbb))", "tree-sitter-query");
114113

115114
const actual = treeSitterFormatter(rootNode, {
116-
...getDefaultOptions(),
117115
indentTabs: true,
118116
});
119117

src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ export interface CLI {
2626
}
2727

2828
export interface Options {
29-
indentTabs: boolean;
30-
indentWidth: number;
31-
lineWidth: number;
32-
columnWidth: number | undefined;
29+
indentTabs?: boolean;
30+
indentWidth?: number;
31+
lineWidth?: number;
32+
columnWidth?: number;
3333
}
3434

3535
export interface ParsedArgs extends Options {

src/util/getDefaultArguments.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import type { Options, ParsedArgs } from "../types.js";
1+
import type { ParsedArgs } from "../types.js";
22

33
export function getDefaultArguments(): ParsedArgs {
44
return {
55
filePatterns: [],
66
help: false,
77
version: false,
88
check: false,
9-
...getDefaultOptions(),
10-
};
11-
}
12-
13-
export function getDefaultOptions(): Options {
14-
return {
15-
indentTabs: false,
16-
indentWidth: 4,
17-
lineWidth: 80,
9+
indentTabs: undefined,
10+
indentWidth: undefined,
11+
lineWidth: undefined,
1812
columnWidth: undefined,
1913
};
2014
}

src/util/getIndentation.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { Options } from "../types.js";
2-
3-
export function getIndentation(options: Options): string {
4-
return options.indentTabs ? "\t" : " ".repeat(options.indentWidth);
1+
export function getIndentation(
2+
indentTabs: boolean | undefined,
3+
indentWidth: number | undefined,
4+
): string {
5+
return indentTabs ? "\t" : " ".repeat(indentWidth ?? 4);
56
}

0 commit comments

Comments
 (0)