Skip to content

Commit 1744319

Browse files
Shared tsconfig.json & update options (#29)
* Update TypeScript configuration from leanix-sandbox:mob/main * Fix optional chaining in hunk reader and enforce non-null assertions in main * Update TypeScript configuration from leanix-sandbox:mob/main * Enforce non-null assertions for actionYaml inputs and regex match groups * Use shared tsconfig * Fix optional chaining for actionYaml input defaults in development environment * Add composite option to tsconfig files and update references * Remove composite option from pr-review and pr-summary tsconfig files; add it to the main tsconfig * Add "$schema" property to pr-review and pr-summary tsconfig files
1 parent 73531d1 commit 1744319

File tree

8 files changed

+72
-67
lines changed

8 files changed

+72
-67
lines changed

pr-review/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if (process.env.NODE_ENV === "development") {
1717
// eslint-disable-next-line @typescript-eslint/no-for-in-array, no-restricted-syntax, guard-for-in
1818
for (const key in actionYaml.inputs) {
1919
const envKey = `INPUT_${key.toUpperCase()}`
20-
const envValue = actionYaml.inputs[key].default
20+
const envValue = actionYaml.inputs[key]?.default
2121
if (envValue && !Object.keys(process.env).includes(envKey)) {
2222
process.env[envKey] = envValue
2323
}

pr-review/src/hunk-reader.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,50 @@ export function resolveHunkReferencesInComments(comments: AiReview["comments"],
3434
core.warning(`Could not find file for comment on ${comment.path}, start ${comment.start}, end ${comment.end}, ${comment.comment}, skipping.`)
3535
} else {
3636
const hunkChangeMap = currentFile.chunks.flatMap(hunk => hunk.changes.map(change => ({ change, hunk })))
37-
let { change: startChange, hunk: startHunk } = hunkChangeMap[comment.start - 1] // eslint-disable-line prefer-const
38-
let { change: endChange, hunk: endHunk } = hunkChangeMap[comment.end - 1] // eslint-disable-line prefer-const
37+
const startEntry = hunkChangeMap[comment.start - 1]
38+
const endEntry = hunkChangeMap[comment.end - 1]
3939

40-
if (!startHunk) {
41-
core.warning(`Could not find hunk for comment on ${comment.path}, start ${comment.start}, end ${comment.end}, ${comment.comment}, skipping.`)
40+
if (!startEntry || !endEntry) {
41+
core.error(`Could not find hunk for comment on ${comment.path}, start ${comment.start}, end ${comment.end}, ${comment.comment}, skipping.`)
4242
} else {
43-
if (startHunk !== endHunk) endChange = startHunk.changes.at(-1)!
43+
const { change: startChange, hunk: startHunk } = startEntry
44+
let { change: endChange, hunk: endHunk } = endEntry // eslint-disable-line prefer-const
4445

45-
const startSide = startChange.type !== "del" ? "RIGHT" : "LEFT"
46-
const endSide = endChange.type !== "del" ? "RIGHT" : "LEFT"
46+
if (!startHunk) {
47+
core.warning(`Could not find hunk for comment on ${comment.path}, start ${comment.start}, end ${comment.end}, ${comment.comment}, skipping.`)
48+
} else {
49+
if (startHunk !== endHunk) endChange = startHunk.changes.at(-1)!
4750

48-
// get start line of the actual comment
49-
let start: number
50-
if (startChange.type === "normal") {
51-
start = startChange.ln2
52-
} else if (startChange.type === "add" || startChange.type === "del") {
53-
start = startChange.ln
54-
} else throw new Error(`Unknown change type.`)
51+
const startSide = startChange.type !== "del" ? "RIGHT" : "LEFT"
52+
const endSide = endChange.type !== "del" ? "RIGHT" : "LEFT"
5553

56-
// get end line of the actual comment
57-
let end: number
58-
if (endChange.type === "normal") {
59-
end = endChange.ln2
60-
} else if (endChange.type === "add" || endChange.type === "del") {
61-
end = endChange.ln
62-
} else throw new Error(`Unknown change type.`)
63-
// make sure start and end are within the hunk
64-
end = Math.min(end, endSide === "RIGHT" ? startHunk.newStart + startHunk.newLines - 1 : startHunk.oldStart + startHunk.oldLines - 1)
54+
// get start line of the actual comment
55+
let start: number
56+
if (startChange.type === "normal") {
57+
start = startChange.ln2
58+
} else if (startChange.type === "add" || startChange.type === "del") {
59+
start = startChange.ln
60+
} else throw new Error(`Unknown change type.`)
6561

66-
result.push({
67-
path: comment.path,
68-
start_side: startSide !== endSide ? startSide : undefined, // only set start_side if it is a multi-line comment
69-
side: startSide !== endSide ? endSide : startSide,
70-
start_line: start !== end && start < end ? start : undefined, // only set start_line if it is a multi-line comment, start must be less than end
71-
line: start !== end && start < end ? end : start,
72-
body: comment.comment,
73-
})
62+
// get end line of the actual comment
63+
let end: number
64+
if (endChange.type === "normal") {
65+
end = endChange.ln2
66+
} else if (endChange.type === "add" || endChange.type === "del") {
67+
end = endChange.ln
68+
} else throw new Error(`Unknown change type.`)
69+
// make sure start and end are within the hunk
70+
end = Math.min(end, endSide === "RIGHT" ? startHunk.newStart + startHunk.newLines - 1 : startHunk.oldStart + startHunk.oldLines - 1)
71+
72+
result.push({
73+
path: comment.path,
74+
start_side: startSide !== endSide ? startSide : undefined, // only set start_side if it is a multi-line comment
75+
side: startSide !== endSide ? endSide : startSide,
76+
start_line: start !== end && start < end ? start : undefined, // only set start_line if it is a multi-line comment, start must be less than end
77+
line: start !== end && start < end ? end : start,
78+
body: comment.comment,
79+
})
80+
}
7481
}
7582
}
7683
})

pr-review/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export async function run(config: Config): Promise<void> {
4343
const regex = new RegExp(`^${markerStart}\\s+<!-- (?<base>\\w+)\\.\\.\\.(?<head>\\w+) -->([\\s\\S]*?)${markerEnd}$`, "g")
4444
previousReviews.forEach(comment => {
4545
;[...(comment.body?.matchAll(regex) ?? [])].forEach(match => {
46-
const commentBase = match.groups!.base
47-
const commentHead = match.groups!.head
46+
const commentBase = match.groups!.base!
47+
const commentHead = match.groups!.head!
4848
if (commentHead) base = commentHead !== head ? commentHead : commentBase
4949
})
5050
})

pr-review/tsconfig.json

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
{
22
"$schema": "https://json.schemastore.org/tsconfig",
3+
"extends": "../tsconfig.json",
34
"compilerOptions": {
4-
"target": "ES2022",
5-
"module": "NodeNext",
6-
"rootDir": "./src",
7-
"moduleResolution": "NodeNext",
8-
"baseUrl": "./",
9-
"sourceMap": true,
10-
"outDir": "./dist",
11-
"noImplicitAny": true,
12-
"esModuleInterop": true,
13-
"forceConsistentCasingInFileNames": true,
14-
"strict": true,
15-
"skipLibCheck": true,
16-
"newLine": "lf"
17-
},
18-
"exclude": ["./dist", "./node_modules", "./__tests__", "./coverage"]
5+
"rootDir": "./src"
6+
}
197
}

pr-summary/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if (process.env.NODE_ENV === "development") {
1717
// eslint-disable-next-line @typescript-eslint/no-for-in-array, no-restricted-syntax, guard-for-in
1818
for (const key in actionYaml.inputs) {
1919
const envKey = `INPUT_${key.toUpperCase()}`
20-
const envValue = actionYaml.inputs[key].default
20+
const envValue = actionYaml.inputs[key]?.default
2121
if (envValue && !Object.keys(process.env).includes(envKey)) {
2222
process.env[envKey] = envValue
2323
}

pr-summary/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export async function run(config: Config): Promise<void> {
4040
const regex = new RegExp(`^${markerStart}\\s+<!-- (?<base>\\w+)\\.\\.\\.(?<head>\\w+) -->([\\s\\S]*?)${markerEnd}$`, "g")
4141
comments.forEach(comment => {
4242
;[...(comment.body?.matchAll(regex) ?? [])].forEach(match => {
43-
const commentBase = match.groups!.base
44-
const commentHead = match.groups!.head
43+
const commentBase = match.groups!.base!
44+
const commentHead = match.groups!.head!
4545
if (commentHead) base = commentHead !== head ? commentHead : commentBase
4646
})
4747
})

pr-summary/tsconfig.json

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
{
22
"$schema": "https://json.schemastore.org/tsconfig",
3+
"extends": "../tsconfig.json",
34
"compilerOptions": {
4-
"target": "ES2022",
5-
"module": "NodeNext",
6-
"rootDir": "./src",
7-
"moduleResolution": "NodeNext",
8-
"baseUrl": "./",
9-
"sourceMap": true,
10-
"outDir": "./dist",
11-
"noImplicitAny": true,
12-
"esModuleInterop": true,
13-
"forceConsistentCasingInFileNames": true,
14-
"strict": true,
15-
"skipLibCheck": true,
16-
"newLine": "lf"
17-
},
18-
"exclude": ["./dist", "./node_modules"]
5+
"rootDir": "./src"
6+
}
197
}

tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"$schema": "https://json.schemastore.org/tsconfig",
3+
"compilerOptions": {
4+
"composite": true,
5+
"target": "esnext",
6+
"module": "nodenext",
7+
"moduleResolution": "nodenext",
8+
"baseUrl": "./",
9+
"sourceMap": true,
10+
"outDir": "./dist",
11+
"noImplicitAny": true,
12+
"esModuleInterop": true,
13+
"forceConsistentCasingInFileNames": true,
14+
"noUncheckedIndexedAccess": true,
15+
"strict": true,
16+
"skipLibCheck": true,
17+
"newLine": "lf",
18+
"erasableSyntaxOnly": true,
19+
"rewriteRelativeImportExtensions": true
20+
},
21+
"exclude": ["./dist", "./node_modules", "./__tests__", "./coverage"]
22+
}

0 commit comments

Comments
 (0)