-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
JoshuaKGoldberg
wants to merge
1
commit into
typescript-eslint:main
from
JoshuaKGoldberg:rule-creator-enforce-parser-for-typed-rules
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.