Skip to content

Commit 9bc1460

Browse files
committed
parse integer, null, this and memberExpressions within callExpressions correctly from arguments
1 parent dac0322 commit 9bc1460

File tree

2 files changed

+56
-38
lines changed

2 files changed

+56
-38
lines changed

lib/app_events_docs_generator/app_events/app_events_details.json

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"lineNumber": 40,
3535
"args": [
3636
{
37-
"argValue": "undefined",
38-
"argType": "undefined",
37+
"argValue": null,
38+
"argType": "null",
3939
"argPosition": 1
4040
},
4141
{
@@ -930,13 +930,13 @@
930930
"lineNumber": 163,
931931
"args": [
932932
{
933-
"argValue": "undefined",
934-
"argType": "undefined",
933+
"argValue": null,
934+
"argType": "null",
935935
"argPosition": 1
936936
},
937937
{
938-
"argValue": "undefined",
939-
"argType": "undefined",
938+
"argValue": 5000,
939+
"argType": "integer",
940940
"argPosition": 2
941941
}
942942
],
@@ -1310,7 +1310,7 @@
13101310
"argPosition": 1
13111311
},
13121312
{
1313-
"argValue": "undefined.attachedTo",
1313+
"argValue": "this.bookmarkModel.attachedTo",
13141314
"argType": "called_function",
13151315
"argPosition": 2
13161316
}
@@ -1376,7 +1376,7 @@
13761376
"lineNumber": 380,
13771377
"args": [
13781378
{
1379-
"argValue": "undefined.trim",
1379+
"argValue": "this...uploadPlaceholder.trim",
13801380
"argType": "called_function",
13811381
"argPosition": 1
13821382
},
@@ -1475,7 +1475,7 @@
14751475
"lineNumber": 525,
14761476
"args": [
14771477
{
1478-
"argValue": "undefined.replace",
1478+
"argValue": "placeholderData.uploadPlaceholder.replace",
14791479
"argType": "called_function",
14801480
"argPosition": 1
14811481
},
@@ -1680,8 +1680,8 @@
16801680
"lineNumber": 946,
16811681
"args": [
16821682
{
1683-
"argValue": "undefined",
1684-
"argType": "undefined",
1683+
"argValue": "this",
1684+
"argType": "this",
16851685
"argPosition": 1
16861686
}
16871687
],
@@ -1693,8 +1693,8 @@
16931693
"lineNumber": 972,
16941694
"args": [
16951695
{
1696-
"argValue": "undefined",
1697-
"argType": "undefined",
1696+
"argValue": "this",
1697+
"argType": "this",
16981698
"argPosition": 1
16991699
}
17001700
],
@@ -1760,8 +1760,8 @@
17601760
"argPosition": 1
17611761
},
17621762
{
1763-
"argValue": "undefined",
1764-
"argType": "undefined",
1763+
"argValue": "this",
1764+
"argType": "this",
17651765
"argPosition": 2
17661766
}
17671767
],
@@ -1905,8 +1905,8 @@
19051905
"lineNumber": 547,
19061906
"args": [
19071907
{
1908-
"argValue": "undefined",
1909-
"argType": "undefined",
1908+
"argValue": null,
1909+
"argType": "null",
19101910
"argPosition": 1
19111911
},
19121912
{
@@ -1932,8 +1932,8 @@
19321932
"lineNumber": 688,
19331933
"args": [
19341934
{
1935-
"argValue": "undefined",
1936-
"argType": "undefined",
1935+
"argValue": null,
1936+
"argType": "null",
19371937
"argPosition": 1
19381938
},
19391939
{
@@ -2685,7 +2685,7 @@
26852685
{
26862686
"eventId": "chat:open-url",
26872687
"filePath": "/plugins/chat/assets/javascripts/discourse/routes/chat.js",
2688-
"lineNumber": 51,
2688+
"lineNumber": 48,
26892689
"args": [
26902690
{
26912691
"argValue": "url",
@@ -2698,7 +2698,7 @@
26982698
{
26992699
"eventId": "chat:toggle-close",
27002700
"filePath": "/plugins/chat/assets/javascripts/discourse/routes/chat.js",
2701-
"lineNumber": 55,
2701+
"lineNumber": 53,
27022702
"args": [],
27032703
"comments": null
27042704
},

scripts/extract_app_events.mjs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import fs from "fs";
2-
import path from "path";
3-
import yaml from "js-yaml";
1+
import "dotenv/config";
42
import parser from "@babel/parser";
53
import _traverse from "@babel/traverse";
64
import * as t from "@babel/types";
7-
import { promisify } from "util";
85
import { Preprocessor } from "content-tag";
9-
import "dotenv/config";
6+
import fs from "fs";
7+
import yaml from "js-yaml";
8+
import path from "path";
9+
import { promisify } from "util";
1010

1111
const discourseDir = process.env.DISCOURSE_CORE;
1212
const traverse = _traverse.default;
@@ -26,7 +26,7 @@ const EXCLUDED_DIR_PATTERNS = [
2626
"/discourse/plugins/chat/test",
2727
"/discourse/plugins/discourse-deprecation-collector/",
2828
];
29-
const UNPARSABLE_INDICATOR = 'undefined';
29+
const UNPARSABLE_INDICATOR = "undefined";
3030
const filesToDebug = [];
3131

3232
async function isExcludedDir(filePath) {
@@ -104,7 +104,7 @@ function extractAppEvents(path, filePath, eventTriggers, hasAppEventsTrigger) {
104104
const args = node.arguments.slice(1).map((arg, index) => {
105105
return {
106106
...extractArgument(arg),
107-
argPosition: index + 1
107+
argPosition: index + 1,
108108
};
109109
});
110110

@@ -152,7 +152,7 @@ function extractAppEventsFromOptionalExpressions(
152152
const args = node.arguments.slice(1).map((arg, index) => {
153153
return {
154154
...extractArgument(arg),
155-
argPosition: index + 1
155+
argPosition: index + 1,
156156
};
157157
});
158158

@@ -175,20 +175,31 @@ function extractArgument(argNode) {
175175
if (t.isStringLiteral(argNode)) {
176176
argValue = argNode.value;
177177
argType = "string";
178+
} else if (t.isNumericLiteral(argNode)){
179+
argValue = argNode.value;
180+
argType = "integer";
178181
} else if (t.isIdentifier(argNode)) {
179182
argValue = argNode.name;
180183
argType = "variable";
181184
} else if (t.isMemberExpression(argNode)) {
182185
argValue = extractNameFromMemberExpression(argNode);
183186
argType = "property";
187+
} else if (t.isThisExpression(argNode)) {
188+
argValue = "this";
189+
argType = "this";
184190
} else if (t.isCallExpression(argNode)) {
185191
let calleeObjectName;
186-
if (argNode.callee.object) {
187-
calleeObjectName = t.isThisExpression(argNode.callee.object)
188-
? "this"
189-
: argNode.callee.object.name;
192+
// TODO: possibly some duplication here? quite nested code
193+
if (t.isMemberExpression(argNode.callee)) {
194+
argValue = extractNameFromMemberExpression(argNode.callee);
195+
} else {
196+
if (argNode.callee.object) {
197+
calleeObjectName = t.isThisExpression(argNode.callee.object)
198+
? "this"
199+
: argNode.callee.object.name;
200+
}
201+
argValue = `${calleeObjectName}.${argNode.callee.property.name}`;
190202
}
191-
argValue = `${calleeObjectName}.${argNode.callee.property.name}`;
192203
argType = "called_function";
193204
} else if (t.isTemplateLiteral(argNode)) {
194205
argValue = argNode.quasis.reduce((acc, quasi, index) => {
@@ -216,9 +227,14 @@ function extractArgument(argNode) {
216227
}, "");
217228
argType = "templated_string";
218229
} else if (t.isObjectExpression(argNode)) {
219-
//TODO process object expressions into structured data - keys and their valueTypes
220230
argValue = extractDetailsFromObjectExpression(argNode.properties);
221231
argType = "object";
232+
} else if (t.isSpreadElement(argNode)) {
233+
argValue = argNode.argument.name;
234+
argType = "spread_element";
235+
} else if (t.isNullLiteral(argNode)) {
236+
argValue = null;
237+
argType = "null";
222238
} else {
223239
argValue = UNPARSABLE_INDICATOR;
224240
argType = UNPARSABLE_INDICATOR;
@@ -229,7 +245,7 @@ function extractArgument(argNode) {
229245

230246
function extractDetailsFromObjectExpression(props) {
231247
return props.reduce((result, prop) => {
232-
const key = prop.key.name || prop.key.value;
248+
const key = prop.key.name || prop.key.value;
233249

234250
let valueType;
235251
if (t.isStringLiteral(prop.value)) {
@@ -251,7 +267,7 @@ function extractDetailsFromObjectExpression(props) {
251267
}
252268

253269
result.push({ key, valueType });
254-
return result
270+
return result;
255271
}, []);
256272
}
257273

@@ -367,7 +383,9 @@ async function parseDirectory(directoryPath) {
367383
`${allEventIds.length} Extracted event IDs saved to ${eventIdsFilePath}`
368384
);
369385

370-
// Output detailed information
386+
// Event triggers are saved without any further processing to group by filePath
387+
// or by event ID intentionally, as this maintains flexibility in how we can
388+
// use the raw data
371389
const detailedOutputPath = path.join(
372390
".",
373391
"lib",

0 commit comments

Comments
 (0)