Skip to content

feat!(utils): call getParserServices before create() in typed rules #8152

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

Closed
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
15 changes: 13 additions & 2 deletions packages/utils/src/eslint-utils/RuleCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
RuleModule,
} from '../ts-eslint/Rule';
import { applyDefault } from './applyDefault';
import { getParserServices } from './getParserServices';

export type { RuleListener, RuleModule };

Expand Down Expand Up @@ -73,6 +74,13 @@ export function RuleCreator(urlCreator: (ruleName: string) => string) {
},
},
...rule,
create(context, optionsWithDefault): RuleListener {
if (meta.docs.requiresTypeChecking) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is essentially an internal-only property that we use for documentation generation purposes - so we can't really rely on it like this.

Additionally this breaks usecases where we want conditional type-awareness - eg naming-convention only uses types if and only if you set certain options. So always running the util would mean the rule always requires parser services - even if you don't use the type-aware options.

There are also going to be usecases externally where people go against our current recommendations and do progressive enhancement of rules wherein this logic would break their usecase entirely.

getParserServices(context);
}

return rule.create(context, optionsWithDefault);
},
});
};
}
Expand All @@ -98,8 +106,11 @@ function createRule<
create(
context: Readonly<RuleContext<TMessageIds, TOptions>>,
): RuleListener {
const optionsWithDefault = applyDefault(defaultOptions, context.options);
return create(context, optionsWithDefault);
if (meta.docs?.requiresTypeChecking) {
getParserServices(context);
}

return create(context, applyDefault(defaultOptions, context.options));
},
defaultOptions,
meta,
Expand Down
147 changes: 123 additions & 24 deletions packages/utils/tests/eslint-utils/RuleCreator.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,143 @@
import { ESLintUtils } from '../../src';
import {
type NamedCreateRuleMeta,
RuleCreator,
} from '../../src/eslint-utils/RuleCreator';
import type { RuleContext } from '../../src/ts-eslint/Rule';

describe('RuleCreator', () => {
const createRule = ESLintUtils.RuleCreator(name => `test/${name}`);
const mockGetParserServices = jest.fn();

jest.mock('../../src/eslint-utils/getParserServices', () => ({
get getParserServices(): typeof mockGetParserServices {
return mockGetParserServices;
},
}));

it('createRule should be a function', () => {
expect(typeof createRule).toBe('function');
describe('RuleCreator', () => {
afterEach(() => {
mockGetParserServices.mockReset();
});

it('should create rule correctly', () => {
const rule = createRule({
name: 'test',
meta: {
describe('factory usage', () => {
const createRule = ESLintUtils.RuleCreator(name => `test/${name}`);

const meta = {
docs: {
description: 'some description',
recommended: 'recommended',
},
messages: {
example: 'some message',
},
schema: [],
type: 'problem',
} satisfies NamedCreateRuleMeta<'example'>;

it('populates rule meta', () => {
const rule = createRule({
name: 'test',
meta,
defaultOptions: [],
create: jest.fn(),
});

expect(rule.meta).toEqual({
...meta,
docs: {
description: 'some description',
recommended: 'recommended',
requiresTypeChecking: true,
...meta.docs,
url: 'test/test',
},
messages: {
foo: 'some message',
});
});

it('does not enforce parserServices in create when the rule is untyped', () => {
const create = jest.fn();
const rule = createRule({
name: 'test',
meta,
defaultOptions: [],
create,
});

rule.create({} as unknown as Readonly<RuleContext<'example', never[]>>);

expect(mockGetParserServices).not.toHaveBeenCalled();
});

it('enforces parserServices in create when the rule is typed', () => {
const create = jest.fn();
const rule = createRule({
name: 'test',
meta: {
...meta,
docs: {
...meta.docs,
requiresTypeChecking: true,
},
},
schema: [],
type: 'problem',
},
defaultOptions: [],
create() {
return {};
},
defaultOptions: [],
create,
});

rule.create({} as unknown as Readonly<RuleContext<'example', never[]>>);

expect(mockGetParserServices).toHaveBeenCalled();
});
expect(rule.meta).toEqual({
});

describe('withoutDocs', () => {
const meta = {
docs: {
description: 'some description',
url: 'test/test',
recommended: 'recommended',
requiresTypeChecking: true,
},
messages: {
foo: 'some message',
example: 'some message',
},
schema: [],
type: 'problem',
} satisfies NamedCreateRuleMeta<'example'>;

it('populates rule meta with added docs.url', () => {
const rule = RuleCreator.withoutDocs({
meta,
defaultOptions: [],
create: jest.fn(),
});

expect(rule.meta).toEqual(meta);
});

it('does not enforce parserServices in create when the rule is untyped', () => {
const create = jest.fn();
const rule = RuleCreator.withoutDocs({
meta,
defaultOptions: [],
create,
});

rule.create({} as unknown as Readonly<RuleContext<'example', never[]>>);

expect(mockGetParserServices).not.toHaveBeenCalled();
});

it('enforces parserServices in create when the rule is typed', () => {
const create = jest.fn();
const rule = RuleCreator.withoutDocs({
meta: {
...meta,
docs: {
...meta.docs,
requiresTypeChecking: true,
},
},
defaultOptions: [],
create,
});

rule.create({} as unknown as Readonly<RuleContext<'example', never[]>>);

expect(mockGetParserServices).toHaveBeenCalled();
});
});
});