CodeSampleX

Ejemplo

postcss 8.5.26: Container.index

Muestra verificada para npm postcss 8.5.26: Container.index. El contrato se ejecutó en node 22 · linux alpine/x64 · docker y pasó.

sha256:c3e69c1c226173102f1bd4daaaadb97e5357e4b361ad21a6d557ab0d5143b525

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 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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-27

Caso

HOW
Objetivo
verify postcss.Container.index in pkg:npm/postcss@8.5.26
Paquetes
Símbolos
  • postcss.Container.index
Entorno
node 22.23.2
Creado
2026-08-27T09:51:40Z

Contrato

  1. postcss.Container.index returns the 0-based integer index of a child node within a container
  2. postcss.Container.index returns the numeric index when passed a number
  3. postcss.Container.index returns -1 when passed a node that is not a child of the container

Archivos

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.cjs

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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 postcss.Container.index in pkg:npm/postcss@8.5.26
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/postcss@8.5.26
Demonstrate these symbols/APIs:
  - postcss.Container.index

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:ac26e5043243de1648f0f2ad421842f3579f6e174b036488688510420a25f64d","contract":["postcss.Container.index returns the 0-based integer index of a child node within a container","postcss.Container.index returns the numeric index when passed a number","postcss.Container.index returns -1 when passed a node that is not a child of the container"],"goal":"verify postcss.Container.index in pkg:npm/postcss@8.5.26","kind":"HOW","packages":["pkg:npm/postcss@8.5.26"],"schemaVersion":1,"symbols":["postcss.Container.index"]},"contractCommand":["node","test/contract.cjs"],"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/postcss@8.5.26"],"schemaVersion":1,"subject":"pkg:npm/postcss@8.5.26","symbols":["postcss.Container.index"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

const postcss = require('postcss');

/**
 * Returns the index of a child node or numeric index within a container.
 * @param {import('postcss').Container} container
 * @param {import('postcss').Node | number} child
 * @returns {number}
 */
function getNodeIndex(container, child) {
  if (!container || typeof container.index !== 'function') {
    throw new TypeError('Expected a PostCSS Container instance');
  }
  return container.index(child);
}

module.exports = {
  getNodeIndex,
  postcss
};
package-lock.json
{
  "name": "sample-postcss-container-index",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-postcss-container-index",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "postcss": "8.5.26"
      }
    },
    "node_modules/nanoid": {
      "version": "3.3.18",
      "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.18.tgz",
      "integrity": "sha512-DTg4MJbGMWkfi6VZFdNt2/caMbQy4Ou+Op/hJQvGEWcnVfoA1QA+xzRKAzw9jD6+GVOOeYr/mIcuDSdug6F6+w==",
      "funding": [
        {
          "type": "github",
          "url": "https://github.com/sponsors/ai"
        }
      ],
      "license": "MIT",
      "bin": {
        "nanoid": "bin/nanoid.cjs"
      },
      "engines": {
        "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
      }
    },
    "node_modules/picocolors": {
      "version": "1.1.1",
      "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
      "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==",
      "license": "ISC"
    },
    "node_modules/postcss": {
      "version": "8.5.26",
      "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.26.tgz",
      "integrity": "sha512-u82N74LFzG8ca+dD8puPnplTXoGH4fTPpVGuIbt36G3qvNlkvfD0lEAZSxaly3KX8TS/L1A1gsCEmvKmBcVbkQ==",
      "funding": [
        {
          "type": "opencollective",
          "url": "https://opencollective.com/postcss/"
        },
        {
          "type": "tidelift",
          "url": "https://tidelift.com/funding/github/npm/postcss"
        },
        {
          "type": "github",
          "url": "https://github.com/sponsors/ai"
        }
      ],
      "license": "MIT",
      "dependencies": {
        "nanoid": "^3.3.17",
        "picocolors": "^1.1.1",
        "source-map-js": "^1.2.1"
      },
      "engines": {
        "node": "^10 || ^12 || >=14"
      }
    },
    "node_modules/source-map-js": {
      "version": "1.2.1",
      "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz",
      "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==",
      "license": "BSD-3-Clause",
      "engines": {
        "node": ">=0.10.0"
      }
    }
  }
}
package.json
{
  "name": "sample-postcss-container-index",
  "version": "1.0.0",
  "private": true,
  "description": "verify postcss.Container.index in pkg:npm/postcss@8.5.26",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.cjs"
  },
  "dependencies": {
    "postcss": "8.5.26"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify postcss.Container.index in pkg:npm/postcss@8.5.26",
  "kind": "HOW",
  "packages": [
    "pkg:npm/postcss@8.5.26"
  ],
  "symbols": [
    "postcss.Container.index"
  ]
}
test/contract.cjs
'use strict';

