-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindStrings.js
29 lines (25 loc) · 977 Bytes
/
findStrings.js
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
let StringBucket = []
function findStrings (astPart) {
if (Array.isArray(astPart)) {
astPart.forEach(findStrings)
} else if (astPart && typeof astPart === 'object' && astPart.type) {
if (astPart.type === 'StringLiteral') {
StringBucket.push(astPart.value)
} else if (astPart.type === 'TemplateLiteral') {
// StringBucket.push( /*astPart )*/ astPart.quasis.map( (v, i) => lo.pick(v, ['type', 'value', 'tail']) ) )
StringBucket.push(astPart.quasis.reduce((str, qv) => qv.type === 'TemplateElement' ? str + qv.value.cooked : str, ''))
} else {
let values = Object.keys(astPart).map(k => astPart[k])
for (var val of values) {
if (val && typeof val === 'object') { findStrings(val) }
}
}
}
}
const babylon = require('babylon')
function findStringsFromJsCode (str) {
StringBucket = []
findStrings(babylon.parse(str, {sourceType: 'module'}))
return StringBucket
}
module.exports = findStringsFromJsCode