@swc/plugin-styled-jsx 1.5.81 → 1.5.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/package.json +1 -1
  2. package/swc_plugin_styled_jsx.wasm +0 -0
  3. package/transform/Cargo.toml +1 -1
  4. package/transform/src/transform_css_lightningcss.rs +72 -12
  5. package/transform/tests/errors/nested-style-tags/output-lightningcss.js +58 -0
  6. package/transform/tests/errors/nested-style-tags/output-swc.js +58 -0
  7. package/transform/tests/errors/nested-style-tags/output-swc.stderr +27 -0
  8. package/transform/tests/errors/no-child/output-lightningcss.js +8 -0
  9. package/transform/tests/errors/no-child/output-swc.js +8 -0
  10. package/transform/tests/errors/no-child/output-swc.stderr +9 -0
  11. package/transform/tests/errors/ts-with-css-resolve/output-lightningcss.js +5 -0
  12. package/transform/tests/errors/ts-with-css-resolve/output-swc.js +5 -0
  13. package/transform/tests/errors/ts-with-css-resolve/output-swc.stderr +2 -0
  14. package/transform/tests/errors/two-children/output-lightningcss.js +12 -0
  15. package/transform/tests/errors/two-children/output-swc.js +12 -0
  16. package/transform/tests/errors/two-children/output-swc.stderr +11 -0
  17. package/transform/tests/errors/wrong-child-type/output-lightningcss.js +10 -0
  18. package/transform/tests/errors/wrong-child-type/output-swc.js +10 -0
  19. package/transform/tests/errors/wrong-child-type/output-swc.stderr +10 -0
  20. package/transform/tests/errors/wrong-jsx-expression-type/output-lightningcss.js +10 -0
  21. package/transform/tests/errors/wrong-jsx-expression-type/output-swc.js +10 -0
  22. package/transform/tests/errors/wrong-jsx-expression-type/output-swc.stderr +10 -0
  23. package/transform/tests/fixture.rs +44 -19
  24. /package/transform/tests/errors/nested-style-tags/{output.stderr → output-lightningcss.stderr} +0 -0
  25. /package/transform/tests/errors/no-child/{output.stderr → output-lightningcss.stderr} +0 -0
  26. /package/transform/tests/errors/ts-with-css-resolve/{output.stderr → output-lightningcss.stderr} +0 -0
  27. /package/transform/tests/errors/two-children/{output.stderr → output-lightningcss.stderr} +0 -0
  28. /package/transform/tests/errors/wrong-child-type/{output.stderr → output-lightningcss.stderr} +0 -0
  29. /package/transform/tests/errors/wrong-jsx-expression-type/{output.stderr → output-lightningcss.stderr} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swc/plugin-styled-jsx",
3
- "version": "1.5.81",
3
+ "version": "1.5.83",
4
4
  "description": "SWC plugin for styled-jsx",
5
5
  "main": "swc_plugin_styled_jsx.wasm",
6
6
  "scripts": {
Binary file
@@ -4,7 +4,7 @@ description = "AST transforms visitor for styled-jsx"
4
4
  edition = "2021"
5
5
  license = "Apache-2.0"
6
6
  name = "styled_jsx"
7
- version = "0.47.0"
7
+ version = "0.49.0"
8
8
 
9
9
  [features]
10
10
  custom_transform = ["swc_common/concurrent"]
@@ -4,21 +4,25 @@ use std::{
4
4
  fmt::Debug,
5
5
  mem::transmute,
6
6
  panic::{catch_unwind, AssertUnwindSafe},
7
- sync::Arc,
7
+ sync::{Arc, RwLock},
8
8
  };
9
9
 
10
10
  use easy_error::{bail, Error, ResultExt};
11
11
  use lightningcss::{
12
+ error::ParserError,
12
13
  properties::custom::{TokenList, TokenOrValue},
13
14
  selector::{Combinator, Component, PseudoClass, Selector},
14
- stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleSheet},
15
+ stylesheet::{MinifyOptions, ParserFlags, ParserOptions, PrinterOptions, StyleSheet},
15
16
  traits::{ParseWithOptions, ToCss},
16
17
  values::ident::Ident,
17
18
  visit_types,
18
19
  visitor::{Visit, VisitTypes, Visitor},
19
20
  };
