Skip to content

Commit

Permalink
refactor(turbopack): Use ResolvedVc<T> for struct fields in extra c…
Browse files Browse the repository at this point in the history
…rates (#73451)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
  • Loading branch information
kdy1 authored Dec 8, 2024
1 parent 6b9baaa commit ea08b0f
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 65 deletions.
30 changes: 21 additions & 9 deletions crates/next-core/src/app_segment_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,26 @@ impl NextSegmentConfig {
/// An issue that occurred while parsing the app segment config.
#[turbo_tasks::value(shared)]
pub struct NextSegmentConfigParsingIssue {
ident: Vc<AssetIdent>,
ident: ResolvedVc<AssetIdent>,
detail: ResolvedVc<StyledString>,
source: Vc<IssueSource>,
source: ResolvedVc<IssueSource>,
}

#[turbo_tasks::value_impl]
impl NextSegmentConfigParsingIssue {
#[turbo_tasks::function]
pub fn new(
ident: ResolvedVc<AssetIdent>,
detail: ResolvedVc<StyledString>,
source: ResolvedVc<IssueSource>,
) -> Vc<Self> {
Self {
ident,
detail,
source,
}
.cell()
}
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -333,13 +350,8 @@ fn parse_config_value(
let detail =
StyledString::Text(format!("{detail} Got {explainer}.{hints}").into()).resolved_cell();

NextSegmentConfigParsingIssue {
ident: source.ident(),
detail,
source: issue_source(source, span),
}
.cell()
.emit();
NextSegmentConfigParsingIssue::new(source.ident(), *detail, issue_source(source, span))
.emit();
};

match &*ident.sym {
Expand Down
57 changes: 30 additions & 27 deletions crates/next-core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,18 @@ impl ValueDefault for NextSourceConfig {
/// An issue that occurred while parsing the page config.
#[turbo_tasks::value(shared)]
pub struct NextSourceConfigParsingIssue {
ident: Vc<AssetIdent>,
ident: ResolvedVc<AssetIdent>,
detail: ResolvedVc<StyledString>,
}

#[turbo_tasks::value_impl]
impl NextSourceConfigParsingIssue {
#[turbo_tasks::function]
pub fn new(ident: ResolvedVc<AssetIdent>, detail: ResolvedVc<StyledString>) -> Vc<Self> {
Self { ident, detail }.cell()
}
}

#[turbo_tasks::value_impl]
impl Issue for NextSourceConfigParsingIssue {
#[turbo_tasks::function]
Expand Down Expand Up @@ -242,13 +250,11 @@ impl Issue for NextSourceConfigParsingIssue {

fn emit_invalid_config_warning(ident: Vc<AssetIdent>, detail: &str, value: &JsValue) {
let (explainer, hints) = value.explain(2, 0);
NextSourceConfigParsingIssue {
NextSourceConfigParsingIssue::new(
ident,
detail: StyledString::Text(format!("{detail} Got {explainer}.{hints}").into())
.resolved_cell(),
}
.resolved_cell()
.emit()
StyledString::Text(format!("{detail} Got {explainer}.{hints}").into()).cell(),
)
.emit();
}

fn parse_route_matcher_from_js_value(
Expand Down Expand Up @@ -409,17 +415,16 @@ pub async fn parse_config_from_source(
Ok(parse_config_from_js_value(*module, &value).cell())
});
} else {
NextSourceConfigParsingIssue {
ident: module.ident(),
detail: StyledString::Text(
NextSourceConfigParsingIssue::new(
module.ident(),
StyledString::Text(
"The exported config object must contain an variable \
initializer."
.into(),
)
.resolved_cell(),
}
.resolved_cell()
.emit()
.cell(),
)
.emit();
}
}
// Or, check if there is segment runtime option
Expand All @@ -428,15 +433,14 @@ pub async fn parse_config_from_source(
.map(|ident| &*ident.sym == "runtime")
.unwrap_or_default()
{
let runtime_value_issue = NextSourceConfigParsingIssue {
ident: module.ident(),
detail: StyledString::Text(
let runtime_value_issue = NextSourceConfigParsingIssue::new(
module.ident(),
StyledString::Text(
"The runtime property must be either \"nodejs\" or \"edge\"."
.into(),
)
.resolved_cell(),
}
.resolved_cell();
.cell(),
);
if let Some(init) = decl.init.as_ref() {
// skipping eval and directly read the expr's value, as we know it
// should be a const string
Expand All @@ -461,17 +465,16 @@ pub async fn parse_config_from_source(
runtime_value_issue.emit();
}
} else {
NextSourceConfigParsingIssue {
ident: module.ident(),
detail: StyledString::Text(
NextSourceConfigParsingIssue::new(
module.ident(),
StyledString::Text(
"The exported segment runtime option must contain an \
variable initializer."
.into(),
)
.resolved_cell(),
}
.resolved_cell()
.emit()
.cell(),
)
.emit();
}
}
}
Expand Down
27 changes: 25 additions & 2 deletions turbopack/crates/turbopack-core/src/issue/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,34 @@ use crate::ident::AssetIdent;
#[turbo_tasks::value(shared)]
pub struct AnalyzeIssue {
pub severity: ResolvedVc<IssueSeverity>,
pub source_ident: Vc<AssetIdent>,
pub source_ident: ResolvedVc<AssetIdent>,
pub title: ResolvedVc<RcStr>,
pub message: ResolvedVc<StyledString>,
pub code: Option<RcStr>,
pub source: Option<Vc<IssueSource>>,
pub source: Option<ResolvedVc<IssueSource>>,
}

#[turbo_tasks::value_impl]
impl AnalyzeIssue {
#[turbo_tasks::function]
pub fn new(
severity: ResolvedVc<IssueSeverity>,
source_ident: ResolvedVc<AssetIdent>,
title: ResolvedVc<RcStr>,
message: ResolvedVc<StyledString>,
code: Option<RcStr>,
source: Option<ResolvedVc<IssueSource>>,
) -> Vc<Self> {
Self {
severity,
source_ident,
title,
message,
code,
source,
}
.cell()
}
}

#[turbo_tasks::value_impl]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,14 @@ pub async fn expand_star_exports(
}

fn emit_star_exports_issue(source_ident: Vc<AssetIdent>, message: RcStr) {
AnalyzeIssue {
code: None,
message: StyledString::Text(message).resolved_cell(),
AnalyzeIssue::new(
IssueSeverity::Warning.cell(),
source_ident,
severity: IssueSeverity::Warning.resolved_cell(),
source: None,
title: ResolvedVc::cell("unexpected export *".into()),
}
.cell()
Vc::cell("unexpected export *".into()),
StyledString::Text(message).cell(),
None,
None,
)
.emit();
}

Expand Down
18 changes: 8 additions & 10 deletions turbopack/crates/turbopack-ecmascript/src/references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,16 +848,14 @@ pub(crate) async fn analyse_ecmascript_module_internal(
.resolved_cell();
analysis.set_async_module(async_module);
} else if let Some(span) = top_level_await_span {
AnalyzeIssue {
code: None,
message: StyledString::Text("top level await is only supported in ESM modules.".into())
.resolved_cell(),
source_ident: source.ident(),
severity: IssueSeverity::Error.resolved_cell(),
source: Some(issue_source(*source, span)),
title: ResolvedVc::cell("unexpected top level await".into()),
}
.cell()
AnalyzeIssue::new(
IssueSeverity::Error.cell(),
source.ident(),
Vc::cell("unexpected top level await".into()),
StyledString::Text("top level await is only supported in ESM modules.".into()).cell(),
None,
Some(issue_source(*source, span)),
)
.emit();
}

Expand Down
17 changes: 8 additions & 9 deletions turbopack/crates/turbopack-swc-utils/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use swc_core::common::{
SourceMap,
};
use turbo_rcstr::RcStr;
use turbo_tasks::ResolvedVc;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
issue::{analyze::AnalyzeIssue, IssueExt, IssueSeverity, IssueSource, StyledString},
source::Source,
Expand All @@ -17,7 +17,7 @@ pub struct IssueEmitter {
pub source: ResolvedVc<Box<dyn Source>>,
pub source_map: Arc<SourceMap>,
pub title: Option<RcStr>,
pub emitted_issues: Vec<ResolvedVc<AnalyzeIssue>>,
pub emitted_issues: Vec<Vc<AnalyzeIssue>>,
}

impl IssueEmitter {
Expand Down Expand Up @@ -83,15 +83,14 @@ impl Emitter for IssueEmitter {
});
// TODO add other primary and secondary spans with labels as sub_issues

let issue = AnalyzeIssue {
severity,
source_ident: self.source.ident(),
title: ResolvedVc::cell(title),
message: StyledString::Text(message.into()).resolved_cell(),
let issue = AnalyzeIssue::new(
*severity,
self.source.ident(),
Vc::cell(title),
StyledString::Text(message.into()).cell(),
code,
source,
}
.resolved_cell();
);

self.emitted_issues.push(issue);

Expand Down

0 comments on commit ea08b0f

Please sign in to comment.