Skip to content

Commit ef20809

Browse files
committed
🐛 Fix boolean option getter returns true|false instead 1|0
Fixes #203
1 parent f7e11ff commit ef20809

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

scripts/gen-option/format.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ function defaultValue(type: OptionType): string {
2424
}
2525
}
2626

27+
function coerceValue(expr: string, type: OptionType): string {
28+
switch (type) {
29+
case "string":
30+
return `(${expr}) as string`;
31+
case "number":
32+
return `(${expr}) as number`;
33+
case "boolean":
34+
// Vim returns (0 | 1) so coerce to boolean.
35+
return `Boolean(${expr})`;
36+
default: {
37+
const unknownType: never = type;
38+
throw new Error(`Unknown type ${unknownType}`);
39+
}
40+
}
41+
}
42+
2743
export function formatDocs(docs: string): string[] {
2844
const lines = docs.replaceAll(/\*\//g, "* /").split("\n");
2945
const normalizedLines = lines.map((v) => ` * ${v}`.trimEnd());
@@ -49,7 +65,8 @@ function formatOption({ name, type, scope, docs }: Option): string[] {
4965
function formatOptionBody(name: string, type: OptionType): string[] {
5066
const lines = [
5167
` async get(denops: Denops): Promise<${type}> {`,
52-
` return await options.get(denops, "${name}") ?? ${defaultValue(type)};`,
68+
` const result = await options.get(denops, "${name}");`,
69+
` return ${coerceValue(`result ?? ${defaultValue(type)}`, type)};`,
5370
` },`,
5471
` set(denops: Denops, value: ${type}): Promise<void> {`,
5572
` return options.set(denops, "${name}", value);`,
@@ -64,9 +81,8 @@ function formatOptionBody(name: string, type: OptionType): string[] {
6481
function formatGlobalOptionBody(name: string, type: OptionType): string[] {
6582
const lines = [
6683
` async getGlobal(denops: Denops): Promise<${type}> {`,
67-
` return await globalOptions.get(denops, "${name}") ?? ${
68-
defaultValue(type)
69-
};`,
84+
` const result = await globalOptions.get(denops, "${name}");`,
85+
` return ${coerceValue(`result ?? ${defaultValue(type)}`, type)};`,
7086
` },`,
7187
` setGlobal(denops: Denops, value: ${type}): Promise<void> {`,
7288
` return globalOptions.set(denops, "${name}", value);`,
@@ -81,9 +97,8 @@ function formatGlobalOptionBody(name: string, type: OptionType): string[] {
8197
function formatLocalOptionBody(name: string, type: OptionType): string[] {
8298
const lines = [
8399
` async getLocal(denops: Denops): Promise<${type}> {`,
84-
` return await localOptions.get(denops, "${name}") ?? ${
85-
defaultValue(type)
86-
};`,
100+
` const result = await localOptions.get(denops, "${name}");`,
101+
` return ${coerceValue(`result ?? ${defaultValue(type)}`, type)};`,
87102
` },`,
88103
` setLocal(denops: Denops, value: ${type}): Promise<void> {`,
89104
` return localOptions.set(denops, "${name}", value);`,
@@ -99,7 +114,7 @@ function formatBufferOptionBody(name: string, type: OptionType): string[] {
99114
const lines = [
100115
` async getBuffer(denops: Denops, bufnr: number): Promise<${type}> {`,
101116
` const result = await getbufvar(denops, bufnr, "&${name}");`,
102-
` return (result as ${type}) ?? ${defaultValue(type)};`,
117+
` return ${coerceValue(`result ?? ${defaultValue(type)}`, type)};`,
103118
` },`,
104119
` setBuffer(denops: Denops, bufnr: number, value: ${type}): Promise<void> {`,
105120
` return setbufvar(denops, bufnr, "&${name}", value);`,
@@ -112,7 +127,7 @@ function formatWindowOptionBody(name: string, type: OptionType): string[] {
112127
const lines = [
113128
` async getWindow(denops: Denops, winnr: number): Promise<${type}> {`,
114129
` const result = await getwinvar(denops, winnr, "&${name}");`,
115-
` return (result as ${type}) ?? ${defaultValue(type)};`,
130+
` return ${coerceValue(`result ?? ${defaultValue(type)}`, type)};`,
116131
` },`,
117132
` setWindow(denops: Denops, winnr: number, value: ${type}): Promise<void> {`,
118133
` return setwinvar(denops, winnr, "&${name}", value);`,

0 commit comments

Comments
 (0)