const assert = require('assert');
const postcss = require('postcss');
const { getNodeIndex } = require('../index.js');

async function testContainerIndex() {
  // 1. Test index with Root container and Rule / Comment children
  const css = `
    /* header comment */
    .header { color: red; }
    .footer { color: blue; }
  `;
  const root = postcss.parse(css);

  assert.strictEqual(root instanceof postcss.Container, true, 'Root must be an instance of postcss.Container');
  assert.strictEqual(root.nodes.length, 3, 'Root must have 3 children');

  const commentNode = root.nodes[0];
  const ruleHeader = root.nodes[1];
  const ruleFooter = root.nodes[2];

  // 1a. Child node index resolution
  assert.strictEqual(root.index(commentNode), 0, 'Comment node must be at index 0');
  assert.strictEqual(root.index(ruleHeader), 1, 'Header rule must be at index 1');
  assert.strictEqual(root.index(ruleFooter), 2, 'Footer rule must be at index 2');

  // 1b. Number passthrough
  assert.strictEqual(root.index(0), 0, 'Passing number 0 to container.index must return 0');
  assert.strictEqual(root.index(1), 1, 'Passing number 1 to container.index must return 1');
  assert.strictEqual(root.index(42), 42, 'Passing number 42 to container.index must return 42');

  // 1c. Non-child node returns -1
  const alienDecl = postcss.decl({ prop: 'font-size', value: '14px' });
  assert.strictEqual(root.index(alienDecl), -1, 'Non-child node must return index -1');

  // 2. Test index with Rule container and Declaration children
  assert.strictEqual(ruleHeader instanceof postcss.Container, true, 'Rule must be an instance of postcss.Container');
  const declColor = ruleHeader.nodes[0];
  assert.strictEqual(ruleHeader.index(declColor), 0, 'Declaration must be at index 0 in rule');
  assert.strictEqual(ruleHeader.index(commentNode), -1, 'Comment node is not child of ruleHeader, must return -1');

  // 3. Test index with AtRule container
  const atRuleRoot = postcss.parse('@media (min-width: 600px) { .sidebar { display: none; } }');
  const atRule = atRuleRoot.nodes[0];
  assert.strictEqual(atRule instanceof postcss.Container, true, 'AtRule must be an instance of postcss.Container');
  const nestedRule = atRule.nodes[0];
  assert.strictEqual(atRule.index(nestedRule), 0, 'Nested rule must be at index 0 in atRule');

  // 4. Test helper getNodeIndex from index.js
  assert.strictEqual(getNodeIndex(root, ruleHeader), 1, 'getNodeIndex must return 1 for ruleHeader in root');
  assert.strictEqual(getNodeIndex(root, 2), 2, 'getNodeIndex must return 2 when passed number 2');
  assert.strictEqual(getNodeIndex(root, alienDecl), -1, 'getNodeIndex must return -1 for non-child node');

  // 5. Test invalid container throws TypeError
  assert.throws(() => {
    getNodeIndex(null, ruleHeader);
  }, /^TypeError: Expected a PostCSS Container instance$/, 'getNodeIndex must throw TypeError if container is invalid');

  console.log('Contract tests passed successfully.');
}

testContainerIndex().catch((err) => {
  console.error('Contract test failed:', err);
  process.exit(1);
});

Seeder de origen

anónimo