CodeSampleX

示例

typescript 7.0.2: isPropertyDeclaration

已验证示例 — npm typescript 7.0.2: isPropertyDeclaration. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: isPropertyDeclaration returns true for AST…

sha256:e13b09aa9dd8240a34124ee6748617b4aa2f37f09cf57f5e84a5c8276d79eda5

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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.isPropertyDeclaration in pkg:npm/typescript@7.0.2
包
符号
  • typescript.isPropertyDeclaration
创建时间
2026-08-28T08:01:47Z

契约

  1. isPropertyDeclaration returns true for AST PropertyDeclaration nodes constructed with createPropertyDeclaration
  2. isPropertyDeclaration returns false for other AST node types such as Identifier, MethodDeclaration, and StringLiteral
  3. isPropertyDeclaration throws TypeError when evaluated on null or undefined
  4. filterPropertyDeclarations extracts and validates PropertyDeclaration nodes from class members using isPropertyDeclaration
  5. findPropertyDeclarationByName and validatePropertyDeclaration inspect PropertyDeclaration nodes and extract structural metadata

文件

  • 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.isPropertyDeclaration 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.isPropertyDeclaration

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:859bc20b95ccb2e89c0327fc28307064c4f6cf72e79a9371d549a3cfb26592ab","contract":["isPropertyDeclaration returns true for AST PropertyDeclaration nodes constructed with createPropertyDeclaration","isPropertyDeclaration returns false for other AST node types such as Identifier, MethodDeclaration, and StringLiteral","isPropertyDeclaration throws TypeError when evaluated on null or undefined","filterPropertyDeclarations extracts and validates PropertyDeclaration nodes from class members using isPropertyDeclaration","findPropertyDeclarationByName and validatePropertyDeclaration inspect PropertyDeclaration nodes and extract structural metadata"],"goal":"verify typescript.isPropertyDeclaration in pkg:npm/typescript@7.0.2","kind":"HOW","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"symbols":["typescript.isPropertyDeclaration"]},"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.isPropertyDeclaration"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-typescript-is-property-declaration",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-typescript-is-property-declaration",
      "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": "sample-typescript-is-property-declaration",
  "version": "1.0.0",
  "description": "Construct and validate TypeScript property declarations using isPropertyDeclaration",
  "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.isPropertyDeclaration in pkg:npm/typescript@7.0.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/typescript@7.0.2"
  ],
  "symbols": [
    "typescript.isPropertyDeclaration"
  ]
}
src/index.mjs
import {
  createPropertyDeclaration,
  createIdentifier,
  createKeywordTypeNode,
  createStringLiteral,
  createNumericLiteral,
  createClassDeclaration,
  createMethodDeclaration
} from 'typescript/unstable/ast/factory';

import {
  isPropertyDeclaration,
  SyntaxKind
} from 'typescript/unstable/ast';

/**
 * Validates that an AST node is a TypeScript PropertyDeclaration node.
 *
 * @param {object} node - AST node to check.
 * @returns {object} Metadata description of the property declaration.
 * @throws {TypeError} If node is missing or not a PropertyDeclaration AST node.
 */
export function validatePropertyDeclaration(node) {
  if (!node || typeof node !== 'object') {
    throw new TypeError('Expected an AST node object');
  }
  if (!isPropertyDeclaration(node)) {
    throw new TypeError('Expected a TypeScript PropertyDeclaration AST node');
  }
  return {
    name: node.name && typeof node.name === 'object' ? node.name.text : undefined,
    kind: node.kind,
    hasInitializer: Boolean(node.initializer),
    hasType: Boolean(node.type)
  };
}

/**
 * Creates a validated PropertyDeclaration AST node with the given name, optional type, and initializer.
 *
 * @param {string} name - Property identifier name.
 * @param {number} [typeKind] - SyntaxKind keyword type (e.g. SyntaxKind.StringKeyword).
 * @param {object} [initializer] - AST node initializer.
 * @returns {object} The created PropertyDeclaration AST node.
 * @throws {TypeError} If name is not a non-empty string.
 */
export function createSafeProperty(name, typeKind, initializer) {
  if (typeof name !== 'string' || name.trim().length === 0) {
    throw new TypeError('Property name must be a non-empty string');
  }
  const nameNode = createIdentifier(name);
  const typeNode = typeKind ? createKeywordTypeNode(typeKind) : undefined;
  const propNode = createPropertyDeclaration(undefined, nameNode, undefined, typeNode, initializer);
  if (!isPropertyDeclaration(propNode)) {
    throw new Error('Failed to construct a valid PropertyDeclaration AST node');
  }
  return propNode;
}

