forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSX.qll
More file actions
289 lines (241 loc) · 7.19 KB
/
Copy pathJSX.qll
File metadata and controls
289 lines (241 loc) · 7.19 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* Provides classes for working with JSX code.
*/
import javascript
/**
* A JSX element or fragment.
*
* Examples:
*
* ```
* <a href={linkTarget()}>{linkText()}</a>
* <Welcome name={user.name}/>
* <><h1>Title</h1>Some <b>text</b></>
* ```
*/
class JsxNode extends Expr, @jsx_element {
/** Gets the `i`th element in the body of this element or fragment. */
Expr getBodyElement(int i) { i >= 0 and result = this.getChildExpr(-i - 2) }
/** Gets an element in the body of this element or fragment. */
Expr getABodyElement() { result = this.getBodyElement(_) }
/**
* Gets the parent JSX element or fragment of this element.
*/
JsxNode getJsxParent() { this = result.getABodyElement() }
override string getAPrimaryQlClass() { result = "JsxNode" }
}
/** DEPRECATED: Alias for JsxNode */
deprecated class JSXNode = JsxNode;
/**
* A JSX element.
*
* Examples:
*
* ```
* <a href={linkTarget()}>{linkText()}</a>
* <Welcome name={user.name}/>
* ```
*/
class JsxElement extends JsxNode {
JsxName name;
JsxElement() { name = this.getChildExpr(-1) }
/** Gets the expression denoting the name of this element. */
JsxName getNameExpr() { result = name }
/** Gets the name of this element. */
string getName() { result = name.getValue() }
/** Gets the `i`th attribute of this element. */
JsxAttribute getAttribute(int i) { properties(result, this, i, _, _) }
/** Gets an attribute of this element. */
JsxAttribute getAnAttribute() { result = this.getAttribute(_) }
/** Gets the attribute of this element with the given name, if any. */
JsxAttribute getAttributeByName(string n) {
result = this.getAnAttribute() and result.getName() = n
}
override ControlFlowNode getFirstControlFlowNode() {
result = this.getNameExpr().getFirstControlFlowNode()
}
override string getAPrimaryQlClass() { result = "JsxElement" }
/**
* Holds if this JSX element is an HTML element.
* That is, the name starts with a lowercase letter.
*/
predicate isHtmlElement() { this.getName().regexpMatch("[a-z].*") }
/** DEPRECATED: Alias for isHtmlElement */
deprecated predicate isHTMLElement() { this.isHtmlElement() }
}
/** DEPRECATED: Alias for JsxElement */
deprecated class JSXElement = JsxElement;
/**
* A JSX fragment.
*
* Example:
*
* ```
* <><h1>Title</h1>Some <b>text</b></>
* ```
*/
class JsxFragment extends JsxNode {
JsxFragment() { not exists(this.getChildExpr(-1)) }
override ControlFlowNode getFirstControlFlowNode() {
result = this.getBodyElement(0).getFirstControlFlowNode()
or
not exists(this.getABodyElement()) and result = this
}
override string getAPrimaryQlClass() { result = "JsxFragment" }
}
/** DEPRECATED: Alias for JsxFragment */
deprecated class JSXFragment = JsxFragment;
/**
* An attribute of a JSX element, including spread attributes.
*
* Examples:
*
* ```
* <a href={linkTarget()}>link</a> // `href={linkTarget()}` is an attribute
* <Welcome name={user.name}/> // `name={user.name}` is an attribute
* <div {...attrs}></div> // `{...attrs}` is a (spread) attribute
* ```
*/
class JsxAttribute extends AstNode, @jsx_attribute {
/**
* Gets the expression denoting the name of this attribute.
*
* This is not defined for spread attributes.
*/
JsxName getNameExpr() { result = this.getChildExpr(0) }
/**
* Gets the name of this attribute.
*
* This is not defined for spread attributes.
*/
string getName() { result = this.getNameExpr().getValue() }
/** Gets the expression denoting the value of this attribute. */
Expr getValue() { result = this.getChildExpr(1) }
/** Gets the value of this attribute as a constant string, if possible. */
string getStringValue() { result = this.getValue().getStringValue() }
/** Gets the JSX element to which this attribute belongs. */
JsxElement getElement() { this = result.getAnAttribute() }
override ControlFlowNode getFirstControlFlowNode() {
result = this.getNameExpr().getFirstControlFlowNode()
or
not exists(this.getNameExpr()) and result = this.getValue().getFirstControlFlowNode()
}
override string toString() { properties(this, _, _, _, result) }
override string getAPrimaryQlClass() { result = "JsxAttribute" }
}
/** DEPRECATED: Alias for JsxAttribute */
deprecated class JSXAttribute = JsxAttribute;
/**
* A spread attribute of a JSX element.
*
* Example:
*
* ```
* <div {...attrs}></div> // `{...attrs}` is a spread attribute
* ```
*/
class JsxSpreadAttribute extends JsxAttribute {
JsxSpreadAttribute() { not exists(this.getNameExpr()) }
override SpreadElement getValue() {
// override for more precise result type
result = super.getValue()
}
}
/** DEPRECATED: Alias for JsxSpreadAttribute */
deprecated class JSXSpreadAttribute = JsxSpreadAttribute;
/**
* A namespace-qualified name such as `n:a`.
*
* Example:
*
* ```
* html:href
* ```
*/
class JsxQualifiedName extends Expr, @jsx_qualified_name {
/** Gets the namespace component of this qualified name. */
Identifier getNamespace() { result = this.getChildExpr(0) }
/** Gets the name component of this qualified name. */
Identifier getName() { result = this.getChildExpr(1) }
override ControlFlowNode getFirstControlFlowNode() {
result = this.getNamespace().getFirstControlFlowNode()
}
override string getAPrimaryQlClass() { result = "JsxQualifiedName" }
}
/** DEPRECATED: Alias for JsxQualifiedName */
deprecated class JSXQualifiedName = JsxQualifiedName;
/**
* A name of an JSX element or attribute (which is
* always an identifier, a dot expression, or a qualified
* namespace name).
*
* Examples:
*
* ```
* href
* html:href
* data.path
* ```
*/
class JsxName extends Expr {
JsxName() {
this instanceof Identifier or
this instanceof ThisExpr or
this.(DotExpr).getBase() instanceof JsxName or
this instanceof JsxQualifiedName
}
/**
* Gets the string value of this name.
*/
string getValue() {
result = this.(Identifier).getName()
or
exists(DotExpr dot | dot = this |
result = dot.getBase().(JsxName).getValue() + "." + dot.getPropertyName()
)
or
exists(JsxQualifiedName qual | qual = this |
result = qual.getNamespace().getName() + ":" + qual.getName().getName()
)
or
this instanceof ThisExpr and
result = "this"
}
}
/** DEPRECATED: Alias for JsxName */
deprecated class JSXName = JsxName;
/**
* An interpolating expression that interpolates nothing.
*
* Example:
*
* <pre>
* { /* TBD */ }
* </pre>
*/
class JsxEmptyExpr extends Expr, @jsx_empty_expr {
override string getAPrimaryQlClass() { result = "JsxEmptyExpr" }
}
/** DEPRECATED: Alias for JsxEmptyExpr */
deprecated class JSXEmptyExpr = JsxEmptyExpr;
/**
* A legacy `@jsx` pragma.
*
* Example:
*
* ```
* @jsx React.DOM
* ```
*/
class JsxPragma extends JSDocTag {
JsxPragma() { this.getTitle() = "jsx" }
/**
* Gets the DOM name specified by the pragma; for `@jsx React.DOM`,
* the result is `React.DOM`.
*/
string getDomName() { result = this.getDescription().trim() }
/** DEPRECATED: Alias for getDomName */
deprecated string getDOMName() { result = this.getDomName() }
}
/** DEPRECATED: Alias for JsxPragma */
deprecated class JSXPragma = JsxPragma;