xojs/xo

Variable naming rule(s)

jonahsnider opened this issue · 4 comments

The @typescript-eslint/naming-convention rule has many possible configurations to enforce variable naming conventions. If possible it should be used to ensure consistent naming style in a project, especially for constants (defined below) being named with UPPER_SNAKE_CASE.

  1. Constants are entirely readonly and never modified
  2. Constants are declared at the highest level scope (ex. not declared in a function)

Related:

Do those bugs prevent any progress from being made on this? It seems like name validation for constants could be added while we wait for better class support.

How to best circumvent the false positives while keeping the rest? Basically I want to apply this diff to my config:

 '@typescript-eslint/naming-convention': [
   'error', {
     selector: 'default',
     format: [
+      'strictPascalCase',
       'strictCamelCase'
     ],

Yikes, this is involved:

.xo-config.cjs:

const { rules } = require('eslint-config-xo-typescript')

const NC = '@typescript-eslint/naming-convention'

const nameRules = rules[NC].map((r) => {
  if (typeof r === 'string') return r
  const { selector, format, ...rest } = r
  if (!selector.includes('variable') || !selector.includes('function')) return r
  return { selector, format: ['strictCamelCase', 'StrictPascalCase'], ...rest }
})

module.exports = {
  semicolon: false,
  extends: [
    'xo-react',
  ],
  rules: {
    [NC]: nameRules,
  }
}