/**
 * Filters an array of AST nodes and returns only those that are valid PropertyDeclaration nodes.
 *
 * @param {object[]} members - Array of candidate class members or AST nodes.
 * @returns {object[]} Array of validated PropertyDeclaration AST nodes.
 */
export function filterPropertyDeclarations(members) {
  if (!Array.isArray(members)) {
    return [];
  }
  return members.filter(member => member && typeof member === 'object' && isPropertyDeclaration(member));
}

/**
 * Finds a property declaration by name within a class declaration AST node.
 *
 * @param {object} classDecl - ClassDeclaration AST node.
 * @param {string} propertyName - Name of the property to search for.
 * @returns {object|undefined} Matching PropertyDeclaration AST node or undefined.
 */
export function findPropertyDeclarationByName(classDecl, propertyName) {
  if (!classDecl || typeof classDecl !== 'object' || !Array.isArray(classDecl.members)) {
    return undefined;
  }
  return classDecl.members.find(member =>
    isPropertyDeclaration(member) &&
    member.name &&
    member.name.text === propertyName
  );
}

/**
 * Checks whether a value is a valid PropertyDeclaration AST node without throwing.
 *
 * @param {unknown} node - Candidate value to test.
 * @returns {boolean} True if value is a PropertyDeclaration AST node, false otherwise.
 */
export function isPropertyDeclarationNode(node) {
  if (!node || typeof node !== 'object') {
    return false;
  }
  return isPropertyDeclaration(node);
}

export {
  isPropertyDeclaration,
  createPropertyDeclaration,
  createIdentifier,
  createKeywordTypeNode,
  createStringLiteral,
  createNumericLiteral,
  createClassDeclaration,
  createMethodDeclaration,
  SyntaxKind
};
test/contract.mjs
import assert from 'node:assert/strict';
import {
  createPropertyDeclaration,
  createIdentifier,
  createKeywordTypeNode,
  createStringLiteral,
  createNumericLiteral,
  createClassDeclaration,
  createMethodDeclaration
} from 'typescript/unstable/ast/factory';

import {
  isPropertyDeclaration,
  validatePropertyDeclaration,
  createSafeProperty,
  filterPropertyDeclarations,
  findPropertyDeclarationByName,
  isPropertyDeclarationNode,
  SyntaxKind
} from '../src/index.mjs';

// Contract 1: isPropertyDeclaration returns true for AST PropertyDeclaration nodes constructed with createPropertyDeclaration
{
  const idNode = createIdentifier('count');
  const typeNode = createKeywordTypeNode(SyntaxKind.NumberKeyword);
  const initNode = createNumericLiteral('42');
  const propNode = createPropertyDeclaration(undefined, idNode, undefined, typeNode, initNode);

  assert.strictEqual(isPropertyDeclaration(propNode), true, 'createPropertyDeclaration node must satisfy isPropertyDeclaration');
  assert.strictEqual(propNode.kind, SyntaxKind.PropertyDeclaration, 'kind must match SyntaxKind.PropertyDeclaration');
  assert.strictEqual(propNode.name.text, 'count', 'name text must match identifier');
  assert.strictEqual(isPropertyDeclarationNode(propNode), true, 'isPropertyDeclarationNode wrapper must return true');

  const safeProp = createSafeProperty('title', SyntaxKind.StringKeyword, createStringLiteral('Antigravity'));
  assert.strictEqual(isPropertyDeclaration(safeProp), true, 'createSafeProperty output must satisfy isPropertyDeclaration');
}

