CodeSampleX

Exemplo

typescript 7.0.2: createTypeReferenceNode

Amostra verificada para npm typescript 7.0.2: createTypeReferenceNode. O contrato rodou em node 22 · linux alpine/x64 · docker e passou.

sha256:d7edcdda882a30242c8b3dac10597c45488e0e53ae554bc33434e8042aabf290

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 npm

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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.createTypeReferenceNode in pkg:npm/typescript@7.0.2
Pacotes
Símbolos
  • typescript.createTypeReferenceNode
Criado
2026-08-28T07:43:51Z

Contrato

  1. createTypeReferenceNode constructs a TypeReferenceNode AST node with SyntaxKind.TypeReference and specified typeName identifier
  2. createTypeReferenceNode accepts typeArguments array to construct generic type references such as Promise<string> or Map<string, number>
  3. isTypeReferenceNode validates AST nodes created by createTypeReferenceNode
  4. updateTypeReferenceNode updates an existing TypeReferenceNode with a new typeName or typeArguments

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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.createTypeReferenceNode 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.createTypeReferenceNode

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:3f89199f88b75e6044607959a2080e358b746c681341f663aa8e9fbd142cb854","contract":["createTypeReferenceNode constructs a TypeReferenceNode AST node with SyntaxKind.TypeReference and specified typeName identifier","createTypeReferenceNode accepts typeArguments array to construct generic type references such as Promise\u003cstring\u003e or Map\u003cstring, number\u003e","isTypeReferenceNode validates AST nodes created by createTypeReferenceNode","updateTypeReferenceNode updates an existing TypeReferenceNode with a new typeName or typeArguments"],"goal":"verify typescript.createTypeReferenceNode in pkg:npm/typescript@7.0.2","kind":"HOW","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"symbols":["typescript.createTypeReferenceNode"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"subject":"pkg:npm/typescript@7.0.2","symbols":["typescript.createTypeReferenceNode"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "typescript-type-reference-node-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "typescript-type-reference-node-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-type-reference-node-sample",
  "version": "1.0.0",
  "description": "Construct and validate TypeScript type reference nodes using createTypeReferenceNode",
  "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.createTypeReferenceNode in pkg:npm/typescript@7.0.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/typescript@7.0.2"
  ],
  "symbols": [
    "typescript.createTypeReferenceNode"
  ]
}
src/index.mjs
import {
  createTypeReferenceNode,
  updateTypeReferenceNode,
  createIdentifier,
  createKeywordTypeNode,
} from 'typescript/unstable/ast/factory';

import {
  SyntaxKind,
  formatSyntaxKind,
  isTypeReferenceNode,
  isIdentifier,
} from 'typescript/unstable/ast';

/**
 * Creates a TypeReferenceNode AST node from a type name (string or Identifier) and optional type arguments.
 *
 * @param {string | object} typeName - Type name string or Identifier AST node.
 * @param {Array<object>} [typeArguments] - Optional array of TypeNode AST nodes.
 * @returns {object} TypeReferenceNode AST node.
 */
export function createTypeReference(typeName, typeArguments) {
  if (!typeName) {
    throw new TypeError('Expected typeName to be a non-empty string or an Identifier AST node');
  }
  const identifierNode = typeof typeName === 'string' ? createIdentifier(typeName) : typeName;
  return createTypeReferenceNode(identifierNode, typeArguments);
}

/**
 * Creates a generic type reference node (e.g. Promise<T>, Map<K, V>).
 *
 * @param {string | object} typeName - Generic type name (e.g. 'Promise', 'Map', 'Array').
 * @param {Array<object>} typeArguments - Array of TypeNode AST nodes.
 * @returns {object} TypeReferenceNode AST node.
 */
export function createGenericType(typeName, typeArguments) {
  if (!Array.isArray(typeArguments) || typeArguments.length === 0) {
    throw new TypeError('Expected typeArguments to be a non-empty array of TypeNode AST nodes');
  }
  return createTypeReference(typeName, typeArguments);
}

/**
 * Validates whether an AST node is a TypeReferenceNode, optionally checking expected type name.
 *
 * @param {object} node - AST node to check.
 * @param {string} [expectedTypeName] - Optional expected type name string.
 * @returns {boolean} True if the node matches the TypeReferenceNode requirements.
 */
export function isTypeReference(node, expectedTypeName) {
  if (!node || typeof node !== 'object' || !isTypeReferenceNode(node)) {
    return false;
  }
  if (expectedTypeName === undefined) {
    return true;
  }
  if (node.typeName && isIdentifier(node.typeName)) {
    return node.typeName.text === expectedTypeName;
  }
  if (typeof node.typeName === 'string') {
    return node.typeName === expectedTypeName;
  }
  return false;
}

