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

fix(type-utils): prevent stack overflow in isTypeReadonly #5860

Merged
merged 2 commits into from Oct 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,6 +5,7 @@ import type {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
} from '../../src/util';
import { readonlynessOptionsDefaults } from '../../src/util';
import { getFixturesRootDir, noFormat, RuleTester } from '../RuleTester';

type MessageIds = InferMessageIdsTypeFromRule<typeof rule>;
Expand Down Expand Up @@ -362,6 +363,23 @@ ruleTester.run('prefer-readonly-parameter-types', rule, {
},
],
},
{
name: 'circular readonly types (Bug: #4476)',
code: `
interface Obj {
readonly [K: string]: Obj;
}

function foo(event: Obj): void {}
`,
options: [
{
checkParameterProperties: true,
ignoreInferredTypes: false,
...readonlynessOptionsDefaults,
},
],
},
],
invalid: [
// arrays
Expand Down
4 changes: 4 additions & 0 deletions packages/type-utils/src/isTypeReadonly.ts
Expand Up @@ -113,6 +113,10 @@ function isTypeReadonlyObject(
return Readonlyness.Mutable;
}

if (indexInfo.type === type) {
return Readonlyness.Readonly;
}

return isTypeReadonlyRecurser(
checker,
indexInfo.type,
Expand Down
14 changes: 14 additions & 0 deletions packages/type-utils/tests/isTypeReadonly.test.ts
Expand Up @@ -134,6 +134,13 @@ describe('isTypeReadonly', () => {
);
});

describe('is readonly circular', () => {
const runTests = runTestIsReadonly;

it('handles circular readonly PropertySignature inside a readonly IndexSignature', () =>
runTests('interface Test { readonly [key: string]: Test };'));
});

describe('is not readonly', () => {
const runTests = runTestIsNotReadonly;

Expand All @@ -145,6 +152,13 @@ describe('isTypeReadonly', () => {
runTests,
);
});

describe('is not readonly circular', () => {
const runTests = runTestIsNotReadonly;

it('handles circular mutable PropertySignature inside a readonly IndexSignature', () =>
runTests('interface Test { [key: string]: Test };'));
});
});

describe('Union', () => {
Expand Down