Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce JSX support #5668

Merged
merged 62 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
37a3b00
Implement jsx option
lukastaegert Mar 1, 2024
5fb5288
Add tests
lukastaegert Mar 2, 2024
eb86704
Pass jsx options to SWC
Martin-Idel Mar 2, 2024
72e9b57
Add JsxElement to ast-types
felixhuttmann Mar 2, 2024
9aa3d5c
Add JsxOpeningElement
AlexDroll Mar 2, 2024
407fc32
Add JSXElementChild -> JSXElement
tiptr Mar 2, 2024
907fd9a
Add JSXElementChild -> JSXText
tiptr Mar 2, 2024
eb4648a
WIP Add JsxAttribute
AlexDroll Mar 2, 2024
2baff98
Add JSXElementChild -> JSXExprContainer and JSXEmptyExpr
tiptr Mar 2, 2024
66ae8fd
Add JsxFragment
felixhuttmann Mar 2, 2024
0579ccf
Align AST types with official types and verify
lukastaegert Apr 28, 2024
d2f9b94
Sort converters into alphabetical order
lukastaegert Apr 28, 2024
da70cc5
Associate identifier with variable
lukastaegert Apr 29, 2024
5655718
Use correct JSX types
lukastaegert May 1, 2024
fcb49b2
Ensure React is included when JSX is used
lukastaegert May 1, 2024
63430c9
Rework JSX option and global variable handling for preserving
lukastaegert May 8, 2024
e7b75cd
Start work on transpilation support
lukastaegert May 9, 2024
0a978c3
Improve option and transpilation support
lukastaegert May 9, 2024
c49be1b
Handle missing jsx factory
lukastaegert May 10, 2024
bca5eab
Add additional tests and support expressions
lukastaegert May 11, 2024
79da44e
Handle JSXText
lukastaegert May 11, 2024
4e25b16
Support attributes transpilation
lukastaegert May 12, 2024
58a786b
Support fragments
lukastaegert May 12, 2024
2bbb572
Extract shared code from fragments
lukastaegert May 12, 2024
9680b65
Support JSXText in all possible positions
lukastaegert May 12, 2024
4fad480
Support all possible JSX attribute types
lukastaegert May 13, 2024
634ddd4
Support JSXMemberExpression
lukastaegert May 13, 2024
c240f3f
Support JSXSpreadChild
lukastaegert May 13, 2024
9eabdc6
Support JSXSpreadAttribute
lukastaegert May 14, 2024
b96aa33
Use correct span for empty expressions
lukastaegert May 21, 2024
22e9bf3
Improve formatting for macro names
lukastaegert Aug 2, 2024
387bb35
Move converters into separate files
lukastaegert Aug 2, 2024
d0d5082
Make everything except the parse function only crate public
lukastaegert Aug 2, 2024
8824ef5
use macros for JSX where possible
lukastaegert Aug 2, 2024
dbb3032
Initial jsx rendering logic for simple cases
lukastaegert Aug 2, 2024
f90dbf3
Split classic and automatic mode
lukastaegert Aug 2, 2024
15467f0
Handle jsx without children
lukastaegert Aug 2, 2024
6c8dd15
Handle jsx with children
lukastaegert Aug 2, 2024
3f208d5
Fix fragment rendering
lukastaegert Aug 3, 2024
be3c7fd
Prepare for fallback rendering
lukastaegert Aug 3, 2024
2979bf1
Reenable tests for now
lukastaegert Aug 3, 2024
eab8818
Update linting
lukastaegert Aug 19, 2024
20074d5
Improve JSX rendering and move to parent element
lukastaegert Aug 22, 2024
43aafdb
Align classic attribute rendering with automatic
lukastaegert Aug 23, 2024
88acff7
Refine classic mode rendering
lukastaegert Aug 31, 2024
e5bbe8d
Refine automatic mode rendering
lukastaegert Sep 1, 2024
6f33fe1
Fix automatic rendering
lukastaegert Sep 1, 2024
b9bfb43
Extract attribute rendering
lukastaegert Sep 2, 2024
dac38b0
Extract shared functionality
lukastaegert Sep 2, 2024
ed2ee43
Move initialize and include functionality to shared base element
lukastaegert Sep 6, 2024
59af083
Extract fragment opening rendering to JSXOpeningFragment
lukastaegert Sep 6, 2024
b335d59
Share rendering functionality with fragments
lukastaegert Sep 6, 2024
5ff0728
Deconflict closing elements
lukastaegert Sep 6, 2024
a10024b
Handle native elements
lukastaegert Sep 9, 2024
9e56a5a
Add additional checks
lukastaegert Sep 10, 2024
17eab3d
Ensure CLI supports presets
lukastaegert Sep 10, 2024
f139fef
Add JSX options to REPL
lukastaegert Sep 20, 2024
9be895b
Add documentation
lukastaegert Sep 20, 2024
6229600
Improve coverage
lukastaegert Oct 1, 2024
ef9bbc4
Fix local browser build
lukastaegert Oct 2, 2024
4b2e94b
Add jsx example
lukastaegert Oct 2, 2024
98f5c5a
Refine docs
lukastaegert Oct 2, 2024
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
Prev Previous commit
Next Next commit
Support JSXText in all possible positions
  • Loading branch information
lukastaegert committed Oct 2, 2024
commit 9680b65b842b823be31e2c597fdc05174f994419
7 changes: 3 additions & 4 deletions rust/parse_ast/src/convert_ast/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,9 @@ impl<'a> AstConverter<'a> {
self.convert_item_list(
&jsx_fragment.children,
end_position + JSX_FRAGMENT_CHILDREN_OFFSET,
|_ast_converter, _jsx_fragment_child| {
unimplemented!("Convert JSXFragmentChild");
// ast_converter.convert_jsx_fragment_child(jsx_fragment_child);
// true
|ast_converter, jsx_element_child| {
ast_converter.convert_jsx_element_child(jsx_element_child);
true
},
);
// closingFragment
Expand Down
27 changes: 26 additions & 1 deletion src/ast/nodes/JSXFragment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type MagicString from 'magic-string';
import type { NormalizedJsxOptions } from '../../rollup/types';
import type { RenderOptions } from '../../utils/renderHelpers';
import type JSXClosingFragment from './JSXClosingFragment';
import type JSXElement from './JSXElement';
import type JSXExpressionContainer from './JSXExpressionContainer';
import JSXEmptyExpression from './JSXEmptyExpression';
import JSXExpressionContainer from './JSXExpressionContainer';
import type JSXFragment from './JSXFragment';
import type JSXOpeningFragment from './JSXOpeningFragment';
import type JSXText from './JSXText';
Expand All @@ -17,4 +21,25 @@ export default class JsxElement extends NodeBase {
| JSXFragment
)[];
closingFragment!: JSXClosingFragment;

render(code: MagicString, options: RenderOptions): void {
const { preserve } = this.scope.context.options.jsx as NormalizedJsxOptions;
if (preserve) {
super.render(code, options);
} else {
this.openingFragment.render(code, options);
for (const child of this.children) {
if (
child instanceof JSXExpressionContainer &&
child.expression instanceof JSXEmptyExpression
) {
code.remove(child.start, child.end);
} else {
child.render(code, options);
code.appendRight(child.start, `, `);
}
}
this.closingFragment?.render(code);
}
}
}
5 changes: 3 additions & 2 deletions test/form/samples/jsx/preserves-jsx-text/_expected.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Foo = () => {};
const result = <Foo>some&amp;\text</Foo>;
const element = <Foo>some&amp;\text</Foo>;
const fragment = <>other&amp;\text</>;

export { result };
export { element, fragment };
3 changes: 2 additions & 1 deletion test/form/samples/jsx/preserves-jsx-text/main.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const Foo = () => {};
export const result = <Foo>some&amp;\text</Foo>;
export const element = <Foo>some&amp;\text</Foo>;
export const fragment = <>other&amp;\text</>;
5 changes: 3 additions & 2 deletions test/form/samples/jsx/transpiles-jsx-text/_expected.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import react from 'react';

const Foo = () => {};
const result = /*#__PURE__*/react.createElement(Foo, null, "some&\\text");
const element = /*#__PURE__*/react.createElement(Foo, null, "some&\\text");
const fragment = /*#__PURE__*/react.createElement(react.Fragment, null, "other&\\text");

export { result };
export { element, fragment };
3 changes: 2 additions & 1 deletion test/form/samples/jsx/transpiles-jsx-text/main.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const Foo = () => {};
export const result = <Foo>some&amp;\text</Foo>;
export const element = <Foo>some&amp;\text</Foo>;
export const fragment = <>other&amp;\text</>;