UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

36 lines 1.42 kB
import { isImportedJSXElement } from './is-imported-jsx-element'; export function createIsFromImportSourceFor() { for (var _len = arguments.length, importSources = new Array(_len), _key = 0; _key < _len; _key++) { importSources[_key] = arguments[_key]; } var literalImportSources = importSources.filter(function (s) { return typeof s === 'string'; }); var matchImportSources = importSources.filter(function (s) { return s instanceof RegExp; }); var varImportSourceMap = new Map(); function isFromImportSource(node) { return isImportedJSXElement(node) && varImportSourceMap.has(node.openingElement.name.name); } isFromImportSource.importDeclarationHook = function (node) { var source = node.source.value; if (typeof source !== 'string' || !(literalImportSources.includes(source) || matchImportSources.some(function (r) { return r.test(source); }))) { return; } node.specifiers.filter(function (spec) { return ['ImportSpecifier', 'ImportDefaultSpecifier'].includes(spec.type); }).forEach(function (spec) { return varImportSourceMap.set(spec.local.name, source); }); }; isFromImportSource.getImportSource = function (node) { if (!isFromImportSource(node)) { throw new Error('Node is not an imported JSX element'); } return varImportSourceMap.get(node.openingElement.name.name); }; return isFromImportSource; }