Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 92x 92x 92x 92x 92x 92x 1x 1x 91x 91x 91x 91x 91x 91x 91x 91x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 4x 4x 4x 4x 4x 4x 4x 4x 24x 24x 24x 24x 24x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 2x 2x 16x | import { SyntaxKind, type Decorator } from "ts-morph"; /** * Gets the argument of a decorator. * @param decorator The decorator to get the argument from. * @param propertyName The name of the property to get the argument for. * @returns The property assignment of the decorator argument. */ export const getDecoratorArgument = ( decorator: Decorator, propertyName: string, ) => { const args = decorator.getArguments(); if (args.length === 0) { return; } const arg = args[0]; const prop = arg .getDescendantsOfKind(SyntaxKind.PropertyAssignment) .find((n) => n.compilerNode.name.getText() === propertyName); return prop; }; /** * Inserts a value into an array property of a decorator. Creates the decorator property if it does not exist. * @param decorator The decorator to insert the value into. * @param propertyName The name of the property to insert the value into. * @param value The value to insert into the array. */ export const insertIntoDecoratorArgArray = ( decorator: Decorator, propertyName: string, value: string, ) => { let property = getDecoratorArgument(decorator, propertyName); if (!property) { property = decorator .getArguments()[0] .asKind(SyntaxKind.ObjectLiteralExpression)! .addPropertyAssignment({ name: propertyName, initializer: "[]", }); } const propertyInitializer = property.getInitializerIfKind( SyntaxKind.ArrayLiteralExpression, )!; propertyInitializer.addElement(value); }; /** * Deletes a value from an array property of a decorator. * @param decorator The decorator to delete the value from. * @param propertyName The name of the property to delete the value from. * @param value The value to delete from the array. */ export const deleteFromDecoratorArgArray = ( decorator: Decorator, propertyName: string, value: string, ) => { const property = getDecoratorArgument(decorator, propertyName); if (!property) { return; } const propertyInitializer = property.getInitializerIfKind( SyntaxKind.ArrayLiteralExpression, )!; const element = propertyInitializer .getElements() .find((e) => e.getText() === value); if (element) { propertyInitializer.removeElement(element); } }; |