eslint-codemod-utils
Version:
A collection of AST helper functions for more complex ESLint rule fixes.
129 lines (128 loc) • 4.69 kB
JavaScript
import { identifier, importDeclaration, importDefaultSpecifier, importSpecifier, literal, } from '../nodes';
import { isNodeOfType } from './is-node-of-type';
import { AST_NODE_TYPES } from '@typescript-eslint/types';
export function hasJSXAttribute(node, attributeName) {
if (!node.openingElement)
return false;
if (!node.openingElement.attributes.length)
return false;
return node.openingElement.attributes.some((attr) => isNodeOfType(attr, AST_NODE_TYPES.JSXAttribute) &&
attr.name.name === attributeName);
}
export function hasJSXChild(node, childIdentifier) {
const jsxIdentifierMatch = isNodeOfType(node.openingElement.name, AST_NODE_TYPES.JSXIdentifier) &&
node.openingElement.name.name &&
node.openingElement.name.name === childIdentifier;
return (jsxIdentifierMatch ||
Boolean(node.children &&
node.children
.filter((child) => isNodeOfType(child, AST_NODE_TYPES.JSXElement))
.find((child) => hasJSXChild(child, childIdentifier))));
}
/**
* Whether a declaration does or does not include a specified source.
*
* @param declaration
* @param source
* @returns
*/
export function hasImportDeclaration(declaration, source) {
return declaration.source.value === source;
}
/**
*
* @param declaration
* @param specifierId
*/
export function hasImportSpecifier(declaration, importName) {
if (importName === 'default') {
return declaration.specifiers.some((spec) => isNodeOfType(spec, AST_NODE_TYPES.ImportDefaultSpecifier));
}
return declaration.specifiers
.filter((spec) => isNodeOfType(spec, AST_NODE_TYPES.ImportSpecifier))
.some((node) => node.imported.name === importName);
}
/**
* Appends or adds an import specifier to an existing import declaration.
*
* Does not validate whether the insertion is already present.
*
* @param declaration
* @param importName
* @param specifierAlias
* @returns {StringableASTNode<ImportDeclaration>}
*/
export function insertImportSpecifier(declaration, importName, specifierAlias) {
if (importName === 'default' && !specifierAlias) {
throw new Error('A specifier name must be provided when inserting the default import.');
}
const id = identifier(importName);
const newSpecifier = importName === 'default'
? // Narrowed above — `specifierAlias` is guaranteed non-undefined here.
importDefaultSpecifier({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
local: identifier(specifierAlias),
})
: importSpecifier({
imported: identifier(importName),
local: specifierAlias ? identifier(specifierAlias) : id,
});
// `.concat` on a heterogenous specifier array widens to the union — build
// the array explicitly to keep types consistent with `WithoutType<…>`.
return importDeclaration({
...declaration,
specifiers: [...declaration.specifiers, newSpecifier],
});
}
/**
* @example
* ```tsx
* insertImportDeclaration('source', ['specifier', 'second'])
*
* // produces
* import { specifier, second } from 'source'
* ```
*
* @example
* ```tsx
* * insertImportDeclaration('source', ['specifier', { imported: 'second', local: 'other' }])
*
* // produces
* import { specifier, second as other } from 'source'
* ```
*/
export function insertImportDeclaration(source, specifiers) {
return importDeclaration({
source: literal(source),
specifiers: specifiers.map((spec) => {
return spec === 'default'
? importDefaultSpecifier({
local: identifier('__default'),
})
: importSpecifier({
imported: typeof spec === 'string'
? identifier(spec)
: identifier(spec.imported),
local: typeof spec === 'string'
? identifier(spec)
: identifier(spec.local),
});
}),
});
}
/**
* Removes an import specifier to an existing import declaration.
*
* @param declaration
* @param importName
* @returns {StringableASTNode<ESTree.ImportDeclaration>}
*/
export function removeImportSpecifier(declaration, importName) {
return importDeclaration({
...declaration,
specifiers: declaration.specifiers.filter((spec) => importName === 'default'
? spec.type !== 'ImportDefaultSpecifier'
: !(isNodeOfType(spec, AST_NODE_TYPES.ImportSpecifier) &&
spec.imported.name === importName)),
});
}