Created
December 8, 2016 21:37
-
-
Save seeflanigan/abdbffa755a29199a1ab5ce7cb9bb948 to your computer and use it in GitHub Desktop.
`sed` onelines to fix `func-name` errors in `ESLint` per `airbnb/legacy` rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Use cases | |
# | |
# Anonymous function expressions assigned by `=` operator to a variable: | |
# (with and without arguments) | |
# | |
# const aFunc = function () {}; | |
# | |
# Anonymous function expressions declared as a property of an object: | |
# | |
# const anObj = { | |
# aFunc: function () {} | |
# }; | |
# | |
# Anonymous function assigned by `=` to a property of a prototype: | |
# | |
# String.prototype.aFunc = function () {}; | |
# | |
# Anonymous function expression as a callback: | |
# | |
# describe('Documentation', function () {}); | |
# | |
EXCLUSIONS_REGEX='assets' | |
CALLBACK_AG_REGEX='[,(]\s+function\s*\(' | |
OBJECT_PROPERTY_AG_REGEX='\w+:\s+function\s*\(' | |
PROTOTYPE_AG_REGEX='prototype\.\w+\s+=\s+function\s*\(' # is this made redundant by the `VARIABLE` case? | |
VARIABLE_AG_REGEX='\w+\s+=\s+function\s*\(' | |
CALLBACK_SUBSTITUTION='s/[,(] *function *(\([^)]*\))/, (\1) =>/' | |
OBJECT_PROPERTY_SUBSTITUTION='s/\([[:alnum:]]*\): *function *(/\1: function \1(/' | |
PROTOTYPE_SUBSTITUTION='s/prototype\.\([[:alnum:]]*\) *= *function *(/prototype\.\1 = function \1(/' | |
VARIABLE_SUBSTITUTION='s/\([[:alnum:]]\) *= *function *(/\1 = function \1(' | |
# this looks like a generic map from one input set (files) to another | |
# can we map over tuples of [REGEX, SUBSTITUTIONS] and apply the 'map' to project | |
# from one set to the next set by composing `ag`, `grep`, and `sed`? | |
ag -l $PROTOTYPE_AG_REGEX | grep -v $EXCLUSIONS_REGEX | xargs sed -i '' $PROTOTYPE_SUBSTITUTION | |
ag -l $OBJECT_PROPERTY_AG_REGEX | grep -v $EXCLUSIONS_REGEX | xargs sed -i '' $OBJECT_PROPERTY_SUBSTITUTION | |
ag -l $CALLBACK_AG_REGEX | grep -v $EXCLUSIONS_REGEX | xargs sed -i '' $CALLBACK_SUBSTITUTION | |
ag -l $VARIABLE_AG_REGEX | grep -v $EXCLUSIONS_REGEX | xargs sed -i '' $VARIABLE_SUBSTITUTION |
Author
seeflanigan
commented
Dec 21, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment