CodeSampleX

Пример

postcss 8.5.26: Container.index

Проверенный пример — npm postcss 8.5.26: Container.index. Контракт выполнен на node 22 · linux alpine/x64 · docker и пройден: Container.prototype.index…

sha256:464c794b83460c658ed25b4019982d89416536c626437bc8149617c18d564fb4

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. 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-27

Кейс

HOW
Цель
verify Container.index in pkg:npm/postcss@8.5.26
Пакеты
Символы
  • Container.index
Окружение
node 22.23.2
Создан
2026-08-27T09:50:39Z

Контракт

  1. Container.prototype.index returns the 0-based index of a child node within a Root container.
  2. Container.prototype.index returns the index of a Declaration node within a Rule container.
  3. Container.prototype.index returns -1 when the given node is not a child of the container.
  4. Container.prototype.index returns the input number unchanged when passed a numeric index.
  5. Container.prototype.index reflects updated child positions after prepend, insert, and remove operations.
  6. Container.prototype.index locates nested rules and declarations within an AtRule container.

Файлы

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

Скачать артефакт с исходным кодом (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 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:
  - 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:6bfb2cf9fba17b3231dc428933c7196a7107fa0ba38ef394eead57fab9f5e485","contract":["Container.prototype.index returns the 0-based index of a child node within a Root container.","Container.prototype.index returns the index of a Declaration node within a Rule container.","Container.prototype.index returns -1 when the given node is not a child of the container.","Container.prototype.index returns the input number unchanged when passed a numeric index.","Container.prototype.index reflects updated child positions after prepend, insert, and remove operations.","Container.prototype.index locates nested rules and declarations within an AtRule container."],"goal":"verify Container.index in pkg:npm/postcss@8.5.26","kind":"HOW","packages":["pkg:npm/postcss@8.5.26"],"schemaVersion":1,"symbols":["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":["Container.index"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

const postcss = require('postcss');

/**
 * Returns the index of a child node within a PostCSS container.
 * @param {import('postcss').Container} container
 * @param {import('postcss').Node | number} child
 * @returns {number}
 */
function getChildIndex(container, child) {
  return container.index(child);
}

/**
 * Creates a CSS root and provides index lookups.
 * @param {string} css
 * @returns {import('postcss').Root}
 */
function parseAndGetRoot(css) {
  return postcss.parse(css);
}

module.exports = {
  postcss,
  getChildIndex,
  parseAndGetRoot,
};
package-lock.json
{
  "name": "verify-postcss-container-index",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "verify-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": "verify-postcss-container-index",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification sample for Container.index in postcss",
  "main": "index.js",
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.cjs"
  },
  "dependencies": {
    "postcss": "8.5.26"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify Container.index in pkg:npm/postcss@8.5.26",
  "kind": "HOW",
  "packages": [
    "pkg:npm/postcss@8.5.26"
  ],
  "symbols": [
    "Container.index"
  ]
}
test/contract.cjs
const assert = require('node:assert/strict');
const { postcss, getChildIndex, parseAndGetRoot } = require('../index.js');

// Contract 1: Container.prototype.index returns the 0-based index of a child node within a Root container.
const rootCss = 'a { color: red; } b { color: blue; } c { color: green; }';
const root = parseAndGetRoot(rootCss);
assert.equal(root.nodes.length, 3);
const ruleA = root.nodes[0];
const ruleB = root.nodes[1];
const ruleC = root.nodes[2];

assert.equal(root.index(ruleA), 0);
assert.equal(root.index(ruleB), 1);
assert.equal(root.index(ruleC), 2);
assert.equal(getChildIndex(root, ruleB), 1);

// Contract 2: Container.prototype.index returns the index of a Declaration node within a Rule container.
const rule = postcss.rule({ selector: '.card' });
const declColor = postcss.decl({ prop: 'color', value: '#333' });
const declMargin = postcss.decl({ prop: 'margin', value: '10px' });
const declPadding = postcss.decl({ prop: 'padding', value: '5px' });

rule.append(declColor);
rule.append(declMargin);
rule.append(declPadding);

assert.equal(rule.index(declColor), 0);
assert.equal(rule.index(declMargin), 1);
assert.equal(rule.index(declPadding), 2);
assert.equal(getChildIndex(rule, declPadding), 2);

// Contract 3: Container.prototype.index returns -1 when the given node is not a child of the container.
const orphanDecl = postcss.decl({ prop: 'display', value: 'none' });
const unattachedRule = postcss.rule({ selector: '.other' });

assert.equal(root.index(orphanDecl), -1);
assert.equal(root.index(unattachedRule), -1);
assert.equal(rule.index(orphanDecl), -1);
assert.equal(getChildIndex(root, orphanDecl), -1);

// Contract 4: Container.prototype.index returns the input number unchanged when passed a numeric index.
assert.equal(root.index(0), 0);
assert.equal(root.index(1), 1);
assert.equal(root.index(42), 42);
assert.equal(rule.index(0), 0);
assert.equal(getChildIndex(rule, 5), 5);

// Contract 5: Container.prototype.index reflects updated child positions after prepend, insert, and remove operations.
const dynamicRoot = postcss.root();
const firstNode = postcss.comment({ text: 'first' });
const secondNode = postcss.rule({ selector: '.header' });
const insertedNode = postcss.decl({ prop: 'z-index', value: '10' });

dynamicRoot.append(secondNode);
assert.equal(dynamicRoot.index(secondNode), 0);

dynamicRoot.prepend(firstNode);
assert.equal(dynamicRoot.index(firstNode), 0);
assert.equal(dynamicRoot.index(secondNode), 1);

dynamicRoot.insertAfter(firstNode, insertedNode);
assert.equal(dynamicRoot.index(firstNode), 0);
assert.equal(dynamicRoot.index(insertedNode), 1);
assert.equal(dynamicRoot.index(secondNode), 2);

dynamicRoot.removeChild(insertedNode);
assert.equal(dynamicRoot.index(firstNode), 0);
assert.equal(dynamicRoot.index(secondNode), 1);
assert.equal(dynamicRoot.index(insertedNode), -1);

// Contract 6: Container.prototype.index locates nested rules and declarations within an AtRule container.
const mediaAtRule = postcss.atRule({ name: 'media', params: 'screen and (min-width: 768px)' });
const nestedRule1 = postcss.rule({ selector: '.container' });
const nestedRule2 = postcss.rule({ selector: '.sidebar' });

mediaAtRule.append(nestedRule1);
mediaAtRule.append(nestedRule2);

assert.equal(mediaAtRule.index(nestedRule1), 0);
assert.equal(mediaAtRule.index(nestedRule2), 1);
assert.equal(getChildIndex(mediaAtRule, nestedRule2), 1);

console.log('All Container.index contract assertions passed.');

Исходный сидер

аноним