Skip to content

Commit fbb9493

Browse files
authored
refactor: source() return SourceValue (#185)
1 parent 90559b4 commit fbb9493

File tree

9 files changed

+293
-384
lines changed

9 files changed

+293
-384
lines changed

src/cached_source.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
stream_chunks_of_source_map, StreamChunks,
1313
},
1414
rope::Rope,
15+
source::SourceValue,
1516
BoxSource, MapOptions, Source, SourceExt, SourceMap,
1617
};
1718

@@ -30,11 +31,11 @@ struct CachedData {
3031
/// ```
3132
/// use rspack_sources::{
3233
/// BoxSource, CachedSource, ConcatSource, MapOptions, OriginalSource,
33-
/// RawSource, Source, SourceExt, SourceMap,
34+
/// RawStringSource, Source, SourceExt, SourceMap,
3435
/// };
3536
///
3637
/// let mut concat = ConcatSource::new([
37-
/// RawSource::from("Hello World\n".to_string()).boxed(),
38+
/// RawStringSource::from("Hello World\n".to_string()).boxed(),
3839
/// OriginalSource::new(
3940
/// "console.log('test');\nconsole.log('test2');\n",
4041
/// "console.js",
@@ -46,12 +47,12 @@ struct CachedData {
4647
/// let cached = CachedSource::new(concat);
4748
///
4849
/// assert_eq!(
49-
/// cached.source(),
50+
/// cached.source().into_string_lossy(),
5051
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
5152
/// );
5253
/// // second time will be fast.
5354
/// assert_eq!(
54-
/// cached.source(),
55+
/// cached.source().into_string_lossy(),
5556
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
5657
/// );
5758
/// ```
@@ -79,7 +80,7 @@ impl CachedSource {
7980
}
8081

8182
impl Source for CachedSource {
82-
fn source(&self) -> Cow<str> {
83+
fn source(&self) -> SourceValue {
8384
self.inner.source()
8485
}
8586

@@ -209,16 +210,16 @@ impl std::fmt::Debug for CachedSource {
209210
#[cfg(test)]
210211
mod tests {
211212
use crate::{
212-
ConcatSource, OriginalSource, RawBufferSource, RawSource, ReplaceSource,
213-
SourceExt, SourceMapSource, WithoutOriginalOptions,
213+
ConcatSource, OriginalSource, RawBufferSource, RawStringSource,
214+
ReplaceSource, SourceExt, SourceMapSource, WithoutOriginalOptions,
214215
};
215216

216217
use super::*;
217218

218219
#[test]
219220
fn line_number_should_not_add_one() {
220221
let source = ConcatSource::new([
221-
CachedSource::new(RawSource::from("\n")).boxed(),
222+
CachedSource::new(RawStringSource::from("\n")).boxed(),
222223
SourceMapSource::new(WithoutOriginalOptions {
223224
value: "\nconsole.log(1);\n".to_string(),
224225
name: "index.js".to_string(),
@@ -305,7 +306,7 @@ mod tests {
305306
final_source: true,
306307
};
307308

308-
let source = RawSource::from("Test\nTest\nTest\n");
309+
let source = RawStringSource::from("Test\nTest\nTest\n");
309310
let mut on_chunk_count = 0;
310311
let mut on_source_count = 0;
311312
let mut on_name_count = 0;
@@ -355,7 +356,7 @@ mod tests {
355356
#[test]
356357
fn should_have_correct_buffer_if_cache_buffer_from_cache_source() {
357358
let buf = vec![128u8];
358-
let source = CachedSource::new(RawSource::from(buf.clone()));
359+
let source = CachedSource::new(RawBufferSource::from(buf.clone()));
359360

360361
source.source();
361362
assert_eq!(source.buffer(), buf.as_slice());

src/concat_source.rs

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
helpers::{get_map, GeneratedInfo, OnChunk, OnName, OnSource, StreamChunks},
1111
linear_map::LinearMap,
1212
source::{Mapping, OriginalLocation},
13-
BoxSource, MapOptions, Rope, Source, SourceExt, SourceMap,
13+
BoxSource, MapOptions, Rope, Source, SourceExt, SourceMap, SourceValue,
1414
};
1515

1616
/// Concatenate multiple [Source]s to a single [Source].
@@ -19,12 +19,12 @@ use crate::{
1919
///
2020
/// ```
2121
/// use rspack_sources::{
22-
/// BoxSource, ConcatSource, MapOptions, OriginalSource, RawSource, Source,
22+
/// BoxSource, ConcatSource, MapOptions, OriginalSource, RawStringSource, Source,
2323
/// SourceExt, SourceMap,
2424
/// };
2525
///
2626
/// let mut source = ConcatSource::new([
27-
/// RawSource::from("Hello World\n".to_string()).boxed(),
27+
/// RawStringSource::from("Hello World\n".to_string()).boxed(),
2828
/// OriginalSource::new(
2929
/// "console.log('test');\nconsole.log('test2');\n",
3030
/// "console.js",
@@ -35,7 +35,7 @@ use crate::{
3535
///
3636
/// assert_eq!(source.size(), 62);
3737
/// assert_eq!(
38-
/// source.source(),
38+
/// source.source().into_string_lossy(),
3939
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
4040
/// );
4141
/// assert_eq!(
@@ -117,13 +117,16 @@ impl ConcatSource {
117117
}
118118

119119
impl Source for ConcatSource {
120-
fn source(&self) -> Cow<str> {
120+
fn source(&self) -> SourceValue {
121121
let children = self.children();
122122
if children.len() == 1 {
123123
children[0].source()
124124
} else {
125-
let all = self.children().iter().map(|child| child.source()).collect();
126-
Cow::Owned(all)
125+
let mut content = String::new();
126+
for child in self.children() {
127+
content.push_str(child.source().into_string_lossy().as_ref());
128+
}
129+
SourceValue::String(Cow::Owned(content))
127130
}
128131
}
129132

@@ -355,14 +358,14 @@ impl StreamChunks for ConcatSource {
355358

356359
#[cfg(test)]
357360
mod tests {
358-
use crate::{OriginalSource, RawBufferSource, RawSource, RawStringSource};
361+
use crate::{OriginalSource, RawBufferSource, RawStringSource};
359362

360363
use super::*;
361364

362365
#[test]
363366
fn should_concat_two_sources() {
364367
let mut source = ConcatSource::new([
365-
RawSource::from("Hello World\n".to_string()).boxed(),
368+
RawStringSource::from("Hello World\n".to_string()).boxed(),
366369
OriginalSource::new(
367370
"console.log('test');\nconsole.log('test2');\n",
368371
"console.js",
@@ -374,7 +377,7 @@ mod tests {
374377
let expected_source =
375378
"Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n";
376379
assert_eq!(source.size(), 62);
377-
assert_eq!(source.source(), expected_source);
380+
assert_eq!(source.source().into_string_lossy(), expected_source);
378381
assert_eq!(
379382
source.map(&MapOptions::new(false)).unwrap(),
380383
SourceMap::from_json(
@@ -424,7 +427,7 @@ mod tests {
424427
let expected_source =
425428
"Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n";
426429
assert_eq!(source.size(), 62);
427-
assert_eq!(source.source(), expected_source);
430+
assert_eq!(source.source().into_string_lossy(), expected_source);
428431
assert_eq!(
429432
source.map(&MapOptions::new(false)).unwrap(),
430433
SourceMap::from_json(
@@ -474,7 +477,7 @@ mod tests {
474477
let expected_source =
475478
"Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n";
476479
assert_eq!(source.size(), 62);
477-
assert_eq!(source.source(), expected_source);
480+
assert_eq!(source.source().into_string_lossy(), expected_source);
478481
assert_eq!(
479482
source.map(&MapOptions::new(false)).unwrap(),
480483
SourceMap::from_json(
@@ -512,18 +515,21 @@ mod tests {
512515
#[test]
513516
fn should_be_able_to_handle_strings_for_all_methods() {
514517
let mut source = ConcatSource::new([
515-
RawSource::from("Hello World\n".to_string()).boxed(),
518+
RawStringSource::from("Hello World\n".to_string()).boxed(),
516519
OriginalSource::new(
517520
"console.log('test');\nconsole.log('test2');\n",
518521
"console.js",
519522
)
520523
.boxed(),
521524
]);
522-
let inner_source =
523-
ConcatSource::new([RawSource::from("("), "'string'".into(), ")".into()]);
524-
source.add(RawSource::from("console"));
525-
source.add(RawSource::from("."));
526-
source.add(RawSource::from("log"));
525+
let inner_source = ConcatSource::new([
526+
RawStringSource::from("("),
527+
"'string'".into(),
528+
")".into(),
529+
]);
530+
source.add(RawStringSource::from("console"));
531+
source.add(RawStringSource::from("."));
532+
source.add(RawStringSource::from("log"));
527533
source.add(inner_source);
528534
let expected_source =
529535
"Hello World\nconsole.log('test');\nconsole.log('test2');\nconsole.log('string')";
@@ -538,7 +544,7 @@ mod tests {
538544
)
539545
.unwrap();
540546
assert_eq!(source.size(), 76);
541-
assert_eq!(source.source(), expected_source);
547+
assert_eq!(source.source().into_string_lossy(), expected_source);
542548
assert_eq!(source.buffer(), expected_source.as_bytes());
543549

544550
let map = source.map(&MapOptions::new(false)).unwrap();
@@ -550,16 +556,19 @@ mod tests {
550556
#[test]
551557
fn should_return_null_as_map_when_only_generated_code_is_concatenated() {
552558
let source = ConcatSource::new([
553-
RawSource::from("Hello World\n"),
554-
RawSource::from("Hello World\n".to_string()),
555-
RawSource::from(""),
559+
RawStringSource::from("Hello World\n"),
560+
RawStringSource::from("Hello World\n".to_string()),
561+
RawStringSource::from(""),
556562
]);
557563

558564
let result_text = source.source();
559565
let result_map = source.map(&MapOptions::default());
560566
let result_list_map = source.map(&MapOptions::new(false));
561567

562-
assert_eq!(result_text, "Hello World\nHello World\n");
568+
assert_eq!(
569+
result_text.into_string_lossy(),
570+
"Hello World\nHello World\n"
571+
);
563572
assert!(result_map.is_none());
564573
assert!(result_list_map.is_none());
565574
}
@@ -568,13 +577,13 @@ mod tests {
568577
fn should_allow_to_concatenate_in_a_single_line() {
569578
let source = ConcatSource::new([
570579
OriginalSource::new("Hello", "hello.txt").boxed(),
571-
RawSource::from(" ").boxed(),
580+
RawStringSource::from(" ").boxed(),
572581
OriginalSource::new("World ", "world.txt").boxed(),
573-
RawSource::from("is here\n").boxed(),
582+
RawStringSource::from("is here\n").boxed(),
574583
OriginalSource::new("Hello\n", "hello.txt").boxed(),
575-
RawSource::from(" \n").boxed(),
584+
RawStringSource::from(" \n").boxed(),
576585
OriginalSource::new("World\n", "world.txt").boxed(),
577-
RawSource::from("is here").boxed(),
586+
RawStringSource::from("is here").boxed(),
578587
]);
579588

580589
assert_eq!(
@@ -591,36 +600,44 @@ mod tests {
591600
.unwrap(),
592601
);
593602
assert_eq!(
594-
source.source(),
603+
source.source().into_string_lossy(),
595604
"Hello World is here\nHello\n \nWorld\nis here",
596605
);
597606
}
598607

599608
#[test]
600609
fn should_allow_to_concat_buffer_sources() {
601610
let source = ConcatSource::new([
602-
RawSource::from("a"),
603-
RawSource::from(Vec::from("b")),
604-
RawSource::from("c"),
611+
RawStringSource::from("a"),
612+
RawStringSource::from("b"),
613+
RawStringSource::from("c"),
605614
]);
606-
assert_eq!(source.source(), "abc");
615+
assert_eq!(source.source().into_string_lossy(), "abc");
607616
assert!(source.map(&MapOptions::default()).is_none());
608617
}
609618

610619
#[test]
611620
fn should_flatten_nested_concat_sources() {
612-
let inner_concat =
613-
ConcatSource::new([RawSource::from("Hello "), RawSource::from("World")]);
621+
let inner_concat = ConcatSource::new([
622+
RawStringSource::from("Hello "),
623+
RawStringSource::from("World"),
624+
]);
614625

615626
let outer_concat = ConcatSource::new([
616627
inner_concat.boxed(),
617-
RawSource::from("!").boxed(),
618-
ConcatSource::new([RawSource::from(" How"), RawSource::from(" are")])
619-
.boxed(),
620-
RawSource::from(" you?").boxed(),
628+
RawStringSource::from("!").boxed(),
629+
ConcatSource::new([
630+
RawStringSource::from(" How"),
631+
RawStringSource::from(" are"),
632+
])
633+
.boxed(),
634+
RawStringSource::from(" you?").boxed(),
621635
]);
622636

623-
assert_eq!(outer_concat.source(), "Hello World! How are you?");
637+
assert_eq!(
638+
outer_concat.source().into_string_lossy(),
639+
"Hello World! How are you?"
640+
);
624641
// The key test: verify that nested ConcatSources are flattened
625642
// Should have 6 direct children instead of nested structure
626643
assert_eq!(outer_concat.children.len(), 6);

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ pub use cached_source::CachedSource;
1919
pub use concat_source::ConcatSource;
2020
pub use error::{Error, Result};
2121
pub use original_source::OriginalSource;
22-
pub use raw_source::{RawBufferSource, RawSource, RawStringSource};
22+
pub use raw_source::{RawBufferSource, RawStringSource};
2323
pub use replace_source::{ReplaceSource, ReplacementEnforce};
2424
pub use rope::Rope;
2525
pub use source::{
2626
BoxSource, MapOptions, Mapping, OriginalLocation, Source, SourceExt,
27-
SourceMap,
27+
SourceMap, SourceValue,
2828
};
2929
pub use source_map_source::{
3030
SourceMapSource, SourceMapSourceOptions, WithoutOriginalOptions,

src/original_source.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
SourceText, StreamChunks,
1111
},
1212
source::{Mapping, OriginalLocation},
13-
MapOptions, Rope, Source, SourceMap,
13+
MapOptions, Rope, Source, SourceMap, SourceValue,
1414
};
1515

1616
/// Represents source code, it will create source map for the source code,
@@ -24,7 +24,7 @@ use crate::{
2424
///
2525
/// let input = "if (hello()) { world(); hi(); there(); } done();\nif (hello()) { world(); hi(); there(); } done();";
2626
/// let source = OriginalSource::new(input, "file.js");
27-
/// assert_eq!(source.source(), input);
27+
/// assert_eq!(source.source().into_string_lossy(), input);
2828
/// assert_eq!(
2929
/// source.map(&MapOptions::default()).unwrap().mappings(),
3030
/// "AAAA,eAAe,SAAS,MAAM,WAAW;AACzC,eAAe,SAAS,MAAM,WAAW",
@@ -51,8 +51,8 @@ impl OriginalSource {
5151
}
5252

5353
impl Source for OriginalSource {
54-
fn source(&self) -> Cow<str> {
55-
Cow::Borrowed(&self.value)
54+
fn source(&self) -> SourceValue {
55+
SourceValue::String(Cow::Borrowed(&self.value))
5656
}
5757

5858
fn rope(&self) -> Rope<'_> {
@@ -247,7 +247,7 @@ mod tests {
247247
let result_map = source.map(&MapOptions::default()).unwrap();
248248
let result_list_map = source.map(&MapOptions::new(false)).unwrap();
249249

250-
assert_eq!(result_text, "Line1\n\nLine3\n");
250+
assert_eq!(result_text.into_string_lossy(), "Line1\n\nLine3\n");
251251
assert_eq!(result_map.sources(), &["file.js".to_string()]);
252252
assert_eq!(result_list_map.sources(), ["file.js".to_string()]);
253253
assert_eq!(
@@ -269,7 +269,7 @@ mod tests {
269269
let result_map = source.map(&MapOptions::default());
270270
let result_list_map = source.map(&MapOptions::new(false));
271271

272-
assert_eq!(result_text, "");
272+
assert_eq!(result_text.into_string_lossy(), "");
273273
assert!(result_map.is_none());
274274
assert!(result_list_map.is_none());
275275
}
@@ -300,7 +300,7 @@ mod tests {
300300
fn should_split_code_into_statements() {
301301
let input = "if (hello()) { world(); hi(); there(); } done();\nif (hello()) { world(); hi(); there(); } done();";
302302
let source = OriginalSource::new(input, "file.js");
303-
assert_eq!(source.source(), input);
303+
assert_eq!(source.source().into_string_lossy(), input);
304304
assert_eq!(
305305
source.map(&MapOptions::default()).unwrap().mappings(),
306306
"AAAA,eAAe,SAAS,MAAM,WAAW;AACzC,eAAe,SAAS,MAAM,WAAW",

0 commit comments

Comments
 (0)