CodeSampleX

サンプル

postcss 8.5.26: parse

検証済みサンプル — npm postcss 8.5.26: parse. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: postcss.parse is exported as a function from postcss

sha256:0a611587400ea5a23256bf6b76d0f257e9f3ad1ad200bb58af961cacfeb70e66

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 npm

検証実行環境

環境 コントラクト ステージ 実行日
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-27

ケース

HOW
ゴール
verify postcss.parse in pkg:npm/postcss@8.5.26
パッケージ
シンボル
  • postcss.parse
作成日
2026-08-27T02:13:11Z

コントラクト

  1. postcss.parse is exported as a function from postcss
  2. postcss.parse returns a Root AST node with type 'root' for valid CSS string
  3. postcss.parse populates child nodes with rule, declaration, and at-rule AST structures
  4. postcss.parse accepts options object including from path and propagates to source.input
  5. postcss.parse handles comments and whitespace without losing structural nodes
  6. postcss.parse throws CssSyntaxError when parsing malformed or unclosed CSS blocks
  7. postcss.parse parsed Root node serializes back to CSS string matching input via toString

ファイル

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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 postcss.parse 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.parse

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:ae61485afade347c3dffe2352fbb39afdfb1b5fea06cd6034cf0c57701629fa3","contract":["postcss.parse is exported as a function from postcss","postcss.parse returns a Root AST node with type 'root' for valid CSS string","postcss.parse populates child nodes with rule, declaration, and at-rule AST structures","postcss.parse accepts options object including from path and propagates to source.input","postcss.parse handles comments and whitespace without losing structural nodes","postcss.parse throws CssSyntaxError when parsing malformed or unclosed CSS blocks","postcss.parse parsed Root node serializes back to CSS string matching input via toString"],"goal":"verify postcss.parse in pkg:npm/postcss@8.5.26","kind":"HOW","packages":["pkg:npm/postcss@8.5.26"],"schemaVersion":1,"symbols":["postcss.parse"]},"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/postcss@8.5.26"],"schemaVersion":1,"subject":"pkg:npm/postcss@8.5.26","symbols":["postcss.parse"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-postcss-parse",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-postcss-parse",
      "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-parse",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for postcss.parse in postcss@8.5.26",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "postcss": "8.5.26"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify postcss.parse in pkg:npm/postcss@8.5.26",
  "kind": "HOW",
  "packages": [
    "pkg:npm/postcss@8.5.26"
  ],
  "symbols": [
    "postcss.parse"
  ]
}
src/index.js
const postcss = require('postcss');

/**
 * Parses CSS text into a PostCSS AST Root node.
 *
 * @param {string} css - The CSS string to parse.
 * @param {object} [opts] - Optional parsing options (e.g. { from: string }).
 * @returns {object} PostCSS Root AST node.
 */
function parseCss(css, opts) {
  return postcss.parse(css, opts);
}

/**
 * Checks whether a given string contains valid CSS syntax without throwing.
 *
 * @param {string} css - The CSS string to check.
 * @returns {boolean} True if syntax is valid CSS, false otherwise.
 */
function isValidCss(css) {
  try {
    postcss.parse(css);
    return true;
  } catch (err) {
    if (err.name === 'CssSyntaxError') {
      return false;
    }
    throw err;
  }
}

/**
 * Extracts all rule selectors from CSS string.
 *
 * @param {string} css - The CSS string to inspect.
 * @returns {string[]} Array of selector strings.
 */
function extractSelectors(css) {
  const root = postcss.parse(css);
  const selectors = [];
  root.walkRules((rule) => {
    selectors.push(rule.selector);
  });
  return selectors;
}

module.exports = {
  postcss,
  parseCss,
  isValidCss,
  extractSelectors
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  postcss,
  parseCss,
  isValidCss,
  extractSelectors
} = require('../src/index.js');

