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(eslint-plugin): [no-input-rename] do not report on directive composition API #1231

Merged
merged 1 commit into from Nov 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
99 changes: 99 additions & 0 deletions packages/eslint-plugin/docs/rules/no-input-rename.md
Expand Up @@ -655,6 +655,105 @@ class Test {}

#### ✅ Valid Code

```ts
@Component({
selector: 'qx-menuitem',
hostDirectives: [{
directive: CdkMenuItem,
inputs: ['cdkMenuItemDisabled: disabled'],
}]
})
class Test {}
```

<br>

---

<br>

#### Default Config

```json
{
"rules": {
"@angular-eslint/no-input-rename": [
"error"
]
}
}
```

<br>

#### ✅ Valid Code

```ts
@Component({
selector: 'qx-menuitem',
'hostDirectives': [{
directive: CdkMenuItem,
inputs: ['cdkMenuItemDisabled: disabled'],
}]
})
class Test {}
```

<br>

---

<br>

#### Default Config

```json
{
"rules": {
"@angular-eslint/no-input-rename": [
"error"
]
}
}
```

<br>

#### ✅ Valid Code

```ts
@Component({
selector: 'qx-menuitem',
['hostDirectives']: [{
directive: CdkMenuItem,
inputs: ['cdkMenuItemDisabled: disabled'],
}]
})
class Test {}
```

<br>

---

<br>

#### Default Config

```json
{
"rules": {
"@angular-eslint/no-input-rename": [
"error"
]
}
}
```

<br>

#### ✅ Valid Code

```ts
@Component({})
class Test {
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint-plugin/src/rules/no-input-rename.ts
Expand Up @@ -139,6 +139,28 @@ export default createESLintRule<Options, MessageIds>({
[Selectors.INPUTS_METADATA_PROPERTY_LITERAL](
node: TSESTree.Literal | TSESTree.TemplateElement,
) {
const ancestorMaybeHostDirectiveAPI =
node.parent?.parent?.parent?.parent?.parent;
if (
ancestorMaybeHostDirectiveAPI &&
ASTUtils.isProperty(ancestorMaybeHostDirectiveAPI)
) {
/**
* Angular v15 introduced the directive composition API: https://angular.io/guide/directive-composition-api
* Renaming host directive inputs using this API is not a bad practice and should not be reported
*/
const hostDirectiveAPIPropertyName = 'hostDirectives';
if (
(ASTUtils.isLiteral(ancestorMaybeHostDirectiveAPI.key) &&
ancestorMaybeHostDirectiveAPI.key.value ===
hostDirectiveAPIPropertyName) ||
(TSESLintASTUtils.isIdentifier(ancestorMaybeHostDirectiveAPI.key) &&
ancestorMaybeHostDirectiveAPI.key.name ===
hostDirectiveAPIPropertyName)
) {
return;
}
}
const [propertyName, aliasName] = withoutBracketsAndWhitespaces(
ASTUtils.getRawText(node),
).split(':');
Expand Down
35 changes: 35 additions & 0 deletions packages/eslint-plugin/tests/rules/no-input-rename/cases.ts
Expand Up @@ -50,6 +50,41 @@ export const valid = [
})
class Test {}
`,
/**
* Renaming inputs when using the directive composition API is not a bad practice
* https://angular.io/guide/directive-composition-api
* https://www.youtube.com/watch?v=EJJwyyjsRGs
*/
`
@Component({
selector: 'qx-menuitem',
hostDirectives: [{
directive: CdkMenuItem,
inputs: ['cdkMenuItemDisabled: disabled'],
}]
})
class Test {}
`,
`
@Component({
selector: 'qx-menuitem',
'hostDirectives': [{
directive: CdkMenuItem,
inputs: ['cdkMenuItemDisabled: disabled'],
}]
})
class Test {}
`,
`
@Component({
selector: 'qx-menuitem',
['hostDirectives']: [{
directive: CdkMenuItem,
inputs: ['cdkMenuItemDisabled: disabled'],
}]
})
class Test {}
`,
`
@Component({})
class Test {
Expand Down