nedbat / cog

Small bits of Python computation for static files
MIT License
340 stars 26 forks source link

refactor: convert camelCase to snake_case #37

Closed vergenzt closed 4 months ago

vergenzt commented 4 months ago

Purposes:

  1. To match typical modern Python coding styles (recommended by PEP 8)
  2. To prepare for argparse usage, which uses snake_case for default dests

Via https://ast-grep.github.io/, with rule:

id: _
language: python

rule:
  pattern: $ID
  kind: identifier
  all:
  - regex: '^[^A-Z]'  # not PascalCase
  - regex: '[a-z][A-Z]'  # has camelCase transition

  not:
    any:
    # backcompat (part of cog module API)
    - regex: ^sOut$
    - inside:
        kind: attribute
        has:
          field: object
          regex: \.cogmodule$

    # unittest methods
    - regex: ^assert
    - regex: ^setUp$
    - regex: ^tearDown$
    - regex: ^addCleanup$

transform:
  ID_SNAKE:
    convert:
      source: $ID
      toCase: snakeCase
  UNDERSCORE:
    replace:
      source: $ID
      replace: '^(_)?.*'
      by: '$1'

fix: $UNDERSCORE$ID_SNAKE

plus manual review of all changes

nedbat commented 4 months ago

The ast-grep rule is impressive. I was a little worried about setOutput used in the doc generation, but I don't seem to be using it anywhere else, so probably no one else is either.

Thanks!