generated from JoshuaKGoldberg/create-typescript-app
-
Notifications
You must be signed in to change notification settings - Fork 19
feat: add ruleCreationMethods rule
#2301
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21f5fc5
feat: add `ruleCreationMethods` rule
kovsu 0e28a93
apply suggestions
kovsu 5d444e7
combine `suggestions` to one line & add `.mdx` file
kovsu 2e3b85a
update changeset
kovsu 722b750
apply suggestion
kovsu 56636e4
fix
kovsu 9723ad2
Update packages/site/src/content/docs/rules/flint/ruleCreationMethods…
kovsu 62e1a89
Merge branch 'main' into fix-#1747
JoshuaKGoldberg 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@flint.fyi/plugin-flint": minor | ||
| --- | ||
|
|
||
| Add `ruleCreationMethods` rule |
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
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
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
50 changes: 50 additions & 0 deletions
50
packages/plugin-flint/src/rules/ruleCreationMethods.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import rule from "./ruleCreationMethods.ts"; | ||
| import { ruleTester } from "./ruleTester.ts"; | ||
|
|
||
| ruleTester.describe(rule, { | ||
| invalid: [ | ||
| { | ||
| code: ` | ||
| export default typescriptLanguage.createRule({ | ||
| about: { | ||
| description: "Test rule", | ||
| id: "testRule", | ||
| presets: ["logical"], | ||
| }, | ||
| messages: {}, | ||
| setup(context) { | ||
| return { visitors: {} }; | ||
| }, | ||
| }); | ||
| `, | ||
| snapshot: ` | ||
| export default typescriptLanguage.createRule({ | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| Plugin rules should be created through RuleCreator instead of calling language.createRule() directly. | ||
| about: { | ||
| description: "Test rule", | ||
| id: "testRule", | ||
| presets: ["logical"], | ||
| }, | ||
| messages: {}, | ||
| setup(context) { | ||
| return { visitors: {} }; | ||
| }, | ||
| }); | ||
| `, | ||
| }, | ||
| ], | ||
| valid: [ | ||
| ` | ||
| interface RuleCreator { createRule<T>(language: any, ruleConfig: { messages: Record<string, string> }): T; } | ||
| declare const ruleCreator: RuleCreator; | ||
|
|
||
| export default ruleCreator.createRule(_, { | ||
| messages: {}, | ||
| setup(context) { | ||
| return { visitors: {} }; | ||
| } | ||
| }); | ||
| `, | ||
| ], | ||
| }); |
kovsu marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { | ||
| getTSNodeRange, | ||
| typescriptLanguage, | ||
| } from "@flint.fyi/typescript-language"; | ||
|
|
||
| import { isLanguageCreateRule } from "../utils/ruleCreatorHelpers.ts"; | ||
| import { ruleCreator } from "./ruleCreator.ts"; | ||
|
|
||
| export default ruleCreator.createRule(typescriptLanguage, { | ||
| about: { | ||
| description: | ||
| "Reports plugin rules created directly with language instead of through RuleCreator.", | ||
| id: "ruleCreationMethods", | ||
| presets: ["logical"], | ||
| }, | ||
| messages: { | ||
| ruleCreationMethods: { | ||
| primary: | ||
| "Plugin rules should be created through RuleCreator instead of calling language.createRule() directly.", | ||
| secondary: [ | ||
| "Direct language creation bypasses the standardized rule metadata and documentation provided by RuleCreator.", | ||
| "RuleCreator adds the `docs` property and ensures consistent rule structure across plugins.", | ||
| ], | ||
| suggestions: [ | ||
| "Create an instance of RuleCreator from `@flint.fyi/core` (e.g. `const ruleCreator = new RuleCreator({ presets: [...] })`), then use `ruleCreator.createRule(language, { ... })` instead of `language.createRule({ ... })`.", | ||
| ], | ||
| }, | ||
| }, | ||
| setup(context) { | ||
| return { | ||
| visitors: { | ||
| CallExpression(node, { sourceFile, typeChecker }) { | ||
| if (!isLanguageCreateRule(node, typeChecker)) { | ||
| return; | ||
| } | ||
|
|
||
| context.report({ | ||
| message: "ruleCreationMethods", | ||
| range: getTSNodeRange(node.expression, sourceFile), | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
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 |
|---|---|---|
|
|
@@ -94,23 +94,33 @@ export function getStringOriginalQuote( | |
| return text[0] ?? '"'; | ||
| } | ||
|
|
||
| export function isLanguageCreateRule( | ||
| node: AST.CallExpression, | ||
| typeChecker: ts.TypeChecker, | ||
| ): boolean { | ||
| return ( | ||
| node.expression.kind === SyntaxKind.PropertyAccessExpression && | ||
| node.expression.name.text === "createRule" && | ||
| !isRuleCreatorCreateRule(node, typeChecker) | ||
| ); | ||
| } | ||
|
|
||
| // TODO: Maybe need to check it more strictly | ||
| // https://github.com/flint-fyi/flint/issues/152 | ||
lishaduck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| export function isRuleCreatorCreateRule( | ||
| node: AST.CallExpression, | ||
| typeChecker: ts.TypeChecker, | ||
| ): boolean { | ||
| if (node.expression.kind !== SyntaxKind.PropertyAccessExpression) { | ||
| return false; | ||
| } | ||
|
|
||
| const propertyAccess = node.expression; | ||
| const type = typeChecker.getTypeAtLocation(propertyAccess.expression); | ||
| const typeName = type.getSymbol()?.getName(); | ||
| if ( | ||
| node.expression.kind === SyntaxKind.PropertyAccessExpression && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I was suggesting something like this const isPropertyAccessExpression = (node: : AST.CallExpression): node is PropertyAccessExpression {
return node.expression.kind === SyntaxKind.PropertyAccessExpression;
}
const isCreateRuleCall = (node: : AST.CallExpression): boolean {
return isPropertyAccessExpression(node) && node.expression.name.text === "createRule";
}But this works fine too. 👍 |
||
| node.expression.name.text === "createRule" | ||
| ) { | ||
| const propertyAccess = node.expression; | ||
| const type = typeChecker.getTypeAtLocation(propertyAccess.expression); | ||
|
|
||
| return ( | ||
| typeName === "RuleCreator" && propertyAccess.name.text === "createRule" | ||
| ); | ||
| return type.getSymbol()?.getName() === "RuleCreator"; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // TODO: Maybe need to check it more strictly | ||
|
|
||
67 changes: 67 additions & 0 deletions
67
packages/site/src/content/docs/rules/flint/ruleCreationMethods.mdx
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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| --- | ||
| description: "Reports plugin rules created directly with language instead of through RuleCreator." | ||
| title: "ruleCreationMethods" | ||
| topic: "rules" | ||
| --- | ||
|
|
||
| import { Tabs, TabItem } from "@astrojs/starlight/components"; | ||
| import { RuleEquivalents } from "~/components/RuleEquivalents"; | ||
| import RuleSummary from "~/components/RuleSummary.astro"; | ||
|
|
||
| <RuleSummary plugin="flint" rule="ruleCreationMethods" /> | ||
|
|
||
| Plugin rules should be created through `RuleCreator` instead of calling `language.createRule()` directly. | ||
| Direct language creation bypasses the standardized rule metadata and documentation provided by `RuleCreator`, which adds the `docs` property and ensures consistent rule structure across plugins. | ||
|
|
||
| ## Examples | ||
|
|
||
| <Tabs> | ||
| <TabItem label="❌ Incorrect"> | ||
|
|
||
| ```ts | ||
| import { typescriptLanguage } from "@flint.fyi/typescript-language"; | ||
|
|
||
| export default typescriptLanguage.createRule({ | ||
| about: { | ||
| description: "My rule", | ||
| id: "myRule", | ||
| presets: ["logical"], | ||
| }, | ||
| setup(context) { | ||
| return { visitors: {} }; | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem label="✅ Correct"> | ||
|
|
||
| ```ts | ||
| import { typescriptLanguage } from "@flint.fyi/typescript-language"; | ||
| import { RuleCreator } from "@flint.fyi/core"; | ||
|
|
||
| const ruleCreator = new RuleCreator({ presets: ["logical"] }); | ||
|
|
||
| export default ruleCreator.createRule(typescriptLanguage, { | ||
| about: { | ||
| description: "My rule", | ||
| id: "myRule", | ||
| presets: ["logical"], | ||
| }, | ||
| setup(context) { | ||
| return { visitors: {} }; | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| </Tabs> | ||
|
|
||
| ## Options | ||
kovsu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| This rule is not configurable. | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If you are developing plugins that are only intended for internal use or tooling that | ||
| will need to call `language.createRule()` directly, you can safely disable this rule. | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I just wanna say this is 🔥 lol.
Actually, TBH maybe we should ignore this rule for the core package specifically, because
it shouldn't be defining any rules?No, that depends on how we structure #2181. Though, having said that, we probably should structure it out-of-core. Ehh, who cares?