CodeSampleX

Exemplo

@babel/helper-module-imports 8.0.0: ImportInjector, addDefault, addNamed

Amostra verificada para npm @babel/helper-module-imports 8.0.0: ImportInjector, addDefault, addNamed. O contrato rodou em node 22 · linux alpine/x64 · docker…

sha256:f466454c9af7f06362e9302820c213363f64d803f1b74748a92849f4d8dca8ce

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 npm

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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-26

Caso

HOW
Objetivo
verify pkg:npm/%40babel/helper-module-imports@8.0.0
Pacotes
Símbolos
  • ImportInjector
  • addDefault
  • addNamed
  • addNamespace
  • addSideEffect
  • isModule
Criado
2026-08-26T06:23:56Z

Contrato

  1. isModule detects whether a Program AST path has module sourceType
  2. addDefault inserts a default import declaration and returns the local identifier
  3. addNamed inserts a named import declaration with specified name and nameHint
  4. addNamespace inserts a namespace import (* as alias) declaration
  5. addSideEffect inserts a bare side-effect import declaration without specifiers
  6. ImportInjector manages program imports and merges multiple import specifiers from the same source

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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 pkg:npm/%40babel/helper-module-imports@8.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40babel/helper-module-imports@8.0.0
Demonstrate these symbols/APIs:
  - ImportInjector
  - addDefault
  - addNamed
  - addNamespace
  - addSideEffect
  - isModule

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:85bd2efae23d4bab403df16f196b85e8b4fed9b4f3da35056e569f481c8df42a","contract":["isModule detects whether a Program AST path has module sourceType","addDefault inserts a default import declaration and returns the local identifier","addNamed inserts a named import declaration with specified name and nameHint","addNamespace inserts a namespace import (* as alias) declaration","addSideEffect inserts a bare side-effect import declaration without specifiers","ImportInjector manages program imports and merges multiple import specifiers from the same source"],"goal":"verify pkg:npm/%40babel/helper-module-imports@8.0.0","kind":"HOW","packages":["pkg:npm/%40babel/helper-module-imports@8.0.0"],"schemaVersion":1,"symbols":["ImportInjector","addDefault","addNamed","addNamespace","addSideEffect","isModule"]},"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/%40babel/helper-module-imports@8.0.0"],"schemaVersion":1,"subject":"pkg:npm/%40babel/helper-module-imports@8.0.0","symbols":["ImportInjector","addDefault","addNamed","addNamespace","addSideEffect","isModule"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-babel-helper-module-imports",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-babel-helper-module-imports",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@babel/helper-module-imports": "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/generator": {
      "version": "8.0.0",
      "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-8.0.0.tgz",
      "integrity": "sha512-NT9NrVwJsbSV6Y2FSstWa71EETOnzrjkL5/wX3D2mYHtKM+qvqB1DvR4D0Setb/gDBsHzRICifwEWMO8CnTF6g==",
      "license": "MIT",
      "dependencies": {
        "@babel/parser": "^8.0.0",
        "@babel/types": "^8.0.0",
        "@jridgewell/gen-mapping": "^0.3.12",
        "@jridgewell/trace-mapping": "^0.3.28",
        "@types/jsesc": "^2.5.0",
        "jsesc": "^3.0.2"
      },
      "engines": {
        "node": "^22.18.0 || >=24.11.0"
      }
    },
    "node_modules/@babel/helper-globals": {
      "version": "8.0.0",
      "resolved": "https://registry.npmjs.org/@babel/helper-globals/-/helper-globals-8.0.0.tgz",
      "integrity": "sha512-lLozHOM6sWWlxNo8CYqHy4MBZeTvHXNgVPBfPOGsjPKUzHC2Az9QwB6gxdQmpwHl6GlQtbGgS+lj5887guDiLw==",
      "license": "MIT",
      "engines": {
        "node": "^22.18.0 || >=24.11.0"
      }
    },
    "node_modules/@babel/helper-module-imports": {
      "version": "8.0.0",
      "resolved": "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-8.0.0.tgz",
      "integrity": "sha512-NZ7mSS93o4ndX4KrbD7W8Sf3QT8Qe24PrnFyUcuOPDzK6faqDFKjY9RG7he7+I7FdiQ4llpnosFqzrXa+Vy3Ew==",
      "license": "MIT",
      "dependencies": {
        "@babel/traverse": "^8.0.0",
        "@babel/types": "^8.0.0"
      },
      "engines": {
        "node": "^22.18.0 || >=24.11.0"
      }
    },
    "node_modules/@babel/helper-string-parser": {
      "version": "8.0.0",
      "resolved": "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-8.0.0.tgz",
      "integrity": "sha512-6mJgmFFFIIO82vvoLt9XtRC7/TkzXfts1t/SpRX4IHSzMgqoPYCWesVu1udUPUWioAE/2fcG6WuI8zrkE1gwrg==",
      "license": "MIT",
      "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/@babel/parser": {
      "version": "8.0.4",
      "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-8.0.4.tgz",
      "integrity": "sha512-srpptsAkEbbNIC/q8nT7o+m6CQe8CJUTV/t7MYc9NnWlgYVtHOb7JH6SorxMhN0kuRJjVqXbKClG6xSbPtzz+g==",
      "license": "MIT",
      "dependencies": {
        "@babel/types": "^8.0.4"
      },
      "bin": {
        "parser": "bin/babel-parser.js"
      },
      "engines": {
        "node": "^22.18.0 || >=24.11.0"
      }
    },
    "node_modules/@babel/template": {
      "version": "8.0.0",
      "resolved": "https://registry.npmjs.org/@babel/template/-/template-8.0.0.tgz",
      "integrity": "sha512-eAD0QW/AlbamBbw0FeGiwasbCVPq5ncW0HNVyLP3B9czqLyh4gvw+5JTSNt6le9+ziAU7mqDZsKTHf3jTb4chQ==",
      "license": "MIT",
      "dependencies": {
        "@babel/code-frame": "^8.0.0",
        "@babel/parser": "^8.0.0",
        "@babel/types": "^8.0.0"
      },
      "engines": {
        "node": "^22.18.0 || >=24.11.0"
      }
    },
    "node_modules/@babel/traverse": {
      "version": "8.0.4",
      "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-8.0.4.tgz",
      "integrity": "sha512-bZnmqzGG8UZneG1lLxBoWIH0G6Gr1D846Yu4/3XnY6FhCndMR49u26nTY08u/dAxWmLWF9vGQOuC+84FfIUoeg==",
      "license": "MIT",
      "dependencies": {
        "@babel/code-frame": "^8.0.0",
        "@babel/generator": "^8.0.0",
        "@babel/helper-globals": "^8.0.0",
        "@babel/parser": "^8.0.4",
        "@babel/template": "^8.0.0",
        "@babel/types": "^8.0.4",
        "obug": "^2.1.1"
      },
      "engines": {
        "node": "^22.18.0 || >=24.11.0"
      }
    },
    "node_modules/@babel/types": {
      "version": "8.0.4",
      "resolved": "https://registry.npmjs.org/@babel/types/-/types-8.0.4.tgz",
      "integrity": "sha512-eY+Yn3dCqTGmyiq2QRU66lA5FL8lqqqvecHt0fF3uHONIa7ToYsaCiWV8lOKqAs0Rb2SjixiKFROngnulPtt2g==",
      "license": "MIT",
      "dependencies": {
        "@babel/helper-string-parser": "^8.0.0",
        "@babel/helper-validator-identifier": "^8.0.4"
      },
      "engines": {
        "node": "^22.18.0 || >=24.11.0"
      }
    },
    "node_modules/@jridgewell/gen-mapping": {
      "version": "0.3.13",
      "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz",
      "integrity": "sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==",
      "license": "MIT",
      "dependencies": {
        "@jridgewell/sourcemap-codec": "^1.5.0",
        "@jridgewell/trace-mapping": "^0.3.24"
      }
    },
    "node_modules/@jridgewell/resolve-uri": {
      "version": "3.1.2",
      "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz",
      "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==",
      "license": "MIT",
      "engines": {
        "node": ">=6.0.0"
      }
    },
    "node_modules/@jridgewell/sourcemap-codec": {
      "version": "1.5.5",
      "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.5.tgz",
      "integrity": "sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og==",
      "license": "MIT"
    },
    "node_modules/@jridgewell/trace-mapping": {
      "version": "0.3.31",
      "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.31.tgz",
      "integrity": "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==",
      "license": "MIT",
      "dependencies": {
        "@jridgewell/resolve-uri": "^3.1.0",
        "@jridgewell/sourcemap-codec": "^1.4.14"
      }
    },
    "node_modules/@types/jsesc": {
      "version": "2.5.1",
      "resolved": "https://registry.npmjs.org/@types/jsesc/-/jsesc-2.5.1.tgz",
      "integrity": "sha512-9VN+6yxLOPLOav+7PwjZbxiID2bVaeq0ED4qSQmdQTdjnXJSaCVKTR58t15oqH1H5t8Ng2ZX1SabJVoN9Q34bw==",
      "license": "MIT"
    },
    "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"
    },
    "node_modules/jsesc": {
      "version": "3.1.0",
      "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-3.1.0.tgz",
      "integrity": "sha512-/sM3dO2FOzXjKQhJuo0Q173wf2KOo8t4I8vHy6lF9poUp7bKT0/NHE8fPX23PwfhnykfqnC2xRxOnVw5XuGIaA==",
      "license": "MIT",
      "bin": {
        "jsesc": "bin/jsesc"
      },
      "engines": {
        "node": ">=6"
      }
    },
    "node_modules/obug": {
      "version": "2.1.4",
      "resolved": "https://registry.npmjs.org/obug/-/obug-2.1.4.tgz",
      "integrity": "sha512-4a+OsYv9UktOJKE+l1A4OufDgdRF9PifWj+tJnHURo/P+WOxpG4GzUFL9qCalmWauao6ogiG+QvnCovwPoyAWA==",
      "funding": [
        "https://github.com/sponsors/sxzz",
        "https://opencollective.com/debug"
      ],
      "license": "MIT",
      "engines": {
        "node": ">=12.20.0"
      }
    }
  }
}
package.json
{
  "name": "sample-babel-helper-module-imports",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "@babel/helper-module-imports": "8.0.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/%40babel/helper-module-imports@8.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40babel/helper-module-imports@8.0.0"
  ],
  "symbols": [
    "ImportInjector",
    "addDefault",
    "addNamed",
    "addNamespace",
    "addSideEffect",
    "isModule"
  ]
}
src/index.mjs
import {
  addDefault,
  addNamed,
  addNamespace,
  addSideEffect,
  isModule,
  ImportInjector
} from "@babel/helper-module-imports";

