Skip to content

Commit b8abf60

Browse files
authored
Docs update (#1375)
* allow nodes outside match * ✨ Improve fetch docstrings and AST docs update logic Updated fetch function documentation for clarity and added precision to AST docs update with adjusted model and parsing logic.
1 parent 0de3085 commit b8abf60

3 files changed

Lines changed: 116 additions & 25 deletions

File tree

packages/core/src/astgrep.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ export async function astGrepFindFiles(
8181

8282
const pending: Record<string, { root: SgRoot; edits: SgEdit[] }> = {}
8383
const replace = (node: SgNode, text: string) => {
84-
if (!matches.includes(node))
85-
throw new Error("node is not included in the matches")
8684
const edit = node.replace(text)
8785
const root = node.getRoot()
8886
const rootEdits =

packages/core/src/fetch.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export type FetchType = (
2727
/**
2828
* Creates a fetch function with retry logic.
2929
*
30-
* This function wraps the `crossFetch` with retry capabilities based
31-
* on provided options. It allows configuring the number of retries,
32-
* delay between retries, and specific HTTP status codes to retry on.
30+
* Wraps `crossFetch` with retry capabilities based on provided options.
31+
* Configures the number of retries, delay between retries, and specific HTTP status codes to retry on.
32+
* Supports cancellation and proxy configuration.
3333
*
34-
* @param options - Options for retry configuration and tracing.
35-
* @returns A fetch function with retry capabilities.
34+
* @param options - Configuration for retries, delays, status codes, cancellation, and tracing.
35+
* @returns A fetch function with retry functionality.
3636
*/
3737
export async function createFetch(
3838
options?: {
@@ -118,12 +118,12 @@ export async function fetch(
118118
/**
119119
* Fetches text content from a URL or file.
120120
*
121-
* This function attempts to fetch content from either a URL or a local file.
122-
* It supports HTTP(S) URLs and reads directly from the file system for local files.
121+
* Attempts to fetch content from an HTTP(S) URL or read from the file system for local files.
122+
* Handles retries, delays, and tracing if configured. Supports binary content via base64 encoding.
123123
*
124-
* @param urlOrFile - The URL or file to fetch from.
125-
* @param fetchOptions - Optional fetch configuration.
126-
* @returns An object containing fetch status and content.
124+
* @param urlOrFile - URL or file path to fetch from.
125+
* @param fetchOptions - Optional configuration for retries, delays, and other fetch settings.
126+
* @returns Object containing fetch status, content, and metadata.
127127
*/
128128
export async function fetchText(
129129
urlOrFile: string | WorkspaceFile,
@@ -195,16 +195,17 @@ export async function fetchText(
195195
}
196196

197197
/**
198-
* Logs a POST request for tracing.
198+
* Logs a POST request for tracing purposes.
199199
*
200-
* Constructs a curl command representing the POST request with appropriate headers
201-
* and body, optionally masking sensitive information like authorization headers.
200+
* Constructs a curl command to represent the POST request, including headers
201+
* and body. Sensitive information in authorization headers can be masked
202+
* based on the provided options.
202203
*
203-
* @param trace - Markdown trace object for logging.
204-
* @param url - The URL of the request.
205-
* @param headers - The request headers.
206-
* @param body - The request body.
207-
* @param options - Options for displaying authorization header.
204+
* @param trace - Object used for logging trace details.
205+
* @param url - The target URL of the request.
206+
* @param headers - The headers to include in the request.
207+
* @param body - The request body, either as FormData or a raw object.
208+
* @param options - Configuration options, including whether to mask authorization headers.
208209
*/
209210
export function traceFetchPost(
210211
trace: MarkdownTrace,
@@ -245,13 +246,13 @@ ${Object.entries(headers)
245246
}
246247

247248
/**
248-
* Converts a response status to a message.
249+
* Converts the HTTP response status and status text into a string list.
249250
*
250-
* Converts the HTTP response status and status text into a string list
251-
* to facilitate logging and debugging.
251+
* Facilitates logging and debugging by converting the status information
252+
* from the response object.
252253
*
253-
* @param res - The response object.
254-
* @returns A list of status and status text.
254+
* @param res - The HTTP response object containing status and statusText.
255+
* @returns A string list with status and status text.
255256
*/
256257
export function statusToMessage(res?: {
257258
status?: number
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
script({
2+
title: "Generate TypeScript function documentation using AST insertion",
3+
accept: ".ts",
4+
files: "src/cowsay.ts",
5+
parameters: {
6+
applyEdits: {
7+
type: "boolean",
8+
default: false,
9+
description: "If true, the script will not modify the files.",
10+
},
11+
},
12+
})
13+
const { output } = env
14+
const { applyEdits } = env.vars
15+
const file = env.files[0]
16+
17+
// find all exported functions with comments
18+
const sg = await host.astGrep()
19+
const { matches, replace, commitEdits } = await sg.search("ts", file.filename, {
20+
rule: {
21+
kind: "export_statement",
22+
follows: {
23+
kind: "comment",
24+
stopBy: "neighbor",
25+
},
26+
has: {
27+
kind: "function_declaration",
28+
},
29+
},
30+
})
31+
32+
// for each match, generate a docstring for functions not documented
33+
for (const match of matches) {
34+
const comment = match.prev()
35+
36+
const res = await runPrompt(
37+
(_) => {
38+
_.def("FILE", match.getRoot().root().text(), { flex: 1 })
39+
_.def("DOCSTRING", comment.text(), { flex: 10 })
40+
_.def("FUNCTION", match.text(), { flex: 10 })
41+
// this needs more eval-ing
42+
_.$`Update the docstring <DOCSTRING> of function <FUNCTION>.
43+
- If the docstring is up to date, return /NOP/.
44+
- Make sure parameters are documented.
45+
- Be concise. Use technical tone.
46+
- do NOT include types, this is for TypeScript.
47+
- Use docstring syntax. do not wrap in markdown code section.
48+
- Minimize updates to the existing docstring.
49+
50+
The full source of the file is in <FILE> for reference.
51+
The source of the function is in <FUNCTION>.
52+
The current docstring is <DOCSTRING>.
53+
`
54+
},
55+
{
56+
model: "large",
57+
responseType: "text",
58+
flexTokens: 12000,
59+
label: match.child(0).text(),
60+
}
61+
)
62+
// if generation is successful, insert the docs
63+
if (res.error) {
64+
output.warn(res.error.message)
65+
continue
66+
}
67+
68+
if (res.text.includes("/NOP/")) continue
69+
70+
const docs = docify(
71+
parsers.unfence(res.text.trim(), ["", "typescript", "ts"])
72+
)
73+
replace(comment, docs)
74+
}
75+
76+
// apply all edits and write to the file
77+
const modified = await commitEdits()
78+
if (applyEdits) {
79+
await workspace.writeFiles(modified)
80+
} else if (modified.length) {
81+
output.diff(file, modified[0])
82+
output.warn(
83+
`edit not applied, use --vars 'applyEdits=true' to apply the edits`
84+
)
85+
}
86+
87+
// normalizes the docstring in case the LLM decides not to generate proper comments
88+
function docify(docs: string) {
89+
if (!/^\/\*\*.*.*\*\/$/s.test(docs))
90+
docs = `/**\n* ${docs.split(/\r?\n/g).join("\n* ")}\n*/`
91+
return docs.replace(/\n+$/, "")
92+
}

0 commit comments

Comments
 (0)