CodeSampleX

Exemple

typescript 5.8.2: ScriptTarget

Échantillon vérifié pour npm typescript 5.8.2: ScriptTarget. Le contrat s'est exécuté sur node 22 · linux debian/x64 · docker et a réussi.

sha256:821a9678da21047df7ccd28d5839685720c66013d172bc051f27024ebbe3de0a

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é linux 24 · ubuntu · glibc 2.39 x64 npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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-06

Cas

HOW
Objectif
verify typescript.ScriptTarget in pkg:npm/typescript@5.8.2
Paquets
Symboles
  • typescript.ScriptTarget
Créé
2026-09-06T00:12:46Z

Contrat

  1. ScriptTarget enum defines numeric constants for ECMAScript language versions and reverse maps to version names
  2. parseScriptTarget resolves ECMAScript version strings and aliases to ScriptTarget enum values
  3. getScriptTargetName formats ScriptTarget enum values into canonical ECMAScript version names
  4. transpileCode and createSourceFileWithTarget apply ScriptTarget to configure compiler transpile output and AST languageVersion

Fichiers

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

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 typescript.ScriptTarget in pkg:npm/typescript@5.8.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/typescript@5.8.2
Demonstrate these symbols/APIs:
  - typescript.ScriptTarget

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:f6319e72541014944daf0c157323557f30252035a40b4913928f8bc0a77b284b","contract":["ScriptTarget enum defines numeric constants for ECMAScript language versions and reverse maps to version names","parseScriptTarget resolves ECMAScript version strings and aliases to ScriptTarget enum values","getScriptTargetName formats ScriptTarget enum values into canonical ECMAScript version names","transpileCode and createSourceFileWithTarget apply ScriptTarget to configure compiler transpile output and AST languageVersion"],"goal":"verify typescript.ScriptTarget in pkg:npm/typescript@5.8.2","kind":"HOW","packages":["pkg:npm/typescript@5.8.2"],"schemaVersion":1,"symbols":["typescript.ScriptTarget"]},"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/typescript@5.8.2"],"schemaVersion":1,"subject":"pkg:npm/typescript@5.8.2","symbols":["typescript.ScriptTarget"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "typescript-script-target-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "typescript-script-target-sample",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "typescript": "5.8.2"
      }
    },
    "node_modules/typescript": {
      "version": "5.8.2",
      "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.8.2.tgz",
      "integrity": "sha512-aJn6wq13/afZp/jT9QZmwEjDqqvSGp1VT5GVg+f/t6/oVyrgXM6BY1h9BRh/O5p3PlUPAe+WuiEZOmb/49RqoQ==",
      "license": "Apache-2.0",
      "bin": {
        "tsc": "bin/tsc",
        "tsserver": "bin/tsserver"
      },
      "engines": {
        "node": ">=14.17"
      }
    }
  }
}
package.json
{
  "name": "typescript-script-target-sample",
  "version": "1.0.0",
  "description": "Verify typescript.ScriptTarget enum values, mapping, and language version compiler configuration",
  "type": "module",
  "main": "src/index.mjs",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "typescript": "5.8.2"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify typescript.ScriptTarget in pkg:npm/typescript@5.8.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/typescript@5.8.2"
  ],
  "symbols": [
    "typescript.ScriptTarget"
  ]
}
src/index.mjs
import ts, { ScriptTarget } from 'typescript';

/**
 * Checks whether a given value is a valid ScriptTarget enum value.
 *
 * @param {unknown} value
 * @returns {boolean}
 */
export function isScriptTarget(value) {
  return typeof value === 'number' && Number.isInteger(value) && typeof ScriptTarget[value] === 'string';
}

/**
 * Returns the canonical name for a given ScriptTarget value.
 *
 * @param {number} target
 * @returns {string}
 */
export function getScriptTargetName(target) {
  if (!isScriptTarget(target)) {
    throw new TypeError(`Invalid ScriptTarget: ${target}`);
  }
  return ScriptTarget[target];
}

/**
 * Resolves standard ECMAScript target names and aliases to ScriptTarget enum values.
 *
 * @param {string} name
 * @returns {number}
 */
