Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const reducersVarName = 'reducers'
const reducersVarStatement =
mainReducerSourceFile.getVariableStatement(reducersVarName) ||
mainReducerSourceFile.addVariableStatement({
isExported: true,
declarations: [
{
name: reducersVarName,
initializer: `combineReducers({})`
}
],
declarationKind: VariableDeclarationKind.Const
})
const reducerObjLiteral = reducersVarStatement.getDescendantsOfKind(
SyntaxKind.ObjectLiteralExpression
)[0]
if (!reducerObjLiteral) {
throw new Error('Invalid reducer file.')
}
reducerObjLiteral.addPropertyAssignment({
name,
initializer: reducerName
})
// generate root action types
const appRootActionsName = naming.store().rootActions()
const appRootActionType = mainReducerSourceFile.getTypeAlias(
appRootActionsName
)
mergeExpression(decorator: Decorator | CallExpression) {
if (!this.args.length) {
return
}
const pos = 0
const ts = this.view.context.tools.ts()
const existingArgs = decorator.getArguments()
if (existingArgs && existingArgs.length > pos) {
// merge existing state map initializer
const arg = existingArgs[pos]
const obj = arg.getFirstDescendantByKind(
SyntaxKind.ObjectLiteralExpression
)
if (obj) {
// found state assignments
// resolve 'state' var name
const identifier = arg.getFirstDescendantByKind(SyntaxKind.Identifier)
ts.mergePropertyAssignments(
obj,
this.args.map(a => ({
...a,
type: a.data.replace(/#state/g, identifier.getText())
}))
)
} else {
name: f.name,
hasQuestionToken: f.optional,
type: f.type,
kind: StructureKind.PropertySignature
}))
)
// set fields initializer
const initializerFields = newFields.filter(f => !f.optional && f.initial)
if (initializerFields.length) {
this.initializerModified = true
const stateInitializer = reducerSourceFile
.getFirstDescendantByKind(SyntaxKind.ArrowFunction)
.getFirstDescendantByKind(SyntaxKind.Parameter)
.getFirstDescendantByKind(SyntaxKind.ObjectLiteralExpression)
stateInitializer.addPropertyAssignments(
initializerFields.map(f => ({
name: f.name,
initializer: f.initial,
kind: StructureKind.PropertyAssignment
}))
)
}
}
const reducerRootName = 'reducers'
const reducersVarStatement =
reducerRootSourceFile.getVariableStatement(reducerRootName) ||
reducerRootSourceFile.addVariableStatement({
isExported: true,
declarations: [
{
name: reducerRootName,
initializer: `combineReducers({})`
}
],
declarationKind: VariableDeclarationKind.Const
})
const reducers = reducersVarStatement.getDescendantsOfKind(
SyntaxKind.ObjectLiteralExpression
)[0]
if (!reducers) {
throw new Error('Invalid reducer file.')
}
reducers.addPropertyAssignment({
name,
initializer: reducerName
})
// generate root action types
const appRootActionsName = naming.store().rootActions()
const appRootActionType = reducerRootSourceFile.getTypeAlias(
appRootActionsName
)
}
const pos = 1
const ts = this.view.context.tools.ts()
const existingArgs = decorator.getArguments() || []
if (existingArgs.length > pos) {
const arg = existingArgs[pos]
// resolve 'dispatch' var name
const identifier = arg
.getFirstDescendantByKind(SyntaxKind.Identifier)
.getText()
const obj = arg.getFirstDescendantByKind(
SyntaxKind.ObjectLiteralExpression
)
ts.mergePropertyAssignments(
obj,
this.args.map(a => ({
...a,
type: a.type.replace(/#dispatch/g, identifier)
}))
)
} else {
if (!existingArgs.length) {
// state arg is null
decorator.addArgument('null')
}
// add this argument
static getDecoratorInfo(node: Node): StoreDecoratorArgInfo {
const name = node.getFirstDescendantByKind(SyntaxKind.Identifier).getText()
const args = node
.getFirstDescendantByKind(SyntaxKind.ObjectLiteralExpression)
.getChildrenOfKind(SyntaxKind.PropertyAssignment)
.map(a => {
const s = a.getText().split(':')
return {
name: s[0].trim(),
value: s[1].trim()
}
})
return {
name,
args
}
}
node.forEachDescendant(item => {
switch (item.getKind()) {
case SyntaxKind.ObjectLiteralExpression:
const obj = this.literalExtractor.extractExpression(item as Expression);
members.push(obj);
break;
case SyntaxKind.ArrayLiteralExpression:
const array = (item as ArrayLiteralExpression).getElements();
array.forEach(element => {
const obj = this.literalExtractor.extractExpression(element as Expression);
members.push(obj);
});
break;
}
});
getStateInitializer(constr: ConstructorDeclaration): KeyValue {
const stmt = this.getStateStatement(constr)
if (!stmt) {
return {}
}
return stmt
.getFirstDescendantByKind(SyntaxKind.ObjectLiteralExpression)
.getProperties()
.map(p => ({
name: p.getChildAtIndex(0).getText(),
value: p.getChildAtIndex(2).getText()
}))
.reduce((acc, curr) => {
acc[curr.name] = curr.value
return acc
}, {})
}
private getObjectLiteral(node: VariableDeclaration): ObjectLiteralExpression | undefined {
const hasAsExpression = node.getInitializerIfKind(SyntaxKind.AsExpression) !== void 0;
let objectLiteral: ObjectLiteralExpression | undefined = void 0;
if (hasAsExpression) {
const asExpression = node.getInitializerIfKindOrThrow(SyntaxKind.AsExpression);
objectLiteral = asExpression.getDescendantsOfKind(SyntaxKind.ObjectLiteralExpression)[0];
} else {
objectLiteral = node.getInitializerIfKind(SyntaxKind.ObjectLiteralExpression);
}
return objectLiteral;
}
node.forEachChild(item => {
const kind = item.getKind();
if (kind === SyntaxKind.ObjectLiteralExpression && !status) {
status = true;
} else if (kind === SyntaxKind.AsExpression && !status) {
const asExpression = item as AsExpression;
const isObjectLiteral = asExpression.getFirstChildIfKind(SyntaxKind.ObjectLiteralExpression);
if (isObjectLiteral) {
status = true;
}
}
});
return status;