CodeSampleX

Ejemplo

typescript 7.0.2: createExpressionWithTypeArguments

Muestra verificada para npm typescript 7.0.2: createExpressionWithTypeArguments. El contrato se ejecutó en node 22 · linux alpine/x64 · docker y pasó.

sha256:676bf995e635dd8649194bdd26f5717e5798df5d3ae8f190bac5b6ecb6199760

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-28

Caso

HOW
Objetivo
verify typescript.createExpressionWithTypeArguments in pkg:npm/typescript@7.0.2
Paquetes
Símbolos
  • typescript.createExpressionWithTypeArguments
Entorno
node 22.23.2
Creado
2026-08-28T07:14:28Z

Contrato

  1. buildExpressionWithTypeArguments constructs ExpressionWithTypeArguments AST nodes with type arguments and validates them with isExpressionWithTypeArguments
  2. buildHeritageClause creates extends and implements heritage clauses containing ExpressionWithTypeArguments nodes
  3. updateExpressionTypeArguments updates type arguments of an ExpressionWithTypeArguments node while preserving the expression
  4. inspectExpressionWithTypeArguments extracts structured metadata including kind, expression name, and type argument count using type guards

Archivos

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • test/contract.mjs

Descargar el artefacto de código fuente (tar.gz)

Código fuente

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify typescript.createExpressionWithTypeArguments in pkg:npm/typescript@7.0.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/typescript@7.0.2
Demonstrate these symbols/APIs:
  - typescript.createExpressionWithTypeArguments
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:fa2382ef1916892737b81e00e0a5f31cb980fd1a9966887da1c9613b4aa81002","constraints":{"executionContext":"node"},"contract":["buildExpressionWithTypeArguments constructs ExpressionWithTypeArguments AST nodes with type arguments and validates them with isExpressionWithTypeArguments","buildHeritageClause creates extends and implements heritage clauses containing ExpressionWithTypeArguments nodes","updateExpressionTypeArguments updates type arguments of an ExpressionWithTypeArguments node while preserving the expression","inspectExpressionWithTypeArguments extracts structured metadata including kind, expression name, and type argument count using type guards"],"goal":"verify typescript.createExpressionWithTypeArguments in pkg:npm/typescript@7.0.2","kind":"HOW","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"symbols":["typescript.createExpressionWithTypeArguments"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"subject":"pkg:npm/typescript@7.0.2","symbols":["typescript.createExpressionWithTypeArguments"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "typescript-expression-with-type-arguments-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "typescript-expression-with-type-arguments-sample",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "typescript": "7.0.2"
      }
    },
    "node_modules/@typescript/typescript-aix-ppc64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-aix-ppc64/-/typescript-aix-ppc64-7.0.2.tgz",
      "integrity": "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ==",
      "cpu": [
        "ppc64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "aix"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-darwin-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-arm64/-/typescript-darwin-arm64-7.0.2.tgz",
      "integrity": "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "darwin"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-darwin-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-x64/-/typescript-darwin-x64-7.0.2.tgz",
      "integrity": "sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "darwin"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-freebsd-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-arm64/-/typescript-freebsd-arm64-7.0.2.tgz",
      "integrity": "sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "freebsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-freebsd-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-x64/-/typescript-freebsd-x64-7.0.2.tgz",
      "integrity": "sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "freebsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-arm": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm/-/typescript-linux-arm-7.0.2.tgz",
      "integrity": "sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ==",
      "cpu": [
        "arm"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm64/-/typescript-linux-arm64-7.0.2.tgz",
      "integrity": "sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-loong64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-loong64/-/typescript-linux-loong64-7.0.2.tgz",
      "integrity": "sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ==",
      "cpu": [
        "loong64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-mips64el": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-mips64el/-/typescript-linux-mips64el-7.0.2.tgz",
      "integrity": "sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA==",
      "cpu": [
        "mips64el"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-ppc64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-ppc64/-/typescript-linux-ppc64-7.0.2.tgz",
      "integrity": "sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA==",
      "cpu": [
        "ppc64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-riscv64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-riscv64/-/typescript-linux-riscv64-7.0.2.tgz",
      "integrity": "sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ==",
      "cpu": [
        "riscv64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-s390x": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-s390x/-/typescript-linux-s390x-7.0.2.tgz",
      "integrity": "sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw==",
      "cpu": [
        "s390x"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-x64/-/typescript-linux-x64-7.0.2.tgz",
      "integrity": "sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-netbsd-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-arm64/-/typescript-netbsd-arm64-7.0.2.tgz",
      "integrity": "sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "netbsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-netbsd-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-x64/-/typescript-netbsd-x64-7.0.2.tgz",
      "integrity": "sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "netbsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-openbsd-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-arm64/-/typescript-openbsd-arm64-7.0.2.tgz",
      "integrity": "sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "openbsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-openbsd-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-x64/-/typescript-openbsd-x64-7.0.2.tgz",
      "integrity": "sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "openbsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-sunos-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-sunos-x64/-/typescript-sunos-x64-7.0.2.tgz",
      "integrity": "sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "sunos"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-win32-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-arm64/-/typescript-win32-arm64-7.0.2.tgz",
      "integrity": "sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "win32"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-win32-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-x64/-/typescript-win32-x64-7.0.2.tgz",
      "integrity": "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "win32"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/typescript": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/typescript/-/typescript-7.0.2.tgz",
      "integrity": "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA==",
      "license": "Apache-2.0",
      "bin": {
        "tsc": "bin/tsc"
      },
      "engines": {
        "node": ">=16.20.0"
      },
      "optionalDependencies": {
        "@typescript/typescript-aix-ppc64": "7.0.2",
        "@typescript/typescript-darwin-arm64": "7.0.2",
        "@typescript/typescript-darwin-x64": "7.0.2",
        "@typescript/typescript-freebsd-arm64": "7.0.2",
        "@typescript/typescript-freebsd-x64": "7.0.2",
        "@typescript/typescript-linux-arm": "7.0.2",
        "@typescript/typescript-linux-arm64": "7.0.2",
        "@typescript/typescript-linux-loong64": "7.0.2",
        "@typescript/typescript-linux-mips64el": "7.0.2",
        "@typescript/typescript-linux-ppc64": "7.0.2",
        "@typescript/typescript-linux-riscv64": "7.0.2",
        "@typescript/typescript-linux-s390x": "7.0.2",
        "@typescript/typescript-linux-x64": "7.0.2",
        "@typescript/typescript-netbsd-arm64": "7.0.2",
        "@typescript/typescript-netbsd-x64": "7.0.2",
        "@typescript/typescript-openbsd-arm64": "7.0.2",
        "@typescript/typescript-openbsd-x64": "7.0.2",
        "@typescript/typescript-sunos-x64": "7.0.2",
        "@typescript/typescript-win32-arm64": "7.0.2",
        "@typescript/typescript-win32-x64": "7.0.2"
      }
    }
  }
}
package.json
{
  "name": "typescript-expression-with-type-arguments-sample",
  "version": "1.0.0",
  "description": "Construct and validate TypeScript ExpressionWithTypeArguments AST nodes using createExpressionWithTypeArguments",
  "type": "module",
  "main": "src/index.mjs",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "typescript": "7.0.2"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify typescript.createExpressionWithTypeArguments in pkg:npm/typescript@7.0.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/typescript@7.0.2"
  ],
  "symbols": [
    "typescript.createExpressionWithTypeArguments"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.mjs
import {
  createExpressionWithTypeArguments,
  updateExpressionWithTypeArguments,
  createHeritageClause,
  createIdentifier,
  createToken,
  createKeywordTypeNode,
  createTypeReferenceNode,
} from 'typescript/unstable/ast/factory';

import {
  SyntaxKind,
  formatSyntaxKind,
  isExpressionWithTypeArguments,
  isHeritageClause,
  isIdentifier,
  isKeywordTypeNode,
  isTypeReferenceNode,
  isToken,
} from 'typescript/unstable/ast';

/**
 * Converts a type descriptor (string or AST node) to a TypeNode AST node.
 *
 * @param {string | object} typeArg - Type name string or TypeNode AST.
 * @returns {object} TypeNode AST node.
 */
export function toTypeNode(typeArg) {
  if (typeof typeArg === 'string') {
    switch (typeArg) {
      case 'string':
        return createKeywordTypeNode(SyntaxKind.StringKeyword);
      case 'number':
        return createKeywordTypeNode(SyntaxKind.NumberKeyword);
      case 'boolean':
        return createKeywordTypeNode(SyntaxKind.BooleanKeyword);
      case 'any':
        return createKeywordTypeNode(SyntaxKind.AnyKeyword);
      case 'void':
        return createKeywordTypeNode(SyntaxKind.VoidKeyword);
      default:
        return createTypeReferenceNode(createIdentifier(typeArg), undefined);
    }
  }
  return typeArg;
}

/**
 * Builds an ExpressionWithTypeArguments AST node using createExpressionWithTypeArguments.
 *
 * @param {string | object} expression - Identifier name string or AST expression node.
 * @param {Array<string | object>} [typeArguments] - Array of type arguments (strings or TypeNodes).
 * @returns {object} ExpressionWithTypeArguments AST node.
 */
export function buildExpressionWithTypeArguments(expression, typeArguments) {
  const exprNode = typeof expression === 'string' ? createIdentifier(expression) : expression;

  let typeArgNodes;
  if (Array.isArray(typeArguments) && typeArguments.length > 0) {
    typeArgNodes = typeArguments.map(toTypeNode);
  }

  return createExpressionWithTypeArguments(exprNode, typeArgNodes);
}

/**
 * Builds a HeritageClause AST node containing ExpressionWithTypeArguments nodes.
 *
 * @param {number} tokenKind - SyntaxKind.ExtendsKeyword or SyntaxKind.ImplementsKeyword.
 * @param {Array<object | string | { expression: string | object, typeArguments?: Array<string | object> }>} typeExpressions - Types list.
 * @returns {object} HeritageClause AST node.
 */
export function buildHeritageClause(tokenKind, typeExpressions = []) {
  const clauseToken = createToken(tokenKind);
  const typeNodes = typeExpressions.map((item) => {
    if (isExpressionWithTypeArguments(item)) {
      return item;
    }
    if (typeof item === 'string') {
      return buildExpressionWithTypeArguments(item);
    }
    if (item && typeof item === 'object') {
      return buildExpressionWithTypeArguments(item.expression, item.typeArguments);
    }
    throw new TypeError('Invalid type expression in heritage clause');
  });

  return createHeritageClause(clauseToken, typeNodes);
}

/**
 * Updates type arguments of an ExpressionWithTypeArguments AST node.
 *
 * @param {object} node - Existing ExpressionWithTypeArguments AST node.
 * @param {Array<string | object>} [newTypeArguments] - New type arguments.
 * @returns {object} Updated ExpressionWithTypeArguments AST node.
 */
export function updateExpressionTypeArguments(node, newTypeArguments) {
  if (!node || !isExpressionWithTypeArguments(node)) {
    throw new TypeError('Expected an ExpressionWithTypeArguments AST node');
  }

  let typeArgNodes;
  if (Array.isArray(newTypeArguments) && newTypeArguments.length > 0) {
    typeArgNodes = newTypeArguments.map(toTypeNode);
  }

  const expr = node._data?.expression ?? node.expression;
  return updateExpressionWithTypeArguments(node, expr, typeArgNodes);
}

/**
 * Inspects an ExpressionWithTypeArguments AST node and extracts structured metadata.
 *
 * @param {object} node - AST node to inspect.
 * @returns {{
 *   kind: number,
 *   kindName: string,
 *   isExpressionWithTypeArguments: boolean,
 *   expressionName: string,
 *   typeArgumentCount: number,
 *   typeArguments: string[]
 * }}
 */
export function inspectExpressionWithTypeArguments(node) {
  if (!node || typeof node !== 'object') {
    throw new TypeError('Expected an AST node object');
  }

  const isEwta = isExpressionWithTypeArguments(node);
  if (!isEwta) {
    return {
      kind: node.kind,
      kindName: formatSyntaxKind(node.kind),
      isExpressionWithTypeArguments: false,
      expressionName: '',
      typeArgumentCount: 0,
      typeArguments: [],
    };
  }

  const expr = node._data?.expression ?? node.expression;
  let exprName = '';
  if (expr) {
    exprName = expr._data?.text ?? expr.text ?? '';
  }

  const rawTypeArgs = node._data?.typeArguments ?? node.typeArguments ?? [];
  const typeArguments = Array.isArray(rawTypeArgs)
    ? rawTypeArgs.map((ta) => {
        if (isKeywordTypeNode(ta)) {
          return formatSyntaxKind(ta.kind);
        }
        if (isTypeReferenceNode(ta)) {
          const tnName = ta._data?.typeName ?? ta.typeName;
          return tnName?._data?.text ?? tnName?.text ?? formatSyntaxKind(ta.kind);
        }
        return formatSyntaxKind(ta.kind);
      })
    : [];

  return {
    kind: node.kind,
    kindName: formatSyntaxKind(node.kind),
    isExpressionWithTypeArguments: true,
    expressionName: exprName,
    typeArgumentCount: typeArguments.length,
    typeArguments,
  };
}

export const typescript = {
  createExpressionWithTypeArguments,
  updateExpressionWithTypeArguments,
};

export {
  createExpressionWithTypeArguments,
  updateExpressionWithTypeArguments,
  createHeritageClause,
  createIdentifier,
  createToken,
  createKeywordTypeNode,
  createTypeReferenceNode,
  isExpressionWithTypeArguments,
  isHeritageClause,
  isIdentifier,
  isKeywordTypeNode,
  isTypeReferenceNode,
  isToken,
  SyntaxKind,
  formatSyntaxKind,
};
test/contract.mjs
import assert from 'node:assert/strict';
import {
  buildExpressionWithTypeArguments,
  buildHeritageClause,
  updateExpressionTypeArguments,
  inspectExpressionWithTypeArguments,
  createExpressionWithTypeArguments,
  updateExpressionWithTypeArguments,
  createHeritageClause,
  createIdentifier,
  createKeywordTypeNode,
  createTypeReferenceNode,
  createToken,
  isExpressionWithTypeArguments,
  isHeritageClause,
  isIdentifier,
  isKeywordTypeNode,
  isTypeReferenceNode,
  isToken,
  SyntaxKind,
  formatSyntaxKind,
  typescript,
} from '../src/index.mjs';

// Contract 1: buildExpressionWithTypeArguments constructs ExpressionWithTypeArguments AST nodes with type arguments and validates them with isExpressionWithTypeArguments
{
  // Simple type argument: BaseService<string>
  const node = buildExpressionWithTypeArguments('BaseService', ['string']);

  assert.ok(node, 'ExpressionWithTypeArguments node should be created');
  assert.strictEqual(isExpressionWithTypeArguments(node), true, 'Node should satisfy isExpressionWithTypeArguments');
  assert.strictEqual(formatSyntaxKind(node.kind), 'ExpressionWithTypeArguments', 'Kind name should be ExpressionWithTypeArguments');

  const expr = node._data?.expression ?? node.expression;
  assert.strictEqual(isIdentifier(expr), true, 'Inner expression should be an Identifier');
  assert.strictEqual(expr._data?.text ?? expr.text, 'BaseService', 'Identifier name should be BaseService');

  const typeArgs = node._data?.typeArguments ?? node.typeArguments;
  assert.strictEqual(Array.isArray(typeArgs), true, 'typeArguments should be an array');
  assert.strictEqual(typeArgs.length, 1, 'typeArguments should have length 1');
  assert.strictEqual(isKeywordTypeNode(typeArgs[0]), true, 'Type argument should be keyword type node');
  assert.strictEqual(typeArgs[0].kind, SyntaxKind.StringKeyword, 'Type argument should be StringKeyword');

  // Multiple and complex type arguments: Repository<User, number, boolean>
  const customType = createTypeReferenceNode(createIdentifier('User'), undefined);
  const multiNode = buildExpressionWithTypeArguments('Repository', [customType, 'number', 'boolean']);
  assert.strictEqual(isExpressionWithTypeArguments(multiNode), true, 'Multi-type node should satisfy type guard');
  const multiArgs = multiNode._data?.typeArguments ?? multiNode.typeArguments;
  assert.strictEqual(multiArgs.length, 3, 'Multi-type node should have 3 type arguments');

  // Direct factory call via namespace
  const directNode = typescript.createExpressionWithTypeArguments(createIdentifier('Entity'), undefined);
  assert.strictEqual(isExpressionWithTypeArguments(directNode), true, 'Direct namespace factory should work');
}

// Contract 2: buildHeritageClause creates extends and implements heritage clauses containing ExpressionWithTypeArguments nodes
{
  // Extends heritage clause with single base class: extends BaseEntity<string>
  const extendsClause = buildHeritageClause(SyntaxKind.ExtendsKeyword, [
    { expression: 'BaseEntity', typeArguments: ['string'] },
  ]);

  assert.ok(extendsClause, 'HeritageClause node should be created');
  assert.strictEqual(isHeritageClause(extendsClause), true, 'Node should satisfy isHeritageClause');
  assert.strictEqual(formatSyntaxKind(extendsClause.kind), 'HeritageClause', 'Kind name should be HeritageClause');

  const clauseToken = extendsClause._data?.token ?? extendsClause.token;
  assert.strictEqual(clauseToken.kind, SyntaxKind.ExtendsKeyword, 'Token kind should be ExtendsKeyword');

  const types = extendsClause._data?.types ?? extendsClause.types;
  assert.strictEqual(types.length, 1, 'Heritage clause should have 1 type expression');
  assert.strictEqual(isExpressionWithTypeArguments(types[0]), true, 'Type element should be ExpressionWithTypeArguments');

  // Implements heritage clause with multiple interfaces: implements Printable<void>, Serializable
  const implementsClause = buildHeritageClause(SyntaxKind.ImplementsKeyword, [
    { expression: 'Printable', typeArguments: ['void'] },
    'Serializable',
  ]);

  assert.strictEqual(isHeritageClause(implementsClause), true, 'Implements clause should satisfy isHeritageClause');
  const implToken = implementsClause._data?.token ?? implementsClause.token;
  assert.strictEqual(implToken.kind, SyntaxKind.ImplementsKeyword, 'Token kind should be ImplementsKeyword');
  const implTypes = implementsClause._data?.types ?? implementsClause.types;
  assert.strictEqual(implTypes.length, 2, 'Implements clause should contain 2 types');
  assert.strictEqual(isExpressionWithTypeArguments(implTypes[0]), true, 'First element should be ExpressionWithTypeArguments');
  assert.strictEqual(isExpressionWithTypeArguments(implTypes[1]), true, 'Second element should be ExpressionWithTypeArguments');
}

// Contract 3: updateExpressionTypeArguments updates type arguments of an ExpressionWithTypeArguments node while preserving the expression
{
  const initialNode = buildExpressionWithTypeArguments('Container', ['string']);
  const updatedNode = updateExpressionTypeArguments(initialNode, ['number', 'boolean']);

  assert.ok(updatedNode, 'Updated node should exist');
  assert.strictEqual(isExpressionWithTypeArguments(updatedNode), true, 'Updated node should satisfy isExpressionWithTypeArguments');

  const initialExpr = initialNode._data?.expression ?? initialNode.expression;
  const updatedExpr = updatedNode._data?.expression ?? updatedNode.expression;
  assert.strictEqual(
    updatedExpr._data?.text ?? updatedExpr.text,
    initialExpr._data?.text ?? initialExpr.text,
    'Expression identifier name must be preserved'
  );

  const newArgs = updatedNode._data?.typeArguments ?? updatedNode.typeArguments;
  assert.strictEqual(newArgs.length, 2, 'Updated node should have 2 type arguments');
  assert.strictEqual(newArgs[0].kind, SyntaxKind.NumberKeyword, 'First type arg should be NumberKeyword');
  assert.strictEqual(newArgs[1].kind, SyntaxKind.BooleanKeyword, 'Second type arg should be BooleanKeyword');

  assert.throws(
    () => updateExpressionTypeArguments(null, ['string']),
    { name: 'TypeError' },
    'updateExpressionTypeArguments should throw TypeError for non-AST input'
  );
}

// Contract 4: inspectExpressionWithTypeArguments extracts structured metadata including kind, expression name, and type argument count using type guards
{
  const node = buildExpressionWithTypeArguments('CacheManager', ['string', 'number']);
  const info = inspectExpressionWithTypeArguments(node);

  assert.strictEqual(info.kindName, 'ExpressionWithTypeArguments', 'kindName should be ExpressionWithTypeArguments');
  assert.strictEqual(info.isExpressionWithTypeArguments, true, 'isExpressionWithTypeArguments should be true');
  assert.strictEqual(info.expressionName, 'CacheManager', 'expressionName should match');
  assert.strictEqual(info.typeArgumentCount, 2, 'typeArgumentCount should be 2');
  assert.deepStrictEqual(info.typeArguments, ['StringKeyword', 'NumberKeyword'], 'typeArguments array should match');

  const emptyNode = buildExpressionWithTypeArguments('PlainBase');
  const emptyInfo = inspectExpressionWithTypeArguments(emptyNode);
  assert.strictEqual(emptyInfo.typeArgumentCount, 0, 'Empty type arguments count should be 0');
  assert.deepStrictEqual(emptyInfo.typeArguments, [], 'Empty type arguments should be empty array');

  const notEwta = createIdentifier('SimpleIdentifier');
  const notEwtaInfo = inspectExpressionWithTypeArguments(notEwta);
  assert.strictEqual(notEwtaInfo.isExpressionWithTypeArguments, false, 'isExpressionWithTypeArguments should be false for Identifier');

  assert.throws(
    () => inspectExpressionWithTypeArguments(null),
    { name: 'TypeError' },
    'inspectExpressionWithTypeArguments should throw TypeError for invalid input'
  );
}

console.log('All pkg:npm/typescript@7.0.2 contracts passed successfully.');

Seeder de origen

anónimo