@navikt/aksel
Version:
Aksel command line interface. Codemods and other utilities for Aksel users.
89 lines (88 loc) • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findComponentImport = findComponentImport;
exports.findJSXElement = findJSXElement;
exports.findProps = findProps;
/**
* Finds a component import, accounting for sub-components and aliases.
* Returns the local name of the component. If the component is not found, returns null.
* root should ideally be the root of the file in most cases (imports happen at top level)
*/
function findComponentImport(input) {
const { j, root, name: _name, packageType = "react" } = input;
/* Account for sub-components */
const name = _name.includes(".") ? _name.split(".")[0] : _name;
let foundName = null;
root.find(j.ImportDeclaration).forEach((path) => {
if (packageType === "react" && !isAkselReactImport(path)) {
return;
}
if (packageType === "tokens" && !isAkselTokensImport(path)) {
return;
}
path.node.specifiers.forEach((specifier) => {
if (specifier.type === "ImportSpecifier" &&
specifier.imported.name === name) {
foundName = specifier.local
? specifier.local.name
: specifier.imported.name;
}
});
});
return foundName;
}
/**
* Finds a JSX element in the AST, accounting for sub-components.
*/
function findJSXElement(input) {
const { root, j, name, originalName } = input;
const isSubComponent = originalName.includes(".");
const openingElement = (isSubComponent
? {
name: {
type: "JSXMemberExpression",
object: {
name,
},
property: {
name: originalName.split(".")[1],
},
},
}
: {
name: {
type: "JSXIdentifier",
name,
},
});
return root.find(j.JSXElement, {
openingElement,
});
}
/**
* Finds a prop in a JSX element.
*/
function findProps(input) {
const { j, path, name } = input;
return j(path.get("openingElement")).find(j.JSXAttribute, {
name: {
name,
},
});
}
/**
* Checks if an import is from @navikt/ds-react.
*/
function isAkselReactImport(path) {
const importSource = path.node.source.value;
return (typeof importSource === "string" &&
importSource.startsWith("@navikt/ds-react"));
}
/**
* Checks if an import is from @navikt/ds-tokens.
*/
function isAkselTokensImport(path) {
const importSource = path.node.source.value;
return (typeof importSource === "string" &&
importSource.startsWith("@navikt/ds-tokens"));
}