-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): add checkForThrowingPlainErrors; add support for ignore paths…
…; fix bug where path was never forwarded to final check
- Loading branch information
1 parent
1564692
commit 4688474
Showing
13 changed files
with
199 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { CheckResult, Severity } from '../interface/Check'; | ||
|
||
import { calculatePass } from '../application/calculatePass'; | ||
|
||
import { logDefaultPathMessage } from '../utils/logDefaultPathMessage'; | ||
import { getAllFiles } from '../utils/getAllFiles'; | ||
import { readFile } from '../utils/readFile'; | ||
import { filterFiles } from '../utils/filterFiles'; | ||
|
||
/** | ||
* @description Checks if plain (non-custom) errors are thrown. | ||
*/ | ||
export function checkForThrowingPlainErrors( | ||
severity: Severity, | ||
basePath: string, | ||
customPath?: string, | ||
ignorePaths?: string[] | ||
): CheckResult { | ||
const path = customPath || 'src'; | ||
const name = 'Error handling'; | ||
const message = 'Check for presence of plain (non-custom) errors'; | ||
|
||
if (!customPath) logDefaultPathMessage(name, path); | ||
|
||
const files = getAllFiles(`${basePath}/${path}`, []); | ||
const filteredFiles = ignorePaths ? filterFiles(files, ignorePaths) : files; | ||
|
||
const regex = /(throw Error|throw new Error)(.*)/gi; | ||
const includesError = filteredFiles.map((test: string) => regex.test(readFile(test))); | ||
|
||
const result = !includesError.includes(true); | ||
|
||
return { | ||
name, | ||
status: calculatePass(result, severity), | ||
message, | ||
path | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* @description Filters out file paths based on provided ignore paths. | ||
*/ | ||
export function filterFiles(files: string[], ignorePaths: string[]) { | ||
return files.filter((file: string) => | ||
ignorePaths.every((ignorePath: string) => { | ||
const localFilePath = file.replace(process.cwd(), ''); // This is so we don't catch on other file paths in the system | ||
return !localFilePath.includes(ignorePath); | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import test from 'ava'; | ||
|
||
import { createNewStandardLint } from '../../src/domain/StandardLint'; | ||
|
||
test('It should pass when not finding any plain errors being thrown', (t) => { | ||
const expected = 'pass'; | ||
|
||
const standardlint = createNewStandardLint({ | ||
basePath: '', | ||
checks: [ | ||
{ | ||
name: 'checkForThrowingPlainErrors', | ||
path: 'tests' | ||
} | ||
] | ||
}); | ||
const result = standardlint.check().results?.[0]?.status; | ||
|
||
t.deepEqual(result, expected); | ||
}); | ||
|
||
/** | ||
* NEGATIVE TESTS | ||
*/ | ||
|
||
test('It should fail if it finds a plain error being thrown', (t) => { | ||
const expected = 'fail'; | ||
|
||
const standardlint = createNewStandardLint({ | ||
checks: ['checkForThrowingPlainErrors'] | ||
}); | ||
const result = standardlint.check().results?.[0]?.status; | ||
|
||
t.deepEqual(result, expected); | ||
}); |
Oops, something went wrong.