From f64a639bcfa1fc2bc89ca7db268f594306edfd7c Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Tue, 16 Mar 2021 18:18:33 +0000 Subject: Add latest changes from gitlab-org/gitlab@13-10-stable-ee --- doc/development/fe_guide/style/javascript.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'doc/development/fe_guide/style/javascript.md') 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]; + ``` -- cgit v1.2.3