source: imaps-frontend/node_modules/eslint-plugin-react/lib/util/isDestructuredFromPragmaImport.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

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