1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | const Components = require('../util/Components');
|
9 | const docsUrl = require('../util/docsUrl');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | module.exports = {
|
16 | meta: {
|
17 | docs: {
|
18 | description: 'State initialization in an ES6 class component should be in a constructor',
|
19 | category: 'Stylistic Issues',
|
20 | recommended: false,
|
21 | url: docsUrl('state-in-constructor')
|
22 | },
|
23 | schema: [{
|
24 | enum: ['always', 'never']
|
25 | }]
|
26 | },
|
27 |
|
28 | create: Components.detect((context, components, utils) => {
|
29 | const option = context.options[0] || 'always';
|
30 | return {
|
31 | ClassProperty(node) {
|
32 | if (
|
33 | option === 'always'
|
34 | && !node.static
|
35 | && node.key.name === 'state'
|
36 | && utils.getParentES6Component()
|
37 | ) {
|
38 | context.report({
|
39 | node,
|
40 | message: 'State initialization should be in a constructor'
|
41 | });
|
42 | }
|
43 | },
|
44 | AssignmentExpression(node) {
|
45 | if (
|
46 | option === 'never'
|
47 | && utils.isStateMemberExpression(node.left)
|
48 | && utils.inConstructor()
|
49 | && utils.getParentES6Component()
|
50 | ) {
|
51 | context.report({
|
52 | node,
|
53 | message: 'State initialization should be in a class property'
|
54 | });
|
55 | }
|
56 | }
|
57 | };
|
58 | })
|
59 | };
|