Skip to content

Commit

Permalink
Inline Istanbul ignore reason rule
Browse files Browse the repository at this point in the history
With updated and stricter tests, outdated plugin no longer required.
  • Loading branch information
osmestad committed Sep 18, 2023
1 parent bebf22f commit 1045acd
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 40 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import tsParser from '@typescript-eslint/parser';
import eslintConfigPrettier from 'eslint-config-prettier';
import cypress from 'eslint-plugin-cypress';
import importPlugin from 'eslint-plugin-import';
import istanbul from 'eslint-plugin-istanbul';
import noOnlyTestsPlugin from 'eslint-plugin-no-only-tests';
import prettier from 'eslint-plugin-prettier';
import sortDestructureKeysPlugin from 'eslint-plugin-sort-destructure-keys';
Expand All @@ -18,6 +17,8 @@ import tsSortKeysPlugin from 'eslint-plugin-typescript-sort-keys';
import vitest from 'eslint-plugin-vitest';
import globals from 'globals';

import internalRules from './internal-rules/index.js';

// mimic CommonJS variables -- not needed if using CommonJS
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -48,7 +49,7 @@ export default [
'@jambit/typed-redux-saga': typedReduxSagaPlugin,
'@typescript-eslint': tsPlugin,
import: importPlugin,
istanbul,
'internal-rules': internalRules,
'no-only-tests': noOnlyTestsPlugin,
prettier,
'sort-destructure-keys': sortDestructureKeysPlugin,
Expand Down Expand Up @@ -137,8 +138,7 @@ export default [
},
],
'import/unambiguous': 'off',
'istanbul/no-ignore-file': 'error',
'istanbul/prefer-ignore-reason': 'error',
'internal-rules/require-coverage-ignore-reason': 'error',
'max-depth': 'error',
'max-param': 'off',
'max-params': 'off',
Expand Down
7 changes: 7 additions & 0 deletions internal-rules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { requireCoverageIgnoreReason } from './require-coverage-ignore-reason.js';

export default {
rules: {
'require-coverage-ignore-reason': requireCoverageIgnoreReason,
},
};
27 changes: 27 additions & 0 deletions internal-rules/require-coverage-ignore-reason.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
// Extracted from: https://github.com/graphql/graphql-js/pull/3388 related to:
// https://github.com/istanbuljs/eslint-plugin-istanbul/pull/17
// Fixed reason length check and converted to ESM.

export function requireCoverageIgnoreReason(context) {
const istanbulRegExp = /^\s*istanbul\s+ignore\s+(if|else|next|file)\s+/;
return {
Program() {
const sourceCode = context.getSourceCode();

for (const node of sourceCode.getAllComments()) {
const comment = node.value;

if (comment.match(istanbulRegExp)) {
const reason = comment.replace(istanbulRegExp, '');
if (reason.length === 0) {
context.report({
message: 'Add a reason why code coverage should be ignored',
node,
});
}
}
}
},
};
}
37 changes: 2 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"eslint-plugin-cypress": "^2.14.0",
"eslint-import-resolver-typescript": "^3.6.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-istanbul": "0.1.2",
"eslint-plugin-no-only-tests": "^3.1.0",
"eslint-plugin-prettier": "5.0.0",
"eslint-plugin-react": "7.33.2",
Expand Down
1 change: 1 addition & 0 deletions test/cases/nice-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export const sha256 = async (/** @type {string} */ message) => {
for (let i = 0; i < len; i += 1) {
binary += String.fromCharCode(bytes[i]);
}
/* istanbul ignore next because we have some very good reason to */
return globalThis.btoa(binary);
};
1 change: 1 addition & 0 deletions test/cases/ugly-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var something = {
return 'hello' + world
},
world(hai = 2) {
/* istanbul ignore next */
return hai;
}
};
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,24 @@ test('Fails on broken files', async t => {
const expectedErrors = [
'prettier/prettier',
'@typescript-eslint/no-unused-vars',
'internal-rules/require-coverage-ignore-reason',
];

const expectedErrorsFound = errorsAsRuleIds.filter(
error => expectedErrors.indexOf(error) !== -1,
);

// All expected error types are found:
t.is(expectedErrorsFound.length, expectedErrors.length);

// All found errors are of expected types:
const unexpectedErrorsFound = errorsAsRuleIds.filter(
error => expectedErrors.indexOf(error) === -1,
);
t.is(unexpectedErrorsFound.length, 0);

// Total number of seen errors:
t.is(errors.length, 6);
});

test('Success on nice files', async t => {
Expand Down

0 comments on commit 1045acd

Please sign in to comment.