Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit c128807

Browse files
authored
Allow trailing comma's (#2)
1 parent 1fb3a34 commit c128807

File tree

1 file changed

+79
-12
lines changed

1 file changed

+79
-12
lines changed

README.md

+79-12
Original file line numberDiff line numberDiff line change
@@ -875,32 +875,99 @@ Forked from the excellent Airbnb JavaScript Style Guide, slightly modified to fi
875875
};
876876
```
877877

878-
- Additional trailing comma: **Nope.** This can cause problems with IE6/7 and IE9 if it's in quirksmode. Also, in some implementations of ES3 would add length to an array if it had an additional trailing comma. This was clarified in ES5 ([source](http://es5.github.io/#D)):
878+
- Additional trailing comma: **Yup.**
879879

880-
> Edition 5 clarifies the fact that a trailing comma at the end of an ArrayInitialiser does not add to the length of the array. This is not a semantic change from Edition 3 but some implementations may have previously misinterpreted this.
880+
> Why? This leads to cleaner git diffs. Also, transpilers like Babel will remove the additional trailing comma in the transpiled code which means you don't have to worry about the [trailing comma problem](https://github.com/airbnb/javascript/blob/es5-deprecated/es5/README.md#commas) in legacy browsers.
881+
882+
```diff
883+
// bad - git diff without trailing comma
884+
const hero = {
885+
firstName: 'Florence',
886+
- lastName: 'Nightingale'
887+
+ lastName: 'Nightingale',
888+
+ inventorOf: ['coxcomb chart', 'modern nursing']
889+
};
890+
891+
// good - git diff with trailing comma
892+
const hero = {
893+
firstName: 'Florence',
894+
lastName: 'Nightingale',
895+
+ inventorOf: ['coxcomb chart', 'modern nursing'],
896+
};
897+
```
881898
882899
```javascript
883900
// bad
884-
var hero = {
885-
firstName: 'Kevin',
886-
lastName: 'Flynn',
901+
const hero = {
902+
firstName: 'Dana',
903+
lastName: 'Scully'
887904
};
888905
889-
var heroes = [
906+
const heroes = [
890907
'Batman',
891-
'Superman',
908+
'Superman'
892909
];
893910
894911
// good
895-
var hero = {
896-
firstName: 'Kevin',
897-
lastName: 'Flynn'
912+
const hero = {
913+
firstName: 'Dana',
914+
lastName: 'Scully',
898915
};
899916
900-
var heroes = [
917+
const heroes = [
901918
'Batman',
902-
'Superman'
919+
'Superman',
903920
];
921+
922+
// bad
923+
function createHero(
924+
firstName,
925+
lastName,
926+
inventorOf
927+
) {
928+
// does nothing
929+
}
930+
931+
// good
932+
function createHero(
933+
firstName,
934+
lastName,
935+
inventorOf,
936+
) {
937+
// does nothing
938+
}
939+
940+
// good (note that a comma must not appear after a "rest" element)
941+
function createHero(
942+
firstName,
943+
lastName,
944+
inventorOf,
945+
...heroArgs
946+
) {
947+
// does nothing
948+
}
949+
950+
// bad
951+
createHero(
952+
firstName,
953+
lastName,
954+
inventorOf
955+
);
956+
957+
// good
958+
createHero(
959+
firstName,
960+
lastName,
961+
inventorOf,
962+
);
963+
964+
// good (note that a comma must not appear after a "rest" element)
965+
createHero(
966+
firstName,
967+
lastName,
968+
inventorOf,
969+
...heroArgs
970+
)
904971
```
905972
906973
**[[⬆]](#TOC)**

0 commit comments

Comments
 (0)