MCPcopy Create free account
hub / github.com/DavidWells/analytics / parseCode

Function parseCode

scripts/docs/parse/index.js:49–99  ·  view source on GitHub ↗
(code)

Source from the content-addressed store, hash-verified

47}
48
49function parseCode(code) {
50 let ast
51 try {
52 // Parse node code
53 ast = Parser.parse(code)
54 } catch (err) {
55 // on fail, try module
56 ast = Parser.parse(code, {
57 sourceType: 'module'
58 })
59 }
60 // console.log(ast)
61
62 const foundExports = getExports(ast.body)
63
64 // If parsing a window iife fallback and regex file
65 if (!foundExports.length) {
66 let m = getWindowExports(code)
67 return {
68 methodsByName: m.map((prop) => prop.name),
69 methodsAndValues: m.map((x) => {
70 return {
71 name: x.name,
72 type: x.statement,
73 value: x.statement
74 }
75 }),
76 foundExports: m
77 }
78 }
79
80 const mainFunction = foundExports.find(exportedThings => exportedThings.isDefault)
81 // console.log('mainFunction', mainFunction)
82
83 const methods = getMethodsReturned(ast.body, mainFunction.name)
84 const methodsAndValues = methods.map((x) => {
85 return {
86 name: x.key.name,
87 type: x.value.type,
88 value: code.substring(x.value.start, x.value.end)
89 }
90 })
91 const methodsByName = methods.map((prop) => prop.key.name)
92 // console.log('methodsByName', methodsByName)
93 // console.log('methodsAndValues', methodsAndValues)
94 return {
95 methodsByName: methodsByName,
96 methodsAndValues: methodsAndValues,
97 foundExports: foundExports
98 }
99}
100
101function getMethodsReturned(body, name) {
102 // First try to find function that === main export

Callers 1

getPluginInfoFunction · 0.85

Calls 3

getExportsFunction · 0.85
getWindowExportsFunction · 0.85
getMethodsReturnedFunction · 0.85

Tested by

no test coverage detected