Skip to content

Commit

Permalink
codemod(turbopack): Rewrite a few instances of Vc as ResolvedVc (#70927)
Browse files Browse the repository at this point in the history
First PR generated by
https://github.com/vercel/turbopack-resolved-vc-codemod (private repo,
hacky low-quality throw-away code)

<img
src="https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/HAZVitxRNnZz8QMiPn4a/1b2b3800-5016-44b8-8868-98c76b58823e.png"
width="400">

The codemod is only able to "fix" compilation errors by rewriting
function arguments as `ResolvedVc`, so it's very limited right now, but
it's able to make some progress.

The codemod uses ast-grep's JS API to make all the changes:
https://ast-grep.github.io/guide/api-usage/js-api.html

I also evaluated gritql, but it didn't seem sufficient for this codemod:
[Internal
discussion](https://vercel.slack.com/archives/C03EWR7LGEN/p1727755062182179?thread_ts=1727709174.550549&cid=C03EWR7LGEN)

It turns out that a lot of these structs are actually dead code, so
#70987 is a follow-up to delete them.
  • Loading branch information
bgw authored Oct 11, 2024
1 parent 5c194b2 commit 57a4e5c
Show file tree
Hide file tree
Showing 26 changed files with 80 additions and 70 deletions.
4 changes: 2 additions & 2 deletions crates/next-core/src/next_client/runtime_entry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{bail, Result};
use turbo_tasks::{ValueToString, Vc};
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
chunk::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets},
Expand All @@ -15,7 +15,7 @@ use turbopack_ecmascript::resolve::cjs_resolve;
pub enum RuntimeEntry {
Request(Vc<Request>, Vc<FileSystemPath>),
Evaluatable(Vc<Box<dyn EvaluatableAsset>>),
Source(Vc<Box<dyn Source>>),
Source(ResolvedVc<Box<dyn Source>>),
}

#[turbo_tasks::value_impl]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
};

plugins.push((
SwcPluginModule::cell(SwcPluginModule::new(
name,
file.content().to_bytes()?.to_vec(),
)),
SwcPluginModule::new(name, file.content().to_bytes()?.to_vec()).resolved_cell(),
config.clone(),
));
}
Expand Down
9 changes: 6 additions & 3 deletions turbopack/crates/turbo-tasks-env/src/dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{env, sync::MutexGuard};

