private computeName()

in lib/facade_converter.ts [135:180]


  private computeName(node: ts.NamedDeclaration): DartNameRecord {
    const fullPath = fullJsPath(node);
    if (this.dartTypes.has(fullPath)) {
      return this.dartTypes.get(fullPath);
    }
    const sourceFile = <ts.SourceFile>base.getAncestor(node, ts.SyntaxKind.SourceFile);
    const fileName = sourceFile.fileName;
    let library: DartLibrary;
    if (this.libraries.has(fileName)) {
      library = this.libraries.get(fileName);
    } else {
      library = new DartLibrary(fileName);
      this.libraries.set(fileName, library);
    }
    const parts = fullPath.split('.');
    for (let i = parts.length - 1; i >= 0; i--) {
      // Find a unique name by including more of the module hierarchy in the
      // name. This is an arbitrary but hopefully unsurprising scheme to
      // generate unique names. There may be classes or members with conflicting
      // names due to a single d.ts file containing multiple modules.
      const candidateIdentifier = ts.createIdentifier(parts.slice(i).join('_'));
      candidateIdentifier.parent = node;
      const candidateName = fixupIdentifierName(candidateIdentifier);
      if (library.addName(candidateName)) {
        // Able to add name to library.
        let ret = new DartNameRecord(node, candidateName, library);
        this.dartTypes.set(fullPath, ret);
        return ret;
      }
    }

    // Usually the module name prefixes should be sufficient to disambiguate
    // names but sometimes we need to add a numeric prefix as well to
    // disambiguate. We could alternately append the full module prefix as well
    // to make the name choice completely unsurprising albeit even uglier.
    // This case should be very rarely hit.
    for (let i = 2;; i++) {  // must use for-loop because of eslint's no-shadow rule
      let candidateName = parts[parts.length - 1] + i;
      if (library.addName(candidateName)) {
        // Able to add name to library.
        let ret = new DartNameRecord(node, candidateName, library);
        this.dartTypes.set(fullPath, ret);
        return ret;
      }
    }
  }