-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathwrapJsxInFragment.ts
85 lines (82 loc) · 3.28 KB
/
wrapJsxInFragment.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import {
codeFixAll,
createCodeFixAction,
registerCodeFix,
} from "../_namespaces/ts.codefix.js";
import {
BinaryExpression,
Diagnostics,
factory,
getTokenAtPosition,
isBinaryExpression,
isJsxChild,
JsxChild,
Node,
nodeIsMissing,
SourceFile,
SyntaxKind,
textChanges,
} from "../_namespaces/ts.js";
const fixID = "wrapJsxInFragment";
const errorCodes = [Diagnostics.JSX_expressions_must_have_one_parent_element.code];
registerCodeFix({
errorCodes,
getCodeActions: function getCodeActionsToWrapJsxInFragment(context) {
const { sourceFile, span } = context;
const node = findNodeToFix(sourceFile, span.start);
if (!node) return undefined;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node));
return [createCodeFixAction(fixID, changes, Diagnostics.Wrap_in_JSX_fragment, fixID, Diagnostics.Wrap_all_unparented_JSX_in_JSX_fragment)];
},
fixIds: [fixID],
getAllCodeActions: context =>
codeFixAll(context, errorCodes, (changes, diag) => {
const node = findNodeToFix(context.sourceFile, diag.start);
if (!node) return undefined;
doChange(changes, context.sourceFile, node);
}),
});
function findNodeToFix(sourceFile: SourceFile, pos: number): BinaryExpression | undefined {
// The error always at 1st token that is "<" in "<a /><a />"
const lessThanToken = getTokenAtPosition(sourceFile, pos);
const firstJsxElementOrOpenElement = lessThanToken.parent;
let binaryExpr = firstJsxElementOrOpenElement.parent;
if (!isBinaryExpression(binaryExpr)) {
// In case the start element is a JsxSelfClosingElement, it the end.
// For JsxOpenElement, find one more parent
binaryExpr = binaryExpr.parent;
if (!isBinaryExpression(binaryExpr)) return undefined;
}
if (!nodeIsMissing(binaryExpr.operatorToken)) return undefined;
return binaryExpr;
}
function doChange(changeTracker: textChanges.ChangeTracker, sf: SourceFile, node: Node) {
const jsx = flattenInvalidBinaryExpr(node);
if (jsx) changeTracker.replaceNode(sf, node, factory.createJsxFragment(factory.createJsxOpeningFragment(), jsx, factory.createJsxJsxClosingFragment()));
}
// The invalid syntax is constructed as
// InvalidJsxTree :: One of
// JsxElement CommaToken InvalidJsxTree
// JsxElement CommaToken JsxElement
function flattenInvalidBinaryExpr(node: Node): JsxChild[] | undefined {
const children: JsxChild[] = [];
let current = node;
while (true) {
if (isBinaryExpression(current) && nodeIsMissing(current.operatorToken) && current.operatorToken.kind === SyntaxKind.CommaToken) {
children.push(current.left as JsxChild);
if (isJsxChild(current.right)) {
children.push(current.right);
// Indicates the tree has go to the bottom
return children;
}
else if (isBinaryExpression(current.right)) {
current = current.right;
continue;
}
// Unreachable case
else return undefined;
}
// Unreachable case
else return undefined;
}
}