@@ -17,6 +17,8 @@ const jsxUtil = require('./jsx');
1717const usedPropTypesUtil = require ( './usedPropTypes' ) ;
1818const defaultPropsUtil = require ( './defaultProps' ) ;
1919const isFirstLetterCapitalized = require ( './isFirstLetterCapitalized' ) ;
20+ const isCreateElement = require ( './isCreateElement' ) ;
21+ const isDestructuredFromPragmaImport = require ( './isDestructuredFromPragmaImport' ) ;
2022
2123function getId ( node ) {
2224 return node && node . range . join ( ':' ) ;
@@ -287,70 +289,7 @@ function componentRule(rule, context) {
287289 * @returns {Boolean } True if createElement is destructured from the pragma
288290 */
289291 isDestructuredFromPragmaImport ( variable ) {
290- const variables = variableUtil . variablesInScope ( context ) ;
291- const variableInScope = variableUtil . getVariable ( variables , variable ) ;
292- if ( variableInScope ) {
293- const latestDef = variableUtil . getLatestVariableDefinition ( variableInScope ) ;
294- if ( latestDef ) {
295- // check if latest definition is a variable declaration: 'variable = value'
296- if ( latestDef . node . type === 'VariableDeclarator' && latestDef . node . init ) {
297- // check for: 'variable = pragma.variable'
298- if (
299- latestDef . node . init . type === 'MemberExpression'
300- && latestDef . node . init . object . type === 'Identifier'
301- && latestDef . node . init . object . name === pragma
302- ) {
303- return true ;
304- }
305- // check for: '{variable} = pragma'
306- if (
307- latestDef . node . init . type === 'Identifier'
308- && latestDef . node . init . name === pragma
309- ) {
310- return true ;
311- }
312-
313- // "require('react')"
314- let requireExpression = null ;
315-
316- // get "require('react')" from: "{variable} = require('react')"
317- if ( latestDef . node . init . type === 'CallExpression' ) {
318- requireExpression = latestDef . node . init ;
319- }
320- // get "require('react')" from: "variable = require('react').variable"
321- if (
322- ! requireExpression
323- && latestDef . node . init . type === 'MemberExpression'
324- && latestDef . node . init . object . type === 'CallExpression'
325- ) {
326- requireExpression = latestDef . node . init . object ;
327- }
328-
329- // check proper require.
330- if (
331- requireExpression
332- && requireExpression . callee
333- && requireExpression . callee . name === 'require'
334- && requireExpression . arguments [ 0 ]
335- && requireExpression . arguments [ 0 ] . value === pragma . toLocaleLowerCase ( )
336- ) {
337- return true ;
338- }
339-
340- return false ;
341- }
342-
343- // latest definition is an import declaration: import {<variable>} from 'react'
344- if (
345- latestDef . parent
346- && latestDef . parent . type === 'ImportDeclaration'
347- && latestDef . parent . source . value === pragma . toLocaleLowerCase ( )
348- ) {
349- return true ;
350- }
351- }
352- }
353- return false ;
292+ return isDestructuredFromPragmaImport ( variable , context ) ;
354293 } ,
355294
356295 /**
@@ -360,29 +299,7 @@ function componentRule(rule, context) {
360299 * @returns {Boolean } True if createElement called from pragma
361300 */
362301 isCreateElement ( node ) {
363- // match `React.createElement()`
364- if (
365- node
366- && node . callee
367- && node . callee . object
368- && node . callee . object . name === pragma
369- && node . callee . property
370- && node . callee . property . name === 'createElement'
371- ) {
372- return true ;
373- }
374-
375- // match `createElement()`
376- if (
377- node
378- && node . callee
379- && node . callee . name === 'createElement'
380- && this . isDestructuredFromPragmaImport ( 'createElement' )
381- ) {
382- return true ;
383- }
384-
385- return false ;
302+ return isCreateElement ( node , context ) ;
386303 } ,
387304
388305 /**
0 commit comments