Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const quasi = parentPath.node.quasi;
if (quasi.expressions.length) {
throw new MacroError('flask-url.macro cannot contain expressions');
}
const endpoint = quasi.quasis[0].value.cooked;
if (builderFuncId) {
builderFuncId = t.cloneDeep(builderFuncId);
}
let replacement;
if (config.mock) {
if (!builderFuncId) {
builderFuncId = addNamedImport(state.file.path, 'mockFlaskURL', config.builder, {
nameHint: 'mockFlaskURL',
});
}
replacement = buildFuncMock({
FUNC: builderFuncId,
ENDPOINT: t.stringLiteral(endpoint),
});
} else {
const data = config.urlMap[endpoint];
if (!data) {
throw new MacroError('flask-url.macro must reference a valid flask endpoint');
}
// generate import
if (!builderFuncId) {
imports.forEach(importName => {
if (!importMap[importName]) {
importMap[importName] = addNamed(
path,
importName,
getSource(state.source, importName),
{
importedInterop: "uncompiled",
ensureLiveReference: true,
},
).name;
}
});
MemberExpression(path) {
const {node} = path;
const obj = node.object;
if (obj.name !== 'Promise') return;
if (!path.isReferenced()) return;
if (path.scope.getBindingIdentifier('Promise')) return;
if (node.computed) {
path.replaceWith(
t.memberExpression(
addNamed(path, 'default', 'bluebird', {nameHint: 'Promise'}),
node.property,
true
)
);
} else {
path.replaceWith(
addNamed(
path,
node.property.name,
'bluebird',
{nameHint: 'Promise'}
)
);
}
},
},
export default function state(stateUpdaters: Map, path: NodePath) {
const variableDeclaration = path.findParent(p => t.isVariableDeclaration(p)) as NodePath;
const stateVariable = path.find(p => t.isVariableDeclarator(p)).get('id') as NodePath;
const initState = getInitState(path);
const hookId = addNamed(path, 'useState', 'react');
const updater = path.scope.generateUidIdentifier(`set${stateVariable.node.name}`);
stateUpdaters.set(stateVariable.node.name, updater);
variableDeclaration.insertAfter(
t.variableDeclaration('const', [
t.variableDeclarator(
t.arrayPattern([t.identifier(stateVariable.node.name), updater]),
t.callExpression(hookId, [initState])
)
])
);
path.replaceWith(initState);
const functionDeclaration = path.getFunctionParent();
functionDeclaration.traverse({
AssignmentExpression(path: NodePath) {
const variable = path.get('left').node;
if (
function registerImportMethod(path, name) {
const imports =
path.scope.getProgramParent().data.imports ||
(path.scope.getProgramParent().data.imports = new Set());
if (!imports.has(name)) {
addNamed(path, name, moduleName, { nameHint: `_$${name}` });
imports.add(name);
}
}
} else if (!t.isImportDefaultSpecifier(path.node.specifiers[0])) {
throw path
.get('specifiers.0')
.buildCodeFrameError(`${importPrefix} imports must use a default import`);
}
const importName = path.node.specifiers[0].local.name;
let builderFuncId = this.builderFuncId;
if (builderFuncId) {
builderFuncId = t.cloneDeep(builderFuncId);
}
let variable;
if (mock) {
if (!builderFuncId) {
builderFuncId = this.builderFuncId = addNamedImport(
path,
'mockFlaskURL',
builderImportLocation,
{
nameHint: 'mockFlaskURL',
}
);
}
variable = t.variableDeclarator(
t.identifier(importName),
buildFuncMock({
FUNC: builderFuncId,
ENDPOINT: t.stringLiteral(endpoint),
})
);
export default function bind(stateUpdaters: Map, path: NodePath) {
const jsxElement = path.findParent(p => t.isJSXOpeningElement(p)) as NodePath;
const createElementCall = path.find(p => t.isCallExpression(p)).findParent(p => t.isCallExpression(p)) as NodePath<
t.CallExpression
>;
const stateVariable = (path.parentPath.get('arguments') as Array)[0] as NodePath;
const eventVariable = path.scope.generateUidIdentifier('e');
const updater = stateUpdaters.get(stateVariable.node.name);
const helperId = addDefault(path, 'reactive.macro/lib/helpers/getValue', {
nameHint: 'getValue'
});
const hookId = addNamed(path, 'useCallback', 'react');
const handler = t.callExpression(hookId, [
t.arrowFunctionExpression(
[eventVariable],
t.callExpression(updater!, [t.callExpression(helperId, [eventVariable])])
),
t.arrayExpression()
]);
if (jsxElement) {
jsxElement.node.attributes.push(t.jsxAttribute(t.jsxIdentifier('onChange'), t.jsxExpressionContainer(handler)));
} else if (createElementCall) {
(createElementCall.node.arguments[1] as t.ObjectExpression).properties.push(
t.objectProperty(t.identifier('onChange'), handler)
);
}
path.findParent(p => t.isCallExpression(p)).replaceWith(stateVariable.node);
}
Function(path, state) {
if (!path.node.async || path.node.generator) return;
let wrapAsync = state.methodWrapper;
if (wrapAsync) {
wrapAsync = t.cloneNode(wrapAsync);
} else {
wrapAsync = state.methodWrapper = addNamed(path, method, module);
}
remapAsyncToGenerator(path, { wrapAsync });
},
},
function pageConfigExpression(path, id) {
const createId = addNamed(path, 'createPageConfig', 'remax')
path.insertAfter(
t.exportDefaultDeclaration(
t.callExpression(
t.identifier('Page'), [t.callExpression(createId, [id])])))
}
message.helper = function (type) {
return imports.addNamed(path, type, 'format-message')
}