Skip to content

Commit c1a79d7

Browse files
authored
Run eslint + prettier in CI, fix existing errors for both (#48)
* Always run eslint in CI * Fix lint errors, add Prettier to CI, run prettier over project too
1 parent 19e8772 commit c1a79d7

20 files changed

+279
-231
lines changed

.eslintrc.json

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
{
2-
"root": true,
3-
"parser": "@typescript-eslint/parser",
4-
"plugins": [
5-
"@typescript-eslint",
6-
"prettier"
7-
],
8-
"extends": [
9-
"standard-with-typescript",
10-
"prettier"
11-
],
12-
"parserOptions": {
13-
"project": "./tsconfig.json",
14-
"ecmaFeatures": {
15-
"modules": true
16-
},
17-
"ecmaVersion": 6,
18-
"sourceType": "module"
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": ["@typescript-eslint", "prettier"],
5+
"extends": ["standard-with-typescript", "prettier", "prettier/@typescript-eslint", "eslint-config-prettier"],
6+
"parserOptions": {
7+
"project": "./tsconfig.json",
8+
"ecmaFeatures": {
9+
"modules": true
1910
},
20-
"rules": {
21-
"@typescript-eslint/strict-boolean-expressions": "off",
22-
"@typescript-eslint/consistent-type-assertions": "warn"
23-
}
24-
}
11+
"ecmaVersion": 6,
12+
"sourceType": "module"
13+
},
14+
"rules": {
15+
"@typescript-eslint/strict-boolean-expressions": "off",
16+
"@typescript-eslint/consistent-type-assertions": "warn"
17+
}
18+
}

.github/workflows/node.js.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ jobs:
2626
node-version: ${{ matrix.node-version }}
2727
- run: npm i -g yarn
2828
- run: yarn
29+
- run: yarn lint
30+
- run: yarn prettier
2931
- run: yarn build
3032
- run: yarn test

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ yarn-error.log*
2525
.nyc_output
2626

2727
# Ignore npm lockfile (we're using yarn)
28-
package-lock.json
28+
package-lock.json
29+
.vscode/settings.json

.prettierignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Ignore artifacts:
2+
build
3+
coverage
4+
.nyc_output
5+
bin
6+
7+
# Types
8+
src/types/
9+
src/types.ts

.prettierrc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true
4+
}

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changelog
22

3+
This changelog is currently manually maintained. Please see commits, PRs and Issue discussions on GitHub for the latest and most correction information.
4+
35
### 5.0.0
46

57
- Updated `calculateComplexity` signature, file extension and typescript/flow flags added

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"lint": "eslint src/*.**",
99
"lint:fix": "eslint --fix src/*.**",
1010
"build": "tsc",
11-
"build:watch": "tsc --watch"
11+
"build:watch": "tsc --watch",
12+
"prettier": "prettier --check src",
13+
"prettier:fix": "prettier --write src"
1214
},
1315
"author": "Sam Brown (https://github.com/sgb-io)",
1416
"repository": {
@@ -37,7 +39,7 @@
3739
"@typescript-eslint/eslint-plugin": "3",
3840
"@typescript-eslint/parser": "^3.3.0",
3941
"eslint": "7",
40-
"eslint-config-prettier": "^6.11.0",
42+
"eslint-config-prettier": "^7.0.0",
4143
"eslint-config-standard-with-typescript": "^18.0.2",
4244
"eslint-plugin-import": "2",
4345
"eslint-plugin-node": "11",

src/analyze.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
CoverageMapping,
66
FileWithContents,
77
CompleteCodehawkComplexityResult,
8-
CodehawkComplexityResult
8+
CodehawkComplexityResult,
99
} from './types'
1010

1111
export const transpileFileSource = (
@@ -22,16 +22,16 @@ export const transpileFileSource = (
2222
[
2323
'@babel/plugin-transform-typescript',
2424
{
25-
isTSX: fileExtension === '.tsx'
26-
}
27-
]
25+
isTSX: fileExtension === '.tsx',
26+
},
27+
],
2828
],
2929
})
3030
contents = transformed.code || ''
3131
} else {
3232
// Assume no other static type systems exist
3333
// Stripping flow types should be safe, even if it's not strictly flow
34-
contents = (enableFlow)
34+
contents = enableFlow
3535
? flowRemoveTypes(contents, { pretty: true }).toString()
3636
: contents
3737
}
@@ -46,12 +46,7 @@ export const calculateComplexity = (
4646
enableFlow: boolean
4747
): CodehawkComplexityResult => {
4848
return escomplexReporter(
49-
transpileFileSource(
50-
sourceCode,
51-
fileExtension,
52-
isTypescript,
53-
enableFlow
54-
)
49+
transpileFileSource(sourceCode, fileExtension, isTypescript, enableFlow)
5550
)
5651
}
5752

