Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const generateExportAST = (
uidl: ComponentUIDL,
propDefinitions: Record,
stateDefinitions: Record,
dataObject: Record,
methodsObject: Record,
t = types
) => {
const componentName = UIDLUtils.getComponentClassName(uidl)
let angularMethodsAST: types.ClassMethod[] = []
if (Object.keys(methodsObject).length > 0) {
angularMethodsAST = createMethodsObject(methodsObject, propDefinitions)
}
const propDeclaration = Object.keys(propDefinitions).map((propKey) => {
const definition = propDefinitions[propKey]
// By default any prop with type function is used to emitting events to the callee
if (definition.type === 'func') {
return t.classProperty(
t.identifier(propKey),
t.newExpression(t.identifier('EventEmitter'), []),
t.typeAnnotation(
t.genericTypeAnnotation(
t.identifier('EventEmitter'),
t.typeParameterInstantiation([t.anyTypeAnnotation()])
{
dependencies.BrowserModule = ANGULAR_PLATFORM_BROWSER
dependencies.ComponentsModule = constructRouteForComponentsModule('.')
dependencies.AppComponent = APP_COMPONENT
const routes = UIDLUtils.extractRoutes(uidl)
routesAST = createRoutesAST(routes, stateDefinitions)
ngModuleAST = createRootModuleDecorator()
moduleDecoratorAST = createExportModuleAST('AppModule')
}
break
case 'page':
{
dependencies.ComponentsModule = constructRouteForComponentsModule('../..')
dependencies.CommonModule = ANGULAR_COMMON_MODULE
const componentName = UIDLUtils.getComponentClassName(uidl)
const fileName = UIDLUtils.getComponentFileName(uidl)
dependencies[componentName] = constructLocalDependency(fileName)
routesAST = createPageRouteAST(componentName)
ngModuleAST = createPageModuleModuleDecorator(componentName)
moduleDecoratorAST = createExportModuleAST(uidl.outputOptions.moduleName)
// Acording to widely followed convention module should have .module in its name
uidl.outputOptions.fileName = fileName.replace('.component', '.module')
}
break
case 'component':
{
dependencies.CommonModule = ANGULAR_COMMON_MODULE
// Looping through all components and importing them into component module
dependencyHandling: 'ignore',
}
)
chunks.push({
type: ChunkType.HAST,
name: angularTemplateChunkName,
fileType: FileType.HTML,
meta: {
nodesLookup: templateLookup,
},
content: templateContent,
linkAfter: [],
})
const componentName = UIDLUtils.getComponentClassName(uidl)
const params = {
selector: UIDLUtils.createWebComponentFriendlyName(componentName),
templateUrl: `${UIDLUtils.getComponentFileName(uidl)}.${FileType.HTML}`,
}
const componentDecoratorAST = ASTBuilders.createComponentDecorator(params)
chunks.push({
type: ChunkType.AST,
name: componentDecoratorChunkName,
fileType: FileType.TS,
linkAfter: tsChunkAfter,
content: componentDecoratorAST,
})
/* We need to import EventEmitter and Output in Angular to temit events to the parent
So, to make sure if we need to import them we need to loop through all the methods and
Object.keys(moduleComponents).forEach((componentKey) => {
const component = moduleComponents[componentKey]
const componentClassName = UIDLUtils.getComponentClassName(component)
const componentFileName = UIDLUtils.getComponentFileName(component)
const componentFolderPath = UIDLUtils.getComponentFolderPath(component)
dependencies[componentClassName] = constructComponentDependency(
componentFolderPath,
componentFileName
)
})
const componentClassNames = Object.keys(moduleComponents).map((componentKey) =>
UIDLUtils.getComponentClassName(moduleComponents[componentKey])
)
const setLocalDependencyPath = (
element: UIDLElement,
components: Record,
fromPath: string[],
toBasePath: string[]
) => {
const componentKey = element.elementType
const component = components[componentKey]
const componentPath = UIDLUtils.getComponentFolderPath(component)
const componentClassName = UIDLUtils.getComponentClassName(component)
const toPath = toBasePath.concat(componentPath)
const importFileName = UIDLUtils.getComponentFileName(component)
const importPath = generateLocalDependenciesPrefix(fromPath, toPath)
element.dependency.path = `${importPath}${importFileName}`
element.elementType = componentClassName
}
},
dependencyHandling: 'ignore',
stateHandling: 'mutation',
slotHandling: 'native',
customElementTag: (name: string) => UIDLUtils.createWebComponentFriendlyName(name),
}
const jsxTagStructure = createJSXSyntax(uidl.node, jsxParams, jsxOptions)
if (uidl.seo && uidl.seo.title) {
const titleAST = ASTBuilders.createSelfClosingJSXTag('stencil-route-title')
ASTUtils.addAttributeToJSXTag(titleAST, 'pageTitle', uidl.seo.title)
jsxTagStructure.children.unshift(titleAST)
}
const componentName = UIDLUtils.getComponentClassName(uidl)
const exportAST = createClassDeclaration(
componentName,
propDefinitions,
stateDefinitions,
jsxTagStructure
)
const params = {
tag: UIDLUtils.createWebComponentFriendlyName(componentName),
shadow: true,
}
const decoratorAST = ASTBuilders.createComponentDecorator(params)
structure.chunks.push({
type: ChunkType.AST,
prop: 'props',
state: 'state',
local: '',
},
dependencyHandling: 'import',
stateHandling: 'function',
slotHandling: 'props',
}
const jsxTagStructure = createJSXSyntax(uidl.node, jsxParams, jsxOptions)
if (!usePureComponent) {
dependencies.Component = PREACT_COMPONENT_DEPENDENCY
}
const componentName = UIDLUtils.getComponentClassName(uidl)
const preactComponent = usePureComponent
? createPureComponent(componentName, propDefinitions, jsxTagStructure)
: createClassComponent(componentName, propDefinitions, stateDefinitions, jsxTagStructure)
structure.chunks.push({
type: ChunkType.AST,
fileType: FileType.JS,
name: componentChunkName,
meta: {
nodesLookup,
dynamicRefPrefix: jsxOptions.dynamicReferencePrefixMap,
},
content: preactComponent,
linkAfter: [importChunkName],
})
'method',
t.identifier('data'),
[],
t.blockStatement([t.returnStatement(dataAST)])
)
)
}
if (Object.keys(methodsObject).length > 0) {
const methodsAST = createMethodsObject(methodsObject, uidl.propDefinitions)
vueObjectProperties.push(
t.objectProperty(t.identifier('methods'), t.objectExpression(methodsAST))
)
}
const componentName = UIDLUtils.getComponentClassName(uidl)
return t.exportDefaultDeclaration(
t.objectExpression([
t.objectProperty(t.identifier('name'), t.stringLiteral(componentName)),
...vueObjectProperties,
])
)
}
}
chunks.push({
type: ChunkType.AST,
fileType: FileType.JS,
name: styleChunkName,
linkAfter: [importChunkName],
content: ASTBuilders.createConstAssignment(
jssDeclarationName,
ASTUtils.objectToObjectExpression(jssStyleMap)
),
})
const exportChunk = chunks.find((chunk) => chunk.name === exportChunkName)
const componentName = UIDLUtils.getComponentClassName(uidl)
const exportStatement = ASTBuilders.createReactJSSDefaultExport(
componentName,
jssDeclarationName
)
if (exportChunk) {
exportChunk.content = exportStatement
exportChunk.linkAfter = [importChunkName, styleChunkName]
} else {
chunks.push({
type: ChunkType.AST,
fileType: FileType.JS,
name: exportChunkName,
content: exportStatement,
linkAfter: [importChunkName, styleChunkName],
})