Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (existsSync(componentFilePath)) {
relativePath = relative(filePath, componentFilePath);
if (relativePath.charAt(0) !== '.') {
relativePath = './' + relativePath;
}
relativePath = relativePath.split(sep).join('/'); // workaround for windows
}
const imports = root.find(j.ImportDeclaration);
const lastImport = imports.at(imports.size() - 1);
lastImport.insertAfter(
j.importDeclaration(
[j.importDefaultSpecifier(
j.identifier(component.componentName)
)],
j.literal(relativePath)
)
);
writeFile(filePath, root.toSource());
}
function getNewPropertyKey(properties, key) {
// if the key has invalid characters, it has to be a string literal
if (key.match(/[^a-zA-Z0-9_]/)) {
return j.literal(key)
}
// infer whether to use a literal or identifier by looking at the other keys
const useIdentifier =
properties.length === 0 || properties.some(p => p.key.type === 'Identifier')
return useIdentifier ? j.identifier(key) : j.literal(key)
}
'insertAfter' :
'insertBefore';
const collection = points
// get parent statement
.map(path => path.parent)
.at(0);
collection[insertMethod].call(
collection,
j.expressionStatement(
j.callExpression(
j.memberExpression(object, j.identifier('model')),
[
j.callExpression(j.identifier('require'), [
j.literal(modelPath)
])
]
)
)
);
},
function createEmptyNode(value) {
if (isArray(value)) {
return j.arrayExpression([])
}
if (isObject(value)) {
return j.objectExpression([])
}
return j.literal('')
}
function getNewImportDeclaration({
name,
local,
}) {
return j.importDeclaration([
j.importDefaultSpecifier(j.identifier(name)),
], j.literal(local))
}
mAST.find(j.Literal).forEach(path => {
let n = path.value.value;
if (typeof n === 'string' && n.includes('.')) {
hasEscape = true;
n = n.replace(/\./g, '\\.');
j(path).replaceWith(j.literal(n));
}
});
if (name) {
identifier = jscodeshift.identifier(name);
variableIds.unshift(
jscodeshift.importDefaultSpecifier(identifier, identifier)
);
} else if (namespace) {
identifier = jscodeshift.identifier(namespace);
variableIds.unshift(
jscodeshift.importNamespaceSpecifier(identifier, identifier)
);
}
}
declaration = jscodeshift.importDeclaration(
variableIds,
jscodeshift.literal(moduleName)
);
declaration.importKind = kind;
return declaration;
}
path.node.properties.forEach(prop => {
if (j.Property.check(prop) && prop.key.name === 'namespace') {
prop.value = j.literal(newNamespace);
}
});
});