@progress/kendo-angular-listbox
Version:
Kendo UI for Angular ListBox
126 lines (125 loc) • 5.9 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
function default_1(fileInfo, api) {
const filePath = fileInfo.path;
if (filePath.endsWith('.html')) {
// HTML transformations not needed for this migration
return;
}
const j = api.jscodeshift;
const rootSource = j(fileInfo.source);
tsSelectedIndexTransformer(fileInfo.source, rootSource, j, 'ListBoxComponent');
return rootSource.toSource();
}
/**
* Custom transformer to convert selectedIndex property access to selectedIndices[0]
* for ListBoxComponent instances
*/
const tsSelectedIndexTransformer = (source, root, j, componentType) => {
if (source.includes(componentType)) {
// Find all class properties that are of type ListBoxComponent
const properties = new Set();
// Find properties with type annotations
root.find(j.ClassProperty, {
typeAnnotation: {
typeAnnotation: {
typeName: {
name: componentType,
},
},
},
}).forEach((path) => {
if (path.node.key.type === 'Identifier') {
properties.add(path.node.key.name);
}
});
// Find function parameters of type componentType
const parameters = new Set();
root.find(j.FunctionDeclaration).forEach((path) => {
if (path.node.params) {
path.node.params.forEach((param) => {
if (param.type === 'Identifier' &&
param.typeAnnotation &&
param.typeAnnotation.typeAnnotation?.type === 'TSTypeReference' &&
param.typeAnnotation.typeAnnotation.typeName.type === 'Identifier' &&
param.typeAnnotation.typeAnnotation.typeName.name === componentType) {
parameters.add(param.name);
}
});
}
});
// Also check method declarations in classes
root.find(j.ClassMethod).forEach((path) => {
if (path.node.params) {
path.node.params.forEach((param) => {
if (param.type === 'Identifier' &&
param.typeAnnotation &&
param.typeAnnotation.typeAnnotation?.type === 'TSTypeReference' &&
param.typeAnnotation.typeAnnotation.typeName.type === 'Identifier' &&
param.typeAnnotation.typeAnnotation.typeName.name === componentType) {
parameters.add(param.name);
}
});
}
});
// Also check arrow functions
root.find(j.ArrowFunctionExpression).forEach((path) => {
if (path.node.params) {
path.node.params.forEach((param) => {
if (param.type === 'Identifier' &&
param.typeAnnotation &&
param.typeAnnotation.typeAnnotation?.type === 'TSTypeReference' &&
param.typeAnnotation.typeAnnotation.typeName.type === 'Identifier' &&
param.typeAnnotation.typeAnnotation.typeName.name === componentType) {
parameters.add(param.name);
}
});
}
});
// Find local variable declarations of type componentType
const localVariables = new Set();
root.find(j.VariableDeclarator).forEach((path) => {
if (path.node.id.type === 'Identifier' &&
path.node.id.typeAnnotation &&
path.node.id.typeAnnotation.typeAnnotation?.type === 'TSTypeReference' &&
path.node.id.typeAnnotation.typeAnnotation.typeName.type === 'Identifier' &&
path.node.id.typeAnnotation.typeAnnotation.typeName.name === componentType) {
localVariables.add(path.node.id.name);
}
});
// Find all member expressions where selectedIndex property is accessed on any ListBoxComponent instance
root.find(j.MemberExpression, {
property: {
type: 'Identifier',
name: 'selectedIndex',
},
})
.filter((path) => {
// Filter to only include accesses on properties that are ListBoxComponent instances
if (path.node.object.type === 'MemberExpression' && path.node.object.property.type === 'Identifier') {
// handle properties of this
if (path.node.object.object.type === 'ThisExpression' &&
properties.has(path.node.object.property.name)) {
return true;
}
}
// Handle function parameters and local variables
if (path.node.object.type === 'Identifier') {
return parameters.has(path.node.object.name) || localVariables.has(path.node.object.name);
}
return false;
})
.forEach((path) => {
// Replace selectedIndex with selectedIndices[0]
const memberExpression = j.memberExpression(j.memberExpression(path.node.object, j.identifier('selectedIndices')), j.numericLiteral(0), true // computed property access (uses brackets)
);
// Replace the entire member expression
j(path).replaceWith(memberExpression);
});
}
};