12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 'use strict';
- const pragmaUtil = require('./pragma');
- const variableUtil = require('./variable');
- module.exports = function isDestructuredFromPragmaImport(variable, context) {
- const pragma = pragmaUtil.getFromContext(context);
- const variables = variableUtil.variablesInScope(context);
- const variableInScope = variableUtil.getVariable(variables, variable);
- if (variableInScope) {
- const latestDef = variableUtil.getLatestVariableDefinition(variableInScope);
- if (latestDef) {
-
- if (latestDef.node.type === 'VariableDeclarator' && latestDef.node.init) {
-
- if (
- latestDef.node.init.type === 'MemberExpression'
- && latestDef.node.init.object.type === 'Identifier'
- && latestDef.node.init.object.name === pragma
- ) {
- return true;
- }
-
- if (
- latestDef.node.init.type === 'Identifier'
- && latestDef.node.init.name === pragma
- ) {
- return true;
- }
-
- let requireExpression = null;
-
- if (latestDef.node.init.type === 'CallExpression') {
- requireExpression = latestDef.node.init;
- }
-
- if (
- !requireExpression
- && latestDef.node.init.type === 'MemberExpression'
- && latestDef.node.init.object.type === 'CallExpression'
- ) {
- requireExpression = latestDef.node.init.object;
- }
-
- if (
- requireExpression
- && requireExpression.callee
- && requireExpression.callee.name === 'require'
- && requireExpression.arguments[0]
- && requireExpression.arguments[0].value === pragma.toLocaleLowerCase()
- ) {
- return true;
- }
- return false;
- }
-
- if (
- latestDef.parent
- && latestDef.parent.type === 'ImportDeclaration'
- && latestDef.parent.source.value === pragma.toLocaleLowerCase()
- ) {
- return true;
- }
- }
- }
- return false;
- };
|