View on GitHub

eslint-plugin-budapestian

Lint rules for Budapestian notation

Enforce global constants adhere to a pattern (global-constant-pattern)

This rule enforces that global literal, array, object and binary expression constants as well as constants that got an identifier assigned are ALL_CAPS_SNAKE_CASE.

馃敡 The --fix option on the command line renames these constants to adhere to the pattern taking existing snake and camel casing into account (someGlobalConstant => SOME_GLOBAL_CONSTANT)

In version 3.0.2 the fix option for this rule has been switched off temporarily as it was a bit too enthousiastic on occasion. It might return in a future version.

Rule Details

Examples of incorrect code for this rule:

// global const literals should be in upper case
// b.t.w. the fixer corrects this to THIS_IS_A_GLOBAL_CONST
const thisIsAGlobalConst = "but it looks like a variable";

// multi const declaration with lower case only
const snakes_but_no_caps = 1,
  secondconstant = 2;

// also assignments of arrays and objects to a global constant
// are subject to this rule
const thisIsAnArray = ["aap", "noot", "mies", "wim", "zus"];

const severity2coolness = {
  info: "cool",
  warn: "not cool - fix later",
  error: "very uncool - fix now",
};

// ass well as binary expressions:
const dayInSeconds = 24 * 60 * 60;

// ... and identifiers
const jourEnSecondes = dayInSeconds;

// the rule also extends to exported constants
export const exportedGlobalConst = "something";

Examples of correct code for this rule:

// upper case as it's an assignment of a literal to a global constant
const GLOBAL_CONSTANT = 123;

// also in multi const declarations
const MULTIPLE = 1,
  CONSTANTS = 2,
  IN_ONE_DECLARATION = 3;

// upper case is cool for array and object assignments
const THIS_IS_AN_ARRAY = ["aap", "noot", "mies", "wim", "zus"];

const SEVERITY2COOLNESS = {
  info: "cool",
  warn: "not cool - fix later",
  error: "very uncool - fix now",
};

// binary expressions are OK too:
const DAY_IN_SECONDS = 24 * 60 * 60;

// ... and identifiers
const JOUR_EN_SECONDES = DAY_IN_SECONDS;

// exported consts are taken into account as well
export const EXPORTED_GLOBAL_CONST = "something";

// assignments of call expressions to global constants are allowed without
// them having to be snaked upper case.
const _ = require("lodash");
const stateMachineCat = require("state-machine-cat");

// the same for are arrow function expressions
const someFunction = () => {
  return "did stuff";
};

Options

exceptions

Type: array

If you want to allow some global constants names to not adhere to the rule, you can specify these. E.g. if you want to use the constant names and e, you can do sol like so:

"budapestian/global-constant-pattern": [
  "error",
  { "exceptions": ["蟺", "e"] }
]

When Not To Use It