Skip to content

Comments

fix(transformer/class-properties): preserve computed keys if they may have side effects when remove_class_fields_without_initializer is enabled#12772

Merged
graphite-app[bot] merged 1 commit intomainfrom
08-03-fix_transformer_class-properties_preserve_computed_keys_if_they_may_have_side_effects_when_remove_class_fields_without_initializer_is_enabled
Aug 3, 2025
Merged

fix(transformer/class-properties): preserve computed keys if they may have side effects when remove_class_fields_without_initializer is enabled#12772
graphite-app[bot] merged 1 commit intomainfrom
08-03-fix_transformer_class-properties_preserve_computed_keys_if_they_may_have_side_effects_when_remove_class_fields_without_initializer_is_enabled

Conversation

@Dunqing
Copy link
Member

@Dunqing Dunqing commented Aug 3, 2025

When remove_class_fields_without_initializer is enabled, fields without initializers would be removed. However, if the key is computed, and it may have side effects, then we should extract it and place it before class, so that it can keep the same behavior as before.

Example:

Input:

import { Collection } from './collection.ts';

export class Obj {
  public readonly [Collection.identifier] = true;
  public readonly [Collection.identifier2];
  public static readonly [Collection.identifier3] = true;
  public static readonly  [Collection.identifier4];
}

Options:

{
  "assumptions": {
    "setPublicClassFields": true
  },
  "plugins": [
    "transform-class-properties",
    [
      "transform-typescript",
      {
        "removeClassFieldsWithoutInitializer": true
      }
    ]
  ]
}

Output:

import { Collection } from "./collection.ts";
let _Collection$identifie, _Collection$identifie2;
_Collection$identifie = Collection.identifier;
Collection.identifier2;
_Collection$identifie2 = Collection.identifier3;
Collection.identifier4;
export class Obj {
  constructor() {
    this[_Collection$identifie] = true;
  }
}
Obj[_Collection$identifie2] = true;

@Dunqing Dunqing requested a review from overlookmotel as a code owner August 3, 2025 04:03
@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-bug Category - Bug labels Aug 3, 2025
Copy link
Member Author

Dunqing commented Aug 3, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 3, 2025

CodSpeed Instrumentation Performance Report

Merging #12772 will not alter performance

Comparing 08-03-fix_transformer_class-properties_preserve_computed_keys_if_they_may_have_side_effects_when_remove_class_fields_without_initializer_is_enabled (d02e17c) with main (701399e)

Summary

✅ 21 untouched benchmarks
🆕 13 new benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
🆕 codegen[RadixUIAdoptionSection.jsx] N/A 128.2 µs N/A
🆕 codegen[binder.ts] N/A 4.2 ms N/A
🆕 codegen[cal.com.tsx] N/A 37 ms N/A
🆕 codegen[react.development.js] N/A 2 ms N/A
🆕 linter[RadixUIAdoptionSection.jsx] N/A 2.5 ms N/A
🆕 linter[binder.ts] N/A 145.9 ms N/A
🆕 linter[cal.com.tsx] N/A 1.2 s N/A
🆕 linter[react.development.js] N/A 52 ms N/A
🆕 estree[checker.ts] N/A 104.7 ms N/A
🆕 parser[RadixUIAdoptionSection.jsx] N/A 84.2 µs N/A
🆕 parser[binder.ts] N/A 3.3 ms N/A
🆕 parser[cal.com.tsx] N/A 26.8 ms N/A
🆕 parser[react.development.js] N/A 1.3 ms N/A

@Dunqing Dunqing force-pushed the 08-03-fix_transformer_class-properties_preserve_computed_keys_if_they_may_have_side_effects_when_remove_class_fields_without_initializer_is_enabled branch from 6ca7456 to 64ada51 Compare August 3, 2025 04:10
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Aug 3, 2025
Copy link
Member

Boshen commented Aug 3, 2025

Merge activity

… have side effects when `remove_class_fields_without_initializer` is enabled (#12772)

When `remove_class_fields_without_initializer` is enabled, fields without initializers would be removed. However, if the key is computed, and it may have side effects, then we should extract it and place it before class, so that it can keep the same behavior as before.

**Example:**

Input:
```ts
import { Collection } from './collection.ts';

export class Obj {
  public readonly [Collection.identifier] = true;
  public readonly [Collection.identifier2];
  public static readonly [Collection.identifier3] = true;
  public static readonly  [Collection.identifier4];
}
```

Options:
```json
{
  "assumptions": {
    "setPublicClassFields": true
  },
  "plugins": [
    "transform-class-properties",
    [
      "transform-typescript",
      {
        "removeClassFieldsWithoutInitializer": true
      }
    ]
  ]
}
```

Output:
```js
import { Collection } from "./collection.ts";
let _Collection$identifie, _Collection$identifie2;
_Collection$identifie = Collection.identifier;
Collection.identifier2;
_Collection$identifie2 = Collection.identifier3;
Collection.identifier4;
export class Obj {
  constructor() {
    this[_Collection$identifie] = true;
  }
}
Obj[_Collection$identifie2] = true;
```
@graphite-app graphite-app bot force-pushed the 08-03-fix_transformer_class-properties_preserve_computed_keys_if_they_may_have_side_effects_when_remove_class_fields_without_initializer_is_enabled branch from 64ada51 to d02e17c Compare August 3, 2025 07:16
@graphite-app graphite-app bot merged commit d02e17c into main Aug 3, 2025
24 checks passed
@graphite-app graphite-app bot deleted the 08-03-fix_transformer_class-properties_preserve_computed_keys_if_they_may_have_side_effects_when_remove_class_fields_without_initializer_is_enabled branch August 3, 2025 07:23
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Aug 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants