All files / packages/fds-uif/generator-lwc/src/blocks createRenderWhenGetter.ts

100% Statements 3/3
100% Branches 2/2
100% Functions 1/1
100% Lines 3/3

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                                                  20x         20x                     20x                
import { factory, SyntaxKind } from '../utils/ts7.js';
import type { GetAccessorDeclaration } from '@typescript/native/unstable/ast';
import type { RenderWhenPropMatch } from '@fds-uif/generator-base';
 
/**
 * Build a getter accessor for a `renderWhen` prop match. Two forms, mirroring
 * the static generator's `isPropMatchExcluded`:
 *
 * - `{ prop, eq }` → equality test:
 *   ```ts
 *   get isIconPositionRight() {
 *     return this.iconPosition === 'right';
 *   }
 *   ```
 * - `{ prop, filled: true }` → non-empty (truthy) test:
 *   ```ts
 *   get isIconNameFilled() {
 *     return !!this.iconName;
 *   }
 *   ```
 */
export function createRenderWhenGetter(
  getterName: string,
  match: RenderWhenPropMatch,
): GetAccessorDeclaration {
  const propAccess = factory.createPropertyAccessExpression(
    factory.createThis(),
    factory.createIdentifier(match.prop),
  );
  const cmp =
    match.eq !== undefined
      ? factory.createBinaryExpression(
          propAccess,
          SyntaxKind.EqualsEqualsEqualsToken,
          factory.createStringLiteral(match.eq),
        )
      : // `filled` form: coerce the prop to a boolean (`!!this.prop`).
        factory.createPrefixUnaryExpression(
          SyntaxKind.ExclamationToken,
          factory.createPrefixUnaryExpression(SyntaxKind.ExclamationToken, propAccess),
        );
  return factory.createGetAccessorDeclaration(
    undefined,
    factory.createIdentifier(getterName),
    [],
    undefined,
    factory.createBlock([factory.createReturnStatement(cmp)], true),
  );
}