From a982b260932471033ad57a18a36fc911d39024bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 05:30:56 +0900 Subject: [PATCH 01/12] Type --- crates/turbopack-core/src/issue/mod.rs | 61 +++------------------- crates/turbopack-core/src/issue/resolve.rs | 4 +- 2 files changed, 8 insertions(+), 57 deletions(-) diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index 6c21ad227c698..7aabed43912d7 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -396,65 +396,16 @@ impl CapturedIssues { } } -/// Use this to pass and store byte offsets for an AST node along with its -/// source. When row/column is needed, this can be lazily converted into a -/// proper `IssueSource` at that time using -/// [LazyIssueSource::to_issue_source]. -#[turbo_tasks::value] -#[derive(Clone, Debug)] -pub struct LazyIssueSource { - pub source: Vc>, - pub start_end: Option<(usize, usize)>, -} - -#[turbo_tasks::value_impl] -impl LazyIssueSource { - /// Create a [`LazyIssueSource`] from byte offsets given by an swc ast node - /// span. - /// - /// Arguments: - /// - /// * `source`: The source code in which to look up the byte offsets. - /// * `start`: The start index of the span. Must use **1-based** indexing. - /// * `end`: The end index of the span. Must use **1-based** indexing. - #[turbo_tasks::function] - pub fn from_swc_offsets(source: Vc>, start: usize, end: usize) -> Vc { - match (start == 0, end == 0) { - (true, true) => Self::cell(LazyIssueSource { - source, - start_end: None, - }), - (false, false) => Self::cell(LazyIssueSource { - source, - start_end: Some((start - 1, end - 1)), - }), - (false, true) => Self::cell(LazyIssueSource { - source, - start_end: Some((start - 1, start - 1)), - }), - (true, false) => Self::cell(LazyIssueSource { - source, - start_end: Some((end - 1, end - 1)), - }), - } - } - - #[turbo_tasks::function] - pub async fn to_issue_source(self: Vc) -> Result> { - let this = &*self.await?; - Ok(if let Some((start, end)) = this.start_end { - IssueSource::from_byte_offset(this.source, start, end) - } else { - IssueSource::from_source_only(this.source) - }) - } -} - #[turbo_tasks::value] #[derive(Clone, Debug)] pub struct IssueSource { source: Vc>, - range: Option<(SourcePos, SourcePos)>, + range: Range, +} + +enum Range { + Normal(Option<(SourcePos, SourcePos)>), + Lazy(Option<(usize, usize)>), } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-core/src/issue/resolve.rs b/crates/turbopack-core/src/issue/resolve.rs index b78159f82169e..18307ae2514e8 100644 --- a/crates/turbopack-core/src/issue/resolve.rs +++ b/crates/turbopack-core/src/issue/resolve.rs @@ -7,7 +7,7 @@ use turbo_tasks_fs::FileSystemPath; use super::{Issue, OptionIssueSource}; use crate::{ error::PrettyPrintError, - issue::{IssueSeverity, LazyIssueSource}, + issue::IssueSeverity, resolve::{options::ResolveOptions, parse::Request}, }; @@ -19,7 +19,7 @@ pub struct ResolvingIssue { pub file_path: Vc, pub resolve_options: Vc, pub error_message: Option, - pub source: Option>, + pub source: Option>, } #[turbo_tasks::value_impl] From 023719fc8c1c994d6becb6675af9440908d3786c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 05:32:54 +0900 Subject: [PATCH 02/12] Replace & Range --- crates/turbopack-core/src/issue/analyze.rs | 4 ++-- crates/turbopack-core/src/issue/mod.rs | 8 +++++--- crates/turbopack-core/src/resolve/mod.rs | 4 ++-- crates/turbopack-css/src/references/import.rs | 4 ++-- crates/turbopack-css/src/references/mod.rs | 2 +- crates/turbopack-css/src/references/url.rs | 4 ++-- crates/turbopack-ecmascript/src/analyzer/imports.rs | 2 +- crates/turbopack-ecmascript/src/references/amd.rs | 8 ++++---- crates/turbopack-ecmascript/src/references/cjs.rs | 12 ++++++------ .../turbopack-ecmascript/src/references/esm/base.rs | 4 ++-- .../src/references/esm/dynamic.rs | 4 ++-- .../turbopack-ecmascript/src/references/esm/url.rs | 4 ++-- crates/turbopack-ecmascript/src/references/mod.rs | 2 +- .../src/references/require_context.rs | 6 +++--- crates/turbopack-ecmascript/src/resolve/mod.rs | 8 ++++---- 15 files changed, 39 insertions(+), 37 deletions(-) diff --git a/crates/turbopack-core/src/issue/analyze.rs b/crates/turbopack-core/src/issue/analyze.rs index 7d5b1e975ed0d..520994ad87259 100644 --- a/crates/turbopack-core/src/issue/analyze.rs +++ b/crates/turbopack-core/src/issue/analyze.rs @@ -2,7 +2,7 @@ use anyhow::Result; use turbo_tasks::Vc; use turbo_tasks_fs::FileSystemPath; -use super::{Issue, IssueSeverity, LazyIssueSource, OptionIssueSource}; +use super::{Issue, IssueSeverity, IssueSource, OptionIssueSource}; use crate::ident::AssetIdent; #[turbo_tasks::value(shared)] @@ -13,7 +13,7 @@ pub struct AnalyzeIssue { pub message: Vc, pub category: Vc, pub code: Option, - pub source: Option>, + pub source: Option>, } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index 7aabed43912d7..119bbdb1de68a 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -400,12 +400,14 @@ impl CapturedIssues { #[derive(Clone, Debug)] pub struct IssueSource { source: Vc>, - range: Range, + range: Option>, } +#[turbo_tasks::value] +#[derive(Clone, Debug)] enum Range { - Normal(Option<(SourcePos, SourcePos)>), - Lazy(Option<(usize, usize)>), + Normal(SourcePos, SourcePos), + Lazy(usize, usize), } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-core/src/resolve/mod.rs b/crates/turbopack-core/src/resolve/mod.rs index 44d66070daf16..edf4f2472074a 100644 --- a/crates/turbopack-core/src/resolve/mod.rs +++ b/crates/turbopack-core/src/resolve/mod.rs @@ -26,7 +26,7 @@ use self::{ }; use crate::{ file_source::FileSource, - issue::{resolve::ResolvingIssue, IssueExt, LazyIssueSource}, + issue::{resolve::ResolvingIssue, IssueExt, IssueSource}, module::{Module, Modules, OptionModule}, output::{OutputAsset, OutputAssets}, package_json::{read_package_json, PackageJsonIssue}, @@ -1931,7 +1931,7 @@ pub async fn handle_resolve_error( request: Vc, resolve_options: Vc, severity: Vc, - source: Option>, + source: Option>, ) -> Result> { Ok(match result.is_unresolveable().await { Ok(unresolveable) => { diff --git a/crates/turbopack-css/src/references/import.rs b/crates/turbopack-css/src/references/import.rs index b3340e71875b5..b6a56bfbdd35e 100644 --- a/crates/turbopack-css/src/references/import.rs +++ b/crates/turbopack-css/src/references/import.rs @@ -187,7 +187,7 @@ pub struct ImportAssetReference { pub request: Vc, pub path: Vc, pub attributes: Vc, - pub issue_source: Vc, + pub issue_source: Vc, } #[turbo_tasks::value_impl] @@ -198,7 +198,7 @@ impl ImportAssetReference { request: Vc, path: Vc, attributes: Vc, - issue_source: Vc, + issue_source: Vc, ) -> Vc { Self::cell(ImportAssetReference { origin, diff --git a/crates/turbopack-css/src/references/mod.rs b/crates/turbopack-css/src/references/mod.rs index 0c9672f3debf9..392f870cd3f72 100644 --- a/crates/turbopack-css/src/references/mod.rs +++ b/crates/turbopack-css/src/references/mod.rs @@ -177,7 +177,7 @@ pub async fn css_resolve( origin: Vc>, request: Vc, ty: Value, - issue_source: Option>, + issue_source: Option>, ) -> Result> { let ty = Value::new(ReferenceType::Css(ty.into_value())); let options = origin.resolve_options(ty.clone()); diff --git a/crates/turbopack-css/src/references/url.rs b/crates/turbopack-css/src/references/url.rs index db471566ee7fe..a58ad3185a0d6 100644 --- a/crates/turbopack-css/src/references/url.rs +++ b/crates/turbopack-css/src/references/url.rs @@ -37,7 +37,7 @@ pub struct UrlAssetReference { pub origin: Vc>, pub request: Vc, pub path: Vc, - pub issue_source: Vc, + pub issue_source: Vc, } #[turbo_tasks::value_impl] @@ -47,7 +47,7 @@ impl UrlAssetReference { origin: Vc>, request: Vc, path: Vc, - issue_source: Vc, + issue_source: Vc, ) -> Vc { Self::cell(UrlAssetReference { origin, diff --git a/crates/turbopack-ecmascript/src/analyzer/imports.rs b/crates/turbopack-ecmascript/src/analyzer/imports.rs index 5c852b654d88d..654b225a2d59e 100644 --- a/crates/turbopack-ecmascript/src/analyzer/imports.rs +++ b/crates/turbopack-ecmascript/src/analyzer/imports.rs @@ -118,7 +118,7 @@ pub(crate) struct ImportMapReference { pub module_path: JsWord, pub imported_symbol: ImportedSymbol, pub annotations: ImportAnnotations, - pub issue_source: Option>, + pub issue_source: Option>, } impl ImportMap { diff --git a/crates/turbopack-ecmascript/src/references/amd.rs b/crates/turbopack-ecmascript/src/references/amd.rs index bb58f76de8f83..80d2288a3d687 100644 --- a/crates/turbopack-ecmascript/src/references/amd.rs +++ b/crates/turbopack-ecmascript/src/references/amd.rs @@ -34,7 +34,7 @@ use crate::{ pub struct AmdDefineAssetReference { origin: Vc>, request: Vc, - issue_source: Vc, + issue_source: Vc, in_try: bool, } @@ -44,7 +44,7 @@ impl AmdDefineAssetReference { pub fn new( origin: Vc>, request: Vc, - issue_source: Vc, + issue_source: Vc, in_try: bool, ) -> Vc { Self::cell(AmdDefineAssetReference { @@ -109,7 +109,7 @@ pub struct AmdDefineWithDependenciesCodeGen { origin: Vc>, path: Vc, factory_type: AmdDefineFactoryType, - issue_source: Vc, + issue_source: Vc, in_try: bool, } @@ -119,7 +119,7 @@ impl AmdDefineWithDependenciesCodeGen { origin: Vc>, path: Vc, factory_type: AmdDefineFactoryType, - issue_source: Vc, + issue_source: Vc, in_try: bool, ) -> Vc { Self::cell(AmdDefineWithDependenciesCodeGen { diff --git a/crates/turbopack-ecmascript/src/references/cjs.rs b/crates/turbopack-ecmascript/src/references/cjs.rs index 18bf52e575d36..5ab63dea2bb24 100644 --- a/crates/turbopack-ecmascript/src/references/cjs.rs +++ b/crates/turbopack-ecmascript/src/references/cjs.rs @@ -25,7 +25,7 @@ use crate::{ pub struct CjsAssetReference { pub origin: Vc>, pub request: Vc, - pub issue_source: Vc, + pub issue_source: Vc, pub in_try: bool, } @@ -35,7 +35,7 @@ impl CjsAssetReference { pub fn new( origin: Vc>, request: Vc, - issue_source: Vc, + issue_source: Vc, in_try: bool, ) -> Vc { Self::cell(CjsAssetReference { @@ -80,7 +80,7 @@ pub struct CjsRequireAssetReference { pub origin: Vc>, pub request: Vc, pub path: Vc, - pub issue_source: Vc, + pub issue_source: Vc, pub in_try: bool, } @@ -91,7 +91,7 @@ impl CjsRequireAssetReference { origin: Vc>, request: Vc, path: Vc, - issue_source: Vc, + issue_source: Vc, in_try: bool, ) -> Vc { Self::cell(CjsRequireAssetReference { @@ -204,7 +204,7 @@ pub struct CjsRequireResolveAssetReference { pub origin: Vc>, pub request: Vc, pub path: Vc, - pub issue_source: Vc, + pub issue_source: Vc, pub in_try: bool, } @@ -215,7 +215,7 @@ impl CjsRequireResolveAssetReference { origin: Vc>, request: Vc, path: Vc, - issue_source: Vc, + issue_source: Vc, in_try: bool, ) -> Vc { Self::cell(CjsRequireResolveAssetReference { diff --git a/crates/turbopack-ecmascript/src/references/esm/base.rs b/crates/turbopack-ecmascript/src/references/esm/base.rs index ea66adb034c0c..a3246f6c07b33 100644 --- a/crates/turbopack-ecmascript/src/references/esm/base.rs +++ b/crates/turbopack-ecmascript/src/references/esm/base.rs @@ -103,7 +103,7 @@ pub struct EsmAssetReference { pub origin: Vc>, pub request: Vc, pub annotations: ImportAnnotations, - pub issue_source: Option>, + pub issue_source: Option>, pub export_name: Option>, } @@ -127,7 +127,7 @@ impl EsmAssetReference { pub fn new( origin: Vc>, request: Vc, - issue_source: Option>, + issue_source: Option>, annotations: Value, export_name: Option>, ) -> Vc { diff --git a/crates/turbopack-ecmascript/src/references/esm/dynamic.rs b/crates/turbopack-ecmascript/src/references/esm/dynamic.rs index 6f2e601c3bf7e..8728e3d8dce04 100644 --- a/crates/turbopack-ecmascript/src/references/esm/dynamic.rs +++ b/crates/turbopack-ecmascript/src/references/esm/dynamic.rs @@ -27,7 +27,7 @@ pub struct EsmAsyncAssetReference { pub origin: Vc>, pub request: Vc, pub path: Vc, - pub issue_source: Vc, + pub issue_source: Vc, pub in_try: bool, } @@ -38,7 +38,7 @@ impl EsmAsyncAssetReference { origin: Vc>, request: Vc, path: Vc, - issue_source: Vc, + issue_source: Vc, in_try: bool, ) -> Vc { Self::cell(EsmAsyncAssetReference { diff --git a/crates/turbopack-ecmascript/src/references/esm/url.rs b/crates/turbopack-ecmascript/src/references/esm/url.rs index 1ebf253a5856f..d1705034a7951 100644 --- a/crates/turbopack-ecmascript/src/references/esm/url.rs +++ b/crates/turbopack-ecmascript/src/references/esm/url.rs @@ -50,7 +50,7 @@ pub struct UrlAssetReference { request: Vc, rendering: Vc, ast_path: Vc, - issue_source: Vc, + issue_source: Vc, in_try: bool, url_rewrite_behavior: Vc, } @@ -63,7 +63,7 @@ impl UrlAssetReference { request: Vc, rendering: Vc, ast_path: Vc, - issue_source: Vc, + issue_source: Vc, in_try: bool, url_rewrite_behavior: Vc, ) -> Vc { diff --git a/crates/turbopack-ecmascript/src/references/mod.rs b/crates/turbopack-ecmascript/src/references/mod.rs index 1c6144f1e98d4..ef8f27700e3dd 100644 --- a/crates/turbopack-ecmascript/src/references/mod.rs +++ b/crates/turbopack-ecmascript/src/references/mod.rs @@ -1792,7 +1792,7 @@ async fn handle_free_var_reference( Ok(true) } -fn issue_source(source: Vc>, span: Span) -> Vc { +fn issue_source(source: Vc>, span: Span) -> Vc { LazyIssueSource::from_swc_offsets(source, span.lo.to_usize(), span.hi.to_usize()) } diff --git a/crates/turbopack-ecmascript/src/references/require_context.rs b/crates/turbopack-ecmascript/src/references/require_context.rs index eb8a9a5687dd1..2d9edf9c0de9f 100644 --- a/crates/turbopack-ecmascript/src/references/require_context.rs +++ b/crates/turbopack-ecmascript/src/references/require_context.rs @@ -165,7 +165,7 @@ impl RequireContextMap { dir: Vc, recursive: bool, filter: Vc, - issue_source: Option>, + issue_source: Option>, issue_severity: Vc, ) -> Result> { let origin_path = &*origin.origin_path().parent().await?; @@ -206,7 +206,7 @@ pub struct RequireContextAssetReference { pub include_subdirs: bool, pub path: Vc, - pub issue_source: Option>, + pub issue_source: Option>, pub in_try: bool, } @@ -220,7 +220,7 @@ impl RequireContextAssetReference { include_subdirs: bool, filter: Vc, path: Vc, - issue_source: Option>, + issue_source: Option>, in_try: bool, ) -> Vc { let map = RequireContextMap::generate( diff --git a/crates/turbopack-ecmascript/src/resolve/mod.rs b/crates/turbopack-ecmascript/src/resolve/mod.rs index 90567ea32b646..d6e096265bc50 100644 --- a/crates/turbopack-ecmascript/src/resolve/mod.rs +++ b/crates/turbopack-ecmascript/src/resolve/mod.rs @@ -65,7 +65,7 @@ pub async fn esm_resolve( request: Vc, ty: Value, issue_severity: Vc, - issue_source: Option>, + issue_source: Option>, ) -> Result> { let ty = Value::new(ReferenceType::EcmaScriptModules(ty.into_value())); let options = apply_esm_specific_options(origin.resolve_options(ty.clone())) @@ -78,7 +78,7 @@ pub async fn esm_resolve( pub async fn cjs_resolve( origin: Vc>, request: Vc, - issue_source: Option>, + issue_source: Option>, issue_severity: Vc, ) -> Result> { // TODO pass CommonJsReferenceSubType @@ -94,7 +94,7 @@ pub async fn url_resolve( origin: Vc>, request: Vc, ty: Value, - issue_source: Vc, + issue_source: Vc, issue_severity: Vc, ) -> Result> { let ty = Value::new(ReferenceType::Url(ty.into_value())); @@ -129,7 +129,7 @@ async fn specific_resolve( options: Vc, reference_type: Value, issue_severity: Vc, - issue_source: Option>, + issue_source: Option>, ) -> Result> { let result = origin.resolve_asset(request, options, reference_type.clone()); From 106211982824ec1293f2230c0059428be3e64443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 05:34:52 +0900 Subject: [PATCH 03/12] Rename --- crates/turbopack-core/src/issue/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index 119bbdb1de68a..eeb3ee516c696 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -400,12 +400,12 @@ impl CapturedIssues { #[derive(Clone, Debug)] pub struct IssueSource { source: Vc>, - range: Option>, + range: Option>, } #[turbo_tasks::value] #[derive(Clone, Debug)] -enum Range { +enum SourceRange { Normal(SourcePos, SourcePos), Lazy(usize, usize), } From 7172634469f9b65dd7fb2e17433f3942e4e956a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 05:37:24 +0900 Subject: [PATCH 04/12] more --- crates/turbopack-core/src/issue/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index eeb3ee516c696..fc196bd88e3a3 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -463,7 +463,7 @@ impl IssueSource { range: if let FileLinesContent::Lines(lines) = &*source.content().lines().await? { let start = find_line_and_column(lines.as_ref(), start); let end = find_line_and_column(lines.as_ref(), end); - Some((start, end)) + Some(SourceRange::Normal(start, end).cell()) } else { None }, From 45647b4604df9c2cc7d7b52d8aacefb6e8cb97cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 06:02:11 +0900 Subject: [PATCH 05/12] wip --- crates/turbopack-core/src/issue/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index fc196bd88e3a3..e28098ecd55ca 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -568,7 +568,12 @@ impl IssueSource { let this = self.await?; Ok(PlainIssueSource { asset: PlainSource::from_source(this.source).await?, - range: this.range, + range: match &*this.range.await? { + SourceRange::Normal(start, end) => Some((start.clone_value(), end.clone_value())), + SourceRange::Lazy(start, end) => { + todo!() + } + }, } .cell()) } From 5ec03332dcc845f6d989827f1980422f6432a14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 06:08:10 +0900 Subject: [PATCH 06/12] fix --- crates/turbopack-core/src/issue/mod.rs | 60 +++++++++++++++----------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index e28098ecd55ca..b503a732909c2 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -439,25 +439,6 @@ impl IssueSource { start: usize, end: usize, ) -> Result> { - fn find_line_and_column(lines: &[FileLine], offset: usize) -> SourcePos { - match lines.binary_search_by(|line| line.bytes_offset.cmp(&offset)) { - Ok(i) => SourcePos { line: i, column: 0 }, - Err(i) => { - if i == 0 { - SourcePos { - line: 0, - column: offset, - } - } else { - let line = &lines[i - 1]; - SourcePos { - line: i - 1, - column: min(line.content.len(), offset - line.bytes_offset), - } - } - } - } - } Ok(Self::cell(IssueSource { source, range: if let FileLinesContent::Lines(lines) = &*source.content().lines().await? { @@ -568,11 +549,22 @@ impl IssueSource { let this = self.await?; Ok(PlainIssueSource { asset: PlainSource::from_source(this.source).await?, - range: match &*this.range.await? { - SourceRange::Normal(start, end) => Some((start.clone_value(), end.clone_value())), - SourceRange::Lazy(start, end) => { - todo!() - } + range: match this.range { + Some(range) => match &*range.await? { + SourceRange::Normal(start, end) => Some((*start, *end)), + SourceRange::Lazy(start, end) => { + if let FileLinesContent::Lines(lines) = + &*this.source.content().lines().await? + { + let start = find_line_and_column(lines.as_ref(), start); + let end = find_line_and_column(lines.as_ref(), end); + Some((start, end)) + } else { + None + } + } + }, + _ => None, }, } .cell()) @@ -788,3 +780,23 @@ pub async fn handle_issues( Ok(()) } } + +fn find_line_and_column(lines: &[FileLine], offset: usize) -> SourcePos { + match lines.binary_search_by(|line| line.bytes_offset.cmp(&offset)) { + Ok(i) => SourcePos { line: i, column: 0 }, + Err(i) => { + if i == 0 { + SourcePos { + line: 0, + column: offset, + } + } else { + let line = &lines[i - 1]; + SourcePos { + line: i - 1, + column: min(line.content.len(), offset - line.bytes_offset), + } + } + } + } +} From b34ebfc0e997c84ccd93075e0ac98aa8a743b9c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 06:20:34 +0900 Subject: [PATCH 07/12] fix --- crates/turbopack-core/src/issue/analyze.rs | 2 +- crates/turbopack-core/src/issue/mod.rs | 4 ++-- crates/turbopack-core/src/issue/resolve.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/turbopack-core/src/issue/analyze.rs b/crates/turbopack-core/src/issue/analyze.rs index 520994ad87259..9e732e2c37e3d 100644 --- a/crates/turbopack-core/src/issue/analyze.rs +++ b/crates/turbopack-core/src/issue/analyze.rs @@ -49,6 +49,6 @@ impl Issue for AnalyzeIssue { #[turbo_tasks::function] fn source(&self) -> Vc { - Vc::cell(self.source.map(|s| s.to_issue_source())) + Vc::cell(self.source) } } diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index b503a732909c2..25fffc02e6433 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -556,8 +556,8 @@ impl IssueSource { if let FileLinesContent::Lines(lines) = &*this.source.content().lines().await? { - let start = find_line_and_column(lines.as_ref(), start); - let end = find_line_and_column(lines.as_ref(), end); + let start = find_line_and_column(lines.as_ref(), *start); + let end = find_line_and_column(lines.as_ref(), *end); Some((start, end)) } else { None diff --git a/crates/turbopack-core/src/issue/resolve.rs b/crates/turbopack-core/src/issue/resolve.rs index 18307ae2514e8..fb7f16f6610a6 100644 --- a/crates/turbopack-core/src/issue/resolve.rs +++ b/crates/turbopack-core/src/issue/resolve.rs @@ -4,7 +4,7 @@ use anyhow::Result; use turbo_tasks::{ValueToString, Vc}; use turbo_tasks_fs::FileSystemPath; -use super::{Issue, OptionIssueSource}; +use super::{Issue, IssueSource, OptionIssueSource}; use crate::{ error::PrettyPrintError, issue::IssueSeverity, @@ -102,7 +102,7 @@ impl Issue for ResolvingIssue { #[turbo_tasks::function] fn source(&self) -> Vc { - Vc::cell(self.source.map(|s| s.to_issue_source())) + Vc::cell(self.source) } // TODO add sub_issue for a description of resolve_options From 076df62f293429242d0dc8c27b0b99919550f4b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 06:22:57 +0900 Subject: [PATCH 08/12] fix --- crates/turbopack-core/src/issue/mod.rs | 21 +++++++++++++++++++++ crates/turbopack-swc-utils/src/emitter.rs | 4 ++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index 25fffc02e6433..7e6093300e09e 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -422,6 +422,27 @@ impl IssueSource { }) } + /// Create a [`IssueSource`] from byte offsets given by an swc ast node + /// span. + /// + /// Arguments: + /// + /// * `source`: The source code in which to look up the byte offsets. + /// * `start`: The start index of the span. Must use **1-based** indexing. + /// * `end`: The end index of the span. Must use **1-based** indexing. + #[turbo_tasks::function] + pub fn from_swc_offsets(source: Vc>, start: usize, end: usize) -> Vc { + Self::cell(IssueSource { + source, + range: match (start == 0, end == 0) { + (true, true) => None, + (false, false) => Some(SourceRange::Lazy(start - 1, end - 1).cell()), + (false, true) => Some((start - 1, start - 1)), + (true, false) => Some((end - 1, end - 1)), + }, + }) + } + #[turbo_tasks::function] /// Returns an `IssueSource` representing a span of code in the `source`. /// Positions are derived from byte offsets and stored as lines and columns. diff --git a/crates/turbopack-swc-utils/src/emitter.rs b/crates/turbopack-swc-utils/src/emitter.rs index ac00ec12dc8fe..07bc7f05d4839 100644 --- a/crates/turbopack-swc-utils/src/emitter.rs +++ b/crates/turbopack-swc-utils/src/emitter.rs @@ -7,7 +7,7 @@ use swc_core::common::{ }; use turbo_tasks::Vc; use turbopack_core::{ - issue::{analyze::AnalyzeIssue, IssueExt, IssueSeverity, LazyIssueSource}, + issue::{analyze::AnalyzeIssue, IssueExt, IssueSeverity, IssueSource}, source::Source, }; @@ -41,7 +41,7 @@ impl Emitter for IssueEmitter { } let source = db.span.primary_span().map(|span| { - LazyIssueSource::from_swc_offsets(self.source, span.lo.to_usize(), span.hi.to_usize()) + IssueSource::from_swc_offsets(self.source, span.lo.to_usize(), span.hi.to_usize()) }); // TODO add other primary and secondary spans with labels as sub_issues From 25a8bd43bc404e5aaed8d2f8427802c4e23d93c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 06:23:31 +0900 Subject: [PATCH 09/12] fix --- crates/turbopack-core/src/issue/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index 7e6093300e09e..358c20682f0e4 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -437,8 +437,8 @@ impl IssueSource { range: match (start == 0, end == 0) { (true, true) => None, (false, false) => Some(SourceRange::Lazy(start - 1, end - 1).cell()), - (false, true) => Some((start - 1, start - 1)), - (true, false) => Some((end - 1, end - 1)), + (false, true) => Some(SourceRange::Lazy(start - 1, start - 1).cell()), + (true, false) => Some(SourceRange::Lazy(end - 1, end - 1).cell()), }, }) } From a491b8bdd22c02727ff4382fd439a6153b0050d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 06:28:08 +0900 Subject: [PATCH 10/12] replace --- crates/turbopack-css/src/references/import.rs | 2 +- crates/turbopack-css/src/references/mod.rs | 6 +++--- crates/turbopack-css/src/references/url.rs | 2 +- crates/turbopack-ecmascript/src/analyzer/imports.rs | 4 ++-- crates/turbopack-ecmascript/src/references/amd.rs | 2 +- crates/turbopack-ecmascript/src/references/cjs.rs | 2 +- .../turbopack-ecmascript/src/references/esm/base.rs | 2 +- .../src/references/esm/dynamic.rs | 2 +- .../turbopack-ecmascript/src/references/esm/url.rs | 2 +- crates/turbopack-ecmascript/src/references/mod.rs | 12 ++++-------- .../src/references/require_context.rs | 2 +- crates/turbopack-ecmascript/src/resolve/mod.rs | 2 +- 12 files changed, 18 insertions(+), 22 deletions(-) diff --git a/crates/turbopack-css/src/references/import.rs b/crates/turbopack-css/src/references/import.rs index b6a56bfbdd35e..e8d253712246a 100644 --- a/crates/turbopack-css/src/references/import.rs +++ b/crates/turbopack-css/src/references/import.rs @@ -12,7 +12,7 @@ use swc_core::{ use turbo_tasks::{Value, ValueToString, Vc}; use turbopack_core::{ chunk::{ChunkableModuleReference, ChunkingContext}, - issue::LazyIssueSource, + issue::IssueSource, reference::ModuleReference, reference_type::CssReferenceSubType, resolve::{origin::ResolveOrigin, parse::Request, ModuleResolveResult}, diff --git a/crates/turbopack-css/src/references/mod.rs b/crates/turbopack-css/src/references/mod.rs index 392f870cd3f72..764524e5c5fb0 100644 --- a/crates/turbopack-css/src/references/mod.rs +++ b/crates/turbopack-css/src/references/mod.rs @@ -12,7 +12,7 @@ use swc_core::{ }; use turbo_tasks::{Value, Vc}; use turbopack_core::{ - issue::{IssueSeverity, LazyIssueSource}, + issue::{IssueSeverity, IssueSource}, reference::{ModuleReference, ModuleReferences}, reference_type::{CssReferenceSubType, ReferenceType}, resolve::{ @@ -133,7 +133,7 @@ impl<'a> VisitAstPath for ModuleReferencesVisitor<'a> { Request::parse(Value::new(src.to_string().into())), Vc::cell(as_parent_path(ast_path)), ImportAttributes::new_from_prelude(i).into(), - LazyIssueSource::from_swc_offsets( + IssueSource::from_swc_offsets( Vc::upcast(self.source), issue_span.lo.to_usize(), issue_span.hi.to_usize(), @@ -160,7 +160,7 @@ impl<'a> VisitAstPath for ModuleReferencesVisitor<'a> { self.origin, Request::parse(Value::new(src.to_string().into())), Vc::cell(as_parent_path(ast_path)), - LazyIssueSource::from_swc_offsets( + IssueSource::from_swc_offsets( Vc::upcast(self.source), issue_span.lo.to_usize(), issue_span.hi.to_usize(), diff --git a/crates/turbopack-css/src/references/url.rs b/crates/turbopack-css/src/references/url.rs index a58ad3185a0d6..82304e7b14646 100644 --- a/crates/turbopack-css/src/references/url.rs +++ b/crates/turbopack-css/src/references/url.rs @@ -10,7 +10,7 @@ use turbopack_core::{ ChunkingTypeOption, }, ident::AssetIdent, - issue::{IssueSeverity, LazyIssueSource}, + issue::{IssueSeverity, IssueSource}, output::OutputAsset, reference::ModuleReference, reference_type::UrlReferenceSubType, diff --git a/crates/turbopack-ecmascript/src/analyzer/imports.rs b/crates/turbopack-ecmascript/src/analyzer/imports.rs index 654b225a2d59e..cdc5838a04a54 100644 --- a/crates/turbopack-ecmascript/src/analyzer/imports.rs +++ b/crates/turbopack-ecmascript/src/analyzer/imports.rs @@ -11,7 +11,7 @@ use swc_core::{ }, }; use turbo_tasks::Vc; -use turbopack_core::{issue::LazyIssueSource, source::Source}; +use turbopack_core::{issue::IssueSource, source::Source}; use super::{JsValue, ModuleValue}; use crate::utils::unparen; @@ -196,7 +196,7 @@ impl<'a> Analyzer<'a> { ) -> usize { let issue_source = self .source - .map(|s| LazyIssueSource::from_swc_offsets(s, span.lo.to_usize(), span.hi.to_usize())); + .map(|s| IssueSource::from_swc_offsets(s, span.lo.to_usize(), span.hi.to_usize())); let r = ImportMapReference { module_path, diff --git a/crates/turbopack-ecmascript/src/references/amd.rs b/crates/turbopack-ecmascript/src/references/amd.rs index 80d2288a3d687..cd42e27a81d16 100644 --- a/crates/turbopack-ecmascript/src/references/amd.rs +++ b/crates/turbopack-ecmascript/src/references/amd.rs @@ -15,7 +15,7 @@ use turbo_tasks::{ }; use turbopack_core::{ chunk::ChunkableModuleReference, - issue::LazyIssueSource, + issue::IssueSource, reference::ModuleReference, resolve::{origin::ResolveOrigin, parse::Request, ModuleResolveResult}, }; diff --git a/crates/turbopack-ecmascript/src/references/cjs.rs b/crates/turbopack-ecmascript/src/references/cjs.rs index 5ab63dea2bb24..555e9a769822d 100644 --- a/crates/turbopack-ecmascript/src/references/cjs.rs +++ b/crates/turbopack-ecmascript/src/references/cjs.rs @@ -6,7 +6,7 @@ use swc_core::{ use turbo_tasks::{Value, ValueToString, Vc}; use turbopack_core::{ chunk::ChunkableModuleReference, - issue::LazyIssueSource, + issue::IssueSource, reference::ModuleReference, resolve::{origin::ResolveOrigin, parse::Request, ModuleResolveResult}, }; diff --git a/crates/turbopack-ecmascript/src/references/esm/base.rs b/crates/turbopack-ecmascript/src/references/esm/base.rs index a3246f6c07b33..7d8aa29d1af7e 100644 --- a/crates/turbopack-ecmascript/src/references/esm/base.rs +++ b/crates/turbopack-ecmascript/src/references/esm/base.rs @@ -11,7 +11,7 @@ use turbopack_core::{ ChunkItemExt, ChunkableModule, ChunkableModuleReference, ChunkingContext, ChunkingType, ChunkingTypeOption, ModuleId, }, - issue::{IssueSeverity, LazyIssueSource}, + issue::{IssueSeverity, IssueSource}, module::Module, reference::ModuleReference, reference_type::EcmaScriptModulesReferenceSubType, diff --git a/crates/turbopack-ecmascript/src/references/esm/dynamic.rs b/crates/turbopack-ecmascript/src/references/esm/dynamic.rs index 8728e3d8dce04..c36ca0a79d4ab 100644 --- a/crates/turbopack-ecmascript/src/references/esm/dynamic.rs +++ b/crates/turbopack-ecmascript/src/references/esm/dynamic.rs @@ -6,7 +6,7 @@ use swc_core::{ use turbo_tasks::{Value, ValueToString, Vc}; use turbopack_core::{ chunk::{ChunkableModuleReference, ChunkingType, ChunkingTypeOption}, - issue::LazyIssueSource, + issue::IssueSource, reference::ModuleReference, reference_type::EcmaScriptModulesReferenceSubType, resolve::{origin::ResolveOrigin, parse::Request, ModuleResolveResult}, diff --git a/crates/turbopack-ecmascript/src/references/esm/url.rs b/crates/turbopack-ecmascript/src/references/esm/url.rs index d1705034a7951..94a66a0b7e823 100644 --- a/crates/turbopack-ecmascript/src/references/esm/url.rs +++ b/crates/turbopack-ecmascript/src/references/esm/url.rs @@ -9,7 +9,7 @@ use turbopack_core::{ ChunkItemExt, ChunkableModule, ChunkableModuleReference, ChunkingType, ChunkingTypeOption, }, environment::Rendering, - issue::{code_gen::CodeGenerationIssue, IssueExt, IssueSeverity, LazyIssueSource}, + issue::{code_gen::CodeGenerationIssue, IssueExt, IssueSeverity, IssueSource}, reference::ModuleReference, reference_type::UrlReferenceSubType, resolve::{origin::ResolveOrigin, parse::Request, ModuleResolveResult}, diff --git a/crates/turbopack-ecmascript/src/references/mod.rs b/crates/turbopack-ecmascript/src/references/mod.rs index ef8f27700e3dd..facc18a34b6eb 100644 --- a/crates/turbopack-ecmascript/src/references/mod.rs +++ b/crates/turbopack-ecmascript/src/references/mod.rs @@ -51,7 +51,7 @@ use turbo_tasks_fs::{FileJsonContent, FileSystemPath}; use turbopack_core::{ compile_time_info::{CompileTimeInfo, FreeVarReference}, error::PrettyPrintError, - issue::{analyze::AnalyzeIssue, IssueExt, IssueSeverity, LazyIssueSource}, + issue::{analyze::AnalyzeIssue, IssueExt, IssueSeverity, IssueSource}, module::Module, reference::{ModuleReference, ModuleReferences, SourceMapReference}, reference_type::{CommonJsReferenceSubType, ReferenceType}, @@ -963,11 +963,7 @@ pub(crate) async fn analyze_ecmascript_module( Request::parse(Value::new(pat)), compile_time_info.environment().rendering(), Vc::cell(ast_path), - LazyIssueSource::from_swc_offsets( - source, - span.lo.to_usize(), - span.hi.to_usize(), - ), + IssueSource::from_swc_offsets(source, span.lo.to_usize(), span.hi.to_usize()), in_try, options .url_rewrite_behavior @@ -1764,7 +1760,7 @@ async fn handle_free_var_reference( )) }), Request::parse(Value::new(request.clone().into())), - Some(LazyIssueSource::from_swc_offsets( + Some(IssueSource::from_swc_offsets( state.source, span.lo.to_usize(), span.hi.to_usize(), @@ -1793,7 +1789,7 @@ async fn handle_free_var_reference( } fn issue_source(source: Vc>, span: Span) -> Vc { - LazyIssueSource::from_swc_offsets(source, span.lo.to_usize(), span.hi.to_usize()) + IssueSource::from_swc_offsets(source, span.lo.to_usize(), span.hi.to_usize()) } fn analyze_amd_define( diff --git a/crates/turbopack-ecmascript/src/references/require_context.rs b/crates/turbopack-ecmascript/src/references/require_context.rs index 2d9edf9c0de9f..7c33ddb0aa256 100644 --- a/crates/turbopack-ecmascript/src/references/require_context.rs +++ b/crates/turbopack-ecmascript/src/references/require_context.rs @@ -22,7 +22,7 @@ use turbopack_core::{ ChunkingContext, }, ident::AssetIdent, - issue::{IssueSeverity, LazyIssueSource}, + issue::{IssueSeverity, IssueSource}, module::Module, reference::{ModuleReference, ModuleReferences}, resolve::{origin::ResolveOrigin, parse::Request, ModuleResolveResult}, diff --git a/crates/turbopack-ecmascript/src/resolve/mod.rs b/crates/turbopack-ecmascript/src/resolve/mod.rs index d6e096265bc50..9433d2a28446b 100644 --- a/crates/turbopack-ecmascript/src/resolve/mod.rs +++ b/crates/turbopack-ecmascript/src/resolve/mod.rs @@ -4,7 +4,7 @@ use anyhow::Result; use turbo_tasks::{Value, Vc}; use turbopack_core::{ context::AssetContext, - issue::{IssueSeverity, LazyIssueSource}, + issue::{IssueSeverity, IssueSource}, reference_type::{ CommonJsReferenceSubType, EcmaScriptModulesReferenceSubType, ReferenceType, UrlReferenceSubType, From ea28cdcd18dfbcc2538a46306704fed6fb922873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Nov 2023 07:42:13 +0900 Subject: [PATCH 11/12] Update crates/turbopack-core/src/issue/mod.rs Co-authored-by: Tobias Koppers --- crates/turbopack-core/src/issue/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index 358c20682f0e4..672b3a96d37f6 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -406,8 +406,8 @@ pub struct IssueSource { #[turbo_tasks::value] #[derive(Clone, Debug)] enum SourceRange { - Normal(SourcePos, SourcePos), - Lazy(usize, usize), + LineColumn(SourcePos, SourcePos), + ByteOffset(usize, usize), } #[turbo_tasks::value_impl] From d1321c145b709488172dd99215aa3d12b4b40090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 14 Nov 2023 07:43:36 +0900 Subject: [PATCH 12/12] fix --- crates/turbopack-core/src/issue/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index 672b3a96d37f6..93a181d0eef09 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -436,9 +436,9 @@ impl IssueSource { source, range: match (start == 0, end == 0) { (true, true) => None, - (false, false) => Some(SourceRange::Lazy(start - 1, end - 1).cell()), - (false, true) => Some(SourceRange::Lazy(start - 1, start - 1).cell()), - (true, false) => Some(SourceRange::Lazy(end - 1, end - 1).cell()), + (false, false) => Some(SourceRange::ByteOffset(start - 1, end - 1).cell()), + (false, true) => Some(SourceRange::ByteOffset(start - 1, start - 1).cell()), + (true, false) => Some(SourceRange::ByteOffset(end - 1, end - 1).cell()), }, }) } @@ -465,7 +465,7 @@ impl IssueSource { range: if let FileLinesContent::Lines(lines) = &*source.content().lines().await? { let start = find_line_and_column(lines.as_ref(), start); let end = find_line_and_column(lines.as_ref(), end); - Some(SourceRange::Normal(start, end).cell()) + Some(SourceRange::LineColumn(start, end).cell()) } else { None }, @@ -572,8 +572,8 @@ impl IssueSource { asset: PlainSource::from_source(this.source).await?, range: match this.range { Some(range) => match &*range.await? { - SourceRange::Normal(start, end) => Some((*start, *end)), - SourceRange::Lazy(start, end) => { + SourceRange::LineColumn(start, end) => Some((*start, *end)), + SourceRange::ByteOffset(start, end) => { if let FileLinesContent::Lines(lines) = &*this.source.content().lines().await? {