// Contract 2: isPropertyDeclaration returns false for other AST node types such as Identifier, MethodDeclaration, and StringLiteral
{
  const idNode = createIdentifier('username');
  assert.strictEqual(isPropertyDeclaration(idNode), false, 'Identifier must not satisfy isPropertyDeclaration');
  assert.notStrictEqual(idNode.kind, SyntaxKind.PropertyDeclaration, 'Identifier kind must not be PropertyDeclaration');

  const strLiteral = createStringLiteral('test value');
  assert.strictEqual(isPropertyDeclaration(strLiteral), false, 'StringLiteral must not satisfy isPropertyDeclaration');

  const methodNode = createMethodDeclaration(
    undefined,
    undefined,
    createIdentifier('execute'),
    undefined,
    undefined,
    [],
    undefined,
    undefined
  );
  assert.strictEqual(isPropertyDeclaration(methodNode), false, 'MethodDeclaration must not satisfy isPropertyDeclaration');

  const fakeNode = { kind: SyntaxKind.PropertyDeclaration, name: idNode };
  assert.strictEqual(isPropertyDeclaration(fakeNode), true, 'Node with PropertyDeclaration kind satisfies isPropertyDeclaration');
}

// Contract 3: isPropertyDeclaration throws TypeError when evaluated on null or undefined
{
  assert.throws(
    () => isPropertyDeclaration(null),
    {
      name: 'TypeError'
    },
    'isPropertyDeclaration on null should throw TypeError'
  );

  assert.throws(
    () => isPropertyDeclaration(undefined),
    {
      name: 'TypeError'
    },
    'isPropertyDeclaration on undefined should throw TypeError'
  );

  assert.strictEqual(isPropertyDeclarationNode(null), false, 'Safe wrapper must return false for null');
  assert.strictEqual(isPropertyDeclarationNode(undefined), false, 'Safe wrapper must return false for undefined');
  assert.strictEqual(isPropertyDeclarationNode('not an ast node'), false, 'Safe wrapper must return false for non-object');
}

// Contract 4: filterPropertyDeclarations extracts and validates PropertyDeclaration nodes from class members using isPropertyDeclaration
{
  const prop1 = createSafeProperty('id', SyntaxKind.NumberKeyword);
  const prop2 = createSafeProperty('email', SyntaxKind.StringKeyword);
  const method = createMethodDeclaration(
    undefined,
    undefined,
    createIdentifier('getEmail'),
    undefined,
    undefined,
    [],
    undefined,
    undefined
  );

  const classDecl = createClassDeclaration(
    undefined,
    createIdentifier('Account'),
    undefined,
    undefined,
    [prop1, method, prop2]
  );

  const props = filterPropertyDeclarations(classDecl.members);
  assert.strictEqual(props.length, 2, 'Should filter exactly 2 property declarations');
  assert.strictEqual(props[0], prop1, 'First filtered property should match prop1');
  assert.strictEqual(props[1], prop2, 'Second filtered property should match prop2');

  assert.deepStrictEqual(filterPropertyDeclarations(null), [], 'Null should return empty array');
  assert.deepStrictEqual(filterPropertyDeclarations(undefined), [], 'Undefined should return empty array');
  assert.deepStrictEqual(filterPropertyDeclarations([null, undefined, {}]), [], 'Non-property items should be filtered out');
}

// Contract 5: findPropertyDeclarationByName and validatePropertyDeclaration inspect PropertyDeclaration nodes and extract structural metadata
{
  const prop = createSafeProperty('status', SyntaxKind.StringKeyword, createStringLiteral('active'));
  const classDecl = createClassDeclaration(
    undefined,
    createIdentifier('Task'),
    undefined,
    undefined,
    [prop]
  );

  const found = findPropertyDeclarationByName(classDecl, 'status');
  assert.strictEqual(found, prop, 'Found property declaration must match prop');

  const notFound = findPropertyDeclarationByName(classDecl, 'nonExistent');
  assert.strictEqual(notFound, undefined, 'Non-existent property should return undefined');

  assert.strictEqual(findPropertyDeclarationByName(null, 'status'), undefined, 'Null class declaration returns undefined');

  const meta = validatePropertyDeclaration(prop);
  assert.strictEqual(meta.name, 'status', 'Extracted metadata name must match');
  assert.strictEqual(meta.kind, SyntaxKind.PropertyDeclaration, 'Extracted metadata kind must match PropertyDeclaration');
  assert.strictEqual(meta.hasInitializer, true, 'Metadata hasInitializer must be true');
  assert.strictEqual(meta.hasType, true, 'Metadata hasType must be true');

  assert.throws(
    () => validatePropertyDeclaration(createIdentifier('dummy')),
    {
      name: 'TypeError',
      message: 'Expected a TypeScript PropertyDeclaration AST node'
    },
    'validatePropertyDeclaration on non-property AST node throws TypeError'
  );
}

console.log('Contract test passed successfully.');

原始种子者

匿名