export function checkIsModule(path) {
  return isModule(path);
}

export function injectDefaultImport(path, source, nameHint) {
  return addDefault(path, source, { nameHint });
}

export function injectNamedImport(path, name, source, nameHint) {
  return addNamed(path, name, source, { nameHint });
}

export function injectNamespaceImport(path, source, nameHint) {
  return addNamespace(path, source, { nameHint });
}

export function injectSideEffectImport(path, source) {
  return addSideEffect(path, source);
}

export function createImportInjector(path) {
  return new ImportInjector(path);
}

export {
  addDefault,
  addNamed,
  addNamespace,
  addSideEffect,
  isModule,
  ImportInjector
};
test/contract.mjs
import assert from "node:assert/strict";
import traverseModule from "@babel/traverse";
import {
  checkIsModule,
  injectDefaultImport,
  injectNamedImport,
  injectNamespaceImport,
  injectSideEffectImport,
  createImportInjector,
  addDefault,
  addNamed,
  addNamespace,
  addSideEffect,
  isModule,
  ImportInjector
} from "../src/index.mjs";

const traverse = traverseModule.default || traverseModule;

function createProgramPath(sourceType = "module") {
  const ast = {
    type: "File",
    program: {
      type: "Program",
      sourceType,
      body: [],
      directives: []
    }
  };
  let programPath;
  traverse(ast, {
    Program(path) {
      programPath = path;
    }
  });
  return { ast, programPath };
}

// Contract 1: isModule detects whether a Program AST path has module sourceType
{
  const { programPath: modulePath } = createProgramPath("module");
  const { programPath: scriptPath } = createProgramPath("script");

  assert.equal(checkIsModule(modulePath), true);
  assert.equal(isModule(modulePath), true);
  assert.equal(checkIsModule(scriptPath), false);
  assert.equal(isModule(scriptPath), false);
}

// Contract 2: addDefault inserts a default import declaration and returns the local identifier
{
  const { ast, programPath } = createProgramPath("module");
  const id = injectDefaultImport(programPath, "lodash/map", "map");

  assert.equal(id.type, "Identifier");
  assert.equal(id.name, "_map");
  assert.equal(ast.program.body.length, 1);
  assert.equal(ast.program.body[0].type, "ImportDeclaration");
  assert.equal(ast.program.body[0].source.value, "lodash/map");
  assert.equal(ast.program.body[0].specifiers.length, 1);
  assert.equal(ast.program.body[0].specifiers[0].type, "ImportDefaultSpecifier");
  assert.equal(ast.program.body[0].specifiers[0].local.name, "_map");
}

// Contract 3: addNamed inserts a named import declaration with specified name and nameHint
{
  const { ast, programPath } = createProgramPath("module");
  const id = injectNamedImport(programPath, "join", "path", "join");

  assert.equal(id.type, "Identifier");
  assert.equal(id.name, "_join");
  assert.equal(ast.program.body.length, 1);
  assert.equal(ast.program.body[0].type, "ImportDeclaration");
  assert.equal(ast.program.body[0].source.value, "path");
  assert.equal(ast.program.body[0].specifiers.length, 1);
  assert.equal(ast.program.body[0].specifiers[0].type, "ImportSpecifier");
  assert.equal(ast.program.body[0].specifiers[0].imported.name, "join");
  assert.equal(ast.program.body[0].specifiers[0].local.name, "_join");
}

// Contract 4: addNamespace inserts a namespace import (* as alias) declaration
{
  const { ast, programPath } = createProgramPath("module");
  const id = injectNamespaceImport(programPath, "node:fs", "fs");

  assert.equal(id.type, "Identifier");
  assert.equal(id.name, "_fs");
  assert.equal(ast.program.body.length, 1);
  assert.equal(ast.program.body[0].type, "ImportDeclaration");
  assert.equal(ast.program.body[0].source.value, "node:fs");
  assert.equal(ast.program.body[0].specifiers.length, 1);
  assert.equal(ast.program.body[0].specifiers[0].type, "ImportNamespaceSpecifier");
  assert.equal(ast.program.body[0].specifiers[0].local.name, "_fs");
}

// Contract 5: addSideEffect inserts a bare side-effect import declaration without specifiers
{
  const { ast, programPath } = createProgramPath("module");
  const result = injectSideEffectImport(programPath, "reset.css");

  assert.equal(result, undefined);
  assert.equal(ast.program.body.length, 1);
  assert.equal(ast.program.body[0].type, "ImportDeclaration");
  assert.equal(ast.program.body[0].source.value, "reset.css");
  assert.equal(ast.program.body[0].specifiers.length, 0);
}

// Contract 6: ImportInjector manages program imports and merges multiple import specifiers from the same source
{
  const { ast, programPath } = createProgramPath("module");
  const injector = createImportInjector(programPath);
  assert.ok(injector instanceof ImportInjector);

  const defId = injector.addDefault("pkg-a", { nameHint: "pkgA" });
  const namedId = injector.addNamed("funcB", "pkg-a", { nameHint: "funcB" });
  const nsId = injector.addNamespace("pkg-c", { nameHint: "pkgC" });
  const sideEffect = injector.addSideEffect("pkg-d/styles.css");

  assert.equal(defId.name, "_pkgA");
  assert.equal(namedId.name, "_funcB");
  assert.equal(nsId.name, "_pkgC");
  assert.equal(sideEffect, undefined);

  assert.equal(ast.program.body.length, 3);
  const pkgADecl = ast.program.body.find(stmt => stmt.source?.value === "pkg-a");
  assert.ok(pkgADecl);
  assert.equal(pkgADecl.specifiers.length, 2);
  assert.equal(pkgADecl.specifiers[0].type, "ImportDefaultSpecifier");
  assert.equal(pkgADecl.specifiers[1].type, "ImportSpecifier");
}

console.log("All @babel/helper-module-imports contract assertions passed.");

Seeder de origem

anônimo