CodeSampleX

Exemple

@babel/code-frame 8.0.0: highlight

Échantillon vérifié pour npm @babel/code-frame 8.0.0: highlight. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a réussi.

sha256:ef05de32bf21663da156edab42e53eae497e790e8e79ab77582175039119bf2f

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20

Cas

HOW
Objectif
verify @babel/code-frame.highlight in pkg:npm/%40babel/code-frame@8.0.0
Paquets
Symboles
  • @babel/code-frame.highlight
Environnement
node 22.23.2
Créé
2026-08-20T21:01:37Z

Contrat

  1. highlight returns an empty string when given an empty input string
  2. highlight formats JavaScript keywords with cyan ANSI escape sequences
  3. highlight formats string literals with green ANSI color codes and numeric literals with magenta
  4. highlight formats single-line comments with gray ANSI escape codes and punctuators with yellow
  5. highlight colors JSX element tags and capitalized component identifiers
  6. stripAnsiEscapeCodes restores the original source code text from highlighted output

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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 @babel/code-frame.highlight in pkg:npm/%40babel/code-frame@8.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40babel/code-frame@8.0.0
Demonstrate these symbols/APIs:
  - @babel/code-frame.highlight
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - 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.
csx.json
{"case":{"caseId":"case:sha256:04529d4a687cd068e3eab1ea81f467a541dfee39c203666821eb6390eb5e75fc","constraints":{"executionContext":"node"},"contract":["highlight returns an empty string when given an empty input string","highlight formats JavaScript keywords with cyan ANSI escape sequences","highlight formats string literals with green ANSI color codes and numeric literals with magenta","highlight formats single-line comments with gray ANSI escape codes and punctuators with yellow","highlight colors JSX element tags and capitalized component identifiers","stripAnsiEscapeCodes restores the original source code text from highlighted output"],"goal":"verify @babel/code-frame.highlight in pkg:npm/%40babel/code-frame@8.0.0","kind":"HOW","packages":["pkg:npm/%40babel/code-frame@8.0.0"],"schemaVersion":1,"symbols":["@babel/code-frame.highlight"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40babel/code-frame@8.0.0"],"schemaVersion":1,"subject":"pkg:npm/%40babel/code-frame@8.0.0","symbols":["@babel/code-frame.highlight"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-babel-code-frame-highlight",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-babel-code-frame-highlight",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@babel/code-frame": "8.0.0"
      }
    },
    "node_modules/@babel/code-frame": {
      "version": "8.0.0",
      "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-8.0.0.tgz",
      "integrity": "sha512-dYYg153EyN2Ekbqw2zAsbd6/JR+9N2SEoC7YV2GyyqMM7x9bLDTjBD6XBhSMLH0wtIVyJj03jWNriQhaN+eoCw==",
      "license": "MIT",
      "dependencies": {
        "@babel/helper-validator-identifier": "^8.0.0",
        "js-tokens": "^10.0.0"
      },
      "engines": {
        "node": "^22.18.0 || >=24.11.0"
      }
    },
    "node_modules/@babel/helper-validator-identifier": {
      "version": "8.0.4",
      "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-8.0.4.tgz",
      "integrity": "sha512-4wFaiLd0bVo4cIoTXI3zKI038NIWE/cr3jvBjejOVYVxV/m8Ltav1USiGzG1fmS5J2RhgEOgXNNK46cRPnRsrg==",
      "license": "MIT",
      "engines": {
        "node": "^22.18.0 || >=24.11.0"
      }
    },
    "node_modules/js-tokens": {
      "version": "10.0.0",
      "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-10.0.0.tgz",
      "integrity": "sha512-lM/UBzQmfJRo9ABXbPWemivdCW8V2G8FHaHdypQaIy523snUjog0W71ayWXTjiR+ixeMyVHN2XcpnTd/liPg/Q==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-babel-code-frame-highlight",
  "version": "1.0.0",
  "description": "verify @babel/code-frame.highlight in pkg:npm/%40babel/code-frame@8.0.0",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "@babel/code-frame": "8.0.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @babel/code-frame.highlight in pkg:npm/%40babel/code-frame@8.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40babel/code-frame@8.0.0"
  ],
  "symbols": [
    "@babel/code-frame.highlight"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "runtime": "node@22.23.2"
  }
}
src/index.js
const { highlight } = require('@babel/code-frame');

/**
 * Highlights a given source code string using Babel code-frame syntax highlighting.
 *
 * @param {string} source - The source code string to highlight.
 * @returns {string} The ANSI highlighted source string.
 */
function highlightSource(source) {
  return highlight(source);
}

/**
 * Checks if the highlighted output contains ANSI escape sequences.
 *
 * @param {string} text - The text to check.
 * @returns {boolean} True if ANSI escape sequences are present.
 */
function hasAnsiEscapeCodes(text) {
  const ansiPattern = /\u001b\[\d+(?:;\d+)*m/;
  return ansiPattern.test(text);
}

/**
 * Strips ANSI escape sequences from a string to recover the original plain text.
 *
 * @param {string} text - The ANSI styled text.
 * @returns {string} The raw plain text without styling.
 */
function stripAnsiEscapeCodes(text) {
  return text.replace(/\u001b\[\d+(?:;\d+)*m/g, '');
}

module.exports = {
  highlightSource,
  hasAnsiEscapeCodes,
  stripAnsiEscapeCodes
};
test/contract.js
const assert = require('assert');
const { highlight } = require('@babel/code-frame');
const {
  highlightSource,
  hasAnsiEscapeCodes,
  stripAnsiEscapeCodes
} = require('../src/index');

// 1. highlight returns an empty string when given an empty input string
{
  const result = highlightSource('');
  assert.strictEqual(result, '', 'Empty input string must return empty string');
  assert.strictEqual(hasAnsiEscapeCodes(result), false, 'Empty result must not have ANSI codes');
}

// 2. highlight formats JavaScript keywords with cyan ANSI escape sequences
{
  const code = 'const count = function() { return 42; };';
  const highlighted = highlightSource(code);
  assert(hasAnsiEscapeCodes(highlighted), 'Highlighted code must contain ANSI escape codes');
  assert(highlighted.includes('\u001b[36mconst\u001b[39m'), 'const keyword must be highlighted in cyan');
  assert(highlighted.includes('\u001b[36mfunction\u001b[39m'), 'function keyword must be highlighted in cyan');
  assert(highlighted.includes('\u001b[36mreturn\u001b[39m'), 'return keyword must be highlighted in cyan');
}

// 3. highlight formats string literals with green ANSI color codes and numeric literals with magenta
{
  const code = 'const greeting = "hello world"; const val = 12345;';
  const highlighted = highlightSource(code);
  assert(highlighted.includes('\u001b[32m"hello world"\u001b[39m'), 'String literal must be highlighted in green');
  assert(highlighted.includes('\u001b[35m12345\u001b[39m'), 'Numeric literal must be highlighted in magenta');
}

// 4. highlight formats single-line comments with gray ANSI escape codes and punctuators with yellow
{
  const code = '// calculate value\nlet x = 10;';
  const highlighted = highlightSource(code);
  assert(highlighted.includes('\u001b[90m// calculate value\u001b[39m'), 'Comments must be highlighted in gray');
  assert(highlighted.includes('\u001b[33m=\u001b[39m'), 'Assignment operator must be highlighted in yellow');
  assert(highlighted.includes('\u001b[33m;\u001b[39m'), 'Semicolon must be highlighted in yellow');
}

// 5. highlight colors JSX element tags and capitalized component identifiers
{
  const code = '<MyComponent active={true} />';
  const highlighted = highlightSource(code);
  assert(highlighted.includes('\u001b[33mMyComponent\u001b[39m'), 'Capitalized component name must be highlighted');
  assert(highlighted.includes('\u001b[33m<\u001b[39m'), 'Opening JSX bracket must be highlighted');
  assert(highlighted.includes('\u001b[33m>\u001b[39m'), 'Closing JSX bracket must be highlighted');
}

// 6. stripAnsiEscapeCodes restores the original source code text from highlighted output
{
  const originalCode = 'function add(a, b) {\n  // sum two numbers\n  return a + b;\n}';
  const highlighted = highlightSource(originalCode);
  const restored = stripAnsiEscapeCodes(highlighted);
  assert.strictEqual(restored, originalCode, 'Stripping ANSI codes must restore the exact original source code');
}

console.log('All contract assertions passed successfully.');

Seeder d'origine

anonyme