Skip to content

Commit d051283

Browse files
authored
[2/n] no-context lint: migrate some crates (vercel/turborepo#5640)
This migrates some crates away from using `context`, specifically: - `node-file-trace` - `turbo-tasks-build` - `turbo-tasks-fetch` - `turbo-tasks` - `turbopack-build` - `turbopack-cli-utils` - `turbopack-cli` - `turbopack-core` - `turbopack-ecmascript` This also moves the lint to `error` and ignores other failing crates.
1 parent 3f25683 commit d051283

File tree

23 files changed

+187
-167
lines changed

23 files changed

+187
-167
lines changed

.config/ast-grep/rules/no-context.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,20 @@ rule:
1818
- kind: field_identifier
1919
- inside:
2020
kind: field_declaration
21+
ignores:
22+
- "./crates/turbopack-core/**"
23+
- "./crates/turbopack-css/**"
24+
- "./crates/turbopack-dev-server/**"
25+
- "./crates/turbopack-dev/**"
26+
- "./crates/turbopack-ecmascript-hmr-protocol/**"
27+
- "./crates/turbopack-ecmascript-plugins/**"
28+
- "./crates/turbopack-ecmascript-runtime/**"
29+
- "./crates/turbopack-ecmascript/**"
30+
- "./crates/turbopack-json/**"
31+
- "./crates/turbopack-mdx/**"
32+
- "./crates/turbopack-node/**"
33+
- "./crates/turbopack-static/**"
34+
- "./crates/turbopack-tests/**"
35+
- "./crates/turbopack/**"
36+
- "./crates/turborepo-cache/**"
37+
- "./crates/turborepo-scm/**"

crates/node-file-trace/src/lib.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ impl Args {
195195
}
196196
}
197197