20
21
  use parcel_selectors::{parser::SelectorIter, SelectorImpl};
21
- use swc_common::{errors::HANDLER, SourceMap, DUMMY_SP};
22
+ use swc_common::{
23
+ errors::{DiagnosticBuilder, Level, HANDLER},
24
+ BytePos, Loc, SourceMap, Span, DUMMY_SP,
25
+ };
22
26
  use swc_ecma_ast::*;
23
27
  use tracing::{debug, error, trace};
24
28
 
@@ -28,37 +32,82 @@ use crate::{
28
32
  utils::{hash_string, string_literal_expr},
29
33
  };
30
34
 
35
+ fn report(
36
+ cm: &SourceMap,
37
+ css_span: Span,
38
+ file_lines_cache: &mut Option<Loc>,
39
+ err: &lightningcss::error::Error<ParserError>,
40
+ level: Level,
41
+ ) {
42
+ // We need :global(selector) to be parsed as a selector.
43
+ if let ParserError::SelectorError(
44
+ lightningcss::error::SelectorError::UnsupportedPseudoClassOrElement(..),
45
+ ) = &err.kind
46
+ {
47
+ return;
48
+ }
49
+
50
+ let file = file_lines_cache.get_or_insert_with(|| cm.lookup_char_pos(css_span.lo));
51
+
52
+ let lo = if let Some(loc) = &err.loc {
53
+ Some(file.file.lines[(loc.line + 1) as usize] + BytePos(loc.column))
54
+ } else {
55
+ None
56
+ };
57
+
58
+ HANDLER.with(|handler| {
59
+ //
60
+
61
+ let mut db = DiagnosticBuilder::new(handler, level, &err.kind.to_string());
62
+ if let Some(lo) = lo {
63
+ db.set_span(Span::new(lo, lo, Default::default()));
64
+ }
65
+
66
+ db.emit();
67
+ });
68
+ }
69
+
31
70
  #[cfg_attr(
32
71
  debug_assertions,
33
- tracing::instrument(skip(_cm, style_info, class_name))
72
+ tracing::instrument(skip(cm, style_info, class_name))
34
73
  )]
