@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
42 lines (41 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createIsFromImportSourceFor = createIsFromImportSourceFor;
var _isImportedJsxElement = require("./is-imported-jsx-element");
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 (0, _isImportedJsxElement.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;
}