Skip to content

Commit a765a41

Browse files
minor changes/fixes
1 parent fabc074 commit a765a41

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

src/convertResults.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ESLint } from "eslint"
2-
import { flatMap } from "lodash"
32

43
import { CodacyResult, LineComplexity } from "./model/codacyResult"
54

@@ -8,21 +7,21 @@ const ComplexityRegex = /.*has a complexity of (\d+).*/
87
export function convertResults(eslintResults: ESLint.LintResult[]): CodacyResult[] {
98
const convertedResults: CodacyResult[] = []
109

11-
eslintResults.forEach((r) => {
12-
const filename = r.filePath
10+
eslintResults.forEach((result) => {
11+
const filename = result.filePath
1312
const lineComplexities: LineComplexity[] = []
1413

15-
r.messages
16-
.filter((m) =>
17-
m.ruleId === "complexity"
18-
&& m.message?.match(ComplexityRegex)?.length === 2
14+
result.messages
15+
.filter((message) =>
16+
message.ruleId === "complexity"
17+
&& message.message?.match(ComplexityRegex)?.length === 2
1918
)
20-
.forEach((m) => {
21-
const complexityMatch = m.message.match(ComplexityRegex)
19+
.forEach((message) => {
20+
const complexityMatch = message.message.match(ComplexityRegex)
2221

23-
if (complexityMatch) {
24-
lineComplexities.push(new LineComplexity(m.line, parseInt(complexityMatch[1], 10)))
25-
}
22+
if (complexityMatch === null) return
23+
24+
lineComplexities.push(new LineComplexity(message.line, parseInt(complexityMatch[1], 10)))
2625
})
2726

2827
const complexity = lineComplexities.reduce((acc, lc) => Math.max(acc, lc.value), 0)

src/eslintDefaultOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const defaultOptions: ESLint.Options = {
2121
parserOptions: {
2222
sourceType: "script",
2323
allowAutomaticSingleRunInference: true,
24-
ecmaVersion: 12,
24+
ecmaVersion: 2022,
2525
errorOnTypeScriptSyntacticAndSemanticIssues: false,
2626
},
2727
root: true,

src/fileUtils.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { readFile, readdir } from "fs/promises";
2-
import path from "path";
1+
import { readFile, readdir } from "fs/promises"
2+
import path from "path"
33

4-
import { Codacyrc } from "./model/codacyInput";
4+
import { Codacyrc } from "./model/codacyInput"
55

66
export async function readCodacyrcFile(file: string): Promise<Codacyrc | undefined> {
77
try {
8-
return parseCodacyrcFile(await readFile(file, { encoding: "utf8" }))
8+
const content = await readFile(file, { encoding: 'utf8' })
9+
10+
return parseCodacyrcFile(content)
911
} catch (e) {
1012
console.error(`Error reading ${file} file`)
1113
return undefined
@@ -18,11 +20,13 @@ export function parseCodacyrcFile(content: string): Codacyrc {
1820

1921
export async function allFilesNames(dir: string): Promise<string[]> {
2022
try {
21-
return (await readdir(dir, { withFileTypes: true, recursive: true }))
23+
const entries = await readdir(dir, { withFileTypes: true, recursive: true })
24+
25+
return entries
2226
.filter((entry) => entry.isFile() || entry.isSymbolicLink())
2327
.map((entry) => path.relative(dir, entry.name))
2428
} catch (error) {
25-
console.error(`Error reading directory ${dir}`);
29+
console.error(`Error reading directory ${dir}`)
2630
return []
2731
}
2832
}

src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { convertResults } from "./convertResults"
44
import { createEslintConfig } from "./configCreator"
55
import { parseTimeoutSeconds } from "./parseTimeoutSeconds"
66

7+
const srcDirPath = "/src"
8+
const timeoutSeconds = parseTimeoutSeconds(process.env.TIMEOUT_SECONDS)
9+
710
const timeoutHandle = setTimeout(() => {
811
console.error("Timeout occurred. Exiting.")
912
process.exit(2)
10-
}, parseTimeoutSeconds(process.env.TIMEOUT_SECONDS) * 1000)
13+
}, timeoutSeconds * 1000)
1114

1215
async function run() {
13-
const srcDirPath = "/src"
1416
const [options, files] = await createEslintConfig(srcDirPath)
15-
1617
const eslint = new ESLint(options)
1718
const eslintResults = await eslint.lintFiles(files)
1819

@@ -24,8 +25,8 @@ async function run() {
2425
}
2526

2627
run()
27-
.catch(e => {
28-
console.error(e)
28+
.catch((error) => {
29+
console.error(error)
2930
process.exit(1)
3031
})
3132
.finally(() => clearTimeout(timeoutHandle))

src/parseTimeoutSeconds.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export const defaultTimeout = 15 * 60
22

33
export function parseTimeoutSeconds(timeoutString?: string): number {
4-
if (!timeoutString) return defaultTimeout
5-
const number = parseInt(timeoutString)
6-
if (isNaN(number) || number < 0) return defaultTimeout
7-
return number
4+
return Math.max(
5+
(parseInt(timeoutString, 10) || defaultTimeout),
6+
0
7+
)
88
}

0 commit comments

Comments
 (0)