35
74
  pub fn transform_css(
36
- _cm: Arc<SourceMap>,
75
+ cm: Arc<SourceMap>,
37
76
  style_info: &LocalStyle,
38
77
  is_global: bool,
39
78
  class_name: &Option<String>,
40
79
  ) -> Result<Expr, Error> {
80
+ let mut file_lines_cache = None;
81
+
41
82
  debug!("CSS: \n{}", style_info.css);
42
83
  let css_str = strip_comments(&style_info.css);
43
84
 
85
+ let warnings: Arc<RwLock<Vec<lightningcss::error::Error<ParserError>>>> = Arc::default();
86
+
44
87
  let result: Result<StyleSheet, _> = StyleSheet::parse(
45
88
  &css_str,
46
89
  ParserOptions {
47
90
  // We cannot use css_modules for `:global` because lightningcss does not support
48
91
  // parsing-only mode.
49
92
  css_modules: None,
93
+ error_recovery: true,
94
+ warnings: Some(warnings.clone()),
95
+ flags: ParserFlags::all(),
50
96
  ..Default::default()
51
97
  },
52
98
  );
99
+
53
100
  let mut ss = match result {
54
101
  Ok(ss) => ss,
55
- Err(_err) => {
102
+ Err(err) => {
56
103
  HANDLER.with(|handler| {
57
- // Print css parsing errors
58
- // TODO:
59
- // err.to_diagnostics(handler).emit();
60
-
61
- // TODO(kdy1): We may print css so the user can see the error, and report it.
104
+ report(
105
+ &cm,
106
+ style_info.css_span,
107
+ &mut file_lines_cache,
108
+ &err,
109
+ Level::Error,
110
+ );
62
111
 
63
112
  handler
64
113
  .struct_span_err(
@@ -72,6 +121,18 @@ pub fn transform_css(
72
121
  }
73
122
  };
74
123
 
124
+ if let Ok(warnings) = warnings.read() {
125
+ for warning in warnings.iter() {
126
+ report(
127
+ &cm,
128
+ style_info.css_span,
129
+ &mut file_lines_cache,
130
+ warning,
131
+ Level::Warning,
132
+ );
133
+ }
134
+ }
135
+
75
136
  ss.visit(&mut CssNamespace {
76
137
  class_name: match class_name {
77
138
  Some(s) => s.clone(),
@@ -83,7 +144,6 @@ pub fn transform_css(
83
144
  .expect("failed to transform css");
84
145
 
85
146
  // Apply auto prefixer
86
- // TODO:
87
147
  ss.minify(MinifyOptions {
88
148
  ..Default::default()
89
149
  })
@@ -0,0 +1,58 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ const Component = ()=><>
3
+
4
+ <header>
5
+
6
+ <Comp>
7
+
8
+ <_JSXStyle id={styles.__hash}>{styles}</_JSXStyle>
9
+
10
+ <_JSXStyle id={"b3e1b6a7c9b96113"}>{""}</_JSXStyle>
11
+
12
+ </Comp>
13
+
14
+ <p className={"jsx-b3e1b6a7c9b96113"}><_JSXStyle id={"b3e1b6a7c9b96113"}>{""}</_JSXStyle></p>
15
+
16
+ </header>
17
+
18
+ <div className={"jsx-b3e1b6a7c9b96113"}>
19
+
20
+ <h1 className={"jsx-b3e1b6a7c9b96113"}>
21
+
22
+ Welcome!
23
+
24
+ <style jsx>{``}</style>
25
+
26
+ </h1>
27
+
28
+ <_JSXStyle id={"b3e1b6a7c9b96113"}>{""}</_JSXStyle>
29
+
30
+ </div>
31
+
32
+ <div className={"jsx-b3e1b6a7c9b96113"}>
33
+
34
+ <_JSXStyle id={"b3e1b6a7c9b96113"}>{""}</_JSXStyle>
35
+
36
+ <h1 className={"jsx-b3e1b6a7c9b96113"}>
37
+
38
+ Hello world!
39
+
40
+ <style jsx global>{``}</style>
41
+
42
+ </h1>
43
+
44
+ </div>
45
+
46
+ <div className={"jsx-b3e1b6a7c9b96113"}>
47
+
48
+ <>
49
+
50
+ <style jsx>{styles}</style>
51
+
52
+ </>
53
+
54
+ <_JSXStyle id={"b3e1b6a7c9b96113"}>{""}</_JSXStyle>
55
+
56
+ </div>
57
+
58
+ </>;
@@ -0,0 +1,58 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ const Component = ()=><>
3
+
4
+ <header>
5
+
6
+ <Comp>
7
+
8
+ <_JSXStyle id={styles.__hash}>{styles}</_JSXStyle>
9
+
10
+ <_JSXStyle id={"b3e1b6a7c9b96113"}>{""}</_JSXStyle>
11
+
12
+ </Comp>
13
+
14
+ <p className={"jsx-b3e1b6a7c9b96113"}><_JSXStyle id={"b3e1b6a7c9b96113"}>{""}</_JSXStyle></p>
15
+
16
+ </header>
17
+
18
+ <div className={"jsx-b3e1b6a7c9b96113"}>
19
+
20
+ <h1 className={"jsx-b3e1b6a7c9b96113"}>
21
+
22
+ Welcome!
23
+
24
+ <style jsx>{``}</style>
25
+
26
+ </h1>
27
+
28
+ <_JSXStyle id={"b3e1b6a7c9b96113"}>{""}</_JSXStyle>
29
+
30
+ </div>
31
+
32
+ <div className={"jsx-b3e1b6a7c9b96113"}>
33
+
34
+ <_JSXStyle id={"b3e1b6a7c9b96113"}>{""}</_JSXStyle>
35
+
36
+ <h1 className={"jsx-b3e1b6a7c9b96113"}>
37
+
38
+ Hello world!
39
+
40
+ <style jsx global>{``}</style>
41
+
42
+ </h1>
43
+
44
+ </div>
45
+
46
+ <div className={"jsx-b3e1b6a7c9b96113"}>
47
+
48
+ <>
49
+
50
+ <style jsx>{styles}</style>
51
+
52
+ </>
53
+
54
+ <_JSXStyle id={"b3e1b6a7c9b96113"}>{""}</_JSXStyle>
55
+
56
+ </div>
57
+
58
+ </>;
@@ -0,0 +1,27 @@
1
+
2
+ x Detected nested styled-jsx tag.
3
+ | Read more: https://nextjs.org/docs/messages/nested-styled-jsx-tags
4
+ ,-[input.js:12:1]
5
+ 12 | Welcome!
6
+ 13 | <style jsx>{``}</style>
7
+ : ^^^^^^^^^^^^^^^^^^^^^^^
8
+ 14 | </h1>
9
+ `----
10
+
11
+ x Detected nested styled-jsx tag.
12
+ | Read more: https://nextjs.org/docs/messages/nested-styled-jsx-tags
13
+ ,-[input.js:20:1]
14
+ 20 | Hello world!
15
+ 21 | <style jsx global>{``}</style>
16
+ : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17
+ 22 | </h1>
18
+ `----
19
+
20
+ x Detected nested styled-jsx tag.
21
+ | Read more: https://nextjs.org/docs/messages/nested-styled-jsx-tags
22
+ ,-[input.js:25:1]
23
+ 25 | <>
24
+ 26 | <style jsx>{styles}</style>
25
+ : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
26
+ 27 | </>
27
+ `----
@@ -0,0 +1,8 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ export default (()=><div>
3
+
4
+ <style jsx>
5
+
6
+ </style>
7
+
8
+ </div>);
@@ -0,0 +1,8 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ export default (()=><div>
3
+
4
+ <style jsx>
5
+
6
+ </style>
7
+
8
+ </div>);
@@ -0,0 +1,9 @@
1
+
2
+ x Expected one child under JSX style tag, but got 0.
3
+ | Read more: https://nextjs.org/docs/messages/invalid-styled-jsx-children
4
+ ,-[input.js:2:1]
5
+ 2 | <div>
6
+ 3 | ,-> <style jsx>
7
+ 4 | `-> </style>
8
+ 5 | </div>
9
+ `----
@@ -0,0 +1,5 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ export default {
3
+ styles: <_JSXStyle id={"71f03d42ea0ec6"}>{".container.jsx-71f03d42ea0ec6{color:#fff;background:#000;height:100px;font-weight:700}"}</_JSXStyle>,
4
+ className: "jsx-71f03d42ea0ec6"
5
+ };
@@ -0,0 +1,5 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ export default {
3
+ styles: <_JSXStyle id={"71f03d42ea0ec6"}>{".container.jsx-71f03d42ea0ec6{background:#000;color:white;font-weight:700;height:100px}"}</_JSXStyle>,
4
+ className: "jsx-71f03d42ea0ec6"
5
+ };
@@ -0,0 +1,2 @@
1
+
2
+ x /some-project/src/some-file.ts uses `css.resolve`, but ends with `.ts`. The file extension needs to be `.tsx` so that the jsx injected by `css.resolve` will be transformed.
@@ -0,0 +1,12 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ export default (()=><div>
3
+
4
+ <style jsx>
5
+
6
+ {`.p {}`}
7
+
8
+ {`.p {}`}
9
+
10
+ </style>
11
+
12
+ </div>);
@@ -0,0 +1,12 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ export default (()=><div>
3
+
4
+ <style jsx>
5
+
6
+ {`.p {}`}
7
+
8
+ {`.p {}`}
9
+
10
+ </style>
11
+
12
+ </div>);
@@ -0,0 +1,11 @@
1
+
2
+ x Expected one child under JSX style tag, but got 2.
3
+ | Read more: https://nextjs.org/docs/messages/invalid-styled-jsx-children
4
+ ,-[input.js:2:1]
5
+ 2 | <div>
6
+ 3 | ,-> <style jsx>
7
+ 4 | | {`.p {}`}
8
+ 5 | | {`.p {}`}
9
+ 6 | `-> </style>
10
+ 7 | </div>
11
+ `----
@@ -0,0 +1,10 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ export default (()=><div>
3
+
4
+ <style jsx>
5
+
6
+ 10
7
+
8
+ </style>
9
+
10
+ </div>);
@@ -0,0 +1,10 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ export default (()=><div>
3
+
4
+ <style jsx>
5
+
6
+ 10
7
+
8
+ </style>
9
+
10
+ </div>);
@@ -0,0 +1,10 @@
1
+
2
+ x Expected a single child of type JSXExpressionContainer under JSX Style tag.
3
+ | Read more: https://nextjs.org/docs/messages/invalid-styled-jsx-children
4
+ ,-[input.js:2:1]
5
+ 2 | <div>
6
+ 3 | ,-> <style jsx>
7
+ 4 | | 10
8
+ 5 | `-> </style>
9
+ 6 | </div>
10
+ `----
@@ -0,0 +1,10 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ export default (()=><div>
3
+
4
+ <style jsx>
5
+
6
+ {[]}
7
+
8
+ </style>
9
+
10
+ </div>);
@@ -0,0 +1,10 @@
1
+ import _JSXStyle from "styled-jsx/style";
2
+ export default (()=><div>
3
+
4
+ <style jsx>
5
+
6
+ {[]}
7
+
8
+ </style>
9
+
10
+ </div>);
@@ -0,0 +1,10 @@
1
+
2
+ x Expected a template literal, string or identifier inside the JSXExpressionContainer.
3
+ | Read more: https://nextjs.org/docs/messages/invalid-styled-jsx-children
4
+ ,-[input.js:2:1]
5
+ 2 | <div>
6
+ 3 | ,-> <style jsx>
7
+ 4 | | {[]}
8
+ 5 | `-> </style>
9
+ 6 | </div>
10
+ `----
@@ -77,28 +77,53 @@ impl swc_ecma_visit::VisitMut for DropSpan {
77
77
 
78
78
  #[fixture("tests/errors/**/input.js")]
79
79
  fn styled_jsx_errors(input: PathBuf) {
80
- let output = input.parent().unwrap().join("output.js");
81
80
  let file_name = match input.to_str().unwrap().contains("ts-with-css-resolve") {
82
81
  true => FileName::Real(PathBuf::from("/some-project/src/some-file.ts")),
83
82
  false => FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
84
83
  };
84
+ {
85
+ let output = input.parent().unwrap().join("output-swc.js");
85
86
 
86
- test_fixture(
87
- syntax(),
88
- &|t| {
89
- styled_jsx(
90
- t.cm.clone(),
91
- file_name.clone(),
92
- styled_jsx::visitor::Config {
93
- use_lightningcss: false,
94
- },
95
- )
96
- },
97
- &input,
98
- &output,
99
- FixtureTestConfig {
100
- allow_error: true,
101
- ..Default::default()
102
- },
103
- );
87
+ test_fixture(
88
+ syntax(),
89
+ &|t| {
90
+ styled_jsx(
91
+ t.cm.clone(),
92
+ file_name.clone(),
93
+ styled_jsx::visitor::Config {
94
+ use_lightningcss: false,
95
+ },
96
+ )
97
+ },
98
+ &input,
99
+ &output,
100
+ FixtureTestConfig {
101
+ allow_error: true,
102
+ ..Default::default()
103
+ },
104
+ );
105
+ }
106
+
107
+ {
108
+ let output = input.parent().unwrap().join("output-lightningcss.js");
109
+
110
+ test_fixture(
111
+ syntax(),
112
+ &|t| {
113
+ styled_jsx(
114
+ t.cm.clone(),
115
+ file_name.clone(),
116
+ styled_jsx::visitor::Config {
117
+ use_lightningcss: true,
118
+ },
119
+ )
120
+ },
121
+ &input,
122
+ &output,
123
+ FixtureTestConfig {
124
+ allow_error: true,
125
+ ..Default::default()
126
+ },
127
+ );
128
+ }
104
129
  }