Skip to content

Commit f341d50

Browse files
authored
Merge pull request github#5662 from asgerf/js/simpler-json-api
Approved by erik-krogh
2 parents 9b0ef2f + e77117f commit f341d50

File tree

6 files changed

+40
-50
lines changed

6 files changed

+40
-50
lines changed

javascript/ql/src/Declarations/UnusedVariable.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ predicate isReactForJSX(UnusedLocal v) {
6565
v.getName() =
6666
tsconfig
6767
.getPropValue("compilerOptions")
68-
.(JSONObject)
69-
.getPropStringValue(["jsxFactory", "jsxFragmentFactory"])
68+
.getPropValue(["jsxFactory", "jsxFragmentFactory"])
69+
.getStringValue()
7070
)
7171
)
7272
}

javascript/ql/src/semmle/javascript/JSON.qll

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ class JSONValue extends @json_value, Locatable {
4141
)
4242
}
4343

44+
/** If this is an object, gets the value of property `name`. */
45+
JSONValue getPropValue(string name) { json_properties(this, name, result) }
46+
47+
/** If this is an array, gets the value of the `i`th element. */
48+
JSONValue getElementValue(int i) { result = this.(JSONArray).getChild(i) }
49+
50+
/** If this is a string constant, gets the value of the string. */
51+
string getStringValue() { result = this.(JSONString).getValue() }
52+
53+
/** If this is an integer constant, gets its numeric value. */
54+
int getIntValue() { result = this.(JSONNumber).getValue().toInt() }
55+
56+
/** If this is a boolean constant, gets its boolean value. */
57+
boolean getBooleanValue() { result.toString() = this.(JSONBoolean).getValue() }
58+
4459
override string getAPrimaryQlClass() { result = "JSONValue" }
4560
}
4661

@@ -129,13 +144,10 @@ class JSONString extends @json_string, JSONPrimitiveValue {
129144
* ```
130145
*/
131146
class JSONArray extends @json_array, JSONValue {
132-
/** Gets the value of the `i`th element of this array. */
133-
JSONValue getElementValue(int i) { result = getChild(i) }
147+
override string getAPrimaryQlClass() { result = "JSONArray" }
134148

135149
/** Gets the string value of the `i`th element of this array. */
136-
string getElementStringValue(int i) { result = getElementValue(i).(JSONString).getValue() }
137-
138-
override string getAPrimaryQlClass() { result = "JSONArray" }
150+
string getElementStringValue(int i) { result = getElementValue(i).getStringValue() }
139151
}
140152

141153
/**
@@ -148,13 +160,10 @@ class JSONArray extends @json_array, JSONValue {
148160
* ```
149161
*/
150162
class JSONObject extends @json_object, JSONValue {
151-
/** Gets the value of property `name` of this object. */
152-
JSONValue getPropValue(string name) { json_properties(this, name, result) }
163+
override string getAPrimaryQlClass() { result = "JSONObject" }
153164

154165
/** Gets the string value of property `name` of this object. */
155-
string getPropStringValue(string name) { result = getPropValue(name).(JSONString).getValue() }
156-
157-
override string getAPrimaryQlClass() { result = "JSONObject" }
166+
string getPropStringValue(string name) { result = getPropValue(name).getStringValue() }
158167
}
159168

160169
/**

javascript/ql/src/semmle/javascript/NPM.qll

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ class PackageJSON extends JSONObject {
4040
ContributorInfo getAuthor() { result = getPropValue("author") }
4141

4242
/** Gets information for a contributor to this package. */
43-
ContributorInfo getAContributor() {
44-
result = getPropValue("contributors").(JSONArray).getElementValue(_)
45-
}
43+
ContributorInfo getAContributor() { result = getPropValue("contributors").getElementValue(_) }
4644

4745
/** Gets the array of files for this package. */
4846
JSONArray getFiles() { result = getPropValue("files") }
@@ -57,13 +55,13 @@ class PackageJSON extends JSONObject {
5755
string getBin(string cmd) {
5856
cmd = getPackageName() and result = getPropStringValue("bin")
5957
or
60-
result = getPropValue("bin").(JSONObject).getPropStringValue(cmd)
58+
result = getPropValue("bin").getPropValue(cmd).getStringValue()
6159
}
6260

6361
/** Gets a manual page for this package. */
6462
string getAManFile() {
6563
result = getPropStringValue("man") or
66-
result = getPropValue("man").(JSONArray).getElementStringValue(_)
64+
result = getPropValue("man").getElementValue(_).getStringValue()
6765
}
6866

6967
/** Gets information about the directories of this package. */
@@ -191,12 +189,12 @@ class BugTrackerInfo extends JSONValue {
191189

192190
/** Gets the bug tracker URL. */
193191
string getUrl() {
194-
result = this.(JSONObject).getPropStringValue("url") or
195-
result = this.(JSONString).getValue()
192+
result = this.getPropValue("url").getStringValue() or
193+
result = this.getStringValue()
196194
}
197195

198196
/** Gets the bug reporting email address. */
199-
string getEmail() { result = this.(JSONObject).getPropStringValue("email") }
197+
string getEmail() { result = this.getPropValue("email").getStringValue() }
200198
}
201199

202200
/**
@@ -206,7 +204,7 @@ class ContributorInfo extends JSONValue {
206204
ContributorInfo() {
207205
exists(PackageJSON pkg |
208206
this = pkg.getPropValue("author") or
209-
this = pkg.getPropValue("contributors").(JSONArray).getElementValue(_)
207+
this = pkg.getPropValue("contributors").getElementValue(_)
210208
) and
211209
(this instanceof JSONObject or this instanceof JSONString)
212210
}
@@ -217,24 +215,24 @@ class ContributorInfo extends JSONValue {
217215
* homepage URL.
218216
*/
219217
private string parseInfo(int group) {
220-
result = this.(JSONString).getValue().regexpCapture("(.*?)(?: <(.*?)>)?(?: \\((.*)?\\))", group)
218+
result = this.getStringValue().regexpCapture("(.*?)(?: <(.*?)>)?(?: \\((.*)?\\))", group)
221219
}
222220

223221
/** Gets the contributor's name. */
224222
string getName() {
225-
result = this.(JSONObject).getPropStringValue("name") or
223+
result = this.getPropValue("name").getStringValue() or
226224
result = parseInfo(1)
227225
}
228226

229227
/** Gets the contributor's email address. */
230228
string getEmail() {
231-
result = this.(JSONObject).getPropStringValue("email") or
229+
result = this.getPropValue("email").getStringValue() or
232230
result = parseInfo(2)
233231
}
234232

235233
/** Gets the contributor's homepage URL. */
236234
string getUrl() {
237-
result = this.(JSONObject).getPropStringValue("url") or
235+
result = this.getPropValue("url").getStringValue() or
238236
result = parseInfo(3)
239237
}
240238
}

javascript/ql/src/semmle/javascript/Paths.qll

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,7 @@ private module TypeScriptOutDir {
233233
tsconfig.getFile().getBaseName().regexpMatch("tsconfig.*\\.json") and
234234
tsconfig.isTopLevel() and
235235
tsconfig.getFile().getParentContainer() = parent and
236-
result =
237-
tsconfig
238-
.getPropValue("compilerOptions")
239-
.(JSONObject)
240-
.getPropValue("outDir")
241-
.(JSONString)
242-
.getValue()
236+
result = tsconfig.getPropValue("compilerOptions").getPropValue("outDir").getStringValue()
243237
}
244238

245239
/**
@@ -281,26 +275,15 @@ private module TypeScriptOutDir {
281275
pragma[inline]
282276
private string getARootDirFromInclude(JSONObject tsconfig) {
283277
result =
284-
getRootFolderFromPath(tsconfig
285-
.getPropValue("include")
286-
.(JSONArray)
287-
.getElementValue(_)
288-
.(JSONString)
289-
.getValue())
278+
getRootFolderFromPath(tsconfig.getPropValue("include").getElementValue(_).getStringValue())
290279
}
291280

292281
/**
293282
* Gets the value of the "rootDir" option from a tsconfig.json.
294283
*/
295284
pragma[inline]
296285
private string getRootDir(JSONObject tsconfig) {
297-
result =
298-
tsconfig
299-
.getPropValue("compilerOptions")
300-
.(JSONObject)
301-
.getPropValue("rootDir")
302-
.(JSONString)
303-
.getValue()
286+
result = tsconfig.getPropValue("compilerOptions").getPropValue("rootDir").getStringValue()
304287
}
305288
}
306289

