forked from angular-eslint/angular-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-outputs-metadata-property.ts
64 lines (59 loc) · 2.22 KB
/
no-outputs-metadata-property.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { ASTUtils, Selectors } from '@angular-eslint/utils';
import type { TSESTree } from '@typescript-eslint/utils';
import { ASTUtils as TSESLintASTUtils } from '@typescript-eslint/utils';
import { createESLintRule } from '../utils/create-eslint-rule';
type Options = [];
export type MessageIds = 'noOutputsMetadataProperty';
export const RULE_NAME = 'no-outputs-metadata-property';
const METADATA_PROPERTY_NAME = 'outputs';
const STYLE_GUIDE_LINK = 'https://angular.io/styleguide#style-05-12';
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: `Disallows usage of the \`${METADATA_PROPERTY_NAME}\` metadata property. See more at ${STYLE_GUIDE_LINK}`,
recommended: 'error',
},
schema: [],
messages: {
noOutputsMetadataProperty: `Use \`@Output\` rather than the \`${METADATA_PROPERTY_NAME}\` metadata property (${STYLE_GUIDE_LINK})`,
},
},
defaultOptions: [],
create(context) {
return {
[`${
Selectors.COMPONENT_OR_DIRECTIVE_CLASS_DECORATOR
} ${Selectors.metadataProperty(METADATA_PROPERTY_NAME)}`](
node: TSESTree.Property,
) {
/**
* Angular v15 introduced the directive composition API: https://angular.io/guide/directive-composition-api
* Using host directive outputs using this API is not a bad practice and should not be reported
*/
const ancestorMayBeHostDirectiveAPI = node.parent?.parent?.parent;
if (
ancestorMayBeHostDirectiveAPI &&
ASTUtils.isProperty(ancestorMayBeHostDirectiveAPI)
) {
const hostDirectiveAPIPropertyName = 'hostDirectives';
if (
(ASTUtils.isLiteral(ancestorMayBeHostDirectiveAPI.key) &&
ancestorMayBeHostDirectiveAPI.key.value ===
hostDirectiveAPIPropertyName) ||
(TSESLintASTUtils.isIdentifier(ancestorMayBeHostDirectiveAPI.key) &&
ancestorMayBeHostDirectiveAPI.key.name ===
hostDirectiveAPIPropertyName)
) {
return;
}
}
context.report({
node,
messageId: 'noOutputsMetadataProperty',
});
},
};
},
});