Skip to content

Commit 6bb7e0a

Browse files
nojafshulhi
andauthoredApr 4, 2025··
Jsx ast (#7286)
* Intial exploration of JSX ast Map child expressions Initial mapping of Pexp_jsx_fragment to 0 Correct location in mapping Update analysis for jsx_fragment Remove unused code Print something for ml print Commit invalid test results for reference Try improve printing Correct fragment range, try and print comments Indent jsx Process comments from children inside fragment Attach comments to fragment tags Fix comment Improve comment formatting Print single element on same line Update comment WIP: Debug More debugging Works Fix some jsx printing Fix the test Clean up Update tests with location changes * Initial mapping from0 * Format code * Fix jsx fragment mapping * Remove fragment * Update test output * Introducing Pexp_jsx_unary_element & Pexp_jsx_container_element * Refactor fragment transformation. * Initial transform of Pexp_jsx_unary_element in automatic mode * Make props for unary element in automatic mode. * Initial custom component unary tag * lowercase container element with children. * Uppercase container elements * Streamline automatic element calls * lowercase container element in classic mode * Deal with uppercase tags in classic mode. * Remove old code * Improve recovery of incomplete jsx tags. Port frontend completion. * Correct range of incomplete jsx elements * Update semantic tokens * Make the closing tag optional for jsx_container_element. * Add tighter pattern match for edge case in jsx props completion. * Update analysis tests for jsx elements ast. * print_jsx_unary_tag * Rough print_jsx_container_tag * Add ml printing * Wing the sexp thing * First step towards ast mapping * prop punning conversion * Map prop value * Map prop spreading * Initial container element mapping. * Try support children spreading * Only print space when there are props * Add space after children. * Restore braces in props and children * Inline is_jsx_expression and remove old code * Better indentation of children * Handle unary tag * Refactor * WIP: Fix unary tag comments handling * Fix comments inside prop expression * Formats * Fix closing tag indentation * Fix closing tag indentation for cases with break vs inline * Handle some edge cases * Refactor * WIP: Handle punning * Fix optional printing with braces * WIP: Handle comments attachment in a different way * Handle empty props * Handle props spread * Fix optional with punning * Indent props if they don't fit on one line. * Fix indentation of children when props are multiline. * Refactor to Pexp_jsx_element * Don't always append make when uppercase component * Use Doc.line for child spreading * Exotic prop name * Don't create record if only spreading prop, use that expression instead. * Key prop can be optional * Keep comment after opening greater than * Print prop value with comments * Update generic jsx completion tests * Revert isJsxComponent * Assign comment to the opening element tag name. * Attach comments to closing > and closing tag * Extract helpers to parsetree_viewer * setup to walk jsx props * Correct range for prop spreading * Complete walk_jsx_prop * Finish comment assignment for container elements * Revisit comment attachment for unary tags * Improve behaviour for comments in unary tag * Keep location as is for prop spreading * print leading comments of closing unary token * Attach comments between empty container tag * comments between opening and closing tag * Correct opening_greater_than_doc * Deal with container element comment edge cases. * Ensure comments inside braced property values are correct. * More accurate locations * Update syntax roundtrip test files * Update mapping test results * comment after prop name * For them older camls * More older caml stuff * Remove unreached code in typecore * Remove leftover comments * Preserve lines between jsx elements * Preserve newline between jsx children, streamline jsx fragment children printing * Update analysis expected * Another location change in expected * One more update? * Remove leftover comments * Remove obsolete comment * Ensure fragment starts after arrow * Add changelog entry * Add missing Pexp_await --------- Co-authored-by: Shulhi Sapli <[email protected]>
1 parent 712dec7 commit 6bb7e0a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2470
-1735
lines changed
 

Diff for: ‎CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
1313
# 12.0.0-alpha.12 (Unreleased)
1414

15+
#### :house: Internal
16+
- Better representation of JSX in AST . https://github.com/rescript-lang/rescript/pull/7286
17+
1518
# 12.0.0-alpha.11
1619

1720
#### :bug: Bug fix

Diff for: ‎analysis/src/CompletionFrontEnd.ml

+37-9
Original file line numberDiff line numberDiff line change
@@ -1233,8 +1233,6 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
12331233
then ValueOrField
12341234
else Value);
12351235
}))
1236-
| Pexp_construct ({txt = Lident ("::" | "()")}, _) ->
1237-
(* Ignore list expressions, used in JSX, unit, and more *) ()
12381236
| Pexp_construct (lid, eOpt) -> (
12391237
let lidPath = flattenLidCheckDot lid in
12401238
if debug then
@@ -1325,10 +1323,29 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
13251323
inJsx = !inJsxContext;
13261324
}))
13271325
| None -> ())
1328-
| Pexp_apply {funct = {pexp_desc = Pexp_ident compName}; args}
1329-
when Res_parsetree_viewer.is_jsx_expression expr ->
1326+
| Pexp_jsx_element
1327+
( Jsx_unary_element
1328+
{
1329+
jsx_unary_element_tag_name = compName;
1330+
jsx_unary_element_props = props;
1331+
}
1332+
| Jsx_container_element
1333+
{
1334+
jsx_container_element_tag_name_start = compName;
1335+
jsx_container_element_props = props;
1336+
} ) ->
13301337
inJsxContext := true;
1331-
let jsxProps = CompletionJsx.extractJsxProps ~compName ~args in
1338+
let children =
1339+
match expr.pexp_desc with
1340+
| Pexp_jsx_element
1341+
(Jsx_container_element
1342+
{jsx_container_element_children = children}) ->
1343+
children
1344+
| _ -> JSXChildrenItems []
1345+
in
1346+
let jsxProps =
1347+
CompletionJsx.extractJsxProps ~compName ~props ~children
1348+
in
13321349
let compNamePath = flattenLidCheckDot ~jsx:true compName in
13331350
if debug then
13341351
Printf.printf "JSX <%s:%s %s> _children:%s\n"
@@ -1345,10 +1362,21 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
13451362
| None -> "None"
13461363
| Some childrenPosStart -> Pos.toString childrenPosStart);
13471364
let jsxCompletable =
1348-
CompletionJsx.findJsxPropsCompletable ~jsxProps
1349-
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
1350-
~posAfterCompName:(Loc.end_ compName.loc)
1351-
~firstCharBeforeCursorNoWhite ~charAtCursor
1365+
match expr.pexp_desc with
1366+
| Pexp_jsx_element
1367+
(Jsx_container_element
1368+
{
1369+
jsx_container_element_closing_tag = None;
1370+
jsx_container_element_children =
1371+
JSXChildrenSpreading _ | JSXChildrenItems (_ :: _);
1372+
}) ->
1373+
(* This is a weird edge case where there is no closing tag but there are children *)
1374+
None
1375+
| _ ->
1376+
CompletionJsx.findJsxPropsCompletable ~jsxProps
1377+
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
1378+
~posAfterCompName:(Loc.end_ compName.loc)
1379+
~firstCharBeforeCursorNoWhite ~charAtCursor
13521380
in
13531381
if jsxCompletable <> None then setResultOpt jsxCompletable
13541382
else if compName.loc |> Loc.hasPos ~pos:posBeforeCursor then

1 commit comments

Comments
 (1)

github-actions[bot] commented on Apr 4, 2025

@github-actions[bot]

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Syntax Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 6bb7e0a Previous: 3f06468 Ratio
Print HeroGraphic.res - time/run 26.232747326666665 ms 7.840572506666666 ms 3.35
Print HeroGraphic.res - allocs/run 10571467 words 1396448 words 7.57

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

Please sign in to comment.