[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | const pragmaUtil = require('./pragma');
|
---|
| 4 | const variableUtil = require('./variable');
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | * Check if variable is destructured from pragma import
|
---|
| 8 | *
|
---|
| 9 | * @param {Context} context eslint context
|
---|
| 10 | * @param {ASTNode} node The AST node to check
|
---|
| 11 | * @param {string} variable The variable name to check
|
---|
| 12 | * @returns {boolean} True if createElement is destructured from the pragma
|
---|
| 13 | */
|
---|
| 14 | module.exports = function isDestructuredFromPragmaImport(context, node, variable) {
|
---|
| 15 | const pragma = pragmaUtil.getFromContext(context);
|
---|
| 16 | const variableInScope = variableUtil.getVariableFromContext(context, node, variable);
|
---|
| 17 | if (variableInScope) {
|
---|
| 18 | const latestDef = variableUtil.getLatestVariableDefinition(variableInScope);
|
---|
| 19 | if (latestDef) {
|
---|
| 20 | // check if latest definition is a variable declaration: 'variable = value'
|
---|
| 21 | if (latestDef.node.type === 'VariableDeclarator' && latestDef.node.init) {
|
---|
| 22 | // check for: 'variable = pragma.variable'
|
---|
| 23 | if (
|
---|
| 24 | latestDef.node.init.type === 'MemberExpression'
|
---|
| 25 | && latestDef.node.init.object.type === 'Identifier'
|
---|
| 26 | && latestDef.node.init.object.name === pragma
|
---|
| 27 | ) {
|
---|
| 28 | return true;
|
---|
| 29 | }
|
---|
| 30 | // check for: '{variable} = pragma'
|
---|
| 31 | if (
|
---|
| 32 | latestDef.node.init.type === 'Identifier'
|
---|
| 33 | && latestDef.node.init.name === pragma
|
---|
| 34 | ) {
|
---|
| 35 | return true;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | // "require('react')"
|
---|
| 39 | let requireExpression = null;
|
---|
| 40 |
|
---|
| 41 | // get "require('react')" from: "{variable} = require('react')"
|
---|
| 42 | if (latestDef.node.init.type === 'CallExpression') {
|
---|
| 43 | requireExpression = latestDef.node.init;
|
---|
| 44 | }
|
---|
| 45 | // get "require('react')" from: "variable = require('react').variable"
|
---|
| 46 | if (
|
---|
| 47 | !requireExpression
|
---|
| 48 | && latestDef.node.init.type === 'MemberExpression'
|
---|
| 49 | && latestDef.node.init.object.type === 'CallExpression'
|
---|
| 50 | ) {
|
---|
| 51 | requireExpression = latestDef.node.init.object;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | // check proper require.
|
---|
| 55 | if (
|
---|
| 56 | requireExpression
|
---|
| 57 | && requireExpression.callee
|
---|
| 58 | && requireExpression.callee.name === 'require'
|
---|
| 59 | && requireExpression.arguments[0]
|
---|
| 60 | && requireExpression.arguments[0].value === pragma.toLocaleLowerCase()
|
---|
| 61 | ) {
|
---|
| 62 | return true;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | return false;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | // latest definition is an import declaration: import {<variable>} from 'react'
|
---|
| 69 | if (
|
---|
| 70 | latestDef.parent
|
---|
| 71 | && latestDef.parent.type === 'ImportDeclaration'
|
---|
| 72 | && latestDef.parent.source.value === pragma.toLocaleLowerCase()
|
---|
| 73 | ) {
|
---|
| 74 | return true;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | return false;
|
---|
| 79 | };
|
---|