Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = function transform(source) {
const ast = parseAst(source);
const tests = [];
let currentTest;
recast.visit(ast, {
visitExpressionStatement(path) {
let node = path.node;
if (isTest(node)) {
if (!testUsesAndThen(node)) {
// don't transform this test
return false;
}
currentTest = {
test: path,
asyncHelpers: [],
andThens: [],
dones: [],
removeDone: testUsesDone(path.node)
};
function getExports(ast: any) {
const exports: any = []
recast.visit(ast, {
visitExportNamedDeclaration(path: any) {
const node = path.node
const specifiers = getSpecifiersOfNode(node.specifiers)
const declarations = Object.keys(getIdentifiers(ast))
exports.push(...new Set(specifiers.concat(declarations)))
this.traverse(path)
},
visitExportDefaultDeclaration(path: any) {
const node = path.node
if (node.declaration.type === types.Identifier.name) {
exports.push(node.declaration.name)
}
/* Commenting it for now, this might needed for further enhancements.
else if (nodeType === types.Literal.name) {
function getStatementWords (node, setupVariables) {
if (!wordNodeCache.has(node)) {
/** @type {Word[]} */
let words = []
// Variable
let varName = mayGetVariableDeclarationName(node)
if (varName) {
words.push({ value: varName, score: 1 })
} else {
// Contained identifiers
visit(node, {
visitIdentifier (path) {
let identifier = path.value.name
if (setupVariables.includes(identifier) && !words.includes(identifier)) {
words.push({ value: identifier, score: 1 })
}
this.traverse(path)
},
})
}
// Processing
const allWords = words.map(n => n.value).join('|')
if (wordCache.has(allWords)) {
words = wordCache.get(allWords)
} else {
words = processWords(words, true)
function generateGroupName (group, index) {
if (group.declarations) {
const vars = {}
// Count variable identifiers
visit(Array.from(group.nodes), {
visitIdentifier (path) {
let identifier = path.value.name
if (group.declarations.includes(identifier)) {
const words = processWords([{ value: identifier, score: 1 }])
for (const word of words) {
if (!vars[word.value]) {
vars[word.value] = 1
} else {
vars[word.value]++
}
}
}
this.traverse(path)
},
})
// Sort by count
function getPropsFromExpression(
parentAst: ASTElement | undefined,
item: ASTNode,
expression: string,
documentation: Documentation,
options: TemplateParserOptions
) {
// this allows for weird expressions like {[t]:val} to be parsed properly
const ast = parser.parse(`(() => (${expression}))()`)
const propsFound: string[] = []
recast.visit(ast.program, {
visitMemberExpression(path) {
const obj = path.node ? path.node.object : undefined
const propName = path.node ? path.node.property : undefined
if (
obj &&
propName &&
bt.isIdentifier(obj) &&
obj.name === 'props' &&
bt.isIdentifier(propName)
) {
const pName = propName.name
const p = documentation.getPropDescriptor(pName)
propsFound.push(pName)
p.type = { name: 'undefined' }
}
return false
function transformToString(source, visitors) {
return recast.print(
recast.visit(
recast.parse(source),
visitors
)
).code;
}
function getIdentifiers(ast) {
const identifiers = createObject(null);
recast.visit(ast, {
visitVariableDeclarator(path) {
const node = path.node;
const nodeType = node.init.type;
if (nodeType === types.Identifier.name) {
if (identifiers[node.init.name]) {
identifiers[node.init.name].push(node.init.name);
} else {
identifiers[node.init.name] = [node.init.name];
}
} else if (nodeType === types.Literal.name) {
if (identifiers[node.id.name]) {
identifiers[node.id.name].push(node.init.value);
} else {
identifiers[node.id.name] = [node.init.value];
}
function replaceNodes(src, varNames, nodes, parser) {
const ast = recast.parse(src, {parser});
recast.visit(ast, getVistor(varNames, nodes));
return ast;
}
module.exports = function (source) {
let ast = recast.parse(source, {
parser: require('recast/parsers/typescript')
});
recast.visit(ast, {
visitObjectExpression: function (path) {
let obj = path.node;
if (obj.properties.find(prop => prop.key && interestingFields.has(prop.key.name))) {
let curId = id++;
obj.properties.push({
type: "ObjectProperty",
shorthand: false,
key: {
type: "StringLiteral",
value: "___$id"
},
value: {
type: "Literal",
value: curId
},
accessibility: null,
function renameClass(renames, src) {
var ast = recast.parse(src);
recast.visit(ast, {
visitJSXOpeningElement: function(path) {
if (path.node.name.name[0] === path.node.name.name[0].toLowerCase()) {
path.node.attributes.forEach(function(attribute) {
var name = attribute.name.name;
if (
name === 'className' &&
n.Literal.check(attribute.value) &&
typeof attribute.value.value === 'string'
) {
attribute.value.value = attribute.value.value.split(' ').map(function(className) {
return renames[className] || className;
}).join(' ');
}
});
}
this.traverse(path);