forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsxAttribute.ts
111 lines (96 loc) · 3.65 KB
/
JsxAttribute.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as errors from "../../../errors";
import { removeChildren, insertIntoParentTextRange } from "../../../manipulation";
import { JsxAttributeStructure, JsxAttributeSpecificStructure, StructureKind } from "../../../structures";
import { ts, SyntaxKind } from "../../../typescript";
import { WriterFunction } from "../../../types";
import { StringUtils, getTextFromStringOrWriter } from "../../../utils";
import { NamedNode } from "../base";
import { Node } from "../common";
import { callBaseGetStructure } from "../callBaseGetStructure";
import { callBaseSet } from "../callBaseSet";
import { StringLiteral } from "../literal";
import { JsxExpression } from "./JsxExpression";
export const JsxAttributeBase = NamedNode(Node);
export class JsxAttribute extends JsxAttributeBase<ts.JsxAttribute> {
/**
* Gets the JSX attribute's initializer or throws if it doesn't exist.
*/
getInitializerOrThrow() {
return errors.throwIfNullOrUndefined(this.getInitializer(), `Expected to find an initializer for the JSX attribute '${this.getName()}'`);
}
/**
* Gets the JSX attribute's initializer or returns undefined if it doesn't exist.
*/
getInitializer(): StringLiteral | JsxExpression | undefined {
return this._getNodeFromCompilerNodeIfExists(this.compilerNode.initializer);
}
/**
* Sets the initializer.
* @param textOrWriterFunction - Text or writer function to set the initializer with.
* @remarks You need to provide the quotes or braces.
*/
setInitializer(textOrWriterFunction: string | WriterFunction) {
const text = getTextFromStringOrWriter(this._getWriterWithQueuedIndentation(), textOrWriterFunction);
if (StringUtils.isNullOrWhitespace(text)) {
this.removeInitializer();
return this;
}
const initializer = this.getInitializer();
if (initializer != null) {
initializer.replaceWithText(text);
return this;
}
insertIntoParentTextRange({
insertPos: this.getNameNode().getEnd(),
parent: this,
newText: `=${text}`
});
return this;
}
/**
* Removes the initializer.
*/
removeInitializer() {
const initializer = this.getInitializer();
if (initializer == null)
return this;
removeChildren({
children: [initializer.getPreviousSiblingIfKindOrThrow(SyntaxKind.EqualsToken), initializer],
removePrecedingSpaces: true,
removePrecedingNewLines: true
});
return this;
}
/**
* Removes the JSX attribute.
*/
remove() {
removeChildren({
children: [this],
removePrecedingNewLines: true,
removePrecedingSpaces: true
});
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<JsxAttributeStructure>) {
callBaseSet(JsxAttributeBase.prototype, this, structure);
if (structure.initializer != null)
this.setInitializer(structure.initializer);
else if (structure.hasOwnProperty(nameof(structure.initializer)))
this.removeInitializer();
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): JsxAttributeStructure {
const initializer = this.getInitializer();
return callBaseGetStructure<JsxAttributeSpecificStructure>(JsxAttributeBase.prototype, this, {
kind: StructureKind.JsxAttribute,
initializer: initializer == null ? undefined : initializer.getText()
});
}
}