Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ result.exports
#=> {"application" => "_8Z4fiW_application"}

result.warnings.first
#=> "'deep' is not recognized as a valid pseudo-class. ... at app/assets/stylesheets/layout.css:0:9"
#=> "'deep' is not recognized as a valid pseudo-class. ... at app/assets/stylesheets/layout.css:1:9"
```

#### CSS modules
Expand Down Expand Up @@ -338,7 +338,7 @@ result.warnings?
#=> true

result.warnings.first
#=> "'deep' is not recognized as a valid pseudo-class. Did you mean '::deep' (pseudo-element) or is this a typo? at :0:9"
#=> "'deep' is not recognized as a valid pseudo-class. Did you mean '::deep' (pseudo-element) or is this a typo? at :1:9"

result.code
#=> ".a:deep(.b){color:red}"
Expand Down
24 changes: 16 additions & 8 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::ptr;
use std::sync::{Arc, RwLock};

use lightningcss::bundler::{BundleErrorKind, Bundler, FileProvider};
use lightningcss::error::Error;
use lightningcss::stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleAttribute, StyleSheet};
use lightningcss::visitor::Visit;

Expand Down Expand Up @@ -67,6 +68,13 @@ pub struct Failure {
message: String,
}

fn describe<T: std::fmt::Display>(error: &Error<T>) -> String {
match &error.loc {
Some(loc) => format!("{} at {}:{}:{}", error.kind, loc.filename, loc.line + 1, loc.column),
None => error.kind.to_string(),
}
}

impl Failure {
fn parse(message: impl Into<String>) -> Self {
Self {
Expand Down Expand Up @@ -135,11 +143,11 @@ fn parse_source<'i>(code: &'i str, options: &TransformOptions) -> Result<ParseRe
..ParserOptions::default()
};

let stylesheet = StyleSheet::parse(code, parser_options).map_err(|error| Failure::parse(error.to_string()))?;
let stylesheet = StyleSheet::parse(code, parser_options).map_err(|error| Failure::parse(describe(&error)))?;

let warnings = collected
.read()
.map(|warnings| warnings.iter().map(|warning| warning.to_string()).collect())
.map(|warnings| warnings.iter().map(describe).collect())
.unwrap_or_default();

Ok(ParseResult { stylesheet, warnings })
Expand All @@ -162,7 +170,7 @@ fn transform_source(code: &str, options: &TransformOptions) -> Result<TransformR
..ParserOptions::default()
};

let mut stylesheet = StyleSheet::parse(code, parser_options).map_err(|error| Failure::parse(error.to_string()))?;
let mut stylesheet = StyleSheet::parse(code, parser_options).map_err(|error| Failure::parse(describe(&error)))?;

if let Some(fragment) = &options.scope {
let mut scoper = Scoper::parse(fragment).map_err(Failure::option)?;
Expand Down Expand Up @@ -201,7 +209,7 @@ fn transform_source(code: &str, options: &TransformOptions) -> Result<TransformR

let warnings = collected
.read()
.map(|warnings| warnings.iter().map(|warning| warning.to_string()).collect())
.map(|warnings| warnings.iter().map(describe).collect())
.unwrap_or_default();

Ok(TransformResult {
Expand Down Expand Up @@ -232,8 +240,8 @@ fn bundle_source(path: &str, options: &TransformOptions) -> Result<TransformResu
let mut stylesheet = bundler
.bundle(std::path::Path::new(path))
.map_err(|error| match &error.kind {
BundleErrorKind::ParserError(_) => Failure::parse(error.to_string()),
_ => Failure::bundle(error.to_string()),
BundleErrorKind::ParserError(_) => Failure::parse(describe(&error)),
_ => Failure::bundle(describe(&error)),
})?;

if let Some(fragment) = &options.scope {
Expand Down Expand Up @@ -270,7 +278,7 @@ fn bundle_source(path: &str, options: &TransformOptions) -> Result<TransformResu

let warnings = collected
.read()
.map(|warnings| warnings.iter().map(|warning| warning.to_string()).collect())
.map(|warnings| warnings.iter().map(describe).collect())
.unwrap_or_default();

Ok(TransformResult {
Expand All @@ -287,7 +295,7 @@ fn transform_attribute(code: &str, options: &TransformOptions) -> Result<Transfo
..ParserOptions::default()
};

let mut attribute = StyleAttribute::parse(code, parser_options).map_err(|error| Failure::parse(error.to_string()))?;
let mut attribute = StyleAttribute::parse(code, parser_options).map_err(|error| Failure::parse(describe(&error)))?;

let targets = options.to_targets().map_err(Failure::option)?;

Expand Down
3 changes: 2 additions & 1 deletion rust/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ impl<'i> Scoper<'i> {
let list = SelectorList::parse_string_with_options(fragment, ParserOptions::default()).map_err(|error| {
format!(
"Invalid scope selector {fragment:?} at :{}:{}",
error.location.line, error.location.column
error.location.line + 1,
error.location.column
)
})?;

Expand Down
2 changes: 1 addition & 1 deletion rust/tests/transform_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn transform_answers_an_error_for_css_it_could_not_read() {

assert_eq!(
answer,
Err("Expected identifier in class selector, got WhiteSpace(\" \") at :0:2".to_string())
Err("Expected identifier in class selector, got WhiteSpace(\" \") at :1:2".to_string())
);
}

Expand Down
4 changes: 2 additions & 2 deletions test/lightningcss/bundle_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BundleTest < Minitest::Spec
BROKEN = File.expand_path("../fixtures/bundle/broken.css", __dir__)
UNREADABLE = File.expand_path("../fixtures/bundle/nested/broken.css", __dir__)
IMPORTED = File.expand_path("../fixtures/bundle/nested/warned.css", __dir__)
WARNING = "'deep' is not recognized as a valid pseudo-class. Did you mean '::deep' (pseudo-element) or is this a typo? at #{IMPORTED}:0:9".freeze
WARNING = "'deep' is not recognized as a valid pseudo-class. Did you mean '::deep' (pseudo-element) or is this a typo? at #{IMPORTED}:1:9".freeze

test "resolves the imports a stylesheet was written with, in the order it imported them" do
code = LightningCSS.bundle(ENTRY, minify: true).code
Expand Down Expand Up @@ -83,7 +83,7 @@ class BundleTest < Minitest::Spec
test "raises a parse error for CSS it could not read, wherever it imported it from" do
error = assert_raises(LightningCSS::ParseError) { LightningCSS.bundle(BROKEN) }

assert_equal "Invalid empty selector at #{UNREADABLE}:0:1", error.message
assert_equal "Invalid empty selector at #{UNREADABLE}:1:1", error.message
end

test "refuses a filename, naming every file it reads by the path it read it from" do
Expand Down
18 changes: 16 additions & 2 deletions test/lightningcss/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ class ErrorsTest < Minitest::Spec
test "raises on CSS it cannot parse" do
error = assert_raises(LightningCSS::ParseError) { LightningCSS.transform(". { color: red }") }

assert_equal 'Expected identifier in class selector, got WhiteSpace(" ") at :0:2', error.message
assert_equal 'Expected identifier in class selector, got WhiteSpace(" ") at :1:2', error.message
end

test "says where it could not parse" do
error = assert_raises(LightningCSS::ParseError) { LightningCSS.transform("!!! { color: red }") }

assert_equal "Invalid empty selector at :0:1", error.message
assert_equal "Invalid empty selector at :1:1", error.message
end

test "counts lines from 1 and columns from 1" do
css = ".a { color: red; }\n.b, { color: blue; }\n"
error = assert_raises(LightningCSS::ParseError) { LightningCSS.transform(css, filename: "main.css") }

assert_equal "Invalid empty selector at main.css:2:5", error.message
end

test "counts the lines of a warning from 1" do
css = ".a { color: red; }\n.b, { color: blue; }\n"
warnings = LightningCSS.transform(css, filename: "main.css", error_recovery: true).warnings

assert_equal ["Invalid empty selector at main.css:2:5"], warnings
end

test "closes an unclosed block the way a browser does" do
Expand Down
2 changes: 1 addition & 1 deletion test/lightningcss/parse_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ParseTest < Minitest::Spec
test "raises on a stylesheet it cannot read" do
error = assert_raises(LightningCSS::ParseError) { LightningCSS.parse(". { color: red }") }

assert_equal 'Expected identifier in class selector, got WhiteSpace(" ") at :0:2', error.message
assert_equal 'Expected identifier in class selector, got WhiteSpace(" ") at :1:2', error.message
end

test "recovers when asked to" do
Expand Down
2 changes: 1 addition & 1 deletion test/lightningcss/scope_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def scoped(source, fragment = "[s]")
test "refuses a fragment that is not a selector" do
error = assert_raises(LightningCSS::OptionError) { scoped(".a { color: red }", "((") }

assert_equal 'Invalid scope selector "((" at :0:1', error.message
assert_equal 'Invalid scope selector "((" at :1:1', error.message
end

test "refuses a fragment carrying more than one selector" do
Expand Down
2 changes: 1 addition & 1 deletion test/lightningcss/style_attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class StyleAttributeTest < Minitest::Spec
LightningCSS.transform_style_attribute(".a { color: red }")
end

assert_equal "Unexpected token Delim('.') at :0:1", error.message
assert_equal "Unexpected token Delim('.') at :1:1", error.message
end

test "has no names to compile, so it reports no exports" do
Expand Down
2 changes: 1 addition & 1 deletion test/lightningcss/warnings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module LightningCSS
class WarningsTest < Minitest::Spec
DEEP = ".a:deep(.b) { color: red }"
WARNING = "'deep' is not recognized as a valid pseudo-class. Did you mean '::deep' (pseudo-element) or is this a typo? at :0:9"
WARNING = "'deep' is not recognized as a valid pseudo-class. Did you mean '::deep' (pseudo-element) or is this a typo? at :1:9"

test "reports what it kept but did not understand" do
result = LightningCSS.transform(DEEP, minify: true)
Expand Down
Loading