198-
async fn create_fs(name: &str, context: &str, watch: bool) -> Result<Vc<Box<dyn FileSystem>>> {
199-
let fs = DiskFileSystem::new(name.to_string(), context.to_string());
198+
async fn create_fs(name: &str, root: &str, watch: bool) -> Result<Vc<Box<dyn FileSystem>>> {
199+
let fs = DiskFileSystem::new(name.to_string(), root.to_string());
200200
if watch {
201201
fs.await?.start_watching()?;
202202
} else {
@@ -206,30 +206,30 @@ async fn create_fs(name: &str, context: &str, watch: bool) -> Result<Vc<Box<dyn
206206
}
207207

208208
async fn add_glob_results(
209-
context: Vc<Box<dyn AssetContext>>,
209+
asset_context: Vc<Box<dyn AssetContext>>,
210210
result: Vc<ReadGlobResult>,
211211
list: &mut Vec<Vc<Box<dyn Module>>>,
212212
) -> Result<()> {
213213
let result = result.await?;
214214
for entry in result.results.values() {
215215
if let DirectoryEntry::File(path) = entry {
216216
let source = Vc::upcast(FileSource::new(*path));
217-
list.push(context.process(
217+
list.push(asset_context.process(
218218
source,
219219
Value::new(turbopack_core::reference_type::ReferenceType::Undefined),
220220
));
221221
}
222222
}
223223
for result in result.inner.values() {
224224
fn recurse<'a>(
225-
context: Vc<Box<dyn AssetContext>>,
225+
asset_context: Vc<Box<dyn AssetContext>>,
226226
result: Vc<ReadGlobResult>,
227227
list: &'a mut Vec<Vc<Box<dyn Module>>>,
228228
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
229-
Box::pin(add_glob_results(context, result, list))
229+
Box::pin(add_glob_results(asset_context, result, list))
230230
}
231231
// Boxing for async recursion
232-
recurse(context, *result, list).await?;
232+
recurse(asset_context, *result, list).await?;
233233
}
234234
Ok(())
235235
}
@@ -240,16 +240,16 @@ async fn input_to_modules(
240240
input: Vec<String>,
241241
exact: bool,
242242
process_cwd: Option<String>,
243-
context: String,
243+
context_directory: String,
244244
module_options: TransientInstance<ModuleOptionsContext>,
245245
resolve_options: TransientInstance<ResolveOptionsContext>,
246246
) -> Result<Vc<Modules>> {
247247
let root = fs.root();
248248
let process_cwd = process_cwd
249249
.clone()
250-
.map(|p| p.trim_start_matches(&context).to_owned());
250+
.map(|p| p.trim_start_matches(&context_directory).to_owned());
251251

252-
let context: Vc<Box<dyn AssetContext>> = Vc::upcast(create_module_asset(
252+
let asset_context: Vc<Box<dyn AssetContext>> = Vc::upcast(create_module_asset(
253253
root,
254254
process_cwd,
255255
module_options,
@@ -260,42 +260,42 @@ async fn input_to_modules(
260260
for input in input {
261261
if exact {
262262
let source = Vc::upcast(FileSource::new(root.join(input)));
263-
list.push(context.process(
263+
list.push(asset_context.process(
264264
source,
265265
Value::new(turbopack_core::reference_type::ReferenceType::Undefined),
266266
));
267267
} else {
268268
let glob = Glob::new(input);
269-
add_glob_results(context, root.read_glob(glob, false), &mut list).await?;
269+
add_glob_results(asset_context, root.read_glob(glob, false), &mut list).await?;
270270
};
271271
}
272272
Ok(Vc::cell(list))
273273
}
274274

275275
fn process_context(dir: &Path, context_directory: Option<&String>) -> Result<String> {
276-
let mut context = PathBuf::from(context_directory.map_or(".", |s| s));
277-
if !context.is_absolute() {
278-
context = dir.join(context);
276+
let mut context_directory = PathBuf::from(context_directory.map_or(".", |s| s));
277+
if !context_directory.is_absolute() {
278+
context_directory = dir.join(context_directory);
279279
}
280280
// context = context.canonicalize().unwrap();
281-
Ok(context
281+
Ok(context_directory
282282
.to_str()
283283
.ok_or_else(|| anyhow!("context directory contains invalid characters"))
284284
.unwrap()
285285
.to_string())
286286
}
287287

288-
fn make_relative_path(dir: &Path, context: &str, input: &str) -> Result<String> {
288+
fn make_relative_path(dir: &Path, context_directory: &str, input: &str) -> Result<String> {
289289
let mut input = PathBuf::from(input);
290290
if !input.is_absolute() {
291291
input = dir.join(input);
292292
}
293293
// input = input.canonicalize()?;
294-
let input = input.strip_prefix(context).with_context(|| {
294+
let input = input.strip_prefix(context_directory).with_context(|| {
295295
anyhow!(
296296
"{} is not part of the context directory {}",
297297
input.display(),
298-
context
298+
context_directory
299299
)
300300
})?;
301301
Ok(input
@@ -304,10 +304,10 @@ fn make_relative_path(dir: &Path, context: &str, input: &str) -> Result<String>
304304
.replace('\\', "/"))
305305
}
306306

307-
fn process_input(dir: &Path, context: &str, input: &[String]) -> Result<Vec<String>> {
307+
fn process_input(dir: &Path, context_directory: &str, input: &[String]) -> Result<Vec<String>> {
308308
input
309309
.iter()
310-
.map(|input| make_relative_path(dir, context, input))
310+
.map(|input| make_relative_path(dir, context_directory, input))
311311
.collect()
312312
}
313313

@@ -547,19 +547,19 @@ async fn main_operation(
547547
ref process_cwd,
548548
..
549549
} = args.common();
550-
let context = process_context(&dir, context_directory.as_ref()).unwrap();
551-
let fs = create_fs("context directory", &context, watch).await?;
550+
let context_directory = process_context(&dir, context_directory.as_ref()).unwrap();
551+
let fs = create_fs("context directory", &context_directory, watch).await?;
552552

553553
match *args {
554554
Args::Print { common: _ } => {
555-
let input = process_input(&dir, &context, input).unwrap();
555+
let input = process_input(&dir, &context_directory, input).unwrap();
556556
let mut result = BTreeSet::new();
557557
let modules = input_to_modules(
558558
fs,
559559
input,
560560
exact,
561561
process_cwd.clone(),
562-
context,
562+
context_directory,
563563
module_options,
564564
resolve_options,
565565
)
@@ -577,15 +577,15 @@ async fn main_operation(
577577
return Ok(Vc::cell(result.into_iter().collect::<Vec<_>>()));
578578
}
579579
Args::Annotate { common: _ } => {
580-
let input = process_input(&dir, &context, input).unwrap();
580+
let input = process_input(&dir, &context_directory, input).unwrap();
581581
let mut output_nft_assets = Vec::new();
582582
let mut emits = Vec::new();
583583
for module in input_to_modules(
584584
fs,
585585
input,
586586
exact,
587587
process_cwd.clone(),
588-
context,
588+
context_directory,
589589
module_options,
590590
resolve_options,
591591
)
@@ -608,7 +608,7 @@ async fn main_operation(
608608
common: _,
609609
} => {
610610
let output = process_context(&dir, Some(output_directory)).unwrap();
611-
let input = process_input(&dir, &context, input).unwrap();
611+
let input = process_input(&dir, &context_directory, input).unwrap();
612612
let out_fs = create_fs("output directory", &output, watch).await?;
613613
let input_dir = fs.root();
614614
let output_dir = out_fs.root();
@@ -618,7 +618,7 @@ async fn main_operation(
618618
input,
619619
exact,
620620
process_cwd.clone(),
621-
context,
621+
context_directory,
622622
module_options,
623623
resolve_options,
624624
)

crates/node-file-trace/src/nft_json.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ impl OutputAsset for NftJsonAsset {
3838
impl Asset for NftJsonAsset {
3939
#[turbo_tasks::function]
4040
async fn content(&self) -> Result<Vc<AssetContent>> {
41-
let context = self.entry.ident().path().parent().await?;
41+
let parent_dir = self.entry.ident().path().parent().await?;
4242
// For clippy -- This explicit deref is necessary
4343
let entry_path = &*self.entry.ident().path().await?;
4444
let mut result = Vec::new();
45-
if let Some(self_path) = context.get_relative_path_to(entry_path) {
45+
if let Some(self_path) = parent_dir.get_relative_path_to(entry_path) {
4646
let set = all_modules(self.entry);
4747
for asset in set.await?.iter() {
4848
let path = asset.ident().path().await?;
49-
if let Some(rel_path) = context.get_relative_path_to(&path) {
49+
if let Some(rel_path) = parent_dir.get_relative_path_to(&path) {
5050
if rel_path != self_path {
5151
result.push(rel_path);
5252
}

crates/turbo-tasks-build/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,13 @@ impl<'a> RegisterContext<'a> {
267267
fn process_mod(&mut self, mod_item: ItemMod) -> Result<()> {
268268
if mod_item.content.is_none() {
269269
let name = mod_item.ident.to_string();
270-
let context = self.file_path.parent().unwrap();
271-
let direct = context.join(format!("{name}.rs"));
270+
let parent_path = self.file_path.parent().unwrap();
271+
let direct = parent_path.join(format!("{name}.rs"));
272272
if direct.exists() {
273273
self.queue
274274
.push((format!("{}::{name}", self.mod_path), direct));
275275
} else {
276-
let nested = context.join(&name).join("mod.rs");
276+
let nested = parent_path.join(&name).join("mod.rs");
277277
if nested.exists() {
278278
self.queue
279279
.push((format!("{}::{name}", self.mod_path), nested));

crates/turbo-tasks-fetch/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ impl FetchError {
108108
pub async fn to_issue(
109109
self: Vc<Self>,
110110
severity: Vc<IssueSeverity>,
111-
context: Vc<FileSystemPath>,
111+
issue_context: Vc<FileSystemPath>,
112112
) -> Result<Vc<FetchIssue>> {
113113
let this = &*self.await?;
114114
Ok(FetchIssue {
115-
context,
115+
issue_context,
116116
severity,
117117
url: this.url,
118118
kind: this.kind,
@@ -124,7 +124,7 @@ impl FetchError {
124124

125125
#[turbo_tasks::value(shared)]
126126
pub struct FetchIssue {
127-
pub context: Vc<FileSystemPath>,
127+
pub issue_context: Vc<FileSystemPath>,
128128
pub severity: Vc<IssueSeverity>,
129129
pub url: Vc<String>,
130130
pub kind: Vc<FetchErrorKind>,
@@ -135,7 +135,7 @@ pub struct FetchIssue {
135135
impl Issue for FetchIssue {
136136
#[turbo_tasks::function]
137137
fn context(&self) -> Vc<FileSystemPath> {
138-
self.context
138+
self.issue_context
139139
}
140140

141141
#[turbo_tasks::function]

crates/turbo-tasks-fs/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -879,25 +879,25 @@ pub struct FileSystemPath {
879879
}
880880

881881
impl FileSystemPath {
882-
pub fn is_inside_ref(&self, context: &FileSystemPath) -> bool {
883-
if self.fs == context.fs && self.path.starts_with(&context.path) {
884-
if context.path.is_empty() {
882+
pub fn is_inside_ref(&self, other: &FileSystemPath) -> bool {
883+
if self.fs == other.fs && self.path.starts_with(&other.path) {
884+
if other.path.is_empty() {
885885
true
886886
} else {
887-
self.path.as_bytes().get(context.path.len()) == Some(&b'/')
887+
self.path.as_bytes().get(other.path.len()) == Some(&b'/')
888888
}
889889
} else {
890890
false
891891
}
892892
}
893893

894-
pub fn is_inside_or_equal_ref(&self, context: &FileSystemPath) -> bool {
895-
if self.fs == context.fs && self.path.starts_with(&context.path) {
896-
if context.path.is_empty() {
894+
pub fn is_inside_or_equal_ref(&self, other: &FileSystemPath) -> bool {
895+
if self.fs == other.fs && self.path.starts_with(&other.path) {
896+
if other.path.is_empty() {
897897
true
898898
} else {
899899
matches!(
900-
self.path.as_bytes().get(context.path.len()),
900+
self.path.as_bytes().get(other.path.len()),
901901
Some(&b'/') | None
902902
)
903903
}

crates/turbo-tasks/src/read_ref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ where
6565
T: VcValueType,
6666
<T::Read as VcRead<T>>::Target: TraceRawVcs,
6767
{
68-
fn trace_raw_vcs(&self, context: &mut TraceRawVcsContext) {
69-
(**self).trace_raw_vcs(context);
68+
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
69+
(**self).trace_raw_vcs(trace_context);
7070
}
7171
}
7272

crates/turbo-tasks/src/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ impl<T: Debug> Debug for State<T> {
3333
}
3434

3535
impl<T: TraceRawVcs> TraceRawVcs for State<T> {
36-
fn trace_raw_vcs(&self, context: &mut crate::trace::TraceRawVcsContext) {
37-
self.inner.lock().value.trace_raw_vcs(context);
36+
fn trace_raw_vcs(&self, trace_context: &mut crate::trace::TraceRawVcsContext) {
37+
self.inner.lock().value.trace_raw_vcs(trace_context);
3838
}
3939
}
4040

0 commit comments

Comments
 (0)