ts-morph#Node TypeScript Examples

The following examples show how to use ts-morph#Node. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: languageServerFeatures.test.ts    From zod with MIT License 6 votes vote down vote up
getPropertyBeingAssigned = (node: Node, name: string) => {
  const propertyAssignment = node.forEachDescendant((descendent) =>
    Node.isPropertyAssignment(descendent) && descendent.getName() == name
      ? descendent
      : undefined
  );

  if (propertyAssignment == null)
    fail(`Could not find property assignment with name ${name}`);

  const propertyLiteral = propertyAssignment.getFirstDescendantByKind(
    SyntaxKind.Identifier
  );

  if (propertyLiteral == null)
    fail(`Could not find property literal with name ${name}`);

  return propertyLiteral;
}
Example #2
Source File: parse.ts    From earl with MIT License 6 votes vote down vote up
function abbreviateSignature(signature: string, project: TSProject): string {
  const isClassMethod = !signature.includes('function ')
  const sourceCode = isClassMethod ? `function ${signature} {}` : signature

  const sourceFile = project.createSourceFile('temp.ts', sourceCode, { overwrite: true })
  const functionDeclaration = sourceFile.getChildAtIndex(0).getChildAtIndex(0)
  assert(Node.isFunctionDeclaration(functionDeclaration))

  if (signature.includes('this:')) {
    functionDeclaration.getParameter('this')!.remove()
  }

  functionDeclaration.removeReturnType()

  let text = functionDeclaration.getText()

  if (isClassMethod) {
    text = text.slice('function '.length, -' {}'.length)
  }

  return text
}