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
- If you don鈥檛 want to have this global constant naming convention.
- When you use constant names that contain non-ascii characters from alphabets that
don鈥檛 have upper and lower case.
E.g.鍙傛暟
will be flagged and cannot be auto corrected a.t.m. However, many non-ascii character sets work out of the box - e.g.泻芯薪褋褌邪薪褌邪校谢褍褔褕械薪懈械
can be auto-corrected. Likewise袣袨袧小孝袗袧孝袗_校袥校效楔袝袧袠袝
is valid. - The fixer doesn鈥檛 take variable shadowning/ name masking into account (yet). This is a bad practice anyway because it鈥檚 a source of subtle, hard to detect bugs. If you suspect (or don鈥檛 know) if you have any shadowed variables switch on eslint core鈥檚 no-shadow rule.