export function parseScriptTarget(name) {
  if (typeof name !== 'string' || !name.trim()) {
    throw new TypeError('Target name must be a non-empty string');
  }
  const normalized = name.trim().toLowerCase();
  const aliasMap = {
    'es3': ScriptTarget.ES3,
    'es5': ScriptTarget.ES5,
    'es6': ScriptTarget.ES2015,
    'es2015': ScriptTarget.ES2015,
    'es2016': ScriptTarget.ES2016,
    'es2017': ScriptTarget.ES2017,
    'es2018': ScriptTarget.ES2018,
    'es2019': ScriptTarget.ES2019,
    'es2020': ScriptTarget.ES2020,
    'es2021': ScriptTarget.ES2021,
    'es2022': ScriptTarget.ES2022,
    'es2023': ScriptTarget.ES2023,
    'es2024': ScriptTarget.ES2024,
    'esnext': ScriptTarget.ESNext,
    'latest': ScriptTarget.Latest,
    'json': ScriptTarget.JSON,
  };
  if (Object.prototype.hasOwnProperty.call(aliasMap, normalized)) {
    return aliasMap[normalized];
  }
  throw new TypeError(`Unknown ECMAScript target name: "${name}"`);
}

/**
 * Transpiles source code targeting the specified ScriptTarget.
 *
 * @param {string} source
 * @param {number} target
 * @returns {{ outputText: string, target: number, targetName: string }}
 */
export function transpileCode(source, target) {
  if (!isScriptTarget(target)) {
    throw new TypeError(`Invalid ScriptTarget: ${target}`);
  }
  const result = ts.transpileModule(source, {
    compilerOptions: { target },
  });
  return {
    outputText: result.outputText,
    target,
    targetName: getScriptTargetName(target),
  };
}

/**
 * Creates a TypeScript SourceFile AST node with the given ScriptTarget languageVersion.
 *
 * @param {string} fileName
 * @param {string} sourceText
 * @param {number} target
 * @returns {ts.SourceFile}
 */
export function createSourceFileWithTarget(fileName, sourceText, target) {
  if (!isScriptTarget(target)) {
    throw new TypeError(`Invalid ScriptTarget: ${target}`);
  }
  return ts.createSourceFile(fileName, sourceText, target, true);
}

export {
  ScriptTarget,
};
test/contract.mjs
import assert from 'node:assert/strict';
import {
  ScriptTarget,
  isScriptTarget,
  getScriptTargetName,
  parseScriptTarget,
  transpileCode,
  createSourceFileWithTarget,
} from '../src/index.mjs';

// Contract 1: ScriptTarget enum defines numeric constants for ECMAScript language versions and reverse maps to version names
{
  assert.strictEqual(typeof ScriptTarget, 'object', 'ScriptTarget must be an enum object');
  assert.strictEqual(ScriptTarget.ES3, 0);
  assert.strictEqual(ScriptTarget.ES5, 1);
  assert.strictEqual(ScriptTarget.ES2015, 2);
  assert.strictEqual(ScriptTarget.ES2016, 3);
  assert.strictEqual(ScriptTarget.ES2017, 4);
  assert.strictEqual(ScriptTarget.ES2018, 5);
  assert.strictEqual(ScriptTarget.ES2019, 6);
  assert.strictEqual(ScriptTarget.ES2020, 7);
  assert.strictEqual(ScriptTarget.ES2021, 8);
  assert.strictEqual(ScriptTarget.ES2022, 9);
  assert.strictEqual(ScriptTarget.ES2023, 10);
  assert.strictEqual(ScriptTarget.ES2024, 11);
  assert.strictEqual(ScriptTarget.ESNext, 99);
  assert.strictEqual(ScriptTarget.Latest, 99);
  assert.strictEqual(ScriptTarget.JSON, 100);

  // Reverse mapping from number to string name (ESNext and Latest both map to 99, reverse resolves to Latest)
  assert.strictEqual(ScriptTarget[ScriptTarget.ES3], 'ES3');
  assert.strictEqual(ScriptTarget[ScriptTarget.ES5], 'ES5');
  assert.strictEqual(ScriptTarget[ScriptTarget.ES2015], 'ES2015');
  assert.strictEqual(ScriptTarget[ScriptTarget.ES2022], 'ES2022');
  assert.strictEqual(ScriptTarget[ScriptTarget.ESNext], 'Latest');
  assert.strictEqual(ScriptTarget[ScriptTarget.Latest], 'Latest');
  assert.strictEqual(ScriptTarget[ScriptTarget.JSON], 'JSON');

  // Enum validation helper
  assert.strictEqual(isScriptTarget(ScriptTarget.ES5), true);
  assert.strictEqual(isScriptTarget(ScriptTarget.ES2022), true);
  assert.strictEqual(isScriptTarget(ScriptTarget.ESNext), true);
  assert.strictEqual(isScriptTarget(99999), false);
  assert.strictEqual(isScriptTarget('ES5'), false);
  assert.strictEqual(isScriptTarget(null), false);
}

// Contract 2: parseScriptTarget resolves ECMAScript version strings and aliases to ScriptTarget enum values
{
  assert.strictEqual(parseScriptTarget('es3'), ScriptTarget.ES3);
  assert.strictEqual(parseScriptTarget('es5'), ScriptTarget.ES5);
  assert.strictEqual(parseScriptTarget('es6'), ScriptTarget.ES2015);
  assert.strictEqual(parseScriptTarget('ES2015'), ScriptTarget.ES2015);
  assert.strictEqual(parseScriptTarget('es2020'), ScriptTarget.ES2020);
  assert.strictEqual(parseScriptTarget('es2022'), ScriptTarget.ES2022);
  assert.strictEqual(parseScriptTarget('es2024'), ScriptTarget.ES2024);
  assert.strictEqual(parseScriptTarget('esnext'), ScriptTarget.ESNext);
  assert.strictEqual(parseScriptTarget('latest'), ScriptTarget.Latest);
  assert.strictEqual(parseScriptTarget('json'), ScriptTarget.JSON);

  assert.throws(() => parseScriptTarget('unknown-target'), { name: 'TypeError' });
  assert.throws(() => parseScriptTarget(''), { name: 'TypeError' });
  assert.throws(() => parseScriptTarget(null), { name: 'TypeError' });
}

// Contract 3: getScriptTargetName formats ScriptTarget enum values into canonical ECMAScript version names
{
  assert.strictEqual(getScriptTargetName(ScriptTarget.ES3), 'ES3');
  assert.strictEqual(getScriptTargetName(ScriptTarget.ES5), 'ES5');
  assert.strictEqual(getScriptTargetName(ScriptTarget.ES2015), 'ES2015');
  assert.strictEqual(getScriptTargetName(ScriptTarget.ES2022), 'ES2022');
  assert.strictEqual(getScriptTargetName(ScriptTarget.ESNext), 'Latest');
  assert.strictEqual(getScriptTargetName(ScriptTarget.Latest), 'Latest');
  assert.strictEqual(getScriptTargetName(ScriptTarget.JSON), 'JSON');

  assert.throws(() => getScriptTargetName(-1), { name: 'TypeError' });
  assert.throws(() => getScriptTargetName(999), { name: 'TypeError' });
  assert.throws(() => getScriptTargetName('ES5'), { name: 'TypeError' });
}

// Contract 4: transpileCode and createSourceFileWithTarget apply ScriptTarget to configure compiler transpile output and AST languageVersion
{
  const sourceFile = createSourceFileWithTarget('sample.ts', 'const x: number = 42;', ScriptTarget.ES2020);
  assert.strictEqual(sourceFile.languageVersion, ScriptTarget.ES2020);

  // When targeting ES5, arrow functions are transformed down to regular function expressions
  const es5Output = transpileCode('const add = (a, b) => a + b;', ScriptTarget.ES5);
  assert.strictEqual(es5Output.target, ScriptTarget.ES5);
  assert.strictEqual(es5Output.targetName, 'ES5');
  assert.match(es5Output.outputText, /function\s*\(a,\s*b\)/);

  // When targeting ES2015+, arrow functions remain arrow functions
  const es2015Output = transpileCode('const add = (a, b) => a + b;', ScriptTarget.ES2015);
  assert.strictEqual(es2015Output.target, ScriptTarget.ES2015);
  assert.strictEqual(es2015Output.targetName, 'ES2015');
  assert.match(es2015Output.outputText, /const\s+add\s*=\s*\(a,\s*b\)\s*=>\s*a\s*\+\s*b/);

  assert.throws(() => transpileCode('const x = 1;', -1), { name: 'TypeError' });
  assert.throws(() => createSourceFileWithTarget('sample.ts', 'const x = 1;', -1), { name: 'TypeError' });
}

console.log('All typescript.ScriptTarget contract tests passed.');

Seeder d'origine

anonyme