Skip to content

Commit

Permalink
Use stricter ESLint config
Browse files Browse the repository at this point in the history
  • Loading branch information
lautis committed Mar 27, 2024
1 parent a875d67 commit 7bfdc04
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
12 changes: 8 additions & 4 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import tseslint from 'typescript-eslint';
export default tseslint.config(
// Global ignores
{
ignores: ['dist/*', 'eslint.config.js', 'jest.config.js']
ignores: ['dist/*']
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs.stylisticTypeChecked,
...tseslint.configs.strictTypeChecked,
{
languageOptions: {
parserOptions: {
Expand All @@ -17,7 +20,7 @@ export default tseslint.config(
}
},
{
files: ['{src,test,test-fixture}/**/*.{ts,tsx,cts,mts}', 'test.ts'],
files: ['{src,test,test-fixture}/**/*.{ts,tsx,cts,mts}'],
rules: {
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
Expand All @@ -26,11 +29,12 @@ export default tseslint.config(
}
},
{
files: ['test-fixture/**/*.{ts,tsx,cts,mts}', 'test-fixture/jest.config.js', 'test.ts'],
files: ['test-fixture/**/*.{ts,tsx,cts,mts,js}'],
rules: {
'no-undef': 'off',
'no-constant-condition': 'off',
'@typescript-eslint/no-explicit-any': 'off'
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unnecessary-condition': 'off'
}
}
);
33 changes: 21 additions & 12 deletions src/covered.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { default as cobertura } from '@cvrg-report/cobertura-json';

export type CoverageFormat = 'lcov' | 'istanbul' | 'cobertura';

type Opts = {
interface Opts {
base: string;
coverage: string;
head: string;
coverageFormat: CoverageFormat;
};
}

export type Lines = Range;
export type Path = string;
Expand All @@ -34,20 +34,25 @@ async function runGit(command: string): Promise<string> {
ok(collected.join(''));
});
run.on('error', (err: Error) => {
fail(`got error from rungit "${err.message}" ${err.stack}
fail(
new Error(
`got error from rungit "${err.message}" ${err.stack}
stderr:
${errors.join('')}`);
${errors.join('')}`,
{ cause: err }
)
);
});
});
}
async function changedLines(opts: Opts): Promise<Record<Path, Lines>> {
const stdout = await runGit(`git diff -w -U0 ${opts.base}...${opts.head}`);
const result: Record<Path, Lines> = {};
const result: Record<Path, Lines | undefined> = {};
let currentFile: Path | null = null;
for (const line of stdout.split('\n')) {
const fileHeader = line.match(/^\+\+\+ b\/(.*)/);
if (fileHeader && fileHeader[1]) {
if (fileHeader?.[1]) {
currentFile = fileHeader[1];
result[currentFile] = new Range();
continue;
Expand All @@ -71,8 +76,8 @@ async function changedLines(opts: Opts): Promise<Record<Path, Lines>> {
}

export function uncovered(args: {
coverage: Record<Path, Hits>;
changes: Record<Path, Lines>;
coverage: Record<Path, Hits | undefined>;
changes: Record<Path, Lines | undefined>;
}): Result {
const result: Record<Path, Lines> = {};
let coveredChanges = 0;
Expand Down Expand Up @@ -119,11 +124,11 @@ export function uncovered(args: {
};
}

type Hits = Array<{
type Hits = {
hits: number;
start: number;
end: number;
}>;
}[];
async function coveredLines(opts: Opts): Promise<Record<Path, Hits>> {
switch (opts.coverageFormat) {
case 'lcov':
Expand Down Expand Up @@ -190,7 +195,11 @@ async function coberturaCoveredLines(opts: Opts): Promise<Record<Path, Hits>> {
);
}

export type Result = { covered: number; total: number; uncoveredLines: Record<Path, Range> };
export interface Result {
covered: number;
total: number;
uncoveredLines: Record<Path, Range>;
}
async function uncoveredLines(opts: Opts): Promise<Result> {
const coverage = await coveredLines(opts);
const changes = await changedLines(opts);
Expand All @@ -200,7 +209,7 @@ async function uncoveredLines(opts: Opts): Promise<Result> {
export async function run(opts: Opts): Promise<Result> {
const file = opts.coverage;
if (opts.coverageFormat === 'istanbul') {
assert(/\.json$/.test(file), `input file '${file}' must be json coverage file`);
assert(file.endsWith('.json'), `input file '${file}' must be json coverage file`);
}

return await uncoveredLines({
Expand Down

0 comments on commit 7bfdc04

Please sign in to comment.