/**
 * Extracts metadata and details from a TypeReferenceNode.
 *
 * @param {object} node - TypeReferenceNode AST node.
 * @returns {object} Metadata with kind, kindName, typeName, and typeArgumentsCount.
 */
export function getTypeReferenceDetails(node) {
  if (!node || typeof node !== 'object' || !isTypeReferenceNode(node)) {
    throw new TypeError('Expected a TypeReferenceNode AST node');
  }
  let typeNameText = '';
  if (node.typeName && isIdentifier(node.typeName)) {
    typeNameText = node.typeName.text;
  } else if (typeof node.typeName === 'string') {
    typeNameText = node.typeName;
  }

  const argsCount = Array.isArray(node.typeArguments) ? node.typeArguments.length : 0;

  return {
    kind: node.kind,
    kindName: formatSyntaxKind(node.kind),
    typeName: typeNameText,
    typeArgumentsCount: argsCount,
    hasTypeArguments: argsCount > 0,
  };
}

export {
  createTypeReferenceNode,
  updateTypeReferenceNode,
  createIdentifier,
  createKeywordTypeNode,
  SyntaxKind,
  formatSyntaxKind,
  isTypeReferenceNode,
  isIdentifier,
};
test/contract.mjs
import assert from 'node:assert/strict';
import {
  createTypeReferenceNode,
  updateTypeReferenceNode,
  createIdentifier,
  createKeywordTypeNode,
  createTypeReference,
  createGenericType,
  isTypeReference,
  getTypeReferenceDetails,
  SyntaxKind,
  formatSyntaxKind,
  isTypeReferenceNode,
  isIdentifier,
} from '../src/index.mjs';

// Contract 1: createTypeReferenceNode constructs a TypeReferenceNode AST node with SyntaxKind.TypeReference and specified typeName identifier
{
  const id = createIdentifier('User');
  assert.ok(id, 'Identifier node should be created');
  assert.strictEqual(isIdentifier(id), true, 'id should satisfy isIdentifier');
  assert.strictEqual(id.text, 'User', 'id text should match');

  const typeRefNode = createTypeReferenceNode(id, undefined);
  assert.ok(typeRefNode, 'TypeReferenceNode should be created');
  assert.strictEqual(typeRefNode.kind, SyntaxKind.TypeReference, 'Kind should be TypeReference');
  assert.strictEqual(formatSyntaxKind(typeRefNode.kind), 'TypeReference', 'Formatted kind name should be TypeReference');
  assert.strictEqual(typeRefNode.typeName, id, 'typeName should be the provided identifier node');
  assert.strictEqual(typeRefNode.typeArguments, undefined, 'typeArguments should be undefined when omitted');

  const helperRef = createTypeReference('CustomType');
  assert.strictEqual(isTypeReferenceNode(helperRef), true, 'helperRef should be a TypeReferenceNode');
  assert.strictEqual(helperRef.typeName.text, 'CustomType', 'helperRef typeName should match CustomType');

  const details = getTypeReferenceDetails(typeRefNode);
  assert.strictEqual(details.kind, SyntaxKind.TypeReference, 'details.kind should match');
  assert.strictEqual(details.kindName, 'TypeReference', 'details.kindName should match');
  assert.strictEqual(details.typeName, 'User', 'details.typeName should match');
  assert.strictEqual(details.hasTypeArguments, false, 'details.hasTypeArguments should be false');
}

