@@ -38,7 +38,8 @@ module.exports = {
3838 create : Components . detect ( ( context , components , utils ) => {
3939 const configuration = context . options [ 0 ] || DEFAULT_OPTION ;
4040 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 ( ) ;
4243 /**
4344 * @param {ASTNode } node We expect either an ArrowFunctionExpression,
4445 * FunctionDeclaration, or FunctionExpression
@@ -61,14 +62,24 @@ module.exports = {
6162 }
6263
6364 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 ) ;
6667 if ( isPropUsed && configuration === 'always' ) {
6768 context . report ( {
6869 node,
6970 message : `Must use destructuring ${ node . object . name } assignment`
7071 } ) ;
7172 }
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+ }
7283 }
7384
7485 function isInClassProperty ( node ) {
@@ -125,13 +136,29 @@ module.exports = {
125136 const SFCComponent = components . get ( context . getScope ( node ) . block ) ;
126137
127138 const destructuring = ( node . init && node . id && node . id . type === 'ObjectPattern' ) ;
139+ const identifier = ( node . init && node . id && node . id . type === 'Identifier' ) ;
128140 // 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' ;
130146 // let {foo} = this.props;
131147 const destructuringClass = destructuring && node . init . object && node . init . object . type === 'ThisExpression' && (
132148 node . init . property . name === 'props' || node . init . property . name === 'context' || node . init . property . name === 'state'
133149 ) ;
134150
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+
135162 if ( SFCComponent && destructuringSFC && configuration === 'never' ) {
136163 context . report ( {
137164 node,
0 commit comments