-
Notifications
You must be signed in to change notification settings - Fork 31
/
decorators-context.ts
63 lines (59 loc) · 1.79 KB
/
decorators-context.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
import { Rule } from 'eslint';
import { stencilComponentContext } from '../utils';
const rule: Rule.RuleModule = {
meta: {
docs: {
description: 'This rule catches Stencil Decorators used in incorrect locations.',
category: 'Possible Errors',
recommended: true
},
schema: [],
type: 'problem'
},
create(context): Rule.RuleListener {
const stencil = stencilComponentContext();
return {
...stencil.rules,
'Decorator': (node: any) => {
if (!stencil.isComponent()) {
return;
}
if (node.expression && node.expression.callee) {
const decName = node.expression.callee.name;
if (
decName === 'Prop' ||
decName === 'State' ||
decName === 'Element' ||
decName === 'Event'
) {
if (node.parent.type !== 'PropertyDefinition') {
context.report({
node: node,
message: `The @${decName} decorator can only be applied to class properties.`
});
}
} else if (
decName === 'Method' ||
decName === 'Watch' ||
decName === 'Listen'
) {
if (node.parent.type !== 'MethodDefinition') {
context.report({
node: node,
message: `The @${decName} decorator can only be applied to class methods.`
});
}
} else if (decName === 'Component') {
if (node.parent.type !== 'ClassDeclaration') {
context.report({
node: node,
message: `The @${decName} decorator can only be applied to a class.`
});
}
}
}
}
};
}
};
export default rule;