Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-09-27 15:06:36 +02:00
parent 871a660643
commit 63963f6c2b
3 changed files with 37 additions and 27 deletions
@@ -80,11 +80,14 @@ const KNOWN_STYLES = [
/**
* filter function to keep only ImportSpecifier nodes
* @param {import('estree').ImportSpecifier | import('estree').ImportDefaultSpecifier| import('estree').ImportNamespaceSpecifier} specifier
* @param {import('estree').ImportSpecifier | import('estree').ImportDefaultSpecifier | import('estree').ImportNamespaceSpecifier} specifier
* @returns {specifier is import('estree').ImportSpecifier}
*/
function importSpecifiersFilter(specifier) {
return specifier.type === 'ImportSpecifier';
return (
specifier.type === 'ImportSpecifier' &&
specifier.imported.type !== 'Literal'
);
}
/**
@@ -146,12 +149,16 @@ module.exports = {
const specifiers = node.specifiers.filter(importSpecifiersFilter);
const specifiersMap = specifiers.map(
const specifiersMap = specifiers.flatMap(
/**
* transform ImportSpecifier to FixerValues to have a simpler object to work with
* @returns {FixerValues}
* @returns {FixerValues[]}
*/
s => {
if (s.imported.type === 'Literal') {
return [];
}
const value = s.imported.name;
const alias = s.local.name === value ? undefined : s.local.name;
@@ -165,19 +172,24 @@ module.exports = {
const emitComponent = !emitProp;
const emitComponentAndProp =
emitProp &&
specifiers.find(s => s.imported.name === propsMatch[1])?.local
.name;
specifiers.find(
s =>
s.imported.type !== 'Literal' &&
s.imported.name === propsMatch[1],
)?.local.name;
return {
emitComponent: emitComponent || Boolean(emitComponentAndProp),
emitProp,
value,
componentValue: propsMatch ? propsMatch[1] : undefined,
componentAlias: emitComponentAndProp
? emitComponentAndProp
: undefined,
alias,
};
return [
{
emitComponent: emitComponent || Boolean(emitComponentAndProp),
emitProp,
value,
componentValue: propsMatch ? propsMatch[1] : undefined,
componentAlias: emitComponentAndProp
? emitComponentAndProp
: undefined,
alias,
},
];
},
);