use anyhow::{anyhow, Context, Result};
use indexmap::IndexMap;
use turbo_tasks::{RcStr, ValueToString, Vc};
use turbo_tasks::{RcStr, ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{FileContent, FileSystemPath};

use crate::{sorted_env_vars, EnvMap, ProcessEnv, GLOBAL_ENV_LOCK};
Expand All @@ -12,14 +12,17 @@ use crate::{sorted_env_vars, EnvMap, ProcessEnv, GLOBAL_ENV_LOCK};
/// from.
#[turbo_tasks::value]
pub struct DotenvProcessEnv {
prior: Option<Vc<Box<dyn ProcessEnv>>>,
prior: Option<ResolvedVc<Box<dyn ProcessEnv>>>,
path: Vc<FileSystemPath>,
}

#[turbo_tasks::value_impl]
impl DotenvProcessEnv {
#[turbo_tasks::function]
pub fn new(prior: Option<Vc<Box<dyn ProcessEnv>>>, path: Vc<FileSystemPath>) -> Vc<Self> {
pub fn new(
prior: Option<ResolvedVc<Box<dyn ProcessEnv>>>,
path: Vc<FileSystemPath>,
) -> Vc<Self> {
DotenvProcessEnv { prior, path }.cell()
}

Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbo-tasks-testing/tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::sync::Mutex;

use turbo_tasks::{debug::ValueDebug, Vc};
use turbo_tasks::{debug::ValueDebug, ResolvedVc, Vc};
use turbo_tasks_testing::{register, run, Registration};

static REGISTRATION: Registration = register!();
Expand Down Expand Up @@ -177,7 +177,7 @@ struct StructWithTransparent {

#[turbo_tasks::value(shared)]
struct StructWithOption {
option: Option<Vc<Transparent>>,
option: Option<ResolvedVc<Transparent>>,
}

#[turbo_tasks::value(shared)]
Expand Down
5 changes: 3 additions & 2 deletions turbopack/crates/turbopack-core/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use turbo_tasks::{
debug::ValueDebugFormat,
graph::{AdjacencyMap, GraphTraversal, GraphTraversalResult, Visit, VisitControlFlow},
trace::TraceRawVcs,
RcStr, ReadRef, TaskInput, TryFlatJoinIterExt, TryJoinIterExt, Upcast, ValueToString, Vc,
RcStr, ReadRef, ResolvedVc, TaskInput, TryFlatJoinIterExt, TryJoinIterExt, Upcast,
ValueToString, Vc,
};
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::DeterministicHash;
Expand Down Expand Up @@ -137,7 +138,7 @@ pub trait Chunk: Asset {
#[derive(Default)]
pub struct OutputChunkRuntimeInfo {
pub included_ids: Option<Vc<ModuleIds>>,
pub excluded_ids: Option<Vc<ModuleIds>>,
pub excluded_ids: Option<ResolvedVc<ModuleIds>>,
/// List of paths of chunks containing individual modules that are part of
/// this chunk. This is useful for selectively loading modules from a chunk
/// without loading the whole chunk.
Expand Down
8 changes: 4 additions & 4 deletions turbopack/crates/turbopack-core/src/ident.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Write;

use anyhow::Result;
use turbo_tasks::{RcStr, Value, ValueToString, Vc};
use turbo_tasks::{RcStr, ResolvedVc, Value, ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::{encode_hex, hash_xxh3_hash64, DeterministicHash, Xxh3Hash64Hasher};

Expand All @@ -15,13 +15,13 @@ pub struct AssetIdent {
/// The query string of the asset (e.g. `?foo=bar`)
pub query: Vc<RcStr>,
/// The fragment of the asset (e.g. `#foo`)
pub fragment: Option<Vc<RcStr>>,
pub fragment: Option<ResolvedVc<RcStr>>,
/// The assets that are nested in this asset
pub assets: Vec<(Vc<RcStr>, Vc<AssetIdent>)>,
/// The modifiers of this asset (e.g. `client chunks`)
pub modifiers: Vec<Vc<RcStr>>,
/// The part of the asset that is a (ECMAScript) module
pub part: Option<Vc<ModulePart>>,
pub part: Option<ResolvedVc<ModulePart>>,
/// The asset layer the asset was created from.
pub layer: Option<Vc<RcStr>>,
}
Expand Down Expand Up @@ -144,7 +144,7 @@ impl AssetIdent {
}

#[turbo_tasks::function]
pub fn with_part(&self, part: Vc<ModulePart>) -> Vc<Self> {
pub fn with_part(&self, part: ResolvedVc<ModulePart>) -> Vc<Self> {
let mut this = self.clone();
this.part = Some(part);
Self::new(Value::new(this))
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-core/src/issue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use async_trait::async_trait;
use auto_hash_map::AutoSet;
use serde::Serialize;
use turbo_tasks::{
emit, CollectiblesSource, RawVc, RcStr, ReadRef, TransientInstance, TransientValue,
emit, CollectiblesSource, RawVc, RcStr, ReadRef, ResolvedVc, TransientInstance, TransientValue,
TryJoinIterExt, Upcast, ValueToString, Vc,
};
use turbo_tasks_fs::{FileContent, FileLine, FileLinesContent, FileSystemPath};
Expand Down Expand Up @@ -353,7 +353,7 @@ where
}

#[turbo_tasks::value(transparent)]
pub struct Issues(Vec<Vc<Box<dyn Issue>>>);
pub struct Issues(Vec<ResolvedVc<Box<dyn Issue>>>);

/// A list of issues captured with [`Issue::peek_issues_with_path`] and
/// [`Issue::take_issues_with_path`].
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-core/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use indexmap::IndexSet;
use turbo_tasks::Vc;
use turbo_tasks::{ResolvedVc, Vc};

use crate::{asset::Asset, ident::AssetIdent, reference::ModuleReferences};

Expand Down Expand Up @@ -38,7 +38,7 @@ impl Modules {

/// A set of [Module]s
#[turbo_tasks::value(transparent)]
pub struct ModulesSet(IndexSet<Vc<Box<dyn Module>>>);
pub struct ModulesSet(IndexSet<ResolvedVc<Box<dyn Module>>>);

#[turbo_tasks::value_impl]
impl ModulesSet {
Expand Down
8 changes: 5 additions & 3 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use anyhow::{bail, Result};
use indexmap::{indexmap, IndexMap, IndexSet};
use serde::{Deserialize, Serialize};
use tracing::{Instrument, Level};
use turbo_tasks::{trace::TraceRawVcs, RcStr, TaskInput, TryJoinIterExt, Value, ValueToString, Vc};
use turbo_tasks::{
trace::TraceRawVcs, RcStr, ResolvedVc, TaskInput, TryJoinIterExt, Value, ValueToString, Vc,
};
use turbo_tasks_fs::{
util::normalize_request, FileSystemEntryType, FileSystemPath, RealPathResult,
};
Expand Down Expand Up @@ -369,12 +371,12 @@ impl ModuleResolveResult {
}

#[turbo_tasks::value(transparent)]
pub struct ModuleResolveResultOption(Option<Vc<ModuleResolveResult>>);
pub struct ModuleResolveResultOption(Option<ResolvedVc<ModuleResolveResult>>);

#[turbo_tasks::value_impl]
impl ModuleResolveResultOption {
#[turbo_tasks::function]
pub fn some(result: Vc<ModuleResolveResult>) -> Vc<Self> {
pub fn some(result: ResolvedVc<ModuleResolveResult>) -> Vc<Self> {
ModuleResolveResultOption(Some(result)).cell()
}

Expand Down
5 changes: 3 additions & 2 deletions turbopack/crates/turbopack-core/src/resolve/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use anyhow::{bail, Result};
use indexmap::IndexSet;
use serde::{Deserialize, Serialize};
use turbo_tasks::{
debug::ValueDebugFormat, trace::TraceRawVcs, RcStr, TryJoinIterExt, Value, ValueToString, Vc,
debug::ValueDebugFormat, trace::TraceRawVcs, RcStr, ResolvedVc, TryJoinIterExt, Value,
ValueToString, Vc,
};
use turbo_tasks_fs::{glob::Glob, FileSystemPath};

Expand Down Expand Up @@ -40,7 +41,7 @@ pub enum ResolveModules {
/// lookup versions based on lockfile in the registry filesystem
/// registry filesystem is assumed to have structure like
/// @scope/module/version/<path-in-package>
Registry(Vc<FileSystemPath>, Vc<LockedVersions>),
Registry(ResolvedVc<FileSystemPath>, ResolvedVc<LockedVersions>),
}

#[derive(TraceRawVcs, Hash, PartialEq, Eq, Clone, Copy, Debug, Serialize, Deserialize)]
Expand Down
10 changes: 5 additions & 5 deletions turbopack/crates/turbopack-core/src/resolve/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use turbo_tasks::{RcStr, Value, Vc};
use turbo_tasks::{RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_fs::{glob::Glob, FileSystemPath};

use crate::{
Expand Down Expand Up @@ -41,19 +41,19 @@ impl AfterResolvePluginCondition {
/// A condition which determines if the hooks of a resolve plugin gets called.
#[turbo_tasks::value]
pub enum BeforeResolvePluginCondition {
Request(Vc<Glob>),
Modules(Vc<Vec<RcStr>>),
Request(ResolvedVc<Glob>),
Modules(ResolvedVc<Vec<RcStr>>),
}

#[turbo_tasks::value_impl]
impl BeforeResolvePluginCondition {
#[turbo_tasks::function]
pub fn from_modules(modules: Vc<Vec<RcStr>>) -> Vc<Self> {
pub fn from_modules(modules: ResolvedVc<Vec<RcStr>>) -> Vc<Self> {
BeforeResolvePluginCondition::Modules(modules).cell()
}

#[turbo_tasks::function]
pub fn from_request_glob(glob: Vc<Glob>) -> Vc<Self> {
pub fn from_request_glob(glob: ResolvedVc<Glob>) -> Vc<Self> {
BeforeResolvePluginCondition::Request(glob).cell()
}
}
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-core/src/source_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ref_cast::RefCast;
use regex::Regex;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sourcemap::{DecodedMap, SourceMap as RegularMap, SourceMapBuilder, SourceMapIndex};
use turbo_tasks::{RcStr, TryJoinIterExt, ValueToString, Vc};
use turbo_tasks::{RcStr, ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use turbo_tasks_fs::{
rope::{Rope, RopeBuilder},
File, FileContent, FileSystem, FileSystemPath, VirtualFileSystem,
Expand Down Expand Up @@ -57,7 +57,7 @@ pub enum SourceMap {
}

#[turbo_tasks::value(transparent)]
pub struct SectionMapping(IndexMap<String, Vc<Box<dyn GenerateSourceMap>>>);
pub struct SectionMapping(IndexMap<String, ResolvedVc<Box<dyn GenerateSourceMap>>>);

#[turbo_tasks::value(transparent)]
pub struct OptionSourceMap(Option<Vc<SourceMap>>);
Expand Down
6 changes: 3 additions & 3 deletions turbopack/crates/turbopack-css/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt::Write;

use anyhow::{bail, Result};
use indexmap::IndexSet;
use turbo_tasks::{RcStr, TryJoinIterExt, Value, ValueDefault, ValueToString, Vc};
use turbo_tasks::{RcStr, ResolvedVc, TryJoinIterExt, Value, ValueDefault, ValueToString, Vc};
use turbo_tasks_fs::{rope::Rope, File, FileSystem};
use turbopack_core::{
asset::{Asset, AssetContent},
Expand Down Expand Up @@ -37,7 +37,7 @@ pub struct CssChunk {
}

#[turbo_tasks::value(transparent)]
pub struct CssChunks(Vec<Vc<CssChunk>>);
pub struct CssChunks(Vec<ResolvedVc<CssChunk>>);

#[turbo_tasks::value_impl]
impl CssChunk {
Expand Down Expand Up @@ -351,7 +351,7 @@ impl CssChunkContext {
pub trait CssChunkPlaceable: ChunkableModule + Module + Asset {}

#[turbo_tasks::value(transparent)]
pub struct CssChunkPlaceables(Vec<Vc<Box<dyn CssChunkPlaceable>>>);
pub struct CssChunkPlaceables(Vec<ResolvedVc<Box<dyn CssChunkPlaceable>>>);

#[derive(Clone, Debug)]
#[turbo_tasks::value(shared)]
Expand Down
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-css/src/code_gen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use turbo_tasks::Vc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::chunk::ChunkingContext;

use crate::chunk::CssImport;
Expand Down Expand Up @@ -26,4 +26,4 @@ pub trait CodeGenerateable {
}

#[turbo_tasks::value(transparent)]
pub struct CodeGenerateables(Vec<Vc<Box<dyn CodeGenerateable>>>);
pub struct CodeGenerateables(Vec<ResolvedVc<Box<dyn CodeGenerateable>>>);
11 changes: 6 additions & 5 deletions turbopack/crates/turbopack-dev-server/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use anyhow::Result;
use futures::{stream::Stream as StreamTrait, TryStreamExt};
use serde::{Deserialize, Serialize};
use turbo_tasks::{
trace::TraceRawVcs, util::SharedError, Completion, RcStr, Upcast, Value, ValueDefault, Vc,
trace::TraceRawVcs, util::SharedError, Completion, RcStr, ResolvedVc, Upcast, Value,
ValueDefault, Vc,
};
use turbo_tasks_bytes::{Bytes, Stream, StreamRead};
use turbo_tasks_fs::FileSystemPath;
Expand Down Expand Up @@ -503,11 +504,11 @@ pub struct Rewrite {

/// A [Headers] which will be appended to the eventual, fully resolved
/// content result. This overwrites any previous matching headers.
pub response_headers: Option<Vc<HeaderList>>,
pub response_headers: Option<ResolvedVc<HeaderList>>,

/// A [HeaderList] which will overwrite the values used during the lookup
/// process. All headers not present in this list will be deleted.
pub request_headers: Option<Vc<HeaderList>>,
pub request_headers: Option<ResolvedVc<HeaderList>>,
}

pub struct RewriteBuilder {
Expand Down Expand Up @@ -553,14 +554,14 @@ impl RewriteBuilder {

/// Sets response headers to append to the eventual, fully resolved content
/// result.
pub fn response_headers(mut self, headers: Vc<HeaderList>) -> Self {
pub fn response_headers(mut self, headers: ResolvedVc<HeaderList>) -> Self {
self.rewrite.response_headers = Some(headers);
self
}

/// Sets request headers to overwrite the headers used during the lookup
/// process.
pub fn request_headers(mut self, headers: Vc<HeaderList>) -> Self {
pub fn request_headers(mut self, headers: ResolvedVc<HeaderList>) -> Self {
self.rewrite.request_headers = Some(headers);
self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use swc_core::{
visit::FoldWith,
},
};
use turbo_tasks::{trace::TraceRawVcs, ValueDefault, Vc};
use turbo_tasks::{trace::TraceRawVcs, ResolvedVc, ValueDefault, Vc};
use turbopack_ecmascript::{CustomTransformer, TransformContext};

#[derive(Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize)]
Expand All @@ -26,7 +26,7 @@ pub enum EmotionLabelKind {
}

#[turbo_tasks::value(transparent)]
pub struct OptionEmotionTransformConfig(Option<Vc<EmotionTransformConfig>>);
pub struct OptionEmotionTransformConfig(Option<ResolvedVc<EmotionTransformConfig>>);

//[TODO]: need to support importmap, there are type mismatch between
//next.config.js to swc's emotion options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use swc_core::{
common::{comments::NoopComments, FileName},
ecma::{ast::Program, atoms::JsWord, visit::VisitMutWith},
};
use turbo_tasks::{ValueDefault, Vc};
use turbo_tasks::{ResolvedVc, ValueDefault, Vc};
use turbopack_ecmascript::{CustomTransformer, TransformContext};

#[turbo_tasks::value(transparent)]
pub struct OptionStyledComponentsTransformConfig(Option<Vc<StyledComponentsTransformConfig>>);
pub struct OptionStyledComponentsTransformConfig(
Option<ResolvedVc<StyledComponentsTransformConfig>>,
);

#[turbo_tasks::value(shared)]
#[derive(Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ impl Issue for UnsupportedSwcEcmaTransformPluginsIssue {
#[derive(Debug)]
pub struct SwcEcmaTransformPluginsTransformer {
#[cfg(feature = "swc_ecma_transform_plugin")]
plugins: Vec<(Vc<SwcPluginModule>, serde_json::Value)>,
plugins: Vec<(turbo_tasks::ResolvedVc<SwcPluginModule>, serde_json::Value)>,
}

impl SwcEcmaTransformPluginsTransformer {
#[cfg(feature = "swc_ecma_transform_plugin")]
pub fn new(plugins: Vec<(Vc<SwcPluginModule>, serde_json::Value)>) -> Self {
pub fn new(
plugins: Vec<(turbo_tasks::ResolvedVc<SwcPluginModule>, serde_json::Value)>,
) -> Self {
Self { plugins }
}

Expand Down
Loading

0 comments on commit 57a4e5c

Please sign in to comment.