-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
jsx-tag-pair.ts
52 lines (46 loc) · 1.28 KB
/
jsx-tag-pair.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import traverse from '@babel/traverse'
import { Selection } from 'vscode'
import type { Handler } from '../types'
/**
* Matches JSX elements' start and end tags.
*
* ```jsx
* ▽
* (<Flex.Item>Hi</Flex.Item>)
* └───────┘ └───────┘
* ```
*
* @name jsx-tag-pair
* @category js
*/
export const jsxTagPairHandler: Handler = {
name: 'jsx-tag-pair',
handle({ selection, doc, getAst }) {
for (const ast of getAst('js')) {
const index = doc.offsetAt(selection.start)
let result: Selection[] | undefined
traverse(ast.root, {
JSXElement(path) {
if (result)
return path.skip()
if (path.node.start == null || path.node.end == null)
return
const elements = [
path.node.openingElement!,
path.node.closingElement!,
].filter(Boolean)
if (!elements.length)
return
if (elements.some(e => e.name.start != null && e.name.start + ast.start === index)) {
result = elements.map(e => new Selection(
doc.positionAt(ast.start + e.name.start!),
doc.positionAt(ast.start + e.name.end!),
))
}
},
})
if (result)
return result
}
},
}