@@ -62,24 +57,31 @@ export const analyzeFile = (
6257
): CompleteCodehawkComplexityResult => {
6358
let report = null
6459
const relativeFilePath = `${file.path}/${file.filename}`.replace(dirPath, '')
65-
const coverageData = projectCoverage.find(c => c.path === relativeFilePath)
60+
const coverageData = projectCoverage.find((c) => c.path === relativeFilePath)
6661

6762
let fileCoverage = '0'
6863

6964
if (coverageData) {
7065
// Coverage can have a bug where 0 things have 100% coverage
71-
const linesPct = coverageData.coverage.lines.total === 0 ? 0 : coverageData.coverage.lines.pct
72-
const fnPct = coverageData.coverage.functions.total === 0 ? 0 : coverageData.coverage.functions.pct
73-
const stmntPct = coverageData.coverage.statements.total === 0 ? 0 : coverageData.coverage.statements.pct
74-
const branchPct = coverageData.coverage.branches.total === 0 ? 0 : coverageData.coverage.branches.pct
66+
const linesPct =
67+
coverageData.coverage.lines.total === 0
68+
? 0
69+
: coverageData.coverage.lines.pct
70+
const fnPct =
71+
coverageData.coverage.functions.total === 0
72+
? 0
73+
: coverageData.coverage.functions.pct
74+
const stmntPct =
75+
coverageData.coverage.statements.total === 0
76+
? 0
77+
: coverageData.coverage.statements.pct
78+
const branchPct =
79+
coverageData.coverage.branches.total === 0
80+
? 0
81+
: coverageData.coverage.branches.pct
7582

7683
// Average the four coverage types.
77-
fileCoverage = ((
78-
linesPct +
79-
fnPct +
80-
stmntPct +
81-
branchPct
82-
) / 4).toFixed(2)
84+
fileCoverage = ((linesPct + fnPct + stmntPct + branchPct) / 4).toFixed(2)
8385
}
8486

8587
const trimmed = file.rawSource.trim()

src/cli-util.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FullyAnalyzedFile } from "./types"
1+
import { FullyAnalyzedFile } from './types'
22

33
const colLengths = {
44
filename: 30,
@@ -9,17 +9,16 @@ const colLengths = {
99

1010
const round = (num: number): number => Math.round(num * 100) / 100
1111

12-
const formatCol = (value: string, limit: number) => {
13-
return (value.length > limit)
14-
? value.slice(limit + 3).padStart(limit, '.')
15-
: value.padEnd(limit, ' ')
12+
const formatCol = (value: string, limit: number): string => {
13+
return value.length > limit
14+
? value.slice(limit + 3).padStart(limit, '.')
15+
: value.padEnd(limit, ' ')
1616
}
1717

1818
const getScoreNote = (score: number): string => {
1919
if (score > 60) return 'OK'
2020

2121
if (score > 50) return '(Could be better)'
22-
2322
return '(Needs improvement)'
2423
}
2524

@@ -31,31 +30,36 @@ const formatComplexityScore = (score: number): string => {
3130
}
3231

3332
const generateTableLines = (flatFileResults: FullyAnalyzedFile[]): string => {
34-
return flatFileResults.map((result) => {
35-
const { complexityReport, filename, timesDependedOn } = result
33+
return flatFileResults
34+
.map((result) => {
35+
const { complexityReport, filename, timesDependedOn } = result
3636

37-
if (!complexityReport) {
38-
return ''
39-
}
37+
if (!complexityReport) {
38+
return ''
39+
}
4040

41-
const { lineEnd, codehawkScore } = complexityReport
42-
const score = formatComplexityScore(codehawkScore)
41+
const { lineEnd, codehawkScore } = complexityReport
42+
const score = formatComplexityScore(codehawkScore)
4343

44-
// Convert output into stdout-friendly, padded columns
45-
const filenameCol = formatCol(filename, colLengths.filename)
46-
const linesCol = formatCol(lineEnd.toString(), colLengths.lines)
47-
// Add 1 to the times depended on, assuming that all files are used at least once
48-
// (Codehawk reports external uses only)
49-
const depsCol = formatCol((timesDependedOn + 1).toString(), colLengths.timesUsed)
50-
const maintainabilityCol = formatCol(score, colLengths.maintainability)
44+
// Convert output into stdout-friendly, padded columns
45+
const filenameCol = formatCol(filename, colLengths.filename)
46+
const linesCol = formatCol(lineEnd.toString(), colLengths.lines)
47+
// Add 1 to the times depended on, assuming that all files are used at least once
48+
// (Codehawk reports external uses only)
49+
const depsCol = formatCol(
50+
(timesDependedOn + 1).toString(),
51+
colLengths.timesUsed
52+
)
53+
const maintainabilityCol = formatCol(score, colLengths.maintainability)
5154

52-
return (
53-
`| ${filenameCol} | ${linesCol} | ${depsCol} | ${maintainabilityCol} |`
54-
)
55-
}).join('\n ') // Newline + 4 spaces
55+
return `| ${filenameCol} | ${linesCol} | ${depsCol} | ${maintainabilityCol} |`
56+
})
57+
.join('\n ') // Newline + 4 spaces
5658
}
5759

58-
export const formatResultsAsTable = (flatFileResults: FullyAnalyzedFile[]): string => {
60+
export const formatResultsAsTable = (
61+
flatFileResults: FullyAnalyzedFile[]
62+
): string => {
5963
return `
6064
Codehawk Static Analysis Results
6165
Top ${flatFileResults.length} file${flatFileResults.length > 1 ? 's' : ''}

0 commit comments

Comments
 (0)