@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
26 lines • 839 B
JavaScript
/**
* Returns the module name of an identifier, if one exists.
*
* getModuleOfIdentifier(source, 'Button'); // "@atlaskit/button"
*/
export const getModuleOfIdentifier = (source, identifierName) => {
for (const node of source.ast.body) {
if (node.type === 'ImportDeclaration') {
for (const spec of node.specifiers) {
if (spec.type === 'ImportDefaultSpecifier' && spec.local.name === identifierName) {
return {
moduleName: node.source.value + '',
importName: identifierName
};
}
if (spec.type === 'ImportSpecifier' && 'name' in spec.imported && spec.local.name === identifierName) {
return {
moduleName: node.source.value + '',
importName: spec.imported.name
};
}
}
}
}
return undefined;
};