Skip to content

Commit

Permalink
feat(eslint-plugin): [use-lifecycle-interface] add fixer for the rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bulldog98 committed Jan 31, 2024
1 parent c0d8a6e commit fcfde57
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/README.md
Expand Up @@ -70,7 +70,7 @@
| [`use-component-selector`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/use-component-selector.md) | Component selector must be declared | | | |
| [`use-component-view-encapsulation`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/use-component-view-encapsulation.md) | Disallows using `ViewEncapsulation.None` | | | :bulb: |
| [`use-injectable-provided-in`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/use-injectable-provided-in.md) | Using the `providedIn` property makes `Injectables` tree-shakable | | | :bulb: |
| [`use-lifecycle-interface`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/use-lifecycle-interface.md) | Ensures that classes implement lifecycle interfaces corresponding to the declared lifecycle methods. See more at https://angular.io/styleguide#style-09-01 | | | |
| [`use-lifecycle-interface`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/use-lifecycle-interface.md) | Ensures that classes implement lifecycle interfaces corresponding to the declared lifecycle methods. See more at https://angular.io/styleguide#style-09-01 | | :wrench: | |
| [`use-pipe-transform-interface`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/use-pipe-transform-interface.md) | Ensures that `Pipes` implement `PipeTransform` interface | :white_check_mark: | :wrench: | |
<!-- prettier-ignore-end -->

Expand Down
Expand Up @@ -18,6 +18,7 @@
Ensures that classes implement lifecycle interfaces corresponding to the declared lifecycle methods. See more at https://angular.io/styleguide#style-09-01

- Type: suggestion
- 🔧 Supports autofix (`--fix`)

<br>

Expand Down
18 changes: 17 additions & 1 deletion packages/eslint-plugin/src/rules/use-lifecycle-interface.ts
@@ -1,4 +1,9 @@
import { ASTUtils, toPattern } from '@angular-eslint/utils';
import {
ASTUtils,
toPattern,
RuleFixes,
isNotNullOrUndefined,
} from '@angular-eslint/utils';
import type { TSESTree } from '@typescript-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';

Expand All @@ -18,6 +23,7 @@ export default createESLintRule<Options, MessageIds>({
messages: {
useLifecycleInterface: `Lifecycle interface '{{interfaceName}}' should be implemented for method '{{methodName}}'. (${STYLE_GUIDE_LINK})`,
},
fixable: 'code',
},
defaultOptions: [],
create(context) {
Expand Down Expand Up @@ -50,6 +56,16 @@ export default createESLintRule<Options, MessageIds>({
node: key,
messageId: 'useLifecycleInterface',
data: { interfaceName, methodName },
fix: (fixer) => {
const { implementsNodeReplace, implementsTextReplace } =
RuleFixes.getImplementsSchemaFixer(parent, interfaceName);
return [
fixer.insertTextAfter(
implementsNodeReplace,
implementsTextReplace,
),
].filter(isNotNullOrUndefined);
},
});
},
};
Expand Down
Expand Up @@ -58,6 +58,14 @@ export const invalid = [
interfaceName: ASTUtils.AngularLifecycleInterfaces.OnInit,
methodName: ASTUtils.AngularLifecycleMethods.ngOnInit,
},
annotatedOutput: `
@Component()
class Test implements OnInit {
ngOnInit() {
}
}
`,
}),
convertAnnotatedSourceToFailureCase({
description:
Expand All @@ -77,6 +85,16 @@ export const invalid = [
interfaceName: ASTUtils.AngularLifecycleInterfaces.OnDestroy,
methodName: ASTUtils.AngularLifecycleMethods.ngOnDestroy,
},
annotatedOutput: `
@Directive()
class Test extends Component implements OnInit, OnDestroy {
ngOnInit() {}
ngOnDestroy() {
}
}
`,
}),
convertAnnotatedSourceToFailureCase({
description:
Expand Down Expand Up @@ -120,6 +138,19 @@ export const invalid = [
},
},
],
annotatedOutput: `
@Injectable()
class Test implements DoBootstrap {
ngDoBootstrap() {}
ngOnInit() {}
ngOnDestroy() {}
}
`,
}),
convertAnnotatedSourceToFailureCase({
description:
Expand All @@ -139,5 +170,15 @@ export const invalid = [
interfaceName: ASTUtils.AngularLifecycleInterfaces.OnDestroy,
methodName: ASTUtils.AngularLifecycleMethods.ngOnDestroy,
},
annotatedOutput: `
@NgModule()
class Test extends Component implements ng.OnInit, OnDestroy {
ngOnInit() {}
ngOnDestroy() {
}
}
`,
}),
];

0 comments on commit fcfde57

Please sign in to comment.