naming-convention

Try in Playground
javascript-best-practicesCode StyleInformational

0

No tags

No CWE or CVE

This rule helps keep your variable naming conventions consistent.

Ast Rule: variable declaration


naming-convention

How to write a rule
const regex = new RegExp("^[_$A-Za-z][$A-Za-z0-9]*$|^[_$A-Z][_$A-Z0-9]+$");

function visit(node, filename, code) {
  if (!node || !node.name || !node.name.value) return;

  const value = node.name.value;


  if (regex.test(value)) return;

  const error = buildError(
    node.name.start.line,
    node.name.start.col,
    node.name.end.line,
    node.name.end.col,
    `Variable follows an inconsistent naming convention`,
    "INFORMATIONAL",
    "CODE_STYLE"
  );

  addError(error);
}

bad.js

Expected test result: has error

const foo_bar = 1;
const baz_ = 2;

good.js

Expected test result: no error

const fooBar = 1;
const _baz = 2;
const FOO_BAR = 3
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
Legal
  • Security
  • Privacy Policy
  • Code Privacy
  • Terms of Service
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.