mirror of
https://github.com/swc-project/swc.git
synced 2024-12-18 11:11:30 +03:00
b206404d94
swc_css_ast: - Add `DeclBlockItem`. - Change `DeclBlock.properties` to `DeclBlock.items`. swc_css_parser: - Add a way to recovered errors.
68 lines
1.9 KiB
Rust
68 lines
1.9 KiB
Rust
use std::{mem::take, path::PathBuf};
|
|
use swc_common::{FileName, Span};
|
|
use swc_css_ast::Stylesheet;
|
|
use swc_css_codegen::{
|
|
writer::basic::{BasicCssWriter, BasicCssWriterConfig},
|
|
CodeGenerator, CodegenConfig, Emit,
|
|
};
|
|
use swc_css_parser::{parse_file, parser::ParserConfig};
|
|
use swc_css_visit::{VisitMut, VisitMutWith};
|
|
use testing::assert_eq;
|
|
|
|
#[testing::fixture("../parser/tests/fixture/**/input.css")]
|
|
fn parse_again(input: PathBuf) {
|
|
eprintln!("{}", input.display());
|
|
|
|
testing::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, ParserConfig { parse_values: true }, &mut errors).unwrap();
|
|
|
|
for err in take(&mut errors) {
|
|
err.to_diagnostics(&handler).emit();
|
|
}
|
|
|
|
let mut css_str = String::new();
|
|
{
|
|
let wr = BasicCssWriter::new(&mut css_str, BasicCssWriterConfig { indent: "\t" });
|
|
let mut gen = CodeGenerator::new(wr, CodegenConfig { minify: false });
|
|
|
|
gen.emit(&stylesheet).unwrap();
|
|
}
|
|
|
|
eprintln!("==== ==== Codegen ==== ====\n{}\n", css_str);
|
|
|
|
let new_fm = cm.new_source_file(FileName::Anon, css_str);
|
|
let mut parsed: Stylesheet =
|
|
parse_file(&new_fm, ParserConfig { parse_values: true }, &mut errors).map_err(
|
|
|err| {
|
|
err.to_diagnostics(&handler).emit();
|
|
},
|
|
)?;
|
|
|
|
for err in errors {
|
|
err.to_diagnostics(&handler).emit();
|
|
}
|
|
|
|
stylesheet.visit_mut_with(&mut DropSpan);
|
|
parsed.visit_mut_with(&mut DropSpan);
|
|
|
|
assert_eq!(stylesheet, parsed);
|
|
|
|
Ok(())
|
|
})
|
|
.unwrap();
|
|
}
|
|
|
|
struct DropSpan;
|
|
|
|
impl VisitMut for DropSpan {
|
|
fn visit_mut_span(&mut self, n: &mut Span) {
|
|
*n = Default::default()
|
|
}
|
|
}
|