async function runTests() {
  // 1. postcss.parse is exported as a function from postcss
  assert.equal(typeof postcss.parse, 'function', 'postcss.parse must be a function');
  assert.equal(typeof parseCss, 'function', 'parseCss helper must be a function');
  assert.equal(typeof isValidCss, 'function', 'isValidCss helper must be a function');
  assert.equal(typeof extractSelectors, 'function', 'extractSelectors helper must be a function');

  // 2. postcss.parse returns a Root AST node with type 'root' for valid CSS string
  const emptyRoot = parseCss('');
  assert.equal(emptyRoot.type, 'root', 'Empty string parses to a root AST node');
  assert.equal(Array.isArray(emptyRoot.nodes), true, 'root.nodes must be an array');
  assert.equal(emptyRoot.nodes.length, 0, 'Empty CSS produces zero nodes');

  const css = 'a { color: red; }';
  const root = parseCss(css);
  assert.equal(root.type, 'root', 'Root node type must be root');
  assert.equal(root.nodes.length, 1, 'Single rule produces one top-level node');

  // 3. postcss.parse populates child nodes with rule, declaration, and at-rule AST structures
  const complexCss = `
    @charset "UTF-8";
    @media (min-width: 600px) {
      .card {
        padding: 16px;
        margin: 0 auto;
      }
    }
    h1, h2 {
      font-size: 2rem;
    }
  `;
  const complexRoot = parseCss(complexCss);
  assert.equal(complexRoot.nodes.length, 3, 'Expected 3 top-level nodes in complex CSS');

  const [atCharset, atMedia, ruleH] = complexRoot.nodes;
  assert.equal(atCharset.type, 'atrule');
  assert.equal(atCharset.name, 'charset');
  assert.equal(atCharset.params, '"UTF-8"');

  assert.equal(atMedia.type, 'atrule');
  assert.equal(atMedia.name, 'media');
  assert.equal(atMedia.params, '(min-width: 600px)');
  assert.equal(atMedia.nodes.length, 1);
  assert.equal(atMedia.first.type, 'rule');
  assert.equal(atMedia.first.selector, '.card');
  assert.equal(atMedia.first.nodes.length, 2);
  assert.equal(atMedia.first.nodes[0].type, 'decl');
  assert.equal(atMedia.first.nodes[0].prop, 'padding');
  assert.equal(atMedia.first.nodes[0].value, '16px');
  assert.equal(atMedia.first.nodes[1].type, 'decl');
  assert.equal(atMedia.first.nodes[1].prop, 'margin');
  assert.equal(atMedia.first.nodes[1].value, '0 auto');

  assert.equal(ruleH.type, 'rule');
  assert.equal(ruleH.selector, 'h1, h2');
  assert.equal(ruleH.first.type, 'decl');
  assert.equal(ruleH.first.prop, 'font-size');
  assert.equal(ruleH.first.value, '2rem');

  const selectors = extractSelectors(complexCss);
  assert.deepEqual(selectors, ['.card', 'h1, h2']);

  // 4. postcss.parse accepts options object including from path and propagates to source.input
  const rootWithOptions = parseCss('body { margin: 0; }', { from: '/path/to/style.css' });
  assert.ok(rootWithOptions.source, 'Root node has source information');
  assert.ok(rootWithOptions.source.input, 'Source has input descriptor');
  assert.equal(rootWithOptions.source.input.from, '/path/to/style.css', 'from option is preserved on input');

  // 5. postcss.parse handles comments and whitespace without losing structural nodes
  const commentedCss = '/* header styles */ nav { display: flex; /* align */ }';
  const commentedRoot = parseCss(commentedCss);
  assert.equal(commentedRoot.nodes.length, 2, 'Comment and rule are top-level nodes');
  assert.equal(commentedRoot.nodes[0].type, 'comment');
  assert.equal(commentedRoot.nodes[0].text, 'header styles');
  assert.equal(commentedRoot.nodes[1].type, 'rule');
  assert.equal(commentedRoot.nodes[1].nodes.length, 2);
  assert.equal(commentedRoot.nodes[1].nodes[0].type, 'decl');
  assert.equal(commentedRoot.nodes[1].nodes[1].type, 'comment');
  assert.equal(commentedRoot.nodes[1].nodes[1].text, 'align');

  // 6. postcss.parse throws CssSyntaxError when parsing malformed or unclosed CSS blocks
  const invalidCss = 'a { color: red';
  assert.equal(isValidCss('a { color: red; }'), true);
  assert.equal(isValidCss(invalidCss), false);

  assert.throws(
    () => parseCss(invalidCss),
    (err) => {
      assert.equal(err.name, 'CssSyntaxError', 'Error name must be CssSyntaxError');
      assert.equal(typeof err.line, 'number', 'CssSyntaxError must have line number');
      assert.equal(typeof err.column, 'number', 'CssSyntaxError must have column number');
      assert.equal(typeof err.reason, 'string', 'CssSyntaxError must have reason string');
      return true;
    }
  );

  // 7. postcss.parse parsed Root node serializes back to CSS string matching input via toString
  const inputSnippet = 'button.primary { background-color: #007bff; border: none; }';
  const parsedRoot = parseCss(inputSnippet);
  assert.equal(parsedRoot.toString(), inputSnippet, 'toString() output must match original input text');

  console.log('All contract assertions for postcss.parse passed successfully.');
}

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

オリジンシーダー

匿名