Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NoQA] Adding "avoid anonymous function" eslint rule into the app #39942

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 66 additions & 3 deletions contributingGuides/STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,48 @@ export {
}
```

Using arrow functions is the preferred way to write an anonymous function such as a callback method.
Using named functions is the preferred way to write a callback method.

```javascript
// Bad
someArray.map(function (item) {...});
people.map(function (item) {/* Long and complex logic */});
people.map((item) => {/* Long and complex logic with many inner loops*/});
useEffect/useMemo/useCallback(() => {/* Long and complex logic */}, []);

// Good
someArray.map((item) => {...});
function mappingPeople(person) {/* Long and complex logic */};
people.map(mappingPeople);
useEffect/useMemo/useCallback(function handlingConnection() {/* Long and complex logic */}, []);
```

You can still use arrow function for declarations or simple logics to keep them readable.

```javascript
// Bad
randomList.push({
onSelected: Utils.checkIfAllowed(function checkTask() { return Utils.canTeamUp(people); }),
});
routeList.filter(function checkIsActive(route) {
return route.isActive;
});

// Good
randomList.push({
onSelected: Utils.checkIfAllowed(() => Utils.canTeamUp(people)),
});
routeList.filter((route) => route.isActive);
const myFunction = () => {...};
const person = { getName: () => {} };
Utils.connect({
callback: (val) => {},
});
useEffect(() => {
if (isFocused) {
return;
}
setError(null, {});
}, [isFocused]);

```

Empty functions (noop) should be declare as arrow functions with no whitespace inside. Avoid _.noop()
Expand Down Expand Up @@ -112,6 +146,35 @@ if (someCondition) {
}
```

## Object / Array Methods

We have standardized on using [underscore.js](https://underscorejs.org/) methods for objects and collections instead of the native [Array instance methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#instance_methods). This is mostly to maintain consistency, but there are some type safety features and conveniences that underscore methods provide us e.g. the ability to iterate over an object and the lack of a `TypeError` thrown if a variable is `undefined`.

```javascript
// Bad
myArray.forEach(item => doSomething(item));
// Good
_.each(myArray, item => doSomething(item));

// Bad
const myArray = Object.keys(someObject).map((key) => doSomething(someObject[key]));
// Good
const myArray = _.map(someObject, (value, key) => doSomething(value));

// Bad
myCollection.includes('item');
// Good
_.contains(myCollection, 'item');

// Bad
const modifiedArray = someArray.filter(filterFunc).map(mapFunc);
// Good
const modifiedArray = _.chain(someArray)
.filter(filterFunc)
.map(mapFunc)
.value();
```

## Accessing Object Properties and Default Values

Use `lodashGet()` to safely access object properties and `||` to short circuit null or undefined values that are not guaranteed to exist in a consistent way throughout the codebase. In the rare case that you want to consider a falsy value as usable and the `||` operator prevents this then be explicit about this in your code and check for the type.
Expand Down
2 changes: 1 addition & 1 deletion contributingGuides/TS_STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ _.each(arr, () => {});

// GOOD
var arr = [];
arr.forEach(() => {});
arr.forEach(function loopArr() {});

// BAD
lodashGet(object, ['foo'], 'bar');
Expand Down
Loading