javascript/ql/src/semmle/javascript/frameworks/Babel.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ module Babel {
2424
plugins = getPropValue("plugins") and
2525
result = plugins.getElementValue(_)
2626
|
27-
result.(JSONString).getValue() = pluginName
27+
result.getStringValue() = pluginName
2828
or
29-
result.(JSONArray).getElementStringValue(0) = pluginName
29+
result.getElementValue(0).getStringValue() = pluginName
3030
)
3131
}
3232

@@ -67,7 +67,7 @@ module Babel {
6767
JSONValue getOptions() { result = this.(JSONArray).getElementValue(1) }
6868

6969
/** Gets a named option from the option object, if present. */
70-
JSONValue getOption(string name) { result = getOptions().(JSONObject).getPropValue(name) }
70+
JSONValue getOption(string name) { result = getOptions().getPropValue(name) }
7171

7272
/** Holds if this plugin applies to `tl`. */
7373
predicate appliesTo(TopLevel tl) { cfg.appliesTo(tl) }
@@ -186,7 +186,7 @@ module Babel {
186186
TransformReactJsxConfig() { pluginName = "transform-react-jsx" }
187187

188188
/** Gets the name of the variable used to create JSX elements. */
189-
string getJsxFactoryVariableName() { result = getOption("pragma").(JSONString).getValue() }
189+
string getJsxFactoryVariableName() { result = getOption("pragma").getStringValue() }
190190
}
191191

192192
/**

javascript/ql/src/semmle/javascript/security/dataflow/RemoteFlowSources.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private class RemoteFlowSourceAccessPath extends JSONString {
108108
exists(JSONObject specs |
109109
specs.isTopLevel() and
110110
this.getFile().getBaseName() = "codeql-javascript-remote-flow-sources.json" and
111-
this = specs.getPropValue(sourceType).(JSONArray).getElementValue(_) and
111+
this = specs.getPropValue(sourceType).getElementValue(_) and
112112
this.getValue().regexpMatch("window(\\.\\w+)+")
113113
)
114114
}

0 commit comments

Comments
 (0)