// Contract 2: createTypeReferenceNode accepts typeArguments array to construct generic type references such as Promise<string> or Map<string, number>
{
  const stringType = createKeywordTypeNode(SyntaxKind.StringKeyword);
  const numberType = createKeywordTypeNode(SyntaxKind.NumberKeyword);

  // Single type argument: Promise<string>
  const promiseId = createIdentifier('Promise');
  const promiseNode = createTypeReferenceNode(promiseId, [stringType]);
  assert.ok(promiseNode, 'Promise TypeReferenceNode should be created');
  assert.strictEqual(promiseNode.kind, SyntaxKind.TypeReference, 'Promise node kind should be TypeReference');
  assert.strictEqual(Array.isArray(promiseNode.typeArguments), true, 'typeArguments should be an array');
  assert.strictEqual(promiseNode.typeArguments.length, 1, 'typeArguments length should be 1');
  assert.strictEqual(promiseNode.typeArguments[0].kind, SyntaxKind.StringKeyword, 'typeArguments[0] should be StringKeyword');

  // Multiple type arguments: Map<string, number>
  const mapNode = createGenericType('Map', [stringType, numberType]);
  assert.strictEqual(isTypeReferenceNode(mapNode), true, 'mapNode should be a TypeReferenceNode');
  assert.strictEqual(mapNode.typeArguments.length, 2, 'mapNode should have 2 type arguments');
  assert.strictEqual(mapNode.typeArguments[0].kind, SyntaxKind.StringKeyword, 'First type argument should be StringKeyword');
  assert.strictEqual(mapNode.typeArguments[1].kind, SyntaxKind.NumberKeyword, 'Second type argument should be NumberKeyword');

  const mapDetails = getTypeReferenceDetails(mapNode);
  assert.strictEqual(mapDetails.typeName, 'Map', 'mapDetails.typeName should be Map');
  assert.strictEqual(mapDetails.typeArgumentsCount, 2, 'mapDetails.typeArgumentsCount should be 2');
  assert.strictEqual(mapDetails.hasTypeArguments, true, 'mapDetails.hasTypeArguments should be true');

  // Throws on invalid arguments
  assert.throws(() => createGenericType('Invalid', null), { name: 'TypeError' }, 'createGenericType should throw on null typeArguments');
  assert.throws(() => createGenericType('Invalid', []), { name: 'TypeError' }, 'createGenericType should throw on empty typeArguments');
  assert.throws(() => createTypeReference(''), { name: 'TypeError' }, 'createTypeReference should throw on empty typeName');
}

// Contract 3: isTypeReferenceNode validates AST nodes created by createTypeReferenceNode
{
  const refNode = createTypeReference('Response');
  assert.strictEqual(isTypeReferenceNode(refNode), true, 'isTypeReferenceNode should return true for valid node');
  assert.strictEqual(isTypeReference(refNode), true, 'isTypeReference helper should return true');
  assert.strictEqual(isTypeReference(refNode, 'Response'), true, 'isTypeReference helper with matching name should return true');
  assert.strictEqual(isTypeReference(refNode, 'Other'), false, 'isTypeReference helper with mismatched name should return false');

  // Non-AST nodes and other node types
  const stringKeywordNode = createKeywordTypeNode(SyntaxKind.StringKeyword);
  assert.strictEqual(isTypeReferenceNode(stringKeywordNode), false, 'isTypeReferenceNode should return false for KeywordTypeNode');
  assert.strictEqual(isTypeReference(stringKeywordNode), false, 'isTypeReference should return false for KeywordTypeNode');
  assert.strictEqual(isTypeReference(null), false, 'isTypeReference should return false for null');
  assert.strictEqual(isTypeReference({}), false, 'isTypeReference should return false for plain object');

  assert.throws(() => getTypeReferenceDetails(null), { name: 'TypeError' }, 'getTypeReferenceDetails should throw on null');
  assert.throws(() => getTypeReferenceDetails(stringKeywordNode), { name: 'TypeError' }, 'getTypeReferenceDetails should throw on non-TypeReferenceNode');
}

// Contract 4: updateTypeReferenceNode updates an existing TypeReferenceNode with a new typeName or typeArguments
{
  const initialId = createIdentifier('OldType');
  const stringType = createKeywordTypeNode(SyntaxKind.StringKeyword);
  const numberType = createKeywordTypeNode(SyntaxKind.NumberKeyword);

  const initialNode = createTypeReferenceNode(initialId, [stringType]);
  assert.strictEqual(initialNode.typeName.text, 'OldType', 'Initial typeName should be OldType');
  assert.strictEqual(initialNode.typeArguments.length, 1, 'Initial typeArguments length should be 1');

  const newId = createIdentifier('UpdatedType');
  const updatedNode = updateTypeReferenceNode(initialNode, newId, [stringType, numberType]);

  assert.ok(updatedNode, 'Updated node should be created');
  assert.strictEqual(isTypeReferenceNode(updatedNode), true, 'Updated node should satisfy isTypeReferenceNode');
  assert.strictEqual(updatedNode.kind, SyntaxKind.TypeReference, 'Updated node kind should be TypeReference');
  assert.strictEqual(updatedNode.typeName.text, 'UpdatedType', 'Updated typeName should be UpdatedType');
  assert.strictEqual(updatedNode.typeArguments.length, 2, 'Updated typeArguments length should be 2');
}

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

Seeder de origem

anônimo