How to use the ts-morph.SyntaxKind.ObjectLiteralExpression function in ts-morph

To help you get started, we’ve selected a few ts-morph examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github anoaland / anoa-cli / src / services / store / builders / reducer / index.ts View on Github external
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
    )
github anoaland / anoa-cli / src / generators / store / redux-connect-generator / state-builder.ts View on Github external
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 {
github anoaland / anoa-cli / src / services / store / builders / state / index.ts View on Github external
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
        }))
      )
    }
  }
github anoaland / anoa-cli / src / generators / store / reducer-generator.ts View on Github external
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
    )
github anoaland / anoa-cli / src / generators / store / redux-connect-generator / thunk-builder.ts View on Github external
}

    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
github anoaland / anoa-cli / src / services / core / redux-utils.ts View on Github external
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
    }
  }
github aurelia / aurelia / tools / api-doc-generator / src / api / extractors / export-assignment / export-assignment-extractor.ts View on Github external
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;
            }
        });
github anoaland / anoa-cli / src / core / tools / react.ts View on Github external
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
      }, {})
  }
github aurelia / aurelia / tools / api-doc-generator / src / api / extractors / literal / literal-extractor.ts View on Github external
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;
    }
github aurelia / aurelia / tools / api-doc-generator / src / api / extractors / literal / literal-extractor.ts View on Github external
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;