forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsxSelfClosingElement.ts
53 lines (45 loc) · 2.24 KB
/
JsxSelfClosingElement.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
import { ts } from "../../../typescript";
import * as errors from "../../../errors";
import { JsxElementStructure } from "../../../structures";
import { callBaseGetStructure } from "../callBaseGetStructure";
import { callBaseSet } from "../callBaseSet";
import { PrimaryExpression } from "../expression";
import { JsxAttributedNode, JsxTagNamedNode } from "./base";
export const JsxSelfClosingElementBase = JsxAttributedNode(JsxTagNamedNode(PrimaryExpression));
export class JsxSelfClosingElement extends JsxSelfClosingElementBase<ts.JsxSelfClosingElement> {
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<JsxElementStructure>) {
callBaseSet(JsxSelfClosingElementBase.prototype, this, structure);
if (structure.attributes != null) {
this.getAttributes().forEach(a => a.remove());
this.addAttributes(structure.attributes);
}
if (structure.isSelfClosing === false)
throw new errors.NotImplementedError("Changing a JsxSelfClosingElement to be non-self closing is not implemented. Please open an issue if you need this.");
if (structure.children != null)
throw new errors.NotImplementedError("Changing a JsxSelfClosingElement to be non-self closing is not implemented. Please open an issue if you need this.");
if (structure.bodyText != null)
throw new errors.NotImplementedError("Changing a JsxSelfClosingElement to be non-self closing is not implemented. Please open an issue if you need this.");
if (structure.name != null)
this.getTagNameNode().replaceWithText(structure.name);
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): JsxElementStructure {
const structure = callBaseGetStructure<JsxElementStructure>(JsxSelfClosingElementBase.prototype, this, {
name: this.getTagNameNode().getText(),
attributes: this.getAttributes().map(a => a.getStructure()),
children: undefined,
isSelfClosing: true,
bodyText: undefined
});
delete structure.children;
delete structure.bodyText;
return structure;
}
}