@@ -39,6 +39,8 @@ module.exports = {
39
39
const configuration = context . options [ 0 ] || DEFAULT_OPTION ;
40
40
const ignoreClassFields = ( context . options [ 1 ] && ( context . options [ 1 ] . ignoreClassFields === true ) ) || false ;
41
41
42
+ // set to save renamed var of useContext
43
+ const contextSet = new Set ( ) ;
42
44
/**
43
45
* @param {ASTNode } node We expect either an ArrowFunctionExpression,
44
46
* FunctionDeclaration, or FunctionExpression
@@ -61,14 +63,24 @@ module.exports = {
61
63
}
62
64
63
65
function handleSFCUsage ( node ) {
64
- // props.aProp || context.aProp
65
- const isPropUsed = ( node . object . name === 'props' || node . object . name === 'context' ) && ! isAssignmentLHS ( node ) ;
66
+ // props.aProp
67
+ const isPropUsed = node . object . name === 'props' && ! isAssignmentLHS ( node ) ;
66
68
if ( isPropUsed && configuration === 'always' ) {
67
69
context . report ( {
68
70
node,
69
71
message : `Must use destructuring ${ node . object . name } assignment`
70
72
} ) ;
71
73
}
74
+
75
+ // const foo = useContext(aContext);
76
+ // foo.aProp
77
+ const isContextUsed = contextSet . has ( node . object . name ) && ! isAssignmentLHS ( node ) ;
78
+ if ( isContextUsed && configuration === 'always' ) {
79
+ context . report ( {
80
+ node,
81
+ message : `Must use destructuring ${ node . object . name } assignment`
82
+ } ) ;
83
+ }
72
84
}
73
85
74
86
function isInClassProperty ( node ) {
@@ -125,13 +137,29 @@ module.exports = {
125
137
const SFCComponent = components . get ( context . getScope ( node ) . block ) ;
126
138
127
139
const destructuring = ( node . init && node . id && node . id . type === 'ObjectPattern' ) ;
140
+ const identifier = ( node . init && node . id && node . id . type === 'Identifier' ) ;
128
141
// let {foo} = props;
129
- const destructuringSFC = destructuring && ( node . init . name === 'props' || node . init . name === 'context' ) ;
142
+ const destructuringSFC = destructuring && node . init . name === 'props' ;
143
+ // let {foo} = useContext(aContext);
144
+ const destructuringUseContext = destructuring && node . init . callee && node . init . callee . name === 'useContext' ;
145
+ // let foo = useContext(aContext);
146
+ const assignUseContext = identifier && node . init . callee && node . init . callee . name === 'useContext' ;
130
147
// let {foo} = this.props;
131
148
const destructuringClass = destructuring && node . init . object && node . init . object . type === 'ThisExpression' && (
132
149
node . init . property . name === 'props' || node . init . property . name === 'context' || node . init . property . name === 'state'
133
150
) ;
134
151
152
+ if ( SFCComponent && assignUseContext ) {
153
+ contextSet . add ( node . id . name ) ;
154
+ }
155
+
156
+ if ( SFCComponent && destructuringUseContext && configuration === 'never' ) {
157
+ context . report ( {
158
+ node,
159
+ message : `Must never use destructuring ${ node . init . callee . name } assignment`
160
+ } ) ;
161
+ }
162
+
135
163
if ( SFCComponent && destructuringSFC && configuration === 'never' ) {
136
164
context . report ( {
137
165
node,
0 commit comments