[Feature Request] API for reverting or resetting a node

オープン
#2,053 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
typescript
領域
api, tooling

調査の方向性

Start by reviewing the existing node.clone() and replaceWith() behavior described in the issue, including parent pointers, raws information, and other internal state. Compare the snapshot/revert, safe-cloning, and transactional alternatives, then establish the intended API and safety guarantees; the work is done when one approach is agreed and covered by tests.

索引モデルが issue の本文から書いたものです。

説明

As developers of tools built on PostCSS (like Stylelint), we often need to perform complex transformations on the AST. A key challenge arises in "generating fixes" scenarios where multiple independent rules might attempt to modify the same node.

Currently, to prevent conflicting writes, we generate only a single fix even if more than one rule targets the same node. This is suboptimal because many of these fixes might not actually conflict. A more powerful approach would be to "try" a fix, analyze its result, and then potentially "undo" or "revert" it if they overlap. (See https://github.com/stylelint/stylelint/issues/8639#issuecomment-3006023674)

A potential solution could be the addition of an API in PostCSS for reverting a node to a previous state. This could take several forms, some ideas include:

Snapshot/Revert mechanism: An explicit API to save and restore a node's state.

const snapshot = node.saveState();

// ... perform transformations on 'node' ...

// If we decide the transformation was incorrect:
node.revertTo(snapshot); 

Safe cloning for reversal: One idea, discussed in the context of a Stylelint issue, is to clone a node before mutation and replace the mutated node with the clone to achieve a rollback.

const nodeClone = node.clone();
// ... perform transformations on 'node' ...
// To revert:
node.replaceWith(nodeClone);

However, it's currently unclear if this is a guaranteed "no-op" that is free of side effects (e.g., incorrect handling of parent pointers, raws information, or other internal state). Formalizing this pattern and ensuring its safety would be a viable solution.

A Transactional API: A higher-level API that handles the state management automatically (Like CSTTransformer in Python's LibCST).

const transaction = postcss.transaction(node);
if (isConflict) {
  transaction.rollback();
} else {
  transaction.commit(); // (or this is the default)
}
主要言語
TypeScript
スター
29k
フォーク
1.8k
平均マージ
18時間 58分
マージ済み PR(30日)
9

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

postcss/postcss のほかの issue

postcss/postcss の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。