CodeSampleX

サンプル

source-map 0.6.1: SourceMapGenerator

検証済みサンプル — npm source-map 0.6.1: SourceMapGenerator. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: SourceMapGenerator constructor creates an…

sha256:67dc4f809fcde2a0e6bdb76e782cca73df9b62095408462ac24d37b7df940974

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

実行証拠

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

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
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-23
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-07

ケース

HOW
ゴール
verify source-map.SourceMapGenerator in pkg:npm/source-map@0.6.1
パッケージ
シンボル
  • source-map.SourceMapGenerator
環境
node 22.23.2
作成日
2026-08-23T13:40:32Z

コントラクト

  1. SourceMapGenerator constructor creates an instance configured with optional file and sourceRoot
  2. addMapping adds line and column mappings between generated code and original source positions
  3. setSourceContent embeds raw source file contents directly into the source map
  4. toJSON produces a valid V3 source map object with version, file, sources, names, mappings, and sourcesContent
  5. toString serializes the source map object to a valid JSON string
  6. fromSourceMap static method creates a new SourceMapGenerator initialized from an existing SourceMapConsumer

ファイル

  • 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 source-map.SourceMapGenerator in pkg:npm/source-map@0.6.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/source-map@0.6.1
Demonstrate these symbols/APIs:
  - source-map.SourceMapGenerator

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:75b652c08e35671e7e7ba274c632276bcf82a86e0b5ca076cbc11446b2bbbf95","constraints":{"executionContext":"node"},"contract":["SourceMapGenerator constructor creates an instance configured with optional file and sourceRoot","addMapping adds line and column mappings between generated code and original source positions","setSourceContent embeds raw source file contents directly into the source map","toJSON produces a valid V3 source map object with version, file, sources, names, mappings, and sourcesContent","toString serializes the source map object to a valid JSON string","fromSourceMap static method creates a new SourceMapGenerator initialized from an existing SourceMapConsumer"],"goal":"verify source-map.SourceMapGenerator in pkg:npm/source-map@0.6.1","kind":"HOW","packages":["pkg:npm/source-map@0.6.1"],"schemaVersion":1,"symbols":["source-map.SourceMapGenerator"]},"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/source-map@0.6.1"],"schemaVersion":1,"subject":"pkg:npm/source-map@0.6.1","symbols":["source-map.SourceMapGenerator"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

const { SourceMapGenerator, SourceMapConsumer } = require('source-map');

/**
 * Creates a source map for generated code given mappings and source contents.
 *
 * @param {Object} options
 * @param {string} [options.file] - The generated file name.
 * @param {string} [options.sourceRoot] - The root URL/path for sources.
 * @param {Array<Object>} mappings - List of mapping objects.
 * @param {Object<string, string>} [sourcesContent] - Map of source filename to source text.
 * @returns {SourceMapGenerator} The populated SourceMapGenerator instance.
 */
function createSourceMap(options, mappings, sourcesContent) {
  const generator = new SourceMapGenerator(options);

  if (Array.isArray(mappings)) {
    for (const mapping of mappings) {
      generator.addMapping(mapping);
    }
  }

  if (sourcesContent && typeof sourcesContent === 'object') {
    for (const [sourceFile, content] of Object.entries(sourcesContent)) {
      generator.setSourceContent(sourceFile, content);
    }
  }

  return generator;
}

/**
 * Clones a source map using SourceMapGenerator.fromSourceMap.
 *
 * @param {SourceMapGenerator|Object} mapOrConsumer
 * @returns {SourceMapGenerator}
 */
function cloneSourceMap(mapOrConsumer) {
  if (typeof mapOrConsumer.toJSON === 'function') {
    const consumer = new SourceMapConsumer(mapOrConsumer.toJSON());
    return SourceMapGenerator.fromSourceMap(consumer);
  }
  return SourceMapGenerator.fromSourceMap(mapOrConsumer);
}

module.exports = {
  SourceMapGenerator,
  createSourceMap,
  cloneSourceMap
};
package-lock.json
{
  "name": "sample-source-map-generator",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-source-map-generator",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "source-map": "0.6.1"
      }
    },
    "node_modules/source-map": {
      "version": "0.6.1",
      "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
      "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
      "license": "BSD-3-Clause",
      "engines": {
        "node": ">=0.10.0"
      }
    }
  }
}
package.json
{
  "name": "sample-source-map-generator",
  "version": "1.0.0",
  "private": true,
  "description": "verify source-map.SourceMapGenerator in pkg:npm/source-map@0.6.1",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.cjs"
  },
  "dependencies": {
    "source-map": "0.6.1"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify source-map.SourceMapGenerator in pkg:npm/source-map@0.6.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/source-map@0.6.1"
  ],
  "symbols": [
    "source-map.SourceMapGenerator"
  ]
}
test/contract.cjs
'use strict';

const assert = require('assert');
const { SourceMapGenerator, SourceMapConsumer } = require('source-map');
const { createSourceMap, cloneSourceMap } = require('../index.js');

async function runContractTests() {
  // 1. SourceMapGenerator constructor creates an instance configured with optional file and sourceRoot
  const generator = new SourceMapGenerator({
    file: 'bundle.js',
    sourceRoot: 'http://example.com/src/'
  });
  assert.ok(generator instanceof SourceMapGenerator, 'Must create an instance of SourceMapGenerator');

  // 2. addMapping adds line and column mappings between generated code and original source positions
  generator.addMapping({
    generated: { line: 1, column: 0 },
    original: { line: 10, column: 4 },
    source: 'foo.js',
    name: 'bar'
  });

  generator.addMapping({
    generated: { line: 2, column: 0 },
    original: { line: 20, column: 8 },
    source: 'baz.js'
  });

  // 3. setSourceContent embeds raw source file contents directly into the source map
  const fooContent = 'function bar() { return true; }';
  const bazContent = 'const val = 123;';
  generator.setSourceContent('foo.js', fooContent);
  generator.setSourceContent('baz.js', bazContent);

  // 4. toJSON produces a valid V3 source map object with version, file, sources, names, mappings, and sourcesContent
  const jsonMap = generator.toJSON();
  assert.strictEqual(jsonMap.version, 3, 'Source map version must be 3');
  assert.strictEqual(jsonMap.file, 'bundle.js', 'File name must match option');
  assert.strictEqual(jsonMap.sourceRoot, 'http://example.com/src/', 'sourceRoot must match option');
  assert.deepStrictEqual(jsonMap.sources, ['foo.js', 'baz.js'], 'sources list must contain added sources');
  assert.deepStrictEqual(jsonMap.names, ['bar'], 'names list must contain added names');
  assert.deepStrictEqual(jsonMap.sourcesContent, [fooContent, bazContent], 'sourcesContent must contain embedded contents');
  assert.ok(typeof jsonMap.mappings === 'string' && jsonMap.mappings.length > 0, 'mappings string must not be empty');

  // 5. toString serializes the source map object to a valid JSON string
  const stringified = generator.toString();
  assert.strictEqual(typeof stringified, 'string', 'toString must return a string');
  const parsed = JSON.parse(stringified);
  assert.deepStrictEqual(parsed, jsonMap, 'Parsed toString output must match toJSON output');

  // 6. Verify mapping resolution with SourceMapConsumer
  const consumer = new SourceMapConsumer(jsonMap);
  const pos1 = consumer.originalPositionFor({ line: 1, column: 0 });
  assert.strictEqual(pos1.line, 10);
  assert.strictEqual(pos1.column, 4);
  assert.strictEqual(pos1.source, 'http://example.com/src/foo.js');
  assert.strictEqual(pos1.name, 'bar');

  const pos2 = consumer.originalPositionFor({ line: 2, column: 0 });
  assert.strictEqual(pos2.line, 20);
  assert.strictEqual(pos2.column, 8);
  assert.strictEqual(pos2.source, 'http://example.com/src/baz.js');
  assert.strictEqual(pos2.name, null);

  // 7. fromSourceMap static method creates a new SourceMapGenerator initialized from an existing consumer
  const clonedGen = SourceMapGenerator.fromSourceMap(consumer);
  assert.ok(clonedGen instanceof SourceMapGenerator, 'fromSourceMap must return a SourceMapGenerator instance');
  const clonedJson = clonedGen.toJSON();
  assert.strictEqual(clonedJson.version, 3);
  assert.strictEqual(clonedJson.file, 'bundle.js');
  assert.deepStrictEqual(clonedJson.sources, ['foo.js', 'baz.js']);
  assert.deepStrictEqual(clonedJson.names, ['bar']);

  // 8. Test helper functions from index.js
  const helperMap = createSourceMap(
    { file: 'app.min.js' },
    [
      {
        generated: { line: 1, column: 0 },
        original: { line: 1, column: 0 },
        source: 'app.js'
      }
    ],
    {
      'app.js': 'console.log("hello");'
    }
  );
  assert.ok(helperMap instanceof SourceMapGenerator);
  const helperJson = helperMap.toJSON();
  assert.strictEqual(helperJson.file, 'app.min.js');
  assert.deepStrictEqual(helperJson.sources, ['app.js']);
  assert.deepStrictEqual(helperJson.sourcesContent, ['console.log("hello");']);

  const clonedViaHelper = cloneSourceMap(helperMap);
  assert.ok(clonedViaHelper instanceof SourceMapGenerator);
  assert.strictEqual(clonedViaHelper.toJSON().file, 'app.min.js');

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

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

オリジンシーダー

匿名