diff options
Diffstat (limited to 'doc/development/fe_guide/style/javascript.md')
-rw-r--r-- | doc/development/fe_guide/style/javascript.md | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/doc/development/fe_guide/style/javascript.md b/doc/development/fe_guide/style/javascript.md index 5c35b880eab..334372af1f4 100644 --- a/doc/development/fe_guide/style/javascript.md +++ b/doc/development/fe_guide/style/javascript.md @@ -14,7 +14,7 @@ In addition to the style guidelines set by Airbnb, we also have a few specific r listed below. NOTE: -You can run ESLint locally by running `yarn eslint` +You can run ESLint locally by running `yarn run lint:eslint:all` or `yarn run lint:eslint $PATH_TO_FILE`. ## Avoid forEach @@ -294,3 +294,24 @@ Strive to write many small pure functions and minimize where mutations occur var c = pureFunction(values.foo); ``` + +## Export constants as primitives + +Prefer exporting constant primitives with a common namespace over exporting objects. This allows for better compile-time reference checks and helps to avoid accidential `undefined`s at runtime. In addition, it helps in reducing bundle sizes. + +Only export the constants as a collection (array, or object) when there is a need to iterate over them, for instance, for a prop validator. + + ```javascript + // bad + export const VARIANT = { + WARNING: 'warning', + ERROR: 'error', + }; + + // good + export const VARIANT_WARNING = 'warning'; + export const VARIANT_ERROR = 'error'; + + // good, if the constants need to be iterated over + export const VARIANTS = [VARIANT_WARNING, VARIANT_ERROR]; + ``` |