Resolve local function exports for file directive export validation

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

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

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

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
45/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
静か
技術スタック
typescript

調査の方向性

module-export-scan.ts から始め、実装とテストのリファレンスとして PR #1380 を使用します。解決済みのローカル関数、未解決のバインディング、ファイルレベルの "use server" または "use cache" の検証を対象に、module-export-scan とディレクティブの統合テストを重点的に追加し、既存の wrapper、effect、filtering、source-map のスナップショットも確認します。完了条件は、汎用変換がディレクティブポリシーを強制しなくても、コールバックが静的に解決されたローカル関数ノードを検証できることです。

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

説明

plugin: rsc

Motivation

The inline directive alignment tracked by #1335 can expose a Next.js-aligned function validator and let directive owners apply it through existing transform callbacks:

runtime(value, name, meta) {
  validateDirectiveFunction(meta.valueNode, directive)
  return wrap(value, name)
}

This composes with transformHoistInlineDirective, transformModuleExportEffect, and transformWrapExport without baking Server Function policy into generic transforms.

For module directives, however, validation is only as complete as ModuleExportMeta.valueNode. Direct declarations and function-valued initializers expose their function node, but locally exported bindings do not currently resolve back to their declaration.

For example:

'use server'

async function action() {
  console.log(arguments)
}

export { action }

and:

'use cache'

const cached = async function () {
  console.log(arguments)
}

export default cached

The transform can register or wrap these exports, but callback metadata does not expose the source function node, so a directive owner cannot apply the same validation used for a direct export.

Next.js Behavior

Next.js performs a module prepass that maps exported local identifiers to export names. Exported functions then inherit the file-level "use server" or "use cache" directive before the function visitor checks this, super, and arguments.

This means validation does not depend on whether the function is written directly inside an export declaration or exported later through a local identifier. Non-exported functions do not inherit the file directive.

Error fixtures 23 and 24 in next-custom-transforms demonstrate the exported versus non-exported distinction and the lexical traversal rules. The local-binding prepass is implemented in server_actions.rs through export_name_by_local_id.

Prior Work

PR #1380 prototyped local function export resolution in module-export-scan.ts. It covered local aliases, default identifiers, exports before declarations, and direct-function metadata. The PR was closed in favor of #1387, which retained a simpler export-wrapper implementation and did not land local binding resolution.

Use #1380 as an implementation and test reference, but separate metadata discovery from its callable-rewriting requirements.

Proposed Direction

Extend module export scanning with an explicit representation for a statically resolved local function source.

Cover:

  • Function declarations exported through local specifiers.
  • const bindings initialized with function expressions or arrows and exported through local specifiers.
  • Local aliases such as export { action as renamed }.
  • Default identifier exports such as export default action.
  • Exports appearing before their local declaration.
  • Multiple export names referring to the same local function.

Do not assume that changing ModuleExportMeta.valueNode is necessarily the right API. It is currently documented as unavailable for export specifiers, and external consumers may rely on that distinction. Compare these shapes:

  • Populate valueNode after local resolution.
  • Add separate resolvedValueNode metadata.
  • Restore a narrow directFunction representation similar to #1380.
  • Expose a separate local-binding resolver consumed by directive-aware integrations.

Prefer the smallest shape that lets transform callbacks validate the source function without coupling scanning to wrapper production.

Integration

Once a local function node is available, existing directive integrations should feed it into the same validator used for direct exports:

  • Built-in file-level "use server" through transformServerActionServer.
  • Framework-owned file-level "use cache" through transformWrapExport or the preferred module wrapper transform.
  • Other custom Server Function directives using module export transform callbacks.

transformModuleExportEffect and transformWrapExport should remain generic. They should expose metadata but should not enforce this, super, or arguments restrictions themselves.

Semantics To Check

  • Local export specifiers are live bindings, while default identifier exports have different binding behavior.
  • Imported bindings and source re-exports do not have a local function body and must remain unresolved.
  • Arbitrary call-expression initializers are not statically known functions.
  • Mutable bindings can stop referring to their initial function, so metadata must not overstate certainty.
  • Destructured declarations should remain unknown rather than assigning the container initializer to each binding.
  • Duplicate aliases should not cause duplicate source rewriting or inconsistent validation.
  • Metadata enrichment must not unexpectedly change existing filter behavior in public transform consumers.

Verification

Add focused module-export-scan and directive integration coverage for:

  • Function declaration followed by a local export specifier.
  • Local export before the function declaration.
  • const arrow and function-expression exports.
  • Renamed local exports.
  • Default identifier exports.
  • Multiple aliases of one local function.
  • Imported bindings, re-exports, mutable bindings, call-expression initializers, and destructuring remaining unresolved where appropriate.
  • File-level "use server" and "use cache" validation rejecting this, super, or arguments through resolved local exports.
  • Non-exported functions remaining outside file-directive validation.
  • Existing module wrapper, effect, filtering, and source-map snapshots remaining stable unless a deliberate metadata change requires updates.

Non-goals

  • Changing inline method lowering from #1335.
  • Baking directive validation into generic export transforms.
  • Redesigning canonical callable production or local wrapped-binding semantics; coordinate those changes with #1350.
  • Resolving functions across module boundaries.
  • Changing proxy generation, reference manifests, closure transport, or cache policy.
主要言語
TypeScript
スター
1.2k
フォーク
287
平均マージ
1日 17時間
マージ済み PR(30日)
19

環境構築

はじめの一歩

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

vitejs/vite-plugin-react のほかの issue

vitejs/vite-plugin-react の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

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

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