Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Klaytn -> Kaia in comments (+ lint all files) #2152

Merged
merged 16 commits into from May 10, 2024

Conversation

ian0371
Copy link
Contributor

@ian0371 ian0371 commented May 8, 2024

Proposed changes

This PR updates Klay/Klaytn/KLAY to Kaia/Kaia/KAIA in the comments, excluding the licenses.
TODO-Klaytn-Issue is left as-is to keep the legacy issue.

Plus, all files are linted so later PRs can just contain the rename diff, especially license update.

Types of changes

  • Bugfix
  • New feature or enhancement
  • Others

Checklist

  • I have read the CONTRIBUTING GUIDELINES doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes ($ make test)
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Further comments

Conversion script:

import Parser = require("tree-sitter");
import { Query } from "tree-sitter";
import TreeSitterGo from "tree-sitter-go";
import * as fs from "fs";
import { join, extname } from "path";

type Replacement = {
  pat: RegExp;
  new: string;
};

function replaceComment(sourceCode: string, replacements: Replacement[]) {
  const parser = new Parser();
  parser.setLanguage(TreeSitterGo);

  const tree = parser.parse(sourceCode, undefined, { bufferSize: 5e6 });
  const query = new Query(TreeSitterGo, `(comment) @comment`);
  const matches = query.captures(tree.rootNode);

  let ret = sourceCode;

  const exclusionRegex =
    /(Copyright|Modified by).* The klaytn Authors|klaytn development|the klaytn library|Klaytn Signed Message|docs\.klaytn\.(com|foundation)/i;
  // Reverse the order of matches to avoid messing up indices when replacing strings
  matches.reverse().forEach((match) => {
    const commentText = match.node.text;
    if (!exclusionRegex.test(commentText)) {
      let newText = commentText;
      replacements.forEach((replacement) => {
        newText = newText.replace(replacement.pat, replacement.new);
      });

      if (newText !== commentText) {
        ret =
          ret.substring(0, match.node.startIndex) +
          newText +
          ret.substring(match.node.endIndex);
      }
    }
  });
  return ret;
}

async function processDirectory(directory: string) {
  const entries = fs.readdirSync(directory, { withFileTypes: true });
  for (const entry of entries) {
    const path = join(directory, entry.name);
    if (entry.isDirectory()) {
      await processDirectory(path); // Recursive call for directories
    } else if (entry.isFile() && extname(entry.name) === ".go") {
      try {
        let code = fs.readFileSync(path, "utf8");
        code = replaceComment(code, [
          { pat: /TODO-klaytn/i, new: "TODO-Kaia" },
          {
            pat: /Go binding around a Klaytn contract/i,
            new: "Go binding around a Kaia contract",
          },
          { pat: /note-Klaytn/gi, new: "NOTE-Kaia" },
          { pat: /klaytn network/gi, new: "Kaia network" },
          { pat: /klaytn blockchain/gi, new: "Kaia blockchain" },
          { pat: /klaytn protocol/gi, new: "Kaia protocol" },
          { pat: /klaytn contract/gi, new: "Kaia contract" },
          { pat: /klaytn account/gi, new: "Kaia account" },
          { pat: /klaytn transaction/gi, new: "Kaia transaction" },
          { pat: /klaytn address/gi, new: "Kaia address" },
          { pat: /klaytn consensus/gi, new: "Kaia consensus" },
          { pat: /([.,!?'"\s])KLAY([.,!?'"\s]|$)/g, new: "$1KAIA$2" },
          { pat: /([.,!?'"\s])klaytn([.,!?'"\s]|$)/gi, new: "$1Kaia$2" },
        ]);
        fs.writeFileSync(path, code);
      } catch (e: any) {
        const errMsg = `Error processing ${path}: ${e.message}\n`;
        console.error(errMsg);
        fs.appendFileSync("error.log", errMsg);
      }
    }
  }
}

async function main() {
  await processDirectory("path-to-klaytn");
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

@ian0371 ian0371 marked this pull request as ready for review May 8, 2024 09:43
@ian0371 ian0371 self-assigned this May 8, 2024
blukat29
blukat29 previously approved these changes May 8, 2024
blockchain/chain_makers_test.go Show resolved Hide resolved
@blukat29 blukat29 merged commit a1efcce into klaytn:dev May 10, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants