-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin)!: add no-different-displayname custom rule (#120)
* feat: add no-different-displayname custom rule * doc: add rule in breaking examples * feat!: add no-different-displayname in recommended config
- Loading branch information
Showing
6 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
31 changes: 31 additions & 0 deletions
31
packages/eslint-plugin/docs/rules/no-different-displayname.md
This file contains 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,31 @@ | ||
# Enforce component displayName to match with component name (`@bam.tech/no-different-displayname`) | ||
|
||
💼 This rule is enabled in the ✅ `recommended` config. | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Enforces component displayName to match with component name | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule : | ||
|
||
```jsx | ||
const MyComponent = () => { | ||
/* ... */ | ||
}; | ||
|
||
MyComponent.displayName = "NotMyComponent"; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
const MyComponent = () => { | ||
/* ... */ | ||
}; | ||
|
||
MyComponent.displayName = "MyComponent"; | ||
``` |
This file contains 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 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,9 +1,11 @@ | ||
import { awaitUserEventRule } from "./await-user-event"; | ||
import { noDifferentDisplaynameRule } from "./no-different-displayname"; | ||
import { preferUserEventRule } from "./prefer-user-event"; | ||
import { requireNamedEffectRule } from "./require-named-effect"; | ||
|
||
export default { | ||
"await-user-event": awaitUserEventRule, | ||
"prefer-user-event": preferUserEventRule, | ||
"require-named-effect": requireNamedEffectRule, | ||
"no-different-displayname": noDifferentDisplaynameRule, | ||
}; |
48 changes: 48 additions & 0 deletions
48
packages/eslint-plugin/lib/rules/no-different-displayname.ts
This file contains 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,48 @@ | ||
/** | ||
* @fileoverview Enforces component displayName to match with component name | ||
* @author Remi Leroy | ||
*/ | ||
|
||
import type { Rule } from "eslint"; | ||
import * as ESTree from "estree"; | ||
|
||
export const noDifferentDisplaynameRule: Rule.RuleModule = { | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "Enforce component displayName to match with component name", | ||
recommended: true, | ||
url: "https://github.com/bamlab/react-native-project-config/tree/main/packages/eslint-plugin/docs/rules/no-different-displayname.md", | ||
}, | ||
messages: { | ||
displayNameMismatch: "DisplayName does not match the component name", | ||
}, | ||
schema: [], | ||
fixable: "code", | ||
}, | ||
|
||
create(context) { | ||
return { | ||
'Program > ExpressionStatement > AssignmentExpression:has(Identifier[name="displayName"])'( | ||
node: ESTree.AssignmentExpression, | ||
) { | ||
if (!("object" in node.left)) return; | ||
if (!("name" in node.left.object)) return; | ||
if (!("value" in node.right)) return; | ||
|
||
const componentName = node.left.object.name; | ||
const displayedName = node.right.value; | ||
|
||
if (componentName !== displayedName) { | ||
context.report({ | ||
node, | ||
message: "DisplayName does not match the component name", | ||
fix(fixer) { | ||
return fixer.replaceText(node.right, `"${componentName}"`); | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
39 changes: 39 additions & 0 deletions
39
packages/eslint-plugin/tests/lib/rules/no-different-displayname.test.ts
This file contains 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,39 @@ | ||
/** | ||
* @fileoverview Enforce component displayName to match with component name | ||
* @author Remi Leroy | ||
*/ | ||
|
||
import { RuleTester } from "eslint"; | ||
import { noDifferentDisplaynameRule } from "../../../lib/rules/no-different-displayname"; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve("@typescript-eslint/parser"), | ||
}); | ||
|
||
const valid = [ | ||
{ | ||
code: ` | ||
const MyComponent = () => {}; | ||
MyComponent.displayName = "MyComponent"; | ||
`, | ||
}, | ||
]; | ||
|
||
const invalid = [ | ||
{ | ||
code: ` | ||
const MyComponent = () => {}; | ||
MyComponent.displayName = "WrongName"; | ||
`, | ||
errors: [{ message: "DisplayName does not match the component name" }], | ||
output: ` | ||
const MyComponent = () => {}; | ||
MyComponent.displayName = "MyComponent"; | ||
`, | ||
}, | ||
]; | ||
|
||
ruleTester.run("no-different-displayname", noDifferentDisplaynameRule, { | ||
valid, | ||
invalid, | ||
}); |