Skip to content

Commit

Permalink
feat(css/parser): Store lexed comments (#8091)
Browse files Browse the repository at this point in the history
**Description:**

For now, we leave every comment as a leading comment. As logic for determining leading vs trailing is quite complex, I'll do it with another PR.
  • Loading branch information
kdy1 authored Oct 10, 2023
1 parent 4453d1b commit d1097cc
Show file tree
Hide file tree
Showing 946 changed files with 3,361 additions and 62 deletions.
8 changes: 4 additions & 4 deletions crates/swc_common/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ impl SingleThreadedComments {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(
any(feature = "rkyv-impl"),
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
Expand All @@ -561,15 +561,15 @@ impl Spanned for Comment {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(
any(feature = "rkyv-impl"),
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))]
pub enum CommentKind {
Line,
Block,
Line = 0,
Block = 1,
}

#[deprecated(
Expand Down
78 changes: 54 additions & 24 deletions crates/swc_css_codegen/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf},
};

use swc_common::{FileName, Span};
use swc_common::{comments::SingleThreadedComments, FileName, Span};
use swc_css_ast::*;
use swc_css_codegen::{
writer::basic::{BasicCssWriter, BasicCssWriterConfig, IndentType, LineFeed},
Expand Down Expand Up @@ -41,10 +41,13 @@ fn run(input: &Path, minify: bool) {
run_test2(false, |cm, handler| {
let fm = cm.load_file(input).unwrap();

let comments = SingleThreadedComments::default();

eprintln!("==== ==== Input ==== ====\n{}\n", fm.src);

let mut errors = vec![];
let mut stylesheet: Stylesheet = parse_file(&fm, Default::default(), &mut errors).unwrap();
let mut stylesheet: Stylesheet =
parse_file(&fm, Some(&comments), Default::default(), &mut errors).unwrap();

for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
Expand Down Expand Up @@ -79,7 +82,7 @@ fn run(input: &Path, minify: bool) {

let mut errors = vec![];
let mut stylesheet_output: Stylesheet =
parse_file(&fm_output, Default::default(), &mut errors).map_err(|err| {
parse_file(&fm_output, None, Default::default(), &mut errors).map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;

Expand Down Expand Up @@ -342,8 +345,11 @@ fn indent_type(input: PathBuf) {

eprintln!("==== ==== Input ==== ====\n{}\n", fm.src);

let comments = SingleThreadedComments::default();

let mut errors = vec![];
let mut stylesheet: Stylesheet = parse_file(&fm, Default::default(), &mut errors).unwrap();
let mut stylesheet: Stylesheet =
parse_file(&fm, Some(&comments), Default::default(), &mut errors).unwrap();

for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
Expand Down Expand Up @@ -375,9 +381,11 @@ fn indent_type(input: PathBuf) {

let mut errors = vec![];
let mut stylesheet_output: Stylesheet =
parse_file(&fm_output, Default::default(), &mut errors).map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;
parse_file(&fm_output, Some(&comments), Default::default(), &mut errors).map_err(
|err| {
err.to_diagnostics(&handler).emit();
},
)?;

for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
Expand All @@ -404,10 +412,13 @@ fn indent_width(input: PathBuf) {
run_test2(false, |cm, handler| {
let fm = cm.load_file(&input).unwrap();

let comments = SingleThreadedComments::default();

eprintln!("==== ==== Input ==== ====\n{}\n", fm.src);

let mut errors = vec![];
let mut stylesheet: Stylesheet = parse_file(&fm, Default::default(), &mut errors).unwrap();
let mut stylesheet: Stylesheet =
parse_file(&fm, Some(&comments), Default::default(), &mut errors).unwrap();

for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
Expand Down Expand Up @@ -439,9 +450,11 @@ fn indent_width(input: PathBuf) {

let mut errors = vec![];
let mut stylesheet_output: Stylesheet =
parse_file(&fm_output, Default::default(), &mut errors).map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;
parse_file(&fm_output, Some(&comments), Default::default(), &mut errors).map_err(
|err| {
err.to_diagnostics(&handler).emit();
},
)?;

for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
Expand All @@ -465,13 +478,16 @@ fn linefeed_lf(input: PathBuf) {
input.extension().unwrap().to_string_lossy()
));

let comments = SingleThreadedComments::default();

run_test2(false, |cm, handler| {
let fm = cm.load_file(&input).unwrap();

eprintln!("==== ==== Input ==== ====\n{}\n", fm.src);

let mut errors = vec![];
let mut stylesheet: Stylesheet = parse_file(&fm, Default::default(), &mut errors).unwrap();
let mut stylesheet: Stylesheet =
parse_file(&fm, Some(&comments), Default::default(), &mut errors).unwrap();

for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
Expand Down Expand Up @@ -505,9 +521,11 @@ fn linefeed_lf(input: PathBuf) {

let mut errors = vec![];
let mut stylesheet_output: Stylesheet =
parse_file(&fm_output, Default::default(), &mut errors).map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;
parse_file(&fm_output, Some(&comments), Default::default(), &mut errors).map_err(
|err| {
err.to_diagnostics(&handler).emit();
},
)?;

for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
Expand All @@ -534,10 +552,13 @@ fn linefeed_crlf(input: PathBuf) {
run_test2(false, |cm, handler| {
let fm = cm.load_file(&input).unwrap();

let comments = SingleThreadedComments::default();

eprintln!("==== ==== Input ==== ====\n{}\n", fm.src);

let mut errors = vec![];
let mut stylesheet: Stylesheet = parse_file(&fm, Default::default(), &mut errors).unwrap();
let mut stylesheet: Stylesheet =
parse_file(&fm, Some(&comments), Default::default(), &mut errors).unwrap();

for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
Expand Down Expand Up @@ -571,9 +592,11 @@ fn linefeed_crlf(input: PathBuf) {

let mut errors = vec![];
let mut stylesheet_output: Stylesheet =
parse_file(&fm_output, Default::default(), &mut errors).map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;
parse_file(&fm_output, Some(&comments), Default::default(), &mut errors).map_err(
|err| {
err.to_diagnostics(&handler).emit();
},
)?;

for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
Expand All @@ -596,11 +619,13 @@ fn parse_again(input: PathBuf) {
testing::run_test2(false, |cm, handler| {
let fm = cm.load_file(&input).unwrap();

let comments = SingleThreadedComments::default();

eprintln!("==== ==== Input ==== ====\n{}\n", fm.src);

let mut errors = vec![];
let mut stylesheet: Stylesheet =
parse_file(&fm, Default::default(), &mut errors).map_err(|err| {
parse_file(&fm, Some(&comments), Default::default(), &mut errors).map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;

Expand All @@ -620,10 +645,15 @@ fn parse_again(input: PathBuf) {

let new_fm = cm.new_source_file(FileName::Anon, css_str);
let mut parsed_errors = vec![];
let mut parsed: Stylesheet = parse_file(&new_fm, Default::default(), &mut parsed_errors)
.map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;
let mut parsed: Stylesheet = parse_file(
&new_fm,
Some(&comments),
Default::default(),
&mut parsed_errors,
)
.map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;

for err in parsed_errors {
err.to_diagnostics(&handler).emit();
Expand Down
1 change: 1 addition & 0 deletions crates/swc_css_compat/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn parse_stylesheet(fm: &Lrc<SourceFile>) -> Stylesheet {
let mut errors = vec![];
let ss: Stylesheet = parse_file(
fm,
None,
ParserConfig {
allow_wrong_line_comments: true,
..Default::default()
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_css_lints/tests/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn pass(input: PathBuf) {
let config = Default::default();

let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm), config);
let lexer = Lexer::new(SourceFileInput::from(&*fm), None, config);
let mut parser = Parser::new(lexer, config);

let stylesheet = match parser.parse_all() {
Expand Down Expand Up @@ -56,7 +56,7 @@ fn fail(input: PathBuf) {
let config = Default::default();

let fm = cm.load_file(&input).unwrap();
let lexer = Lexer::new(SourceFileInput::from(&*fm), config);
let lexer = Lexer::new(SourceFileInput::from(&*fm), None, config);
let mut parser = Parser::new(lexer, config);

let stylesheet = match parser.parse_all() {
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_css_minifier/benches/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ fn run(src: &str) {
let fm = cm.new_source_file(FileName::Anon, src.into());

let mut errors = vec![];
let mut ss: Stylesheet = parse_file(&fm, Default::default(), &mut errors).unwrap();
let mut ss: Stylesheet =
parse_file(&fm, None, Default::default(), &mut errors).unwrap();

minify(&mut ss, Default::default());

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_minifier/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn minify_fixtures(input: PathBuf) {
let fm = cm.load_file(&input).unwrap();

let mut errors = vec![];
let res: Result<Stylesheet, _> = parse_file(&fm, Default::default(), &mut errors);
let res: Result<Stylesheet, _> = parse_file(&fm, None, Default::default(), &mut errors);

for err in errors {
err.to_diagnostics(handler).emit();
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_css_modules/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn imports(input: PathBuf) {
let mut errors = vec![];
let ss = swc_css_parser::parse_file(
&fm,
None,
ParserConfig {
css_modules: true,
..Default::default()
Expand Down Expand Up @@ -51,6 +52,7 @@ fn compile(input: PathBuf) {
let mut errors = vec![];
let mut ss = swc_css_parser::parse_file(
&fm,
None,
ParserConfig {
css_modules: true,
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions crates/swc_css_modules/tests/with_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn test_full(input: PathBuf, suffix: Option<&str>) {
let mut errors = vec![];
let mut ss: Stylesheet = parse_file(
&fm,
None,
ParserConfig {
css_modules: true,
legacy_nesting: true,
Expand Down
8 changes: 6 additions & 2 deletions crates/swc_css_parser/benches/compare.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
extern crate swc_node_base;

use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use swc_common::{input::StringInput, FileName, Span, SyntaxContext, DUMMY_SP};
use swc_common::{
comments::SingleThreadedComments, input::StringInput, FileName, Span, SyntaxContext, DUMMY_SP,
};
use swc_css_ast::Stylesheet;
use swc_css_parser::{lexer::Lexer, parser::Parser};
use swc_css_visit::{Fold, FoldWith, VisitMut, VisitMutWith};
Expand All @@ -13,9 +15,11 @@ where
F: FnMut(Stylesheet) -> Stylesheet,
{
let _ = ::testing::run_test(false, |cm, _| {
let comments = SingleThreadedComments::default();

let fm = cm.new_source_file(FileName::Anon, SOURCE.into());

let lexer = Lexer::new(StringInput::from(&*fm), Default::default());
let lexer = Lexer::new(StringInput::from(&*fm), Some(&comments), Default::default());
let mut parser = Parser::new(lexer, Default::default());
let stylesheet: Stylesheet = parser.parse_all().unwrap();

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_parser/benches/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn bench_stylesheet(b: &mut Bencher, src: &'static str) {
let fm = cm.new_source_file(FileName::Anon, src.into());

b.iter(|| {
let lexer = Lexer::new(StringInput::from(&*fm), Default::default());
let lexer = Lexer::new(StringInput::from(&*fm), None, Default::default());

for t in lexer {
black_box(t);
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_parser/benches/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn bench_stylesheet(b: &mut Bencher, src: &'static str) {

b.iter(|| {
let _ = black_box({
let lexer = Lexer::new(StringInput::from(&*fm), Default::default());
let lexer = Lexer::new(StringInput::from(&*fm), None, Default::default());
let mut parser = Parser::new(lexer, Default::default());

parser.parse_all()
Expand Down
Loading

1 comment on commit d1097cc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: d1097cc Previous: 3b449e3 Ratio
es/full/bugs-1 284412 ns/iter (± 5574) 287827 ns/iter (± 5397) 0.99
es/full/minify/libraries/antd 1382465135 ns/iter (± 23776206) 1400276528 ns/iter (± 17686894) 0.99
es/full/minify/libraries/d3 290641214 ns/iter (± 5363316) 292603471 ns/iter (± 4457003) 0.99
es/full/minify/libraries/echarts 1100053416 ns/iter (± 8357677) 1114290397 ns/iter (± 17198750) 0.99
es/full/minify/libraries/jquery 88394994 ns/iter (± 714115) 88022132 ns/iter (± 265329) 1.00
es/full/minify/libraries/lodash 103250254 ns/iter (± 824728) 102976947 ns/iter (± 272124) 1.00
es/full/minify/libraries/moment 52036555 ns/iter (± 280807) 51917099 ns/iter (± 164823) 1.00
es/full/minify/libraries/react 18715132 ns/iter (± 60752) 18724305 ns/iter (± 90419) 1.00
es/full/minify/libraries/terser 229500386 ns/iter (± 1057658) 229270812 ns/iter (± 1448828) 1.00
es/full/minify/libraries/three 403790052 ns/iter (± 2798784) 403200760 ns/iter (± 7352475) 1.00
es/full/minify/libraries/typescript 2777808506 ns/iter (± 14920143) 2740230463 ns/iter (± 21726462) 1.01
es/full/minify/libraries/victory 594974165 ns/iter (± 7447994) 591656572 ns/iter (± 11074894) 1.01
es/full/minify/libraries/vue 125430808 ns/iter (± 454021) 124792493 ns/iter (± 493662) 1.01
es/full/codegen/es3 34796 ns/iter (± 201) 34301 ns/iter (± 142) 1.01
es/full/codegen/es5 34871 ns/iter (± 54) 34435 ns/iter (± 81) 1.01
es/full/codegen/es2015 34833 ns/iter (± 64) 34435 ns/iter (± 71) 1.01
es/full/codegen/es2016 34815 ns/iter (± 134) 34397 ns/iter (± 235) 1.01
es/full/codegen/es2017 34749 ns/iter (± 101) 34480 ns/iter (± 106) 1.01
es/full/codegen/es2018 34787 ns/iter (± 87) 34426 ns/iter (± 62) 1.01
es/full/codegen/es2019 34833 ns/iter (± 81) 34412 ns/iter (± 52) 1.01
es/full/codegen/es2020 34836 ns/iter (± 115) 34409 ns/iter (± 96) 1.01
es/full/all/es3 178529776 ns/iter (± 1427070) 179004027 ns/iter (± 1663832) 1.00
es/full/all/es5 170624765 ns/iter (± 646962) 173419539 ns/iter (± 1443581) 0.98
es/full/all/es2015 130278330 ns/iter (± 764238) 130615097 ns/iter (± 1401183) 1.00
es/full/all/es2016 128495223 ns/iter (± 986615) 129154893 ns/iter (± 993530) 0.99
es/full/all/es2017 128604065 ns/iter (± 793785) 128464462 ns/iter (± 1240300) 1.00
es/full/all/es2018 125830639 ns/iter (± 759453) 126562629 ns/iter (± 748685) 0.99
es/full/all/es2019 125723036 ns/iter (± 591833) 125487934 ns/iter (± 1661165) 1.00
es/full/all/es2020 121641064 ns/iter (± 1006678) 121161631 ns/iter (± 1170963) 1.00
es/full/parser 578858 ns/iter (± 3383) 575473 ns/iter (± 4129) 1.01
es/full/base/fixer 17719 ns/iter (± 82) 17618 ns/iter (± 118) 1.01
es/full/base/resolver_and_hygiene 85127 ns/iter (± 198) 83157 ns/iter (± 268) 1.02

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.