Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configured Object.freeze and memoize to be excluded from side-effects rule #217

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ const tsConfigurations = [

// at this point, it's allowed to use in production code
'@checkdigit/no-serve-runtime': 'off',

// configure this to not report side effects for certain functions
'@checkdigit/no-side-effects': [
'error',
{ excludedIdentifiers: ['debug', 'log', 'memoize', 'Object.freeze', 'promisify', 'Symbol.for'] },
],
},
},
{
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@checkdigit/eslint-config",
"version": "11.2.1",
"version": "11.2.2",
"private": false,
"description": "Check Digit standard eslint configuration",
"keywords": [
Expand Down Expand Up @@ -40,7 +40,7 @@
"fclone": "^1.0.11"
},
"peerDependencies": {
"@checkdigit/eslint-plugin": "7.12.0",
"@checkdigit/eslint-plugin": "7.13.1",
"@eslint/compat": "1.2.7",
"@eslint/eslintrc": "3.3.0",
"@eslint/js": "9.22.0",
Expand Down
36 changes: 21 additions & 15 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ import fs from 'node:fs';

// eslint-disable-next-line n/prefer-global/url-search-params, @checkdigit/no-duplicated-imports
import { URLSearchParams } from 'node:url';
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions,@checkdigit/require-assert-message
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions,@checkdigit/require-assert-message, @checkdigit/no-side-effects
assert.ok(URLSearchParams);

import { format } from 'node:url';
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition,@checkdigit/require-assert-message
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition,@checkdigit/require-assert-message, @checkdigit/no-side-effects
assert.ok(format !== undefined);

// eslint-disable-next-line @checkdigit/no-test-import
import testHello from './typescript.test.ts';
// eslint-disable-next-line @checkdigit/no-side-effects
testHello();

// eslint-disable-next-line n/no-sync
// eslint-disable-next-line n/no-sync, @checkdigit/no-side-effects
assert.ok(fs.existsSync('.'), 'cwd does not exist?');

// eslint-disable-next-line n/no-process-env,@checkdigit/require-assert-message
// eslint-disable-next-line n/no-process-env,@checkdigit/require-assert-message, @checkdigit/no-side-effects
assert.ok(process.env['DEBUG'] !== undefined);

function hello(_?: string): bigint {
Expand All @@ -45,12 +46,14 @@ function hello(_?: string): bigint {

// eslint-disable-next-line @checkdigit/no-uuid
// uuid: 'c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd' <- not ok since this is a non-test file
// eslint-disable-next-line @checkdigit/no-side-effects
hello();

// eslint-disable-next-line require-yield
async function* paginator() {
throw new Error('should not be called');
}
// eslint-disable-next-line @checkdigit/no-side-effects
paginator();

// eslint-disable-next-line @typescript-eslint/no-for-in-array,guard-for-in,no-magic-numbers
Expand All @@ -59,13 +62,13 @@ for (const value in [1, 2, 3]) {
console.log(value);
}

// eslint-disable-next-line no-eval
// eslint-disable-next-line no-eval, @checkdigit/no-side-effects
eval('console.log("no-no");');

// eslint-disable-next-line no-invalid-this,@typescript-eslint/strict-boolean-expressions,@checkdigit/require-assert-message
// eslint-disable-next-line no-invalid-this,@typescript-eslint/strict-boolean-expressions,@checkdigit/require-assert-message, @checkdigit/no-side-effects
assert.ok(this);

// eslint-disable-next-line sonarjs/pseudo-random
// eslint-disable-next-line sonarjs/pseudo-random, @checkdigit/no-side-effects
if (Math.random()) {
// ...
} else {
Expand All @@ -75,15 +78,15 @@ if (Math.random()) {
}
}

// eslint-disable-next-line sonarjs/pseudo-random,@checkdigit/require-assert-message
// eslint-disable-next-line sonarjs/pseudo-random,@checkdigit/require-assert-message, @checkdigit/no-side-effects
assert.ok(Math.random() === 2);

// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/no-unnecessary-type-parameters
const foo = <T>(argument: T) => (argument ? 1 : 0);
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions,@checkdigit/require-assert-message
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions,@checkdigit/require-assert-message, @checkdigit/no-side-effects
assert.ok(foo);

// eslint-disable-next-line @typescript-eslint/no-base-to-string,@checkdigit/require-assert-message
// eslint-disable-next-line @typescript-eslint/no-base-to-string,@checkdigit/require-assert-message, @checkdigit/no-side-effects
assert.ok({}.toString());

export default hello;
Expand All @@ -92,10 +95,11 @@ const numberValue = 1;
const booleanValue = true;
const objectValue = { key: 'value' };
// linting error is not reported because @typescript-eslint/restrict-template-expressions is configured to allow number and boolean
// eslint-disable-next-line @checkdigit/no-side-effects
assert.ok(`I'm a number, ${numberValue}`, 'number?');
// eslint-disable-next-line unicorn/consistent-assert
assert(`I'm a boolean, ${booleanValue}`, 'boolean?');
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string,unicorn/consistent-assert
// eslint-disable-next-line @checkdigit/no-side-effects
assert.ok(`I'm a boolean, ${booleanValue}`, 'boolean?');
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string,unicorn/consistent-assert, @checkdigit/no-side-effects
assert(`I'm a object, ${objectValue}`, 'object?');

// linting error is not reported because n/no-unsupported-features/node-builtins is disabled
Expand All @@ -104,11 +108,13 @@ await fetch('https://example.com');

// test rule @checkdigit/invalid-json-stringify
const objectToSerialize = { key: 'value' };
// eslint-disable-next-line @checkdigit/require-assert-message
// eslint-disable-next-line @checkdigit/require-assert-message, @checkdigit/no-side-effects
assert.equal(JSON.stringify(objectToSerialize), '{"key":"value"}');
// eslint-disable-next-line @checkdigit/no-side-effects
const newError = new Error('error');
// eslint-disable-next-line @checkdigit/invalid-json-stringify,@checkdigit/require-assert-message
// eslint-disable-next-line @checkdigit/invalid-json-stringify,@checkdigit/require-assert-message, @checkdigit/no-side-effects
assert.equal(JSON.stringify(newError), '{}'); // serialization of Error object is losing information, hance the error
// eslint-disable-next-line @checkdigit/no-side-effects
try {
//
} catch (caughtError) {
Expand Down