UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

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