示例
typescript 7.0.2: createParameterDeclaration
已验证示例 — npm typescript 7.0.2: createParameterDeclaration. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: createParameterDeclaration constructs a…
sha256:6e819b15e4db9b7910417cd93c9f59387eb0d9bf6e5e96c09911205d2e6419d5
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 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.createParameterDeclaration in pkg:npm/typescript@7.0.2
- 符号
-
- typescript.createParameterDeclaration
- 环境
- node 22.23.2
- 创建时间
- 2026-08-28T07:27:39Z
契约
- createParameterDeclaration constructs a ParameterDeclaration AST node with the specified identifier and type
- createParameterDeclaration supports optional, rest, and initialized parameter configurations
- createParameterDeclaration supports modifier tokens such as readonly on parameter declarations
- updateParameterDeclaration updates existing parameter declaration AST nodes while preserving parameter structure
文件
- PROMPT.md
- csx.json
- package-lock.json
- package.json
- spec.json
- src/index.mjs
- test/contract.mjs
源代码
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.createParameterDeclaration 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.createParameterDeclaration
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.
{"case":{"caseId":"case:sha256:451dc20a8178fbbd5e6bbc7245703290188fce2111bf14d69d10171390f106c2","constraints":{"executionContext":"node"},"contract":["createParameterDeclaration constructs a ParameterDeclaration AST node with the specified identifier and type","createParameterDeclaration supports optional, rest, and initialized parameter configurations","createParameterDeclaration supports modifier tokens such as readonly on parameter declarations","updateParameterDeclaration updates existing parameter declaration AST nodes while preserving parameter structure"],"goal":"verify typescript.createParameterDeclaration in pkg:npm/typescript@7.0.2","kind":"HOW","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"symbols":["typescript.createParameterDeclaration"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","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.createParameterDeclaration"],"verifierAdapter":"node-typescript@1"}
{
"name": "typescript-parameter-declaration-sample",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "typescript-parameter-declaration-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"
}
}
}
}
{
"name": "typescript-parameter-declaration-sample",
"version": "1.0.0",
"description": "Create and inspect AST parameter declarations using typescript.createParameterDeclaration",
"type": "module",
"main": "src/index.mjs",
"scripts": {
"test": "node test/contract.mjs"
},
"dependencies": {
"typescript": "7.0.2"
},
"license": "MIT-0"
}
{
"schemaVersion": 1,
"goal": "verify typescript.createParameterDeclaration in pkg:npm/typescript@7.0.2",
"kind": "HOW",
"packages": [
"pkg:npm/typescript@7.0.2"
],
"symbols": [
"typescript.createParameterDeclaration"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@10.9.8",
"runtime": "node@22.23.2"
}
}
import {
createParameterDeclaration,
updateParameterDeclaration,
createIdentifier,
createKeywordTypeNode,
createToken,
createNumericLiteral,
createStringLiteral,
createFunctionDeclaration,
createBlock,
} from 'typescript/unstable/ast/factory';
import {
SyntaxKind,
formatSyntaxKind,
isParameterDeclaration,
isIdentifier,
isFunctionDeclaration,
} from 'typescript/unstable/ast';
const KEYWORD_MAP = {
string: SyntaxKind.StringKeyword,
number: SyntaxKind.NumberKeyword,
boolean: SyntaxKind.BooleanKeyword,
void: SyntaxKind.VoidKeyword,
any: SyntaxKind.AnyKeyword,
unknown: SyntaxKind.UnknownKeyword,
never: SyntaxKind.NeverKeyword,
undefined: SyntaxKind.UndefinedKeyword,
null: SyntaxKind.NullKeyword,
symbol: SyntaxKind.SymbolKeyword,
bigint: SyntaxKind.BigIntKeyword,
object: SyntaxKind.ObjectKeyword,
};
/**
* Resolves a type descriptor (string, SyntaxKind number, or AST node) to an AST TypeNode.
*
* @param {string | number | object} type - Type name, SyntaxKind number, or TypeNode.
* @returns {object | undefined} TypeNode AST node or undefined.
*/
export function resolveTypeNode(type) {
if (type === undefined || type === null) {
return undefined;
}
if (typeof type === 'number') {
return createKeywordTypeNode(type);
}
if (typeof type === 'string') {
const kind = KEYWORD_MAP[type.toLowerCase()];
if (kind !== undefined) {
return createKeywordTypeNode(kind);
}
throw new TypeError(`Unknown keyword type name: ${type}`);
}
if (typeof type === 'object' && typeof type.kind === 'number') {
return type;
}
throw new TypeError('Expected type to be a string, SyntaxKind number, or AST TypeNode');
}
/**
* High-level helper to create a ParameterDeclaration AST node with optional attributes.
*
* @param {string | object} name - Parameter name string or Identifier AST node.
* @param {object} [options] - Parameter options.
* @param {string | number | object} [options.type] - Keyword type name, SyntaxKind, or TypeNode.
* @param {boolean} [options.optional] - Whether the parameter is optional (adds QuestionToken).
* @param {boolean} [options.isRest] - Whether the parameter is a rest parameter (adds DotDotDotToken).
* @param {Array<number | object>} [options.modifiers] - Modifier kinds or token nodes.
* @param {object} [options.initializer] - Initializer expression AST node.
* @returns {object} ParameterDeclaration AST node.
*/
export function createParameter(name, options = {}) {
if (!name || (typeof name !== 'string' && !isIdentifier(name))) {
throw new TypeError('Expected parameter name to be a non-empty string or Identifier AST node');
}
const nameNode = typeof name === 'string' ? createIdentifier(name) : name;
const dotDotDotToken = options.isRest ? createToken(SyntaxKind.DotDotDotToken) : undefined;
const questionToken = options.optional ? createToken(SyntaxKind.QuestionToken) : undefined;
const typeNode = resolveTypeNode(options.type);
let modifiers;
if (options.modifiers && Array.isArray(options.modifiers)) {
modifiers = options.modifiers.map((m) => (typeof m === 'number' ? createToken(m) : m));
}
return createParameterDeclaration(
modifiers,
dotDotDotToken,
nameNode,
questionToken,
typeNode,
options.initializer
);
}
/**
* Inspects a ParameterDeclaration AST node and extracts structured metadata.
*
* @param {object} node - AST node to inspect.
* @returns {object} Extracted parameter metadata.
*/
export function getParameterInfo(node) {
if (!node || !isParameterDeclaration(node)) {
throw new TypeError('Expected a ParameterDeclaration AST node');
}
const name = node.name && node.name.text ? node.name.text : undefined;
const isOptional = Boolean(node.questionToken);
const isRest = Boolean(node.dotDotDotToken);
const hasInitializer = Boolean(node.initializer);
const hasType = Boolean(node.type);
const modifierCount = node.modifiers ? node.modifiers.length : 0;
return {
kind: node.kind,
kindName: formatSyntaxKind(node.kind),
name,
isOptional,
isRest,
hasInitializer,
hasType,
modifierCount,
};
}
/**
* Creates an array of ParameterDeclaration nodes from parameter specs.
*
* @param {Array<{name: string, type?: string|number|object, optional?: boolean, isRest?: boolean}>} specs - Parameter specifications.
* @returns {Array<object>} Array of ParameterDeclaration AST nodes.
*/
export function createParameterList(specs) {
if (!Array.isArray(specs)) {
throw new TypeError('Expected specs to be an array of parameter definitions');
}
return specs.map((spec) => {
if (!spec || typeof spec !== 'object') {
throw new TypeError('Each parameter spec must be an object');
}
const { name, ...opts } = spec;
return createParameter(name, opts);
});
}
export {
createParameterDeclaration,
updateParameterDeclaration,
createIdentifier,
createKeywordTypeNode,
createToken,
createNumericLiteral,
createStringLiteral,
createFunctionDeclaration,
createBlock,
SyntaxKind,
formatSyntaxKind,
isParameterDeclaration,
isIdentifier,
isFunctionDeclaration,
};
import assert from 'node:assert/strict';
import {
createParameterDeclaration,
updateParameterDeclaration,
createParameter,
getParameterInfo,
createParameterList,
resolveTypeNode,
createIdentifier,
createKeywordTypeNode,
createToken,
createNumericLiteral,
createStringLiteral,
createFunctionDeclaration,
createBlock,
SyntaxKind,
formatSyntaxKind,
isParameterDeclaration,
isIdentifier,
isFunctionDeclaration,
} from '../src/index.mjs';
// Contract 1: createParameterDeclaration constructs a ParameterDeclaration AST node with the specified identifier and type
{
const idNode = createIdentifier('userId');
const typeNode = createKeywordTypeNode(SyntaxKind.StringKeyword);
const param = createParameterDeclaration(
undefined,
undefined,
idNode,
undefined,
typeNode,
undefined
);
assert.ok(param, 'Parameter node should be defined');
assert.strictEqual(isParameterDeclaration(param), true, 'param should satisfy isParameterDeclaration');
assert.strictEqual(param.kind, SyntaxKind.Parameter, 'param.kind should be SyntaxKind.Parameter');
assert.strictEqual(formatSyntaxKind(param.kind), 'Parameter', 'Formatted kind name should be Parameter');
assert.strictEqual(param.name.text, 'userId', 'param.name.text should be userId');
assert.strictEqual(param.type.kind, SyntaxKind.StringKeyword, 'param.type.kind should be StringKeyword');
assert.strictEqual(param.questionToken, undefined, 'questionToken should be undefined');
assert.strictEqual(param.dotDotDotToken, undefined, 'dotDotDotToken should be undefined');
assert.strictEqual(param.initializer, undefined, 'initializer should be undefined');
// Validate helper createParameter with primitive type names
const numParam = createParameter('count', { type: 'number' });
assert.strictEqual(isParameterDeclaration(numParam), true, 'numParam should be parameter declaration');
const numInfo = getParameterInfo(numParam);
assert.strictEqual(numInfo.name, 'count', 'name should match count');
assert.strictEqual(numInfo.hasType, true, 'hasType should be true');
assert.strictEqual(numInfo.isOptional, false, 'isOptional should be false');
assert.strictEqual(numInfo.isRest, false, 'isRest should be false');
assert.strictEqual(numInfo.hasInitializer, false, 'hasInitializer should be false');
// Verify within FunctionDeclaration
const fnNode = createFunctionDeclaration(
undefined,
undefined,
createIdentifier('getUser'),
undefined,
[param, numParam],
createKeywordTypeNode(SyntaxKind.VoidKeyword),
createBlock([], true)
);
assert.strictEqual(isFunctionDeclaration(fnNode), true, 'fnNode should be FunctionDeclaration');
assert.strictEqual(fnNode.parameters.length, 2, 'Function should have 2 parameters');
assert.strictEqual(isParameterDeclaration(fnNode.parameters[0]), true, 'First param should be ParameterDeclaration');
assert.strictEqual(isParameterDeclaration(fnNode.parameters[1]), true, 'Second param should be ParameterDeclaration');
}
// Contract 2: createParameterDeclaration supports optional, rest, and initialized parameter configurations
{
// Optional parameter with QuestionToken
const optParam = createParameterDeclaration(
undefined,
undefined,
createIdentifier('timeout'),
createToken(SyntaxKind.QuestionToken),
createKeywordTypeNode(SyntaxKind.NumberKeyword),
undefined
);
assert.strictEqual(isParameterDeclaration(optParam), true, 'optParam should satisfy isParameterDeclaration');
assert.ok(optParam.questionToken, 'questionToken should be defined');
assert.strictEqual(optParam.questionToken.kind, SyntaxKind.QuestionToken, 'questionToken kind should match QuestionToken');
const optInfo = getParameterInfo(optParam);
assert.strictEqual(optInfo.isOptional, true, 'isOptional should be true');
assert.strictEqual(optInfo.isRest, false, 'isRest should be false');
// Rest parameter with DotDotDotToken
const restParam = createParameterDeclaration(
undefined,
createToken(SyntaxKind.DotDotDotToken),
createIdentifier('items'),
undefined,
createKeywordTypeNode(SyntaxKind.AnyKeyword),
undefined
);
assert.strictEqual(isParameterDeclaration(restParam), true, 'restParam should satisfy isParameterDeclaration');
assert.ok(restParam.dotDotDotToken, 'dotDotDotToken should be defined');
assert.strictEqual(restParam.dotDotDotToken.kind, SyntaxKind.DotDotDotToken, 'dotDotDotToken kind should match DotDotDotToken');
const restInfo = getParameterInfo(restParam);
assert.strictEqual(restInfo.isRest, true, 'isRest should be true');
assert.strictEqual(restInfo.isOptional, false, 'isOptional should be false');
// Initialized parameter with default value expression
const initParam = createParameterDeclaration(
undefined,
undefined,
createIdentifier('retries'),
undefined,
createKeywordTypeNode(SyntaxKind.NumberKeyword),
createNumericLiteral(3)
);
assert.strictEqual(isParameterDeclaration(initParam), true, 'initParam should satisfy isParameterDeclaration');
assert.ok(initParam.initializer, 'initializer should be defined');
assert.strictEqual(initParam.initializer.kind, SyntaxKind.NumericLiteral, 'initializer should be NumericLiteral');
const initInfo = getParameterInfo(initParam);
assert.strictEqual(initInfo.hasInitializer, true, 'hasInitializer should be true');
// Helper createParameterList
const paramList = createParameterList([
{ name: 'req', type: 'string' },
{ name: 'mode', type: 'string', optional: true },
{ name: 'extra', isRest: true },
]);
assert.strictEqual(paramList.length, 3, 'createParameterList should produce 3 parameters');
assert.strictEqual(getParameterInfo(paramList[0]).name, 'req');
assert.strictEqual(getParameterInfo(paramList[1]).isOptional, true);
assert.strictEqual(getParameterInfo(paramList[2]).isRest, true);
}
// Contract 3: createParameterDeclaration supports modifier tokens such as readonly on parameter declarations
{
const readonlyModifier = createToken(SyntaxKind.ReadonlyKeyword);
const paramWithModifier = createParameterDeclaration(
[readonlyModifier],
undefined,
createIdentifier('config'),
undefined,
createKeywordTypeNode(SyntaxKind.ObjectKeyword),
undefined
);
assert.strictEqual(isParameterDeclaration(paramWithModifier), true, 'paramWithModifier should be ParameterDeclaration');
assert.ok(paramWithModifier.modifiers, 'modifiers should be defined');
assert.strictEqual(paramWithModifier.modifiers.length, 1, 'modifiers length should be 1');
assert.strictEqual(paramWithModifier.modifiers[0].kind, SyntaxKind.ReadonlyKeyword, 'Modifier kind should be ReadonlyKeyword');
const info = getParameterInfo(paramWithModifier);
assert.strictEqual(info.modifierCount, 1, 'modifierCount should be 1');
// Helper createParameter with modifier enum
const helperModifierParam = createParameter('data', {
type: 'string',
modifiers: [SyntaxKind.ReadonlyKeyword],
});
assert.strictEqual(getParameterInfo(helperModifierParam).modifierCount, 1);
// Validation / edge cases
assert.throws(() => createParameter(''), { name: 'TypeError' }, 'Empty name should throw TypeError');
assert.throws(() => createParameter(null), { name: 'TypeError' }, 'Null name should throw TypeError');
assert.throws(() => createParameter('x', { type: 'unknown_type_name_123' }), { name: 'TypeError' }, 'Invalid type should throw TypeError');
assert.throws(() => getParameterInfo(null), { name: 'TypeError' }, 'Null node should throw TypeError');
assert.throws(() => getParameterInfo({ kind: 1 }), { name: 'TypeError' }, 'Non-parameter node should throw TypeError');
assert.throws(() => createParameterList(null), { name: 'TypeError' }, 'Null spec list should throw TypeError');
assert.throws(() => createParameterList([null]), { name: 'TypeError' }, 'Invalid spec entry should throw TypeError');
}
// Contract 4: updateParameterDeclaration updates existing parameter declaration AST nodes while preserving parameter structure
{
const original = createParameterDeclaration(
undefined,
undefined,
createIdentifier('oldName'),
undefined,
createKeywordTypeNode(SyntaxKind.StringKeyword),
undefined
);
const updated = updateParameterDeclaration(
original,
undefined,
undefined,
createIdentifier('newName'),
createToken(SyntaxKind.QuestionToken),
createKeywordTypeNode(SyntaxKind.NumberKeyword),
createNumericLiteral(100)
);
assert.strictEqual(isParameterDeclaration(updated), true, 'Updated node should be ParameterDeclaration');
assert.strictEqual(updated.name.text, 'newName', 'Name should be updated to newName');
assert.strictEqual(updated.type.kind, SyntaxKind.NumberKeyword, 'Type should be updated to NumberKeyword');
assert.ok(updated.questionToken, 'questionToken should be present on updated node');
assert.ok(updated.initializer, 'initializer should be present on updated node');
// Test no-op update returns identical or structurally equal node
const unchanged = updateParameterDeclaration(
original,
original.modifiers,
original.dotDotDotToken,
original.name,
original.questionToken,
original.type,
original.initializer
);
assert.strictEqual(unchanged, original, 'Unchanged update should return same reference in TypeScript factory');
}
console.log('All pkg:npm/typescript@7.0.2 parameter declaration contracts passed successfully.');
原始种子者
匿名