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