Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function addAutoImports(path, state) {
const imports = getImportNames(path, state);
if (isModule(path)) {
// import {jsx} from "react";
// import {createElement} from "react";
const importMap = {};
imports.forEach(importName => {
if (!importMap[importName]) {
importMap[importName] = addNamed(
path,
importName,
getSource(state.source, importName),
{
importedInterop: "uncompiled",
ensureLiveReference: true,
},
).name;
}
imports.forEach(importName => {
const source = getSource(state.source, importName);
if (!importMap[importName]) {
if (!sourceMap[source]) {
// var _react = require("react")
sourceMap[source] = addNamespace(path, source, {
importedInterop: "uncompiled",
ensureLiveReference: true,
}).name;
}
importMap[importName] = sourceMap[source];
}
});
return importMap;
insertElementAdjacentToChild(position) {
const { containerPath, currentOperation, onWarning } = this;
const { targetChild, componentPath } = currentOperation;
const targetChildPath = this.findContainerChildByName(targetChild);
if (!targetChildPath) {
onWarning(
`Attempted to inject a PWA Studio extension, but specified targetChild was not found\n` +
`operation: ${currentOperation.operation}\n` +
`targetContainer: ${currentOperation.targetContainer}\n` +
`targetChild: ${currentOperation.targetChild}`
);
return;
}
const componentIdent = addDefault(targetChildPath, componentPath, {
nameHint: 'Extension'
}).name;
// TODO: extensionNode needs to be wrapped in a new ContainerChild,
// and an error boundary
const extensionNode = this.buildCreateElementCall(componentIdent);
this.insertAdjacentArgumentsNode(
position,
containerPath,
targetChildPath.node,
extensionNode
);
}
importMethod(methodName, file, pluginState) {
if (!pluginState.selectedMethods[methodName]) {
const libraryDirectory = this.libraryDirectory;
const style = this.style;
const transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line
? transCamel(methodName, '_')
: this.camel2DashComponentName
? transCamel(methodName, '-')
: methodName;
const path = winPath(
this.customName ? this.customName(transformedMethodName) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
);
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
? addDefault(file.path, path, { nameHint: methodName })
: addNamed(file.path, methodName, path);
if (this.customStyleName) {
const stylePath = winPath(this.customStyleName(transformedMethodName));
addSideEffect(file.path, `${stylePath}`);
} else if (this.styleLibraryDirectory) {
const stylePath = winPath(
join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName)
);
addSideEffect(file.path, `${stylePath}`);
} else if (style === true) {
addSideEffect(file.path, `${path}/style`);
} else if (style === 'css') {
addSideEffect(file.path, `${path}/style/css`);
} else if (typeof style === 'function') {
const stylePath = style(path, file);
if (stylePath) {
});
}
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) {
builderFuncId = addDefaultImport(state.file.path, config.builder, {
nameHint: 'buildFlaskURL',
});
}
replacement = buildFunc({
FUNC: builderFuncId,
RULE: t.valueToNode(data),
BASE: t.stringLiteral(config.basePath),
});
}
// replace the tagged template expression with the builder function
parentPath.replaceWith(replacement);
});
};
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) {
// remove variable declarations of css``;
cssRefs.forEach(ref => {
ref.parentPath.parentPath.remove();
});
// if no styles have been used, there's no more work to do
if (!Object.keys(styleSheet.usage).length) return;
const filename = state.file.opts.filename;
// choose a file to save the styles
const outputFilename = relative(process.cwd(), filename.replace(/\.[^.]+$/, '.zero.css'));
// include this file as an import to the referenced module, so that css-loader can pick it up at bundle-time
addSideEffect(stylesRefs[0], './' + basename(outputFilename));
// combine all the used styles
const cssText = styleSheet.toString();
// Read the file first to compare the content
// Write the new content only if it's changed
// This will prevent unnecessary reloads
let currentCssText;
try {
currentCssText = readFileSync(outputFilename, 'utf-8');
} catch (e) {
// Ignore error
}
// if the files hasn't changed, nothing more to do
? transCamel(methodName, '-')
: methodName;
const path = winPath(
this.customName ? this.customName(transformedMethodName) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
);
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
? addDefault(file.path, path, { nameHint: methodName })
: addNamed(file.path, methodName, path);
if (this.customStyleName) {
const stylePath = winPath(this.customStyleName(transformedMethodName));
addSideEffect(file.path, `${stylePath}`);
} else if (this.styleLibraryDirectory) {
const stylePath = winPath(
join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName)
);
addSideEffect(file.path, `${stylePath}`);
} else if (style === true) {
addSideEffect(file.path, `${path}/style`);
} else if (style === 'css') {
addSideEffect(file.path, `${path}/style/css`);
} else if (typeof style === 'function') {
const stylePath = style(path, file);
if (stylePath) {
addSideEffect(file.path, stylePath);
}
}
}
return Object.assign({}, pluginState.selectedMethods[methodName]);
}
const libraryDirectory = this.libraryDirectory;
const style = this.style;
const transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line
? transCamel(methodName, '_')
: this.camel2DashComponentName
? transCamel(methodName, '-')
: methodName;
const path = winPath(
this.customName ? this.customName(transformedMethodName) : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
);
pluginState.selectedMethods[methodName] = this.transformToDefaultImport // eslint-disable-line
? addDefault(file.path, path, { nameHint: methodName })
: addNamed(file.path, methodName, path);
if (this.customStyleName) {
const stylePath = winPath(this.customStyleName(transformedMethodName));
addSideEffect(file.path, `${stylePath}`);
} else if (this.styleLibraryDirectory) {
const stylePath = winPath(
join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName)
);
addSideEffect(file.path, `${stylePath}`);
} else if (style === true) {
addSideEffect(file.path, `${path}/style`);
} else if (style === 'css') {
addSideEffect(file.path, `${path}/style/css`);
} else if (typeof style === 'function') {
const stylePath = style(path, file);
if (stylePath) {
addSideEffect(file.path, stylePath);
}
}
}
this.addDefaultImport = (source, nameHint, blockHoist) => {
// If something on the page adds a helper when the file is an ES6
// file, we can't reused the cached helper name after things have been
// transformed because it has almost certainly been renamed.
const cacheKey = isModule(file.path);
const key = `${source}:${nameHint}:${cacheKey || ''}`;
let cached = cache.get(key);
if (cached) {
cached = t.cloneDeep(cached);
} else {
cached = addDefault(file.path, source, {
importedInterop: 'uncompiled',
nameHint,
blockHoist,
});
cache.set(key, cached);
}
cached[Symbol.for('helper')] = true;