Docs in code: unify documented function definitions

This commit is contained in:
Zef Hemel
2026-07-16 09:00:18 +02:00
parent 31b62816d3
commit 37d6c59555
15 changed files with 678 additions and 626 deletions
+108 -114
View File
@@ -28,8 +28,8 @@ function capturesToLua(caps: CaptureResult[]): any {
}
export const stringApi = new LuaTable({
byte: new LuaBuiltinFunction(
(_sf, s: string, i?: number, j?: number) => {
byte: new LuaBuiltinFunction({
callback: (_sf, s: string, i?: number, j?: number) => {
i = i ?? 1;
j = j ?? i;
if (j > s.length) j = s.length;
@@ -40,8 +40,7 @@ export const stringApi = new LuaTable({
}
return new LuaMultiRes(result);
},
{
kind: "builtin",
documentation: {
description:
"Returns the numeric character codes in the inclusive range from `i` to `j`.",
parameters: [
@@ -51,18 +50,17 @@ export const stringApi = new LuaTable({
],
returns: [{ type: "integer", description: "One result per character." }],
},
),
char: new LuaBuiltinFunction(
(_sf, ...args: number[]) => String.fromCharCode(...args),
{
kind: "builtin",
}),
char: new LuaBuiltinFunction({
callback: (_sf, ...args: number[]) => String.fromCharCode(...args),
documentation: {
description: "Creates a string from numeric character codes.",
signatures: ["string.char(...): string"],
returns: [{ type: "string" }],
},
),
find: new LuaBuiltinFunction(
(_sf, s: string, pattern: string, init = 1, plain = false) => {
}),
find: new LuaBuiltinFunction({
callback: (_sf, s: string, pattern: string, init = 1, plain = false) => {
const r = patternFind(s, pattern, init, plain);
if (!r) return null;
const result: any[] = [r.start, r.end];
@@ -71,8 +69,7 @@ export const stringApi = new LuaTable({
}
return new LuaMultiRes(result);
},
{
kind: "builtin",
documentation: {
description:
"Finds the first Lua-pattern match and returns its bounds followed by captures.",
parameters: [
@@ -86,16 +83,15 @@ export const stringApi = new LuaTable({
{ type: "integer", description: "End index." },
],
},
),
format: new LuaBuiltinFunction(
(_sf, format: string, ...args: any[]) => {
}),
format: new LuaBuiltinFunction({
callback: (_sf, format: string, ...args: any[]) => {
for (let i = 0; i < args.length; i++) {
args[i] = untagNumber(args[i]);
}
return luaFormat(format, ...args);
},
{
kind: "builtin",
documentation: {
description: "Formats values according to a C-style format string.",
signatures: ["string.format(format, ...): string"],
parameters: [
@@ -110,9 +106,9 @@ export const stringApi = new LuaTable({
{ code: 'print(string.format("Name: %s, score: %.1f", "Ada", 9.5))' },
],
},
),
gmatch: new LuaBuiltinFunction(
(_sf, s: string, pattern: string, init = 1) => {
}),
gmatch: new LuaBuiltinFunction({
callback: (_sf, s: string, pattern: string, init = 1) => {
const iter = patternGmatch(s, pattern, init);
return () => {
const caps = iter();
@@ -120,8 +116,7 @@ export const stringApi = new LuaTable({
return capturesToLua(caps);
};
},
{
kind: "builtin",
documentation: {
description:
"Returns an iterator over successive Lua-pattern matches and captures.",
parameters: [
@@ -136,9 +131,9 @@ export const stringApi = new LuaTable({
},
],
},
),
gsub: new LuaBuiltinFunction(
async (sf, s: string, pattern: string, repl: any, n?: number) => {
}),
gsub: new LuaBuiltinFunction({
callback: async (sf, s: string, pattern: string, repl: any, n?: number) => {
const callbacks: GsubCallbacks = {};
if (typeof repl === "string") {
callbacks.replString = repl;
@@ -171,8 +166,7 @@ export const stringApi = new LuaTable({
const [result, count] = await patternGsub(s, pattern, callbacks, n);
return new LuaMultiRes([result, count]);
},
{
kind: "builtin",
documentation: {
description:
"Replaces Lua-pattern matches using a string, table, or function replacement.",
parameters: [
@@ -191,39 +185,38 @@ export const stringApi = new LuaTable({
},
],
},
),
len: new LuaBuiltinFunction((_sf, s: string) => s.length, {
kind: "builtin",
description: "Returns the length of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "integer" }],
}),
lower: new LuaBuiltinFunction(
(_sf, s: string) => luaToString(s.toLowerCase()),
{
kind: "builtin",
len: new LuaBuiltinFunction({
callback: (_sf, s: string) => s.length,
documentation: {
description: "Returns the length of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "integer" }],
},
}),
lower: new LuaBuiltinFunction({
callback: (_sf, s: string) => luaToString(s.toLowerCase()),
documentation: {
description: "Returns a copy of a string converted to lowercase.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
),
upper: new LuaBuiltinFunction(
(_sf, s: string) => luaToString(s.toUpperCase()),
{
kind: "builtin",
}),
upper: new LuaBuiltinFunction({
callback: (_sf, s: string) => luaToString(s.toUpperCase()),
documentation: {
description: "Returns a copy of a string converted to uppercase.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
),
match: new LuaBuiltinFunction(
(_sf, s: string, pattern: string, init = 1) => {
}),
match: new LuaBuiltinFunction({
callback: (_sf, s: string, pattern: string, init = 1) => {
const caps = patternMatch(s, pattern, init);
if (!caps) return null;
return capturesToLua(caps);
},
{
kind: "builtin",
documentation: {
description:
"Returns captures from the first Lua-pattern match, or `nil` when none is found.",
parameters: [
@@ -236,9 +229,9 @@ export const stringApi = new LuaTable({
{ code: 'local year, month = string.match("2024-03", "(%d+)%-(%d+)")' },
],
},
),
rep: new LuaBuiltinFunction(
(_sf, s: string, n: number, sep?: string) => {
}),
rep: new LuaBuiltinFunction({
callback: (_sf, s: string, n: number, sep?: string) => {
if (n <= 0) return "";
sep = sep ?? "";
const parts: string[] = [];
@@ -247,8 +240,7 @@ export const stringApi = new LuaTable({
}
return parts.join(sep);
},
{
kind: "builtin",
documentation: {
description:
"Returns `n` copies of a string joined by an optional separator.",
parameters: [
@@ -258,18 +250,17 @@ export const stringApi = new LuaTable({
],
returns: [{ type: "string" }],
},
),
reverse: new LuaBuiltinFunction(
(_sf, s: string) => s.split("").reverse().join(""),
{
kind: "builtin",
}),
reverse: new LuaBuiltinFunction({
callback: (_sf, s: string) => s.split("").reverse().join(""),
documentation: {
description: "Returns a string with its characters in reverse order.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
),
sub: new LuaBuiltinFunction(
(_sf, s: string, i: number, j?: number) => {
}),
sub: new LuaBuiltinFunction({
callback: (_sf, s: string, i: number, j?: number) => {
const len = s.length;
let start: number;
if (i > 0) {
@@ -294,8 +285,7 @@ export const stringApi = new LuaTable({
}
return "";
},
{
kind: "builtin",
documentation: {
description:
"Returns the substring from inclusive index `i` through `j`, supporting negative indices.",
parameters: [
@@ -305,22 +295,24 @@ export const stringApi = new LuaTable({
],
returns: [{ type: "string" }],
},
),
}),
split: new LuaBuiltinFunction((_sf, s: string, sep: string) => s.split(sep), {
kind: "builtin",
description:
"Splits a string on a literal separator and returns the substrings.",
parameters: [
{ name: "s", type: "string" },
{ name: "sep", type: "string" },
],
returns: [{ type: "table" }],
examples: [
{
code: 'for part in each(string.split("a,b,c", ",")) do\n print(part)\nend',
},
],
split: new LuaBuiltinFunction({
callback: (_sf, s: string, sep: string) => s.split(sep),
documentation: {
description:
"Splits a string on a literal separator and returns the substrings.",
parameters: [
{ name: "s", type: "string" },
{ name: "sep", type: "string" },
],
returns: [{ type: "table" }],
examples: [
{
code: 'for part in each(string.split("a,b,c", ",")) do\n print(part)\nend',
},
],
},
}),
pack: strPackFn,
@@ -328,10 +320,9 @@ export const stringApi = new LuaTable({
packsize: strPackSizeFn,
// Non-standard extensions
startsWith: new LuaBuiltinFunction(
(_sf, s: string, prefix: string) => s.startsWith(prefix),
{
kind: "builtin",
startsWith: new LuaBuiltinFunction({
callback: (_sf, s: string, prefix: string) => s.startsWith(prefix),
documentation: {
description: "Returns whether a string starts with a literal prefix.",
parameters: [
{ name: "s", type: "string" },
@@ -339,11 +330,10 @@ export const stringApi = new LuaTable({
],
returns: [{ type: "boolean" }],
},
),
endsWith: new LuaBuiltinFunction(
(_sf, s: string, suffix: string) => s.endsWith(suffix),
{
kind: "builtin",
}),
endsWith: new LuaBuiltinFunction({
callback: (_sf, s: string, suffix: string) => s.endsWith(suffix),
documentation: {
description: "Returns whether a string ends with a literal suffix.",
parameters: [
{ name: "s", type: "string" },
@@ -351,33 +341,38 @@ export const stringApi = new LuaTable({
],
returns: [{ type: "boolean" }],
},
),
trim: new LuaBuiltinFunction((_sf, s: string) => s.trim(), {
kind: "builtin",
description: "Removes whitespace from both ends of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
}),
trimStart: new LuaBuiltinFunction((_sf, s: string) => s.trimStart(), {
kind: "builtin",
description: "Removes whitespace from the beginning of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
trim: new LuaBuiltinFunction({
callback: (_sf, s: string) => s.trim(),
documentation: {
description: "Removes whitespace from both ends of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
}),
trimEnd: new LuaBuiltinFunction((_sf, s: string) => s.trimEnd(), {
kind: "builtin",
description: "Removes whitespace from the end of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
trimStart: new LuaBuiltinFunction({
callback: (_sf, s: string) => s.trimStart(),
documentation: {
description: "Removes whitespace from the beginning of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
}),
matchRegex: new LuaBuiltinFunction(
(_sf, s: string, pattern: string) => {
trimEnd: new LuaBuiltinFunction({
callback: (_sf, s: string) => s.trimEnd(),
documentation: {
description: "Removes whitespace from the end of a string.",
parameters: [{ name: "s", type: "string" }],
returns: [{ type: "string" }],
},
}),
matchRegex: new LuaBuiltinFunction({
callback: (_sf, s: string, pattern: string) => {
const regex = new RegExp(pattern);
const result = s.match(regex);
return jsToLuaValue(result);
},
{
kind: "builtin",
documentation: {
description:
"Matches a string with a JavaScript regular expression and returns the match array.",
parameters: [
@@ -391,9 +386,9 @@ export const stringApi = new LuaTable({
},
],
},
),
matchRegexAll: new LuaBuiltinFunction(
(_sf, s: string, pattern: string) => {
}),
matchRegexAll: new LuaBuiltinFunction({
callback: (_sf, s: string, pattern: string) => {
const regex = new RegExp(pattern, "g");
return () => {
const match = regex.exec(s);
@@ -403,8 +398,7 @@ export const stringApi = new LuaTable({
return jsToLuaValue(match);
};
},
{
kind: "builtin",
documentation: {
description:
"Returns an iterator over all JavaScript regular-expression matches.",
parameters: [
@@ -420,5 +414,5 @@ export const stringApi = new LuaTable({
},
],
},
),
}),
});