Ejemplo
typescript 7.0.2: Construct and validate TypeScript conditional statements using createIfStatement and createBlock
Muestra verificada para npm typescript 7.0.2: Construct and validate TypeScript conditional statements using createIfStatement and createBlock. El contrato…
sha256:f135f1666bd8aadd4caf71de5311ea3735c6a432ead5f3c505850dba3cf0cd8a
Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido.
Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas.
MIT-0
Evidencia de ejecución
El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.
- Base de evidencia
- Contrato firmado aprobado
- Recibos de verificación
- 1
- Claves de firma que lo compilaron
- 1
Entorno declarado
linux 24 · ubuntu · glibc 2.39 x64 npm
Entornos de las ejecuciones de verificación
| Entorno | Contrato | Etapas | Ejecución |
|---|---|---|---|
| node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-20 |
Caso
HOW- Objetivo
- Construct and validate TypeScript conditional statements using createIfStatement and createBlock
- Paquetes
- Símbolos
-
- createBlock
- createIfStatement
- Creado
- 2026-08-20T04:38:38Z
Contrato
- buildIfStatement constructs an if statement with condition expression and then-block validating it with isIfStatement
- buildIfElseStatement constructs an if-else statement with both then and else branch blocks
- buildNestedIfStatement constructs nested conditional branching structures within block statements
- inspectIfStatement extracts condition expressions, then-branches, and else-branches using AST type guards
Archivos
- PROMPT.md
- csx.json
- package-lock.json
- package.json
- spec.json
- src/index.mjs
- test/contract.mjs
Código fuente
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: Construct and validate TypeScript conditional statements using createIfStatement and createBlock
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/typescript@7.0.2
Demonstrate these symbols/APIs:
- createBlock
- createIfStatement
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:94e4259be80302a354e92134918412320d04c7c20e5c13b098f340d63b275aca","contract":["buildIfStatement constructs an if statement with condition expression and then-block validating it with isIfStatement","buildIfElseStatement constructs an if-else statement with both then and else branch blocks","buildNestedIfStatement constructs nested conditional branching structures within block statements","inspectIfStatement extracts condition expressions, then-branches, and else-branches using AST type guards"],"goal":"Construct and validate TypeScript conditional statements using createIfStatement and createBlock","kind":"HOW","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"symbols":["createBlock","createIfStatement"]},"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,"symbols":["createBlock","createIfStatement"],"verifierAdapter":"node-typescript@1"}
{
"name": "typescript-conditional-statements-sample",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "typescript-conditional-statements-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-conditional-statements-sample",
"version": "1.0.0",
"description": "Construct and validate TypeScript conditional statements using createIfStatement and createBlock",
"type": "module",
"main": "src/index.mjs",
"scripts": {
"test": "node test/contract.mjs"
},
"dependencies": {
"typescript": "7.0.2"
},
"license": "MIT-0"
}
{
"schemaVersion": 1,
"goal": "Construct and validate TypeScript conditional statements using createIfStatement and createBlock",
"kind": "HOW",
"packages": [
"pkg:npm/typescript@7.0.2"
],
"symbols": [
"createBlock",
"createIfStatement"
]
}
import {
createIfStatement,
createBlock,
createIdentifier,
createBinaryExpression,
createReturnStatement,
createNumericLiteral,
createStringLiteral,
createToken,
} from 'typescript/unstable/ast/factory';
import {
isIfStatement,
isBlock,
isIdentifier,
isBinaryExpression,
isReturnStatement,
isToken,
} from 'typescript/unstable/ast/is';
import {
SyntaxKind,
formatSyntaxKind,
} from 'typescript/unstable/ast';
/**
* Converts a primitive value, identifier name, or AST node into an expression node.
*
* @param {string | number | object} value - Value or AST node.
* @returns {object} AST expression node.
*/
export function toExpressionNode(value) {
if (typeof value === 'number') {
return createNumericLiteral(String(value));
}
if (typeof value === 'string') {
if (value.startsWith('"') && value.endsWith('"')) {
return createStringLiteral(value.slice(1, -1));
}
return createIdentifier(value);
}
return value;
}
/**
* Constructs an IfStatement node with a condition expression and a then-block.
*
* @param {string | object} condition - Condition expression or identifier name.
* @param {object[]} thenStatements - Statements to include in the then-branch block.
* @param {boolean} [multiLine=true] - Whether the block is multiline.
* @returns {object} IfStatement AST node.
*/
export function buildIfStatement(condition, thenStatements, multiLine = true) {
const conditionNode = toExpressionNode(condition);
const thenBlock = createBlock(thenStatements, multiLine);
return createIfStatement(conditionNode, thenBlock, undefined);
}
/**
* Constructs an IfStatement node with both then and else branch statement blocks.
*
* @param {string | object} condition - Condition expression or identifier name.
* @param {object[]} thenStatements - Statements for the then-branch block.
* @param {object[]} elseStatements - Statements for the else-branch block.
* @param {boolean} [multiLine=true] - Whether blocks are multiline.
* @returns {object} IfStatement AST node with else branch.
*/
export function buildIfElseStatement(condition, thenStatements, elseStatements, multiLine = true) {
const conditionNode = toExpressionNode(condition);
const thenBlock = createBlock(thenStatements, multiLine);
const elseBlock = createBlock(elseStatements, multiLine);
return createIfStatement(conditionNode, thenBlock, elseBlock);
}
/**
* Constructs a nested conditional structure containing an inner if-statement inside the outer then-block.
*
* @param {string | object} outerCondition - Condition for the outer if-statement.
* @param {string | object} innerCondition - Condition for the inner if-statement.
* @param {object[]} innerStatements - Statements for the inner then-block.
* @param {object[]} [outerElseStatements=[]] - Optional statements for the outer else-branch.
* @returns {object} Outer IfStatement AST node.
*/
export function buildNestedIfStatement(outerCondition, innerCondition, innerStatements, outerElseStatements = []) {
const innerIf = buildIfStatement(innerCondition, innerStatements, true);
const thenBlock = createBlock([innerIf], true);
const elseBlock = outerElseStatements.length > 0 ? createBlock(outerElseStatements, true) : undefined;
return createIfStatement(toExpressionNode(outerCondition), thenBlock, elseBlock);
}
/**
* Inspects an IfStatement AST node and extracts structural metadata using type guards.
*
* @param {object} node - AST node to inspect.
* @returns {{
* kind: number,
* kindName: string,
* isIf: boolean,
* hasElse: boolean,
* thenIsBlock: boolean,
* elseIsBlock: boolean
* }}
*/
export function inspectIfStatement(node) {
if (!node || typeof node !== 'object' || !isIfStatement(node)) {
throw new TypeError('Expected an IfStatement AST node');
}
return {
kind: node.kind,
kindName: formatSyntaxKind(node.kind),
isIf: isIfStatement(node),
hasElse: node.elseStatement !== undefined,
thenIsBlock: node.thenStatement ? isBlock(node.thenStatement) : false,
elseIsBlock: node.elseStatement ? isBlock(node.elseStatement) : false,
};
}
export {
createIfStatement,
createBlock,
createIdentifier,
createBinaryExpression,
createReturnStatement,
createNumericLiteral,
createStringLiteral,
createToken,
isIfStatement,
isBlock,
isIdentifier,
isBinaryExpression,
isReturnStatement,
isToken,
SyntaxKind,
formatSyntaxKind,
};
import assert from 'node:assert/strict';
import {
buildIfStatement,
buildIfElseStatement,
buildNestedIfStatement,
inspectIfStatement,
createBinaryExpression,
createIdentifier,
createNumericLiteral,
createStringLiteral,
createReturnStatement,
createToken,
isIfStatement,
isBlock,
isIdentifier,
isBinaryExpression,
isReturnStatement,
isToken,
SyntaxKind,
formatSyntaxKind,
} from '../src/index.mjs';
// Contract 1: buildIfStatement constructs an if statement with condition expression and then-block validating it with isIfStatement
{
const condition = createBinaryExpression(
createIdentifier('score'),
createToken(SyntaxKind.GreaterThanEqualsToken),
createNumericLiteral('70')
);
const returnStmt = createReturnStatement(createStringLiteral('"pass"'));
const ifNode = buildIfStatement(condition, [returnStmt]);
assert.ok(ifNode, 'If statement node should be created');
assert.strictEqual(isIfStatement(ifNode), true, 'Node should satisfy isIfStatement type guard');
assert.strictEqual(formatSyntaxKind(ifNode.kind), 'IfStatement', 'Kind name should be IfStatement');
assert.strictEqual(isBinaryExpression(ifNode.expression), true, 'Condition should be binary expression');
assert.strictEqual(isBlock(ifNode.thenStatement), true, 'Then branch should be a block');
assert.strictEqual(ifNode.thenStatement.statements.length, 1, 'Then block should contain 1 statement');
assert.strictEqual(isReturnStatement(ifNode.thenStatement.statements[0]), true, 'Inner statement should be ReturnStatement');
assert.strictEqual(ifNode.elseStatement, undefined, 'Else statement should be undefined');
}
// Contract 2: buildIfElseStatement constructs an if-else statement with both then and else branch blocks
{
const condition = createBinaryExpression(
createIdentifier('count'),
createToken(SyntaxKind.GreaterThanToken),
createNumericLiteral('0')
);
const thenStmt = createReturnStatement(createStringLiteral('"positive"'));
const elseStmt = createReturnStatement(createStringLiteral('"zero or negative"'));
const ifElseNode = buildIfElseStatement(condition, [thenStmt], [elseStmt]);
assert.ok(ifElseNode, 'If-else statement node should be created');
assert.strictEqual(isIfStatement(ifElseNode), true, 'Node should satisfy isIfStatement');
assert.strictEqual(isBlock(ifElseNode.thenStatement), true, 'Then branch should be a block');
assert.strictEqual(isBlock(ifElseNode.elseStatement), true, 'Else branch should be a block');
assert.strictEqual(ifElseNode.thenStatement.statements.length, 1, 'Then block has 1 statement');
assert.strictEqual(ifElseNode.elseStatement.statements.length, 1, 'Else block has 1 statement');
assert.strictEqual(isReturnStatement(ifElseNode.thenStatement.statements[0]), true, 'Then statement is ReturnStatement');
assert.strictEqual(isReturnStatement(ifElseNode.elseStatement.statements[0]), true, 'Else statement is ReturnStatement');
}
// Contract 3: buildNestedIfStatement constructs nested conditional branching structures within block statements
{
const innerCond = createBinaryExpression(
createIdentifier('level'),
createToken(SyntaxKind.EqualsEqualsEqualsToken),
createNumericLiteral('2')
);
const innerReturn = createReturnStatement(createStringLiteral('"level-2"'));
const fallbackReturn = createReturnStatement(createStringLiteral('"unauthorized"'));
const nestedNode = buildNestedIfStatement('isAuthenticated', innerCond, [innerReturn], [fallbackReturn]);
assert.ok(nestedNode, 'Nested if statement should be created');
assert.strictEqual(isIfStatement(nestedNode), true, 'Outer node should be IfStatement');
assert.strictEqual(isIdentifier(nestedNode.expression), true, 'Outer condition should be identifier');
assert.strictEqual(isBlock(nestedNode.thenStatement), true, 'Outer then should be a block');
const innerStmt = nestedNode.thenStatement.statements[0];
assert.strictEqual(isIfStatement(innerStmt), true, 'Inner statement should be an IfStatement');
assert.strictEqual(isBinaryExpression(innerStmt.expression), true, 'Inner condition should be binary');
assert.strictEqual(isBlock(innerStmt.thenStatement), true, 'Inner then should be a block');
assert.strictEqual(isBlock(nestedNode.elseStatement), true, 'Outer else should be a block');
assert.strictEqual(isReturnStatement(nestedNode.elseStatement.statements[0]), true, 'Outer else statement is return');
}
// Contract 4: inspectIfStatement extracts condition expressions, then-branches, and else-branches using AST type guards
{
const simpleIf = buildIfStatement('isReady', [createReturnStatement(createNumericLiteral('1'))]);
const simpleInfo = inspectIfStatement(simpleIf);
assert.strictEqual(simpleInfo.kindName, 'IfStatement', 'Kind name should match IfStatement');
assert.strictEqual(simpleInfo.isIf, true, 'isIf should be true');
assert.strictEqual(simpleInfo.hasElse, false, 'hasElse should be false for simple if');
assert.strictEqual(simpleInfo.thenIsBlock, true, 'thenIsBlock should be true');
assert.strictEqual(simpleInfo.elseIsBlock, false, 'elseIsBlock should be false');
const ifElse = buildIfElseStatement(
'flag',
[createReturnStatement(createNumericLiteral('1'))],
[createReturnStatement(createNumericLiteral('0'))]
);
const ifElseInfo = inspectIfStatement(ifElse);
assert.strictEqual(ifElseInfo.hasElse, true, 'hasElse should be true for if-else');
assert.strictEqual(ifElseInfo.thenIsBlock, true, 'thenIsBlock should be true');
assert.strictEqual(ifElseInfo.elseIsBlock, true, 'elseIsBlock should be true');
assert.throws(
() => inspectIfStatement(null),
{ name: 'TypeError' },
'inspectIfStatement should throw TypeError for null input'
);
assert.throws(
() => inspectIfStatement({ kind: 999 }),
{ name: 'TypeError' },
'inspectIfStatement should throw TypeError for non-IfStatement node'
);
}
console.log('All pkg:npm/typescript@7.0.2 contracts passed successfully.');
Seeder de origen
anónimo