CodeSampleX

示例

typescript 7.0.2: SyntaxKind

已验证示例 — npm typescript 7.0.2: SyntaxKind. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: SyntaxKind enum defines distinct integer identifiers for…

sha256:b8d4d7cc31b3bca3c14be398dfc48077fee3f5da5751ce4d6a23202fa95e1ae9

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
verify typescript.SyntaxKind in pkg:npm/typescript@7.0.2
包
符号
  • typescript.SyntaxKind
环境
node 22.23.2
创建时间
2026-08-28T07:04:01Z

契约

  1. SyntaxKind enum defines distinct integer identifiers for tokens, keywords, literals, and AST node types
  2. formatSyntaxKind maps numeric SyntaxKind identifiers to canonical AST node and token names
  3. classifySyntaxKind categorizes SyntaxKind identifiers into tokens, keywords, literals, and AST node kinds
  4. inspectNodeSyntaxKind validates AST nodes and extracts their SyntaxKind metadata and formatted names

文件

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

下载源代码构件 (tar.gz)

源代码

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.SyntaxKind 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.SyntaxKind
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:45fc42debde8450c4c4a45b43de86dd707ad1b0b31ff66353e282e0e1dcfc2d1","constraints":{"executionContext":"node"},"contract":["SyntaxKind enum defines distinct integer identifiers for tokens, keywords, literals, and AST node types","formatSyntaxKind maps numeric SyntaxKind identifiers to canonical AST node and token names","classifySyntaxKind categorizes SyntaxKind identifiers into tokens, keywords, literals, and AST node kinds","inspectNodeSyntaxKind validates AST nodes and extracts their SyntaxKind metadata and formatted names"],"goal":"verify typescript.SyntaxKind in pkg:npm/typescript@7.0.2","kind":"HOW","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"symbols":["typescript.SyntaxKind"]},"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.SyntaxKind"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "typescript-syntax-kind-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "typescript-syntax-kind-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-syntax-kind-sample",
  "version": "1.0.0",
  "description": "Inspect, classify, and validate TypeScript AST syntax kinds and node tokens",
  "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.SyntaxKind in pkg:npm/typescript@7.0.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/typescript@7.0.2"
  ],
  "symbols": [
    "typescript.SyntaxKind"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.mjs
import {
  SyntaxKind,
  formatSyntaxKind,
  isIdentifier,
  isToken,
  isBinaryExpression,
  isVariableStatement,
  isStringLiteral,
  isNumericLiteral,
} from 'typescript/unstable/ast';

import {
  createIdentifier,
  createNumericLiteral,
  createStringLiteral,
  createToken,
  createBinaryExpression,
  createVariableDeclaration,
  createVariableDeclarationList,
  createVariableStatement,
} from 'typescript/unstable/ast/factory';

/**
 * Checks whether a given value is a valid SyntaxKind enum number.
 *
 * @param {unknown} kind
 * @returns {boolean}
 */
export function isSyntaxKind(kind) {
  return typeof kind === 'number' && Number.isInteger(kind) && typeof SyntaxKind[kind] === 'string';
}

/**
 * Returns the canonical name for a given SyntaxKind value.
 *
 * @param {number} kind
 * @returns {string}
 */
export function getSyntaxKindName(kind) {
  if (!isSyntaxKind(kind)) {
    throw new TypeError(`Invalid SyntaxKind value: ${kind}`);
  }
  return formatSyntaxKind(kind);
}

/**
 * Classifies a SyntaxKind value into categories (token, keyword, literal, node).
 *
 * @param {number} kind
 * @returns {{
 *   kind: number,
 *   name: string,
 *   category: 'token' | 'keyword' | 'literal' | 'node',
 *   isKeyword: boolean,
 *   isLiteral: boolean,
 *   isNode: boolean,
 *   isToken: boolean
 * }}
 */
export function classifySyntaxKind(kind) {
  if (!isSyntaxKind(kind)) {
    throw new TypeError(`Invalid SyntaxKind value: ${kind}`);
  }

  const isKeyword = kind >= SyntaxKind.FirstKeyword && kind <= SyntaxKind.LastKeyword;
  const isLiteral = (
    kind === SyntaxKind.NumericLiteral ||
    kind === SyntaxKind.BigIntLiteral ||
    kind === SyntaxKind.StringLiteral ||
    kind === SyntaxKind.RegularExpressionLiteral ||
    kind === SyntaxKind.NoSubstitutionTemplateLiteral
  );
  const isToken = kind >= SyntaxKind.FirstToken && kind <= SyntaxKind.LastToken;
  const isNode = kind >= SyntaxKind.FirstNode;

  let category = 'node';
  if (isKeyword) {
    category = 'keyword';
  } else if (isLiteral) {
    category = 'literal';
  } else if (isToken) {
    category = 'token';
  }

  return {
    kind,
    name: formatSyntaxKind(kind),
    category,
    isKeyword,
    isLiteral,
    isNode,
    isToken,
  };
}

/**
 * Inspects an AST node or token and returns its SyntaxKind metadata.
 *
 * @param {object} node
 * @returns {{
 *   kind: number,
 *   kindName: string,
 *   isNode: boolean,
 *   isToken: boolean
 * }}
 */
export function inspectNodeSyntaxKind(node) {
  if (!node || typeof node !== 'object' || typeof node.kind !== 'number') {
    throw new TypeError('Expected an AST node object with a numeric kind property');
  }

  const kind = node.kind;
  if (!isSyntaxKind(kind)) {
    throw new TypeError(`Node kind is not a valid SyntaxKind: ${kind}`);
  }

  const isNode = kind >= SyntaxKind.FirstNode;
  const isToken = kind >= SyntaxKind.FirstToken && kind <= SyntaxKind.LastToken;

  return {
    kind,
    kindName: formatSyntaxKind(kind),
    isNode,
    isToken,
  };
}

/**
 * Builds a binary expression AST node and validates its SyntaxKind structure.
 *
 * @param {string} leftName
 * @param {number} operatorKind
 * @param {number|string} rightValue
 * @returns {object} BinaryExpression AST node
 */
export function buildBinaryExpressionAST(leftName, operatorKind, rightValue) {
  if (typeof leftName !== 'string' || !leftName) {
    throw new TypeError('leftName must be a non-empty string');
  }
  if (!isSyntaxKind(operatorKind)) {
    throw new TypeError(`operatorKind must be a valid SyntaxKind, got: ${operatorKind}`);
  }

  const leftNode = createIdentifier(leftName);
  const operatorToken = createToken(operatorKind);
  let rightNode;
  if (typeof rightValue === 'number') {
    rightNode = createNumericLiteral(String(rightValue));
  } else if (typeof rightValue === 'string') {
    rightNode = createStringLiteral(rightValue);
  } else {
    throw new TypeError('rightValue must be a number or string');
  }

  return createBinaryExpression(undefined, leftNode, undefined, operatorToken, rightNode);
}

/**
 * Builds a variable declaration statement AST node.
 *
 * @param {string} varName
 * @param {number|string} literalValue
 * @returns {object} VariableStatement AST node
 */
export function buildVariableStatementAST(varName, literalValue) {
  if (typeof varName !== 'string' || !varName) {
    throw new TypeError('varName must be a non-empty string');
  }

  const nameNode = createIdentifier(varName);
  let initNode;
  if (typeof literalValue === 'number') {
    initNode = createNumericLiteral(String(literalValue));
  } else if (typeof literalValue === 'string') {
    initNode = createStringLiteral(literalValue);
  } else {
    throw new TypeError('literalValue must be a number or string');
  }

  const decl = createVariableDeclaration(nameNode, undefined, undefined, initNode);
  const declList = createVariableDeclarationList([decl], 0);
  return createVariableStatement(undefined, declList);
}

export {
  SyntaxKind,
  formatSyntaxKind,
  isIdentifier,
  isToken,
  isBinaryExpression,
  isVariableStatement,
  isStringLiteral,
  isNumericLiteral,
  createIdentifier,
  createNumericLiteral,
  createStringLiteral,
  createToken,
  createBinaryExpression,
  createVariableDeclaration,
  createVariableDeclarationList,
  createVariableStatement,
};
test/contract.mjs
import assert from 'node:assert/strict';
import {
  SyntaxKind,
  formatSyntaxKind,
  isSyntaxKind,
  getSyntaxKindName,
  classifySyntaxKind,
  inspectNodeSyntaxKind,
  buildBinaryExpressionAST,
  buildVariableStatementAST,
  isIdentifier,
  isToken,
  isBinaryExpression,
  isVariableStatement,
  isNumericLiteral,
  isStringLiteral,
  createIdentifier,
  createNumericLiteral,
  createStringLiteral,
  createToken,
} from '../src/index.mjs';

// Contract 1: SyntaxKind enum defines distinct integer identifiers for tokens, keywords, literals, and AST node types
{
  assert.strictEqual(typeof SyntaxKind, 'object', 'SyntaxKind must be an enum object');
  assert.strictEqual(typeof SyntaxKind.Identifier, 'number', 'SyntaxKind.Identifier must be a number');
  assert.strictEqual(typeof SyntaxKind.NumericLiteral, 'number', 'SyntaxKind.NumericLiteral must be a number');
  assert.strictEqual(typeof SyntaxKind.StringLiteral, 'number', 'SyntaxKind.StringLiteral must be a number');
  assert.strictEqual(typeof SyntaxKind.PlusToken, 'number', 'SyntaxKind.PlusToken must be a number');
  assert.strictEqual(typeof SyntaxKind.SourceFile, 'number', 'SyntaxKind.SourceFile must be a number');
  assert.strictEqual(typeof SyntaxKind.FunctionDeclaration, 'number', 'SyntaxKind.FunctionDeclaration must be a number');
  assert.strictEqual(typeof SyntaxKind.VariableStatement, 'number', 'SyntaxKind.VariableStatement must be a number');

  assert.strictEqual(isSyntaxKind(SyntaxKind.Identifier), true);
  assert.strictEqual(isSyntaxKind(SyntaxKind.SourceFile), true);
  assert.strictEqual(isSyntaxKind(9999999), false);
  assert.strictEqual(isSyntaxKind('Identifier'), false);
  assert.strictEqual(isSyntaxKind(null), false);
}

// Contract 2: formatSyntaxKind maps numeric SyntaxKind identifiers to canonical AST node and token names
{
  assert.strictEqual(formatSyntaxKind(SyntaxKind.Identifier), 'Identifier');
  assert.strictEqual(formatSyntaxKind(SyntaxKind.SourceFile), 'SourceFile');
  assert.strictEqual(formatSyntaxKind(SyntaxKind.FunctionDeclaration), 'FunctionDeclaration');
  assert.strictEqual(formatSyntaxKind(SyntaxKind.BinaryExpression), 'BinaryExpression');
  assert.strictEqual(formatSyntaxKind(SyntaxKind.PlusToken), 'PlusToken');
  assert.strictEqual(formatSyntaxKind(SyntaxKind.StringLiteral), 'StringLiteral');

  assert.strictEqual(getSyntaxKindName(SyntaxKind.Identifier), 'Identifier');
  assert.strictEqual(getSyntaxKindName(SyntaxKind.VariableStatement), 'VariableStatement');
  assert.throws(() => getSyntaxKindName(-1), { name: 'TypeError' });
}

// Contract 3: classifySyntaxKind categorizes SyntaxKind identifiers into tokens, keywords, literals, and AST node kinds
{
  const idInfo = classifySyntaxKind(SyntaxKind.Identifier);
  assert.strictEqual(idInfo.name, 'Identifier');
  assert.strictEqual(idInfo.category, 'token');
  assert.strictEqual(idInfo.isToken, true);
  assert.strictEqual(idInfo.isKeyword, false);
  assert.strictEqual(idInfo.isNode, false);

  const keywordInfo = classifySyntaxKind(SyntaxKind.TrueKeyword);
  assert.strictEqual(keywordInfo.name, 'TrueKeyword');
  assert.strictEqual(keywordInfo.category, 'keyword');
  assert.strictEqual(keywordInfo.isKeyword, true);
  assert.strictEqual(keywordInfo.isToken, true);
  assert.strictEqual(keywordInfo.isNode, false);

  const litInfo = classifySyntaxKind(SyntaxKind.StringLiteral);
  assert.strictEqual(litInfo.name, 'StringLiteral');
  assert.strictEqual(litInfo.category, 'literal');
  assert.strictEqual(litInfo.isLiteral, true);
  assert.strictEqual(litInfo.isToken, true);

  const nodeInfo = classifySyntaxKind(SyntaxKind.BinaryExpression);
  assert.strictEqual(nodeInfo.name, 'BinaryExpression');
  assert.strictEqual(nodeInfo.category, 'node');
  assert.strictEqual(nodeInfo.isNode, true);
  assert.strictEqual(nodeInfo.isToken, false);

  assert.throws(() => classifySyntaxKind('invalid'), { name: 'TypeError' });
}

// Contract 4: inspectNodeSyntaxKind validates AST nodes and extracts their SyntaxKind metadata and formatted names
{
  const binNode = buildBinaryExpressionAST('total', SyntaxKind.PlusToken, 50);
  assert.strictEqual(isBinaryExpression(binNode), true);
  assert.strictEqual(binNode.kind, SyntaxKind.BinaryExpression);

  const binInspect = inspectNodeSyntaxKind(binNode);
  assert.strictEqual(binInspect.kind, SyntaxKind.BinaryExpression);
  assert.strictEqual(binInspect.kindName, 'BinaryExpression');
  assert.strictEqual(binInspect.isNode, true);

  const leftInspect = inspectNodeSyntaxKind(binNode.left);
  assert.strictEqual(leftInspect.kind, SyntaxKind.Identifier);
  assert.strictEqual(leftInspect.kindName, 'Identifier');
  assert.strictEqual(leftInspect.isToken, true);

  const opInspect = inspectNodeSyntaxKind(binNode.operatorToken);
  assert.strictEqual(opInspect.kind, SyntaxKind.PlusToken);
  assert.strictEqual(opInspect.kindName, 'PlusToken');
  assert.strictEqual(opInspect.isToken, true);

  const rightInspect = inspectNodeSyntaxKind(binNode.right);
  assert.strictEqual(rightInspect.kind, SyntaxKind.NumericLiteral);
  assert.strictEqual(rightInspect.kindName, 'NumericLiteral');

  const varStmt = buildVariableStatementAST('greeting', 'hello');
  assert.strictEqual(isVariableStatement(varStmt), true);
  const stmtInspect = inspectNodeSyntaxKind(varStmt);
  assert.strictEqual(stmtInspect.kind, SyntaxKind.VariableStatement);
  assert.strictEqual(stmtInspect.kindName, 'VariableStatement');

  assert.throws(() => inspectNodeSyntaxKind(null), { name: 'TypeError' });
  assert.throws(() => inspectNodeSyntaxKind({ kind: 'not-a-number' }), { name: 'TypeError' });
}

console.log('All typescript.SyntaxKind contract tests passed.');

原始种子者

匿名