ts-morph#ImportDeclaration TypeScript Examples

The following examples show how to use ts-morph#ImportDeclaration. 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: import.ts    From cli with MIT License 6 votes vote down vote up
export function appendStatementAfterImports(
  source: SourceFile,
  statement: string,
  apply = true
) {
  const imports = source
    .getFirstChildByKind(SyntaxKind.SyntaxList)
    .getChildrenOfKind(SyntaxKind.ImportDeclaration);

  let appendIdx: number;

  if (!imports.length) {
    appendIdx = 0;
  } else {
    appendIdx =
      source.getLastChildByKind(SyntaxKind.ImportDeclaration).getChildIndex() +
      1;
  }

  source.insertStatements(appendIdx, writer => {
    writer.newLine();
    writer.write(statement);
  });
  apply && source.saveSync();
}
Example #2
Source File: import.ts    From cli with MIT License 6 votes vote down vote up
// 获得所有导入声明
export function findImportsDeclaration(
  source: SourceFile,
  specifier?: string
): ImportDeclaration | ImportDeclaration[] {
  const importDeclarations = source
    .getFirstChildByKind(SyntaxKind.SyntaxList)
    .getChildrenOfKind(SyntaxKind.ImportDeclaration);

  if (specifier) {
    return importDeclarations.filter(imp => {
      const str = imp.getFirstChildByKind(SyntaxKind.StringLiteral);
      return str.getText() === `'${specifier}'`;
    })[0];
  }

  return importDeclarations;
}
Example #3
Source File: import.ts    From cli with MIT License 5 votes vote down vote up
// 新增具名导入成员
// 在importSpec不存在时,会新增一条具名导入
// 会过滤已存在的具名导入成员
export function addNamedImportsMember(
  source: SourceFile,
  importSpec: string,
  members: string[],
  apply = true
): void {
  const importDec = source
    .getFirstChildByKind(SyntaxKind.SyntaxList)
    .getChildrenOfKind(SyntaxKind.ImportDeclaration)
    .filter(importDec => {
      const importString = importDec
        .getFirstChildByKind(SyntaxKind.StringLiteral)
        .getText();
      return `'${importSpec}'` === importString;
    })[0];

  if (!importDec) {
    source.addImportDeclaration({
      moduleSpecifier: importSpec,
      namedImports: members,
    });
    apply && source.saveSync();

    return;
  }

  const importClause = importDec.getImportClause();
  const namedImports = importClause.getNamedImports().map(x => x.getText());

  const namedImportsCanBeAdded = members.filter(
    mem => !namedImports.includes(mem)
  );

  if (!namedImportsCanBeAdded.length) {
    return;
  }

  importDec.addNamedImports(namedImportsCanBeAdded);

  apply && source.saveSync();
}
Example #4
Source File: import.ts    From cli with MIT License 5 votes vote down vote up
// 检查是否是默认导入
export function isDefaultImport(importSpec: ImportDeclaration): boolean {
  return Boolean(importSpec.getDefaultImport());
}
Example #5
Source File: import.ts    From cli with MIT License 5 votes vote down vote up
// 检查是否是命名空间导入
export function isNamespaceImport(importSpec: ImportDeclaration): boolean {
  return Boolean(importSpec.getNamespaceImport());
}
Example #6
Source File: import.ts    From cli with MIT License 5 votes vote down vote up
// 检查是否是具名导入
export function isNamedImport(importSpec: ImportDeclaration): boolean {
  return Boolean(importSpec.getNamedImports().length);
}