Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.
Closed
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
9 changes: 9 additions & 0 deletions src/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ export const javascriptLanguage = LRLanguage.define({
}),
foldNodeProp.add({
"Block ClassBody SwitchBody EnumBody ObjectExpression ArrayExpression ObjectType": foldInside,
JSXElement(node) {
const first = node.firstChild;
const start = first?.firstChild ? first.firstChild.nextSibling : null;
const end = first?.name === 'JSXSelfClosingTag' ? first.lastChild : node.lastChild;

if (!start || !end) return null;

return start && start.to < end.from ? { from: start.to, to: end.type.isError ? node.to : end.from } : null;
},
BlockComment(tree) { return {from: tree.from + 2, to: tree.to - 2} }
})
]
Expand Down
47 changes: 47 additions & 0 deletions test/test-folding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import ist from "ist"
import {EditorState} from "@codemirror/state"
import {javascript} from "@codemirror/lang-javascript"
import {foldable} from "@codemirror/language"

function jsxState(doc: string) {
return EditorState.create({doc, extensions: [javascript({jsx: true})]})
}

function fold(doc: string) {
let state = jsxState(doc)
const firstLine = doc.slice(0, doc.indexOf('\n'))
const folded = foldable(state, 0, firstLine.length)
if (folded) doc = doc.slice(0, folded.from) + ' ... ' + doc.slice(folded.to)
return doc
}

describe("JSX folding", () => {
it("should fold self-closing tags", () => {
ist(fold(`<div \n />`), '<div ... />')
})

it("should fold self-closing tags with attributes", () => {
ist(fold(`<div a="1" \n />`), '<div ... />')
})

it("should fold regular tags", () => {
ist(fold(`<div>\n foo</div>`), '<div ... </div>')
})

it("should fold regular tags with attributes", () => {
ist(fold(`<div a="1" >\nfoo</div>`), '<div ... </div>')
})

it("should fold regular tags with children", () => {
ist(fold(`<div> \n<div>\nbar</div> </div>`), '<div ... </div>')
})

it("should fold fragment tags", () => {
ist(fold(`<> \nsdf </>`), '<> ... </>')
})

it("should not fold inline tags", () => {
ist(fold(`<div><div></div></div>`), '<div><div></div></div>')
})
})