@as-pect/transform

中文文档
9.0.0发布于 2026/06/18MITES Module近 30 天下载 7968

The transform used by the as-pect core and assembly packages to enable strict equality.

原始使用文档
文档目录5 节
  1. Generated shape
  2. ClassReflectionTransform
  3. Generation responsibilities
  4. Runtime relationship
  5. Compatibility expectations

@as-pect/transform

AssemblyScript transform for as-pect reflection support.

This package implements the Class reflection transform. It injects the generated class reflection shape that the runtime helpers in @as-pect/assembly depend on when tests compare class instances or print reflected object values.

Generated shape

For each class declaration found in source files and namespaces, the transform appends two public instance methods:

  • __aspectStrictEquals(rawRef: Object, stack: usize[], cache: usize[], ignore: StaticArray<i64>): bool
  • __aspectAddReflectedValueKeyValuePairs(reflectedValue: i32, seen: Map<usize, i32>, ignore: StaticArray<i64>): void

These method names are compatibility-sensitive. packages/assembly/assembly/internal/Reflect.ts calls them directly when it cannot use a built-in comparison or reflected-value path.

ClassReflectionTransform

ClassReflectionTransform is the module that owns the shared Class-member plan for generated class reflection behavior. It inspects each class once and records the instance members that should participate in both strict equality and reflected key/value generation.

The member plan includes, in source order:

  • instance fields
  • instance getters

The member plan excludes:

  • static members
  • regular instance methods
  • inherited members, which are handled by generated super calls

Each planned member carries its name, source range, getter/field kind, and djb2 name hash. Generated methods pass these hashes through ignore: StaticArray<i64> when calling super so inherited generated methods do not duplicate members overridden by a child class.

Hash values intentionally remain the compatibility seam with existing generated code. If two inherited member names ever collide under djb2, the generated ignore list treats them as the same inherited member and suppresses the parent entry deterministically; the transform does not rename members or attempt a runtime fallback.

Generation responsibilities

Class reflection generation is split across a few narrow responsibilities:

  • index.ts walks parsed sources and namespaces, then delegates class and interface declarations to the generation module.
  • appendGeneratedClassReflectionMembers.ts orchestrates which generated members or interface contracts should be appended, including idempotence checks and user-authored collision errors.
  • ClassReflectionTransform.ts owns compatibility-sensitive generated member names, generated-member marking, class/interface collision checks, local equality-operator detection, and the Class-member plan.
  • createStrictEqualsMember.ts, createAddReflectedValueKeyValuePairsMember.ts, and createInterfaceReflectionMembers.ts build the generated AssemblyScript AST for each runtime method shape.

This keeps traversal, planning, collision policy, and AST construction separate enough that a change to one generated method does not require re-learning every transform rule.

Runtime relationship

@as-pect/assembly uses the generated methods in two places:

  1. Reflect.equals() calls __aspectStrictEquals to compare class instances structurally.
  2. Reflect.toReflectedValue() calls __aspectAddReflectedValueKeyValuePairs to add reflected object keys and values.

The transform and runtime therefore share a seam: the transform owns the generated method shape, and the runtime assumes that shape exists after compilation with @as-pect/transform.

Compatibility expectations

  • Keep both generated method names unchanged unless a coordinated runtime migration is planned.
  • Keep the ignore hash behavior stable so inherited and overridden members are not reported twice.
  • Keep generated behavior dependency-free; this package should remain a small local transform.
  • Preserve source order when adding reflected/equality-relevant members so reporter output remains stable.
  • Keep transform generation idempotent for the same parsed source; repeated passes should not append duplicate generated members.
  • Reject user-authored __aspectStrictEquals or __aspectAddReflectedValueKeyValuePairs members with a clear transform error instead of silently overwriting them.

版本与发布记录

@as-pect/transform 共发布 7 个版本,首次发布于 2022/08/24;过去 12 个月发布了 1 个版本。

版本发布日期距今
9.0.0latest2026/06/184 个月前
8.1.02023/04/193 年前
8.0.12022/12/133 年前
8.0.02022/11/153 年前
7.0.12022/09/014 年前
7.0.02022/09/014 年前

关于 @as-pect/transform 的常见问题

@as-pect/transform 是什么?

The transform used by the as-pect core and assembly packages to enable strict equality.

如何安装 @as-pect/transform?

在项目目录运行 npm install @as-pect/transform,或使用 pnpm add / yarn add / bun add。

@as-pect/transform 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/as-pect__transform 是否存在。

@as-pect/transform 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@as-pect/transform 可以商用吗?

@as-pect/transform 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@as-pect/transform 的下载量有多少?

根据 npm 官方下载统计,@as-pect/transform 在 2026/10/07 – 2026/11/05 这 30 天内被下载 7,968 次,日均约 266 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@as-pect/transform 还在维护吗?

最新版本 9.0.0 发布于 2026/06/18(4 个月前);至今共发布 7 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/08/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。