- Notifications
You must be signed in to change notification settings - Fork0
Collect statistics on ?. syntax used in CoffeeScript projects
License
alangpierce/coffeescript-soak-stats
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A command-line tool that you can run on a CoffeeScript codebase to getstatistics on different types of soak operations (?.
syntax).
Built to help inform the discussion around theJavaScript Optional Chaining proposal.
npm install -g coffeescript-soak-statscoffeescript-soak-stats my-project
Themanual_test/clone_all_repos.py
script clones about 500,000 lines ofCoffeeScript from various projects on GitHub. Note that I intentionally excludedthe CoffeeScript compiler source code because its test suite isn'trepresentative of real-world code.
Here's what it prints when running stats on all of them:
Error processing file manual_test/sharelatex/latex-hints-sharelatex/public/app/ide/controllers/LatexHintsController.coffee2297/2297Total files: 2296Total lines: 453254Total soak operations: 4447Total soaked member accesses: 3674Total soaked dynamic member accesses: 244Total soaked function applications: 529Total soaked new invocations: 0Total soak operations using short-circuiting: 1394Total soak operations using short-circuiting (excluding methods): 225Total soaked assignments (including compound assignments): 36Total soaked deletes: 1Total cases where parens affected the soak container: 0Total soak operations chained on top of another soak: 590Total accesses of undeclared globals in soak operations: 74
Soak operation refers to the class of?
operations here. It's theCoffeeScript term for optional chaining. It doesnot include the binary?
operator or?=
compound assignment operator.
Thesoak container of a soak operation is a term I use to refer to theexpression whose evaluation will be skipped if the soaked value is null orundefined. For example, in the expressiona(b?.c.d)
, the soak container isb?.c.d
.
Error processing file sharelatex/latex-hints-sharelatex/public/app/ide/controllers/LatexHintsController.coffee
This is an invalid CoffeeScript file.
2297/2297Total files: 2296Total lines: 453254Total soak operations: 4447
"Total soak operations" includes all uses of soak synax:a?.b
,a?[b]
,a?()
,new A?()
, anda?[b..c]
.
Total soaked member accesses: 3674
A soaked member access is an expression likea?.b
.
Total soaked dynamic member accesses: 244
A soaked dynamic member access is an expression likea?[b]
.
Total soaked function applications: 529
A soaked function application is an expression likea?()
.
Total soaked new invocations: 0
A soaked new invocation is an expression likenew A?()
.
Total soak operations using short-circuiting: 1394
A soak expression counts toward this stat if the expression evaluating to nullwould cause "short-circuit" effects; that is, if there's anything else in thesoak container that would be skipped.
Examples:
a?.b.c
a?.b()
Non-examples:
a(b?.c)
Total soak operations using short-circuiting (excluding methods): 225
This is the same as the previous statistic, except that it excludes expressionsof the forma?.b()
. In other words, the vast majority of short circuiting inpractice is only used for method call syntax and doesn't extend beyond that, andthis stat counts the number of cases thatdo extend beyond that.
Examples:
a?.b.c
a?.b().c
Non-examples:
a?.b()
a?.b(c, d, e)
Total soaked assignments (including compound assignments): 36
A soaked assignment is an expression likea?.b = c
. A soaked compoundassignment is an expression likea?.b += c
.
This doesnot include cases likea = b?.c
. It's only cases where theassignment would be skipped if the soaked expression evaluates tonull
orundefined
.
Total soaked deletes: 1
A soaked delete is an expression likedelete a?.b
.
Total cases where parens affected the soak container: 0
This counts cases where the existence of parentheses affected the evaluation ofa soak operation. For example,(a?.b).c
crashes ifa
is null or undefined,even thougha?.b.c
does not crash in that case.
Total soak operations chained on top of another soak: 590
This counts cases likea?.b?.c?.d
, where the code explicitly uses multiplesoak operations in a chain. The actual number is the number of soak operationsin a non-initial position in a chain, soa?.b?.c?.d
adds 2 to this stat,a?.b.c?.d
adds 1 to this stat, anda?.b.c.d
adds 0 to this stat.
This is meant to help understand how common it is to explicitly use multiplesoak opertions in a row vs. relying on short circuiting.
Total accesses of undeclared globals in soak operations: 74
This counts cases likewindow?.a
, wherewindow
is never declared in thisscope (or a parent scope). In other words, these instances rely on the behaviorthata?.b
evaluates toundefined
rather than crashing whena
is anundeclared variable and isn't in the global scope.
About
Collect statistics on ?. syntax used